class Project(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), unique=True) description = db.Column(db.String(200)) creation_date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) created_on = db.Column(db.DateTime) tasks = db.relationship('Task', backref='in_project') users = db.relationship('User', secondary=user_project, backref=db.backref('assigned_projects', lazy='dynamic'))
class User(db.Model): __tablename__ = 'user' id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(255), unique=True) password = db.Column(db.String(255)) active = db.Column(db.Boolean(), default=False) confirmed_at = db.Column(db.DateTime()) accounts = db.relationship('Account', backref='user', lazy=True) parent_id = db.Column(db.Integer, db.ForeignKey('user.id')) affiliates = db.relationship("User") def __repr__(self): return '<User %r>' % self.email
class Role(db.Model): __tablename__ = 'roles' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64), unique=True) default = db.Column(db.Boolean, default=False, index=True) permissions = db.Column(db.Integer) users = db.relationship('User', backref='role', lazy='dynamic') @staticmethod def insert_roles(): roles = { 'User': (Permission.FOLLOW | Permission.COMMENT | Permission.WRITE_ARTICLES, True), 'Moderator': (Permission.FOLLOW | Permission.COMMENT | Permission.WRITE_ARTICLES | Permission.MODERATE_COMMENTS, False), 'Administrator': (0xff, False) } for r in roles: role = Role.query.filter_by(name=r).first() if role is None: role = Role(name=r) role.permissions = roles[r][0] role.default = roles[r][1] db.session.add(role) db.session.commit() def __repr__(self): return '<Role %r>' % self.name
class Artical_class(db.Model): __tablename__ = 'artical_class' ac_id = db.Column(db.Integer, primary_key=True, autoincrement=True) ac_class = db.Column(db.String(64), nullable=False) articals = db.relationship('Artical', backref='artical_class') def __init__(self, ac_class): self.ac_class = ac_class
class Role(db.Model): __tablename__ = 'roles' role_id = db.Column(db.Integer, primary_key=True, autoincrement=True) role_name = db.Column(db.String(64), unique=True) users = db.relationship('Users', backref='role') def __init__(self, role_name): self.role_name = role_name
class Account(db.Model): __tablename__ = 'account' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(80), unique=True, nullable=False) password = db.Column(db.String(80), nullable=False) settings = db.Column(db.Text(), nullable=False) timetables = db.relationship('TimeTable', backref='account', lazy=True) running = db.relationship('Running', backref='account', lazy=True) timestamp = db.Column(db.TIMESTAMP, nullable=False) started = db.Column(db.Boolean, default=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) credit = db.Column(db.Integer, default=0) subscription = db.Column(db.String(10), nullable=False, unique=True) paid = db.Column(db.Boolean, default=False) def __repr__(self): return '<Account %r>' % self.username
class Dish(db.Model): __tablename__ = "dishes" id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String, nullable=False) price = db.Column(db.Integer, nullable=False) description = db.Column(db.String, nullable=False) picture = db.Column(db.String, nullable=False) category_id = db.Column(db.Integer, db.ForeignKey("categories.id")) category = db.relationship("Category")
class Role(db.Model): __tablename__ = 'role' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64)) status = db.Column(db.Integer, default=1) created_time = db.Column(db.Integer, default=time.time()) updated_time = db.Column(db.Integer, default=time.time()) users = db.relationship('User', backref='role', lazy='dynamic') def __init__(self, name): self.name = name
class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) # personal info first_name = db.Column(db.String(100)) last_name = db.Column(db.String(100)) email = db.Column(db.String(100), unique=True) password = db.Column(db.String(100)) re_pass = db.Column(db.String(100)) tasks = db.relationship('Task', backref='assigned_to')
class Employee(db.Model): id = db.Column(db.Integer, primary_key=True) telegram_id = db.Column(db.Integer, unique=True, nullable=False) name = db.Column(db.String(80), nullable=False) added = db.Column(db.DateTime, default=datetime.datetime.now()) logs = db.relationship('Log', backref='employee') def __init__(self, telegram_id, name): self.telegram_id = telegram_id self.name = name def __repr__(self): return '<Employee name is {}>'.format(self.name)
class Order(db.Model): __tablename__ = "orders" id = db.Column(db.Integer, primary_key=True) date = db.Column(db.String, nullable=False) sum_price = db.Column(db.Integer, nullable=False) status = db.Column(db.Integer, nullable=False) mail = db.Column(db.String, nullable=False) name = db.Column(db.String, nullable=False) phone = db.Column(db.String, nullable=False) address = db.Column(db.String, nullable=False) user = db.relationship("User") user_id = db.Column(db.Integer, db.ForeignKey("users.id")) dishes = db.Column(db.JSON)
class Artical(db.Model): __tablename__ = 'articals' artical_id = db.Column(db.Integer, primary_key=True, autoincrement=True) artical_name = db.Column(db.String(64), nullable=False) artical_summary = db.Column(db.Text, nullable=False) artical_time = db.Column(db.DateTime, nullable=False) artical_content = db.Column(db.Text, nullable=False) acid = db.Column(db.Integer, db.ForeignKey(Artical_class.ac_id)) artical_comment = db.relationship('Comment', backref='artical') def __init__(self, artical_name, artical_content, artical_time, artical_summary,acid): self.artical_name = artical_name self.artical_content = artical_content self.artical_time = artical_time self.artical_summary = artical_summary self.acid=acid
class User(db.Model): __tablename__ = "users" id = db.Column(db.Integer, primary_key=True) mail = db.Column(db.String, nullable=False) password = db.Column(db.String, nullable=False) orders = db.relationship("Order") password_hash = db.Column(db.String(128), nullable=False) @property def password_(self): raise AttributeError("Вам не нужно знать пароль!") @password_.setter def password_(self, password_): self.password_ = generate_password_hash(password_) def password_valid(self, password_): return check_password_hash(self.password_hash, password_)
class Post(db.Model): __tablename__ = 'posts' id = db.Column(db.Integer, primary_key=True) body = db.Column(db.Text) timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow) author_id = db.Column(db.Integer, db.ForeignKey('users.id')) body_html = db.Column(db.Text) comments = db.relationship('Comment', backref='post', lazy='dynamic') @staticmethod def on_changed_body(target, value, oldvalue, initiator): allowed_tags = [ 'a', 'abbr', 'acronym', 'b', 'blockquote', 'code', 'em', 'i', 'li', 'ol', 'pre', 'strong', 'ul', 'h1', 'h2', 'h3', 'p' ] target.body_html = bleach.linkify( bleach.clean(markdown(value, output_format='html'), tags=allowed_tags, strip=True))
class Users(UserMixin,db.Model): __tablename__ = 'users' user_id = db.Column(db.Integer, primary_key=True, autoincrement=True) user_name = db.Column(db.String(64), unique=True, nullable=False) password_hash = db.Column(db.String(256), nullable=False) user_email = db.Column(db.String(64), unique=True, nullable=False) role_id = db.Column(db.Integer, db.ForeignKey(Role.role_id)) comments = db.relationship('Comment', backref='user') # def __init__(self, user_name, user_email): # self.user_name = user_name # # self.password = password # self.user_email = user_email @property def password(self): raise AttributeError('password is not a readable attribute') @password.setter def password(self, password): self.password_hash = generate_password_hash(password) def verify_password(self, password): return check_password_hash(self.password_hash, password)
class User(UserMixin, db.Model): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(64), unique=True, index=True) username = db.Column(db.String(64), unique=True, index=True) password_hash = db.Column(db.String(128)) confirmed = db.Column(db.Boolean, default=False) role_id = db.Column(db.Integer, db.ForeignKey('roles.id')) location = db.Column(db.String(64)) name = db.Column(db.String(64)) about_me = db.Column(db.Text()) member_since = db.Column(db.DateTime(), default=datetime.utcnow) last_seen = db.Column(db.DateTime(), default=datetime.utcnow) avatar_hash = db.Column(db.String(32)) posts = db.relationship('Post', backref='author', lazy='dynamic') followed = db.relationship('Follow', foreign_keys=[Follow.follower_id], backref=db.backref('follower', lazy='joined'), lazy='dynamic', cascade='all, delete-orphan') followers = db.relationship('Follow', foreign_keys=[Follow.followed_id], backref=db.backref('followed', lazy='joined'), lazy='dynamic', cascade='all, delete-orphan') comments = db.relationship('Comment', backref='author', lazy='dynamic') def __init__(self, **kwargs): super(User, self).__init__(**kwargs) if self.role is None: if self.email == current_app.config['FLASKY_ADMIN']: self.role = Role.query.filter_by(permissions=0xff).first() if self.role is None: self.role = Role.query.filter_by(default=True).first() if self.email is not None and self.avatar_hash is None: self.avatar_hash = hashlib.md5( self.email.encode('utf-8')).hexdigest() @property def followed_posts(self): return Post.query.join(Follow, Follow.followed_id == Post.author_id)\ .filter(Follow.follower_id == self.id) def ping(self): self.last_seen = datetime.utcnow() db.session.add(self) db.session.commit() def gravatar(self, size=100, default='identicon', rating='g'): if request.is_secure: url = 'https://secure.gravatar.com/avatar' else: url = 'http://www.gravatar.com/avatar' hash = self.avatar_hash or hashlib.md5( self.email.encode('utf-8')).hexdigest() return '{url}/{hash}?s={size}&d={default}&r={rating}'.format( url=url, hash=hash, size=size, default=default, rating=rating) @property def password(self): raise AttributeError(u'密码为不可读字段') @password.setter def password(self, password): self.password_hash = generate_password_hash(password) def verify_password(self, password): return check_password_hash(self.password_hash.encode('utf-8'), password) def confirm(self, token): s = Serializer(current_app.config['SECRET_KEY']) try: data = s.loads(token) except: return False if data.get('confirm') != self.id: return False self.confirmed = True db.session.add(self) return True def generate_confirmation_token(self, expiration=3600): s = Serializer(current_app.config['SECRET_KEY'], expiration) return s.dumps({'confirm': self.id}) @classmethod def get_user_by_token(cls, token): s = Serializer(current_app.config['SECRET_KEY']) try: data = s.loads(token) except: return None user_id = data.get('confirm') user = User.query.filter_by(id=user_id).first() return user def can(self, permissions): return self.role is not None and \ (self.role.permissions & permissions) == permissions def is_administrator(self): return self.can(Permission.ADMINISTER) def follow(self, user): if not self.is_following(user): f = Follow(follower=self, followed=user) db.session.add(f) def unfollow(self, user): f = self.followed.filter_by(followed_id=user.id).first() if f: db.session.delete(f) def is_following(self, user): return self.followed.filter_by(followed_id=user.id).first() is not None def is_followed_by(self, user): return self.followers.filter_by( follower_id=user.id).first() is not None
class Category(db.Model): __tablename__ = "categories" id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String, nullable=False) meals = db.relationship("Dish")