File size: 1,304 Bytes
211843b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from pydantic import BaseModel, Field
from typing import Optional 
from bson.objectid import ObjectId
import bcrypt
import hashlib
# https://www.mongodb.com/developer/how-to/flask-python-mongodb/
# https://codehandbook.org/creating-rest-api-using-python-mongodb/
# https://zetcode.com/python/bcrypt/
class PydanticObjectID(ObjectId):
    @classmethod
    def __get_validators__(cls):
        yield cls.validate
    @classmethod
    def validate(cls, v):
        if not isinstance(v, ObjectId):
            raise TypeError(f'Must be an ObjectId')
        return str(v)
class Users(BaseModel):
    id : PydanticObjectID # Sets MongoDB ID 
    email: str
    password: str
    access: bool
    def generate_hash_pw(self):
        salt = bcrypt.gensalt()
        data = self.dict(by_alias=True, exclude_none=True)
        hashed = bcrypt.hashpw(data["password"].encode('utf-8'), salt)
        return hashed,str(salt).replace("b'","").replace("'","")
    
    def to_bson(self):
        data = self.dict(by_alias=True,exclude_none=True)
        hashed = hashlib.sha256(data["password"].encode('utf-8')).hexdigest()
        del data["password"]
        data["password"] = hashed
        data["_id"] = data["id"]
        del data["id"]
        return data
        #data_val = json.loads(json_util.dumps(data))