Module models.user

Expand source code
from datetime import datetime
import enum
from typing import Optional
from beanie import UnionDoc, Document, Indexed, Replace, before_event, Insert
from pydantic import BaseModel, EmailStr
from passlib.context import CryptContext

pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")

class UserCollection(UnionDoc):
    """ The collection holding all User Documents"""
    class Settings:
        collection_name = 'user_collection'

class UserRole(enum.Enum):
    """All available User Roles, extend this to add a new role"""
    admin = 'admin'
    user = 'user'

class UserCreate(BaseModel):
    """Model for user creation"""
    email: EmailStr
    password: str
    @property
    def password_hash(self):
        return pwd_context.hash(self.password)

class NameInfo(BaseModel):
    """Model for name info of the person behind a user"""
    firstname: str
    lastname: str

class UserUpdate(BaseModel):
    """Model for patching a user"""
    name: Optional[NameInfo]

class UserRead(BaseModel):
    """Model that is returned on api requests. Add the fields you want to show"""
    email: EmailStr
    name: Optional[NameInfo]
    role: UserRole

class User(Document):
    """Database model for Users, inherit from this class to create a specialized type of user"""
    email: Indexed(EmailStr, unique=True)
    name: Optional[NameInfo]
    password_hash: str
    role: UserRole = UserRole.user
    created_on: datetime
    updated_on: datetime

    @property
    def is_admin(self)->bool:
        return self.role == UserRole.admin

    def verify_password(self, password):
        return pwd_context.verify(password, self.password_hash)

    @before_event([Replace, Insert])
    def update_timestamp(self):
        self.updated_on = datetime.utcnow()
    class Settings:
        union_doc = UserCollection


class Admin(User):
    """Admin user model"""
    role = UserRole.admin

Classes

class Admin (*args, **kwargs)

Admin user model

Create a new model by parsing and validating input data from keyword arguments.

Raises ValidationError if the input data cannot be parsed to form a valid model.

Expand source code
class Admin(User):
    """Admin user model"""
    role = UserRole.admin

Ancestors

  • User
  • beanie.odm.documents.Document
  • pydantic.main.BaseModel
  • pydantic.utils.Representation
  • beanie.odm.interfaces.find.FindInterface
  • beanie.odm.interfaces.aggregate.AggregateInterface
  • beanie.odm.interfaces.getters.OtherGettersInterface

Subclasses

  • pydantic.main.Admin
class NameInfo (**data: Any)

Model for name info of the person behind a user

Create a new model by parsing and validating input data from keyword arguments.

Raises ValidationError if the input data cannot be parsed to form a valid model.

Expand source code
class NameInfo(BaseModel):
    """Model for name info of the person behind a user"""
    firstname: str
    lastname: str

Ancestors

  • pydantic.main.BaseModel
  • pydantic.utils.Representation

Subclasses

  • pydantic.main.NameInfo
  • pydantic.main.NameInfo
  • pydantic.main.NameInfo
  • pydantic.main.NameInfo
  • pydantic.main.NameInfo
  • pydantic.main.NameInfo

Class variables

var firstname : str
var lastname : str
class User (*args, **kwargs)

Database model for Users, inherit from this class to create a specialized type of user

Create a new model by parsing and validating input data from keyword arguments.

Raises ValidationError if the input data cannot be parsed to form a valid model.

Expand source code
class User(Document):
    """Database model for Users, inherit from this class to create a specialized type of user"""
    email: Indexed(EmailStr, unique=True)
    name: Optional[NameInfo]
    password_hash: str
    role: UserRole = UserRole.user
    created_on: datetime
    updated_on: datetime

    @property
    def is_admin(self)->bool:
        return self.role == UserRole.admin

    def verify_password(self, password):
        return pwd_context.verify(password, self.password_hash)

    @before_event([Replace, Insert])
    def update_timestamp(self):
        self.updated_on = datetime.utcnow()
    class Settings:
        union_doc = UserCollection

Ancestors

  • beanie.odm.documents.Document
  • pydantic.main.BaseModel
  • pydantic.utils.Representation
  • beanie.odm.interfaces.find.FindInterface
  • beanie.odm.interfaces.aggregate.AggregateInterface
  • beanie.odm.interfaces.getters.OtherGettersInterface

Subclasses

  • Admin
  • pydantic.main.User

Class variables

var Settings
var created_on : datetime.datetime
var email : beanie.odm.fields.Indexed..NewType
var name : Optional[NameInfo]
var password_hash : str
var roleUserRole
var updated_on : datetime.datetime

Instance variables

var is_admin : bool
Expand source code
@property
def is_admin(self)->bool:
    return self.role == UserRole.admin

Methods

def update_timestamp(self)
Expand source code
@before_event([Replace, Insert])
def update_timestamp(self):
    self.updated_on = datetime.utcnow()
def verify_password(self, password)
Expand source code
def verify_password(self, password):
    return pwd_context.verify(password, self.password_hash)
class UserCollection

The collection holding all User Documents

Expand source code
class UserCollection(UnionDoc):
    """ The collection holding all User Documents"""
    class Settings:
        collection_name = 'user_collection'

Ancestors

  • beanie.odm.union_doc.UnionDoc
  • beanie.odm.interfaces.find.FindInterface
  • beanie.odm.interfaces.aggregate.AggregateInterface
  • beanie.odm.interfaces.getters.OtherGettersInterface
  • beanie.odm.interfaces.detector.DetectionInterface

Class variables

var Settings
class UserCreate (**data: Any)

Model for user creation

Create a new model by parsing and validating input data from keyword arguments.

Raises ValidationError if the input data cannot be parsed to form a valid model.

Expand source code
class UserCreate(BaseModel):
    """Model for user creation"""
    email: EmailStr
    password: str
    @property
    def password_hash(self):
        return pwd_context.hash(self.password)

Ancestors

  • pydantic.main.BaseModel
  • pydantic.utils.Representation

Class variables

var email : pydantic.networks.EmailStr
var password : str

Instance variables

var password_hash
Expand source code
@property
def password_hash(self):
    return pwd_context.hash(self.password)
class UserRead (**data: Any)

Model that is returned on api requests. Add the fields you want to show

Create a new model by parsing and validating input data from keyword arguments.

Raises ValidationError if the input data cannot be parsed to form a valid model.

Expand source code
class UserRead(BaseModel):
    """Model that is returned on api requests. Add the fields you want to show"""
    email: EmailStr
    name: Optional[NameInfo]
    role: UserRole

Ancestors

  • pydantic.main.BaseModel
  • pydantic.utils.Representation

Subclasses

  • pydantic.main.UserRead
  • pydantic.main.UserRead
  • pydantic.main.UserRead
  • pydantic.main.UserRead

Class variables

var email : pydantic.networks.EmailStr
var name : Optional[NameInfo]
var roleUserRole
class UserRole (value, names=None, *, module=None, qualname=None, type=None, start=1)

All available User Roles, extend this to add a new role

Expand source code
class UserRole(enum.Enum):
    """All available User Roles, extend this to add a new role"""
    admin = 'admin'
    user = 'user'

Ancestors

  • enum.Enum

Class variables

var admin
var user
class UserUpdate (**data: Any)

Model for patching a user

Create a new model by parsing and validating input data from keyword arguments.

Raises ValidationError if the input data cannot be parsed to form a valid model.

Expand source code
class UserUpdate(BaseModel):
    """Model for patching a user"""
    name: Optional[NameInfo]

Ancestors

  • pydantic.main.BaseModel
  • pydantic.utils.Representation

Class variables

var name : Optional[NameInfo]