2018-04-27 17:16:43 +00:00
|
|
|
from uuid import uuid4
|
|
|
|
|
2018-08-03 16:15:08 +00:00
|
|
|
from flask import current_app as app
|
|
|
|
from sqlalchemy import Column
|
2018-04-27 17:16:43 +00:00
|
|
|
from sqlalchemy.dialects.postgresql import UUID
|
2018-08-03 16:15:08 +00:00
|
|
|
from sqlalchemy_utils import EmailType, PasswordType
|
2019-12-16 19:38:41 +00:00
|
|
|
from citext import CIText
|
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
|
2018-08-03 16:15:08 +00:00
|
|
|
from ereuse_devicehub.resources.models import STR_SIZE, Thing
|
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(
|
2018-05-30 10:49:40 +00:00
|
|
|
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-12-16 19:38:41 +00:00
|
|
|
ethereum_address = Column(CIText(), unique=True, default=None)
|
2019-02-18 11:43:50 +00:00
|
|
|
|
2019-01-23 15:55:04 +00:00
|
|
|
# todo set restriction that user has, at least, one active db
|
|
|
|
|
2019-12-16 19:38:41 +00:00
|
|
|
def __init__(self, email, password=None, ethereum_address=None, inventories=None) -> None:
|
2019-06-19 11:35:26 +00:00
|
|
|
"""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}
|
2019-12-16 19:38:41 +00:00
|
|
|
super().__init__(email=email, password=password, ethereum_address=ethereum_address, inventories=inventories)
|
2018-05-13 13:13:12 +00:00
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
2018-08-03 16:15:08 +00:00
|
|
|
return '<User {0.email}>'.format(self)
|
2018-05-30 10:49:40 +00:00
|
|
|
|
2019-02-18 11:43:50 +00:00
|
|
|
@property
|
|
|
|
def type(self) -> str:
|
|
|
|
return self.__class__.__name__
|
|
|
|
|
2018-08-03 16:15:08 +00:00
|
|
|
@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
|
|
|
|
2019-12-16 19:38:41 +00:00
|
|
|
@property
|
|
|
|
def get_ethereum_address(self):
|
|
|
|
"""The ethereum address in Blockchain, or None."""
|
|
|
|
return next(iter(self.ethereum_address), 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)
|