This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
devicehub-teal/ereuse_devicehub/resources/user/models.py

76 lines
3 KiB
Python
Raw Normal View History

2018-04-27 17:16:43 +00:00
from uuid import uuid4
2020-08-17 14:45:18 +00:00
from citext import CIText
from flask import current_app as app
2021-04-13 16:33:15 +00:00
from sqlalchemy import Column, BigInteger, Sequence
2018-04-27 17:16:43 +00:00
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy_utils import EmailType, PasswordType
2021-04-13 16:33:15 +00:00
from teal.db import IntEnum
2018-04-27 17:16:43 +00:00
2019-01-21 15:08:55 +00:00
from ereuse_devicehub.db import db
from ereuse_devicehub.resources.inventory.model import Inventory
from ereuse_devicehub.resources.models import STR_SIZE, Thing
2021-04-13 16:33:15 +00:00
from ereuse_devicehub.resources.enums import SessionType
2018-04-27 17:16:43 +00:00
class User(Thing):
__table_args__ = {'schema': 'common'}
id = Column(UUID(as_uuid=True), default=uuid4, primary_key=True)
email = Column(EmailType, nullable=False, unique=True)
password = Column(PasswordType(max_length=STR_SIZE,
onload=lambda **kwargs: dict(
schemes=app.config['PASSWORD_SCHEMES'],
2018-04-27 17:16:43 +00:00
**kwargs
)))
2019-01-23 15:55:04 +00:00
token = Column(UUID(as_uuid=True), default=uuid4, unique=True, nullable=False)
2019-01-21 15:08:55 +00:00
inventories = db.relationship(Inventory,
backref=db.backref('users', lazy=True, collection_class=set),
secondary=lambda: UserInventory.__table__,
collection_class=set)
2019-01-23 15:55:04 +00:00
# todo set restriction that user has, at least, one active db
def __init__(self, email, password=None, inventories=None) -> None:
"""Creates an user.
2019-01-23 15:55:04 +00:00
:param email:
:param password:
:param inventories: A set of Inventory where the user has
access to. If none, the user is granted access to the current
inventory.
"""
inventories = inventories or {Inventory.current}
super().__init__(email=email, password=password, inventories=inventories)
def __repr__(self) -> str:
return '<User {0.email}>'.format(self)
@property
def type(self) -> str:
return self.__class__.__name__
@property
def individual(self):
"""The individual associated for this database, or None."""
return next(iter(self.individuals), None)
2019-01-21 15:08:55 +00:00
class UserInventory(db.Model):
"""Relationship between users and their inventories."""
__table_args__ = {'schema': 'common'}
user_id = db.Column(db.UUID(as_uuid=True), db.ForeignKey(User.id), primary_key=True)
inventory_id = db.Column(db.Unicode(), db.ForeignKey(Inventory.id), primary_key=True)
2021-04-13 16:33:15 +00:00
class Session(Thing):
id = Column(BigInteger, Sequence('device_seq'), primary_key=True)
expired = Column(BigInteger, default=0)
token = Column(UUID(as_uuid=True), default=uuid4, unique=True, nullable=False)
type = Column(IntEnum(SessionType), default=SessionType.Internal, nullable=False)
user_id = db.Column(db.UUID(as_uuid=True), db.ForeignKey(User.id))
2021-04-15 19:18:15 +00:00
user = db.relationship(User,
backref=db.backref('sessions', lazy=True, collection_class=set),
collection_class=set)
def __str__(self) -> str:
return '{0.token}'.format(self)