class User(UserMixin, SurrogatePK, Model): """A user of the app.""" __tablename__ = 'users' email = Column(db.String(80), unique=True, nullable=False) password = Column(db.LargeBinary(128), nullable=True) created_at = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow) active = Column(db.Boolean(), default=False) is_admin = Column(db.Boolean(), default=False) def __init__(self, email, password=None, **kwargs): """Create instance.""" db.Model.__init__(self, email=email, **kwargs) if password: self.set_password(password) else: self.password = None def set_password(self, password): """Set password.""" self.password = bcrypt.generate_password_hash(password) def check_password(self, value): """Check password.""" return bcrypt.check_password_hash(self.password, value) def __repr__(self): """Represent instance as a unique string.""" return '<User({email!r})>'.format(email=self.email)
class Recipient(BasePersonModel): __tablename__ = 'recipient' height = Column(db.Integer) bike_recieved = Column(db.DateTime) delivery_date = Column(db.DateTime) consult_date = Column(db.DateTime) bike_id = reference_col('bike', nullable=True) bike = relationship('Bike', backref='recipient') accessory_id = reference_col('accessory', nullable=True) accessory = relationship('Accessory', backref='recipient')
class BasePersonModel(PkModel): __abstract__ = True first_name = Column(db.String(100)) last_name = Column(db.String(100)) pronouns = Column(db.String(50)) email = Column(db.String(100)) mobile_phone = Column(db.String(15)) address = Column(db.String(255)) address2 = Column(db.String(255)) city = Column(db.String(100)) state = Column(db.String(50)) zip_code = Column(db.String(20))
class Locality(SurrogatePK, Model): """The localities.""" __tablename__ = 'localities' name = Column(db.String(80), nullable=False) postal_code = Column(db.Integer(), nullable=False) province_id = reference_col('provinces', nullable=False) province = relationship('Province', backref='localities') def __init__(self, name, **kwargs): """Create instance.""" db.Model.__init__(self, name=name, **kwargs) def __repr__(self): return '<Locality({name})>'.format(name=self.name)
class Comment(Model, SurrogatePK): __tablename__ = 'comment' id = db.Column(db.Integer, primary_key=True) body = Column(db.Text) createdAt = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow) updatedAt = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow) author_id = reference_col('userprofile', nullable=False) author = relationship('UserProfile', backref=db.backref('comments')) article_id = reference_col('article', nullable=False) def __init__(self, article, author, body, **kwargs): db.Model.__init__(self, author=author, body=body, article=article, **kwargs)
class DrivingLicense(SurrogatePK, Model): """The driving license.""" __tablename__ = 'driving_licenses_types' type = Column(db.String(4), nullable=True) def __init__(self, type, **kwargs): """Create instance.""" db.Model.__init__(self, type=type, **kwargs) def __repr__(self): return '<DivingLicense({type})>'.format(type=self.type)
class Province(SurrogatePK, Model): """The provinces.""" __tablename__ = 'provinces' name = Column(db.String(80), nullable=False) def __init__(self, name, **kwargs): """Create instance.""" db.Model.__init__(self, name=name, **kwargs) def __repr__(self): return '<Province({name})>'.format(name=self.name)
class EyeColor(SurrogatePK, Model): """The hair colors.""" __tablename__ = 'eye_colors' name = Column(db.String(80), nullable=True) def __init__(self, name, **kwargs): """Create instance.""" db.Model.__init__(self, name=name, **kwargs) def __repr__(self): return '<EyeColor({name})>'.format(name=self.name)
class FootSize(SurrogatePK, Model): """The hair colors.""" __tablename__ = 'foot_sizes' size = Column(db.String(3), nullable=True) def __init__(self, size, **kwargs): """Create instance.""" db.Model.__init__(self, size=size, **kwargs) def __repr__(self): return '<FootSize({size})>'.format(size=self.size)
class User(SurrogatePK, Model): __tablename__ = 'users' username = Column(db.String(80), unique=True, nullable=False) email = Column(db.String(100), unique=True, nullable=False) password = Column(db.Binary(128), nullable=True) created_at = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow) updated_at = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow) bio = Column(db.String(300), nullable=True) image = Column(db.String(120), nullable=True) def __init__(self, username, email, password=None, **kwargs): """Create instance.""" db.Model.__init__(self, username=username, email=email, **kwargs) if password: self.set_password(password) else: self.password = None def set_password(self, password): """Set password.""" self.password = bcrypt.generate_password_hash(password) def check_password(self, value): """Check password.""" return bcrypt.check_password_hash(self.password, value) @property def token(self): return _default_jwt_encode_handler(self).decode('utf-8') def __repr__(self): """Represent instance as a unique string.""" return '<User({username!r})>'.format(username=self.username)
class Paper(Model): __tablename__ = 'papers' id = Column(db.Integer, primary_key=True) author = Column(db.Str, primary_key=True) title = Column(db.Str, nullable=True) subject = Column(db.Text, nullable=True) keywords = Column(db.String, nullable=True) pages = Column(db.Integer) # 文献的路径信息可以由文献的DOI号或者是文献标题获取到(判断有无),文件的存放路径应该在配置文件中规定,不存到数据库,可以通过 # property规范 # user.profile.papers 获取到用户的所有信息 owners = relationship( 'UserProfile', secondary=user_assoc, # 个人文库,library backref='papers' ) def __init__(self, author, title, subject, keyword): db.Model.__init__(cls, author=author, title=title) @property def name(self): if self.doi: return self.doi return self.title @property def pdf_path(self): return PDF_PATH.join(self.doi ? self.doi : self.title) @property def html_path(self): return HTML_PATH.join(...) @property def css_path(self): return CSS_PATH.join()
class Accessory(PkModel): __tablename__ = 'accessory' item_name = Column(db.String(200)) donor_id = reference_col('donor') donor = relationship('Donor', backref='accessory')
class Extra(SurrogatePK, Model): """A extra profile.""" __tablename__ = 'extras' user_id = reference_col('users', nullable=True) user = relationship('User', backref='extras') name = Column(db.String(80), nullable=True) last_name = Column(db.String(255), nullable=True) birthday = Column(db.DateTime(), nullable=True) phone = Column(db.Integer(), nullable=True) genre = Column(db.String(6), nullable=True) address = Column(db.String(200), nullable=True) province_id = reference_col('provinces', nullable=True) province = relationship('Province', backref='extras') locality_id = reference_col('localities', nullable=True) locality = relationship('Locality', backref='extras') nationality = Column(db.String(20), nullable=True) vat_number = Column(db.String(11), unique=True, nullable=True) insurance_number = Column(db.BigInteger(), unique=True, nullable=True) height = Column(db.Integer(), nullable=True) weight = Column(db.Integer(), nullable=True) tshirt_size_id = reference_col('tshirt_sizes', nullable=True) tshirt_size = relationship('TshirtSize', backref='extras') trouser_size_id = reference_col('trouser_sizes', nullable=True) trouser_size = relationship('TrouserSize', backref='extras') foot_size_id = reference_col('foot_sizes', nullable=True) foot_size = relationship('FootSize', backref='extras') eye_color_id = reference_col('eye_colors', nullable=True) eye_color = relationship('EyeColor', backref='extras') hair_color_id = reference_col('hair_colors', nullable=True) hair_color = relationship('HairColor', backref='extras') profession = Column(db.String(80), nullable=True) availability = Column(db.String(255), nullable=True) driving_license = Column(db.Boolean(), nullable=True) driving_license_type_id = reference_col('driving_licenses_types', nullable=True) driving_license_type = relationship('DrivingLicense', backref='extras') hobbies = Column(db.String(200), nullable=True) extra_experience = Column(db.Boolean(), nullable=True) dance_experience = Column(db.Boolean(), nullable=True) singing_experience = Column(db.Boolean(), nullable=True) sea_experience = Column(db.Boolean(), nullable=True) waiter_experience = Column(db.Boolean(), nullable=True) other_experience = Column(db.String(80), nullable=True) face_image = Column(db.String(255), nullable=True) body_image = Column(db.String(255), nullable=True) created_at = Column(db.DateTime(), nullable=True, default=dt.datetime.utcnow) def __init__(self, vat_number, **kwargs): """Create instance.""" db.Model.__init__(self, vat_number=vat_number, **kwargs) def __repr__(self): return '<Extra({vat_number})>'.format(vat_number=self.vat_number)
class User(SurrogatePK, Model): """A user of the app.""" __tablename__ = 'users' email = Column(db.String(80), unique=True, nullable=False) password = Column(db.String(255), nullable=True) created_at = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow) is_admin = Column(db.Boolean(), default=False) def __init__(self, email, password=None, **kwargs): """Create instance.""" db.Model.__init__(self, email=email, **kwargs) if password: self.set_password(password) else: self.password = None def set_password(self, password): """Set password.""" self.password = bcrypt.generate_password_hash( password, current_app.config.get('BCRYPT_LOG_ROUNDS') ).decode() def check_password(self, value): """Check password.""" return bcrypt.check_password_hash(self.password, value) def encode_auth_token(self, user_id): try: payload = { 'exp': dt.datetime.utcnow() + dt.timedelta(days=0, seconds=5), 'iat': dt.datetime.utcnow(), 'sub': user_id } return jwt.encode( payload, current_app.config.get('SECRET_KEY'), algorithm='HS256' ) except Exception as e: return e @staticmethod def decode_auth_token(token): """ Validates the auth token :param token: :return: integer|string """ try: payload = jwt.decode(token, current_app.config.get('SECRET_KEY')) return payload['sub'] except jwt.ExpiredSignatureError: return 'Signature expired. Please log in again.' except jwt.InvalidTokenError: return 'Invalid token. Please log in again.' @property def serialize(self): """Return object data in easily serializeable format""" return { 'id': self.id, 'email': self.email, 'created_at': self.created_at, 'active': self.active, 'is_admin': self.is_admin }
class Donor(BasePersonModel): __tablename__ = 'donor' pickup_date = Column(db.DateTime) platform = Column(db.String(100))
class Bike(PkModel): __tablename__ = 'bike' name = Column(db.String(150)) color = Column(db.String(100)) size_chart_to_use = Column(db.String(50)) bottom_tube_length = Column(db.Integer) brakes = Column(db.String(50)) shifters = Column(db.String(50)) water_bottle = Column(db.Boolean) lights = Column(db.Boolean) bell = Column(db.Boolean) rack = Column(db.Boolean) pannier = Column(db.Boolean) kickstand = Column(db.Boolean) notes = Column(db.Text) intake_date = Column(db.DateTime) bike_tech_review_date = Column(db.DateTime) drop_off_date = Column(db.DateTime) mechanic_id = reference_col('mechanic', nullable=True) mechanic = relationship('Mechanic', backref='bike') donor_id = reference_col('donor', nullable=True) donor = relationship('Donor', backref='bike')
class Article(SurrogatePK, Model): __tablename__ = 'article' id = db.Column(db.Integer, primary_key=True) slug = Column(db.Text, unique=True) title = Column(db.String(100), nullable=False) description = Column(db.Text, nullable=False) body = Column(db.Text) createdAt = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow) updatedAt = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow) author_id = reference_col('userprofile', nullable=False) author = relationship('UserProfile', backref=db.backref('articles')) favoriters = relationship('UserProfile', secondary=favoriter_assoc, backref='favorites', lazy='dynamic') tagList = relationship('Tags', secondary=tag_assoc, backref='articles') comments = relationship('Comment', backref=db.backref('article'), lazy='dynamic') def __init__(self, author, title, body, description, slug=None, **kwargs): db.Model.__init__(self, author=author, title=title, description=description, body=body, slug=slug or slugify(title), **kwargs) def favourite(self, profile): if not self.is_favourite(profile): self.favoriters.append(profile) return True return False def unfavourite(self, profile): if self.is_favourite(profile): self.favoriters.remove(profile) return True return False def is_favourite(self, profile): return bool( self.query.filter( favoriter_assoc.c.favoriter == profile.id).count()) def add_tag(self, tag): if tag not in self.tagList: self.tagList.append(tag) return True return False def remove_tag(self, tag): if tag in self.tagList: self.tagList.remove(tag) return True return False @property def favoritesCount(self): return len(self.favoriters.all()) @property def favorited(self): if current_identity: profile = current_identity.profile return self.query.join(Article.favoriters).filter( UserProfile.id == profile.id).count() == 1 return False