class User(Resource, db.Model): def __init__(self, email, password) -> None: super().__init__() self.email = email self.password = password name = db.Column(db.String(100), nullable=True, unique=False) email = db.Column(db.String(100), nullable=False, unique=True) _password = db.Column(db.String(60), nullable=False) birthday = db.Column(db.Date(), nullable=True) detection = db.Column(db.Date(), nullable=True) sex = db.Column(db.String(), db.Enum(Sex), nullable=True) diabetes = db.Column(db.String(), db.Enum(DiabetesType), nullable=True) avatar = db.Column(db.String(250), nullable=True) @hybrid_property def password(self): return self._password @password.setter def password(self, password): self._password = sha256.hash(password) @classmethod def login(cls, email, password): user = cls.query.filter_by(email=email).first() if cls.verifyHash(password, user.password): return user return None @classmethod def find(cls, email): return cls.query.filter_by(email=email).first() @classmethod def find_avatar(cls, email): return cls.query.with_entities( User.avatar).filter_by(email=email).first() @hybrid_method def allowed_avatar(cls, filename): return '.' in filename and filename.rsplit( '.', 1)[1].lower() in ['jpg', 'png', 'jpeg'] @hybrid_method def verifyHash(cls, password, hash): return sha256.verify(password, hash) @classmethod def findByKeys(cls, username, key): from models.users.keys.read import Read as Key return cls.query.join(Key).filter(Key.username == username and Key.value == key).first() def __repr__(self): return f"{self.email}"
class Car(base.Model): id = db.Column(db.Integer, primary_key=True) color = db.Column(db.Enum(CarColor), nullable=False) model = db.Column(db.Enum(CarModel), nullable=False) owner_id = db.Column(db.Integer, db.ForeignKey(persons.Person.id), nullable=False) owner = db.relationship(persons.Person, backref="cars") def save(self): person = persons.Person.retrieve(self.owner_id) if person.sale_opportunity: super().save() else: raise Exception("Person has reached cars limit")
def S3ResModel(name, prefix=None, types=None, optional=False): has_type = types is None or len(types) > 1 prefix = f'{str(prefix)}_' if prefix else '' bucket_key = f'{prefix}s3_bucket' key_key = f'{prefix}s3_key' compressed_bucket_key = f'high_quality_{prefix}s3_bucket' compressed_key_key = f'high_quality_{prefix}s3_key' preview_key = f'{prefix}preview' type_key = f'{prefix}type' def __init__(self, f, bucket, user_id=None, allowed_types=types) -> None: res = upload_file(f, bucket, user_id, allowed_types) if f else [None] * 5 key, bucket, preview, content_type, compressed_key = res setattr(self, key_key, key) setattr(self, bucket_key, bucket) setattr(self, preview_key, preview) setattr(self, compressed_key_key, compressed_key) setattr(self, compressed_bucket_key, bucket) if has_type and content_type: setattr(self, type_key, content_type) def get_url(self, compressed=False): bucket = getattr(self, bucket_key, None) key = getattr(self, key_key, None) if compressed: bucket = getattr(self, compressed_bucket_key, None) or bucket key = getattr(self, compressed_key_key, None) or key if not bucket or not key: return None return f'https://{bucket}.s3.amazonaws.com/{key}' def get_preview(self): preview = getattr(self, preview_key, None) return preview and f'data:image/png;base64,{preview}' attributes = { bucket_key: db.Column(db.String(64), nullable=optional), key_key: db.Column(db.String(512), nullable=optional), compressed_bucket_key: db.Column(db.String(64), nullable=True), compressed_key_key: db.Column(db.String(512), nullable=True), preview_key: db.Column(db.Text), f'get_{prefix}url': get_url, f'get_{prefix}preview': get_preview, f'update_{prefix}res': __init__, '__init__': __init__, } if has_type: attributes[type_key] = db.Column( db.Enum(S3_RES_TYPE, validate_strings=True)) return type(name, (), attributes)
class AssignmentType(db.Model): __tablename__ = 'assignment_type' id = db.Column(db.Integer(), primary_key=True) name = db.Column(db.String(255)) grade_type = db.Column(db.Enum('additive', 'checkmark', 'use'), default='additive') weight = db.Column(db.Numeric(), default=1.0) points_possible = db.Column(db.Integer(), default=0) assignments_count = db.Column(db.Integer(), default=0) assignments_dropped = db.Column(db.Integer(), default=0) def __repr__(self): return self.name
class Task(db.Model): """ Task table """ id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(512)) status = db.Column(db.Enum(Status)) game_id = db.Column(db.Integer, db.ForeignKey('game.id'), nullable=False) def __init__(self, name=None, status=None, game_id=None): self.name = name self.status = status self.game_id = game_id def __repr__(self): return '<Object %r %r>' % (self.id, self.name, self.status)
class Game(db.Model): """ Game table """ id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(512)) status = db.Column(db.Enum(Status)) uuid = db.Column(db.String(512)) tasks = db.relationship("Task", backref="game", lazy=True) def __init__(self, name=None, status=None): self.name = name self.status = status self.uuid = str(uuid.uuid4()) def __repr__(self): return '<Object %r %r>' % (self.id, self.name, self.status, self.uuid)
class User(BaseUser, UserMixin, SuspendMixin): __tablename__ = 'users' __mapper_args__ = { 'polymorphic_identity': 'user', } id = db.Column(db.BigInteger, db.ForeignKey('base_users.id'), primary_key=True, autoincrement=False) facebook_id = db.Column(db.String(128), nullable=True) google_id = db.Column(db.String(128), nullable=True) birthday = db.Column(db.DateTime, nullable=True) gender = db.Column(db.Enum(Gender), nullable=True) city = db.Column(db.String(256), nullable=True)
class HatModel(db.Model, BaseModel): """ A class for Hat model """ __tablename__ = 'hat' id = db.Column(db.Integer, primary_key=True) color = db.Column(db.Enum( Colors, values_callable=lambda obj: [e.value for e in obj]), nullable=False) character_id = db.Column(db.Integer, db.ForeignKey('character.id'), nullable=True) character = db.relationship("CharacterModel", uselist=False, backref="character", lazy=True) def __init__(self, color: Colors): self.color = color
class Food(Entry, db.Model): def __init__(self) -> None: super().__init__() __tablename__ = 'food' record_id = db.Column(db.Integer, db.ForeignKey('{}.id'.format(Record.__tablename__)), nullable=False) food_id = db.Column(db.String(20), nullable=False) calories = db.Column(db.Integer, nullable=False) name = db.Column(db.String(100), nullable=False) description = db.Column(db.String(100), nullable=True) meal_type = db.Column(db.String(), db.Enum(MealType), nullable=True) created_date = db.Column(db.DateTime(), nullable=False) insulin_per_20_grams = db.Column(db.Integer, nullable=True) @classmethod def getById(cls, id): return cls.query.filter_by(id=id).first() @classmethod def deleteById(cls, id): return cls.query.filter_by(id=id).delete()
class Tag(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(20), nullable=False, unique=True) lang = db.Column(db.Enum(TagEnum), default=TagEnum.ko, nullable=False)