class User(db.Model): """User object """ id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(120), index=True, unique=True, nullable=False) password = db.Column(db.String(60), index=True, nullable=False) is_verified = db.Column(db.Boolean(), default=False, nullable=False) password_reset_requests = db.relationship('PasswordResetRequest', backref=db.backref('user')) email_verification_requests = db.relationship('EmailVerificationRequest', backref=db.backref('user')) def __repr__(self): return '<User %r>' % self.email # =========================== # Flask-Login methods # =========================== def is_authenticated(self): return True def is_active(self): return True def is_anonymous(self): return False def get_id(self): return unicode(self.id)
class User(db.Model): __tablename__ = 'user' id = db.Column(db.Integer, primary_key=True) account_id = db.Column(db.Integer, unique=True) user_id = db.Column(db.Integer) username = db.Column(db.String(100)) role = db.Column(db.String(30)) is_banned = db.Column(db.Boolean) end_ban_date = db.Column(db.DateTime, nullable=True) reputation = db.Column(db.Integer) profile_image = db.Column(db.String(200)) profile_link = db.Column(db.String(200)) def __init__(self, account_id, user_id, username, reputation, profile_image, profile_link, role="user", is_banned=False): self.account_id = account_id self.user_id = user_id self.username = username self.reputation = reputation self.profile_image = profile_image self.profile_link = profile_link self.role = role self.is_banned = is_banned def __repr__(self): return '<User %r>' % str(self.id)
class User(db.Model): __tablename__ = 'user' id = db.Column(db.Integer, primary_key=True) account_id = db.Column(db.Integer, unique=True) user_id = db.Column(db.Integer) username = db.Column(db.String(100)) role = db.Column(db.String(30)) is_banned = db.Column(db.Boolean, default=False) end_ban_date = db.Column(db.DateTime, nullable=True) reputation = db.Column(db.Integer) profile_image = db.Column(db.String(200)) profile_link = db.Column(db.String(200)) creation_date = db.Column(db.DateTime, nullable=False) def __init__(self, account_id, user_id, username, reputation, profile_image, profile_link, role="user", is_banned=False): self.creation_date = datetime.datetime.now() self.account_id = account_id self.user_id = user_id self.username = username self.reputation = reputation self.profile_image = profile_image self.profile_link = profile_link self.role = role self.is_banned = is_banned def __repr__(self): return '<User %r>' % str(self.id) @staticmethod def is_exist_with_account_id(adder, account_id): return True if adder.session.query(func.count(User.id)).filter_by( account_id=account_id).scalar() > 0 else False @staticmethod def by_account_id(account_id): session = db_session() query = session.query(User).filter_by(account_id=account_id).order_by( desc(User.creation_date)) result = query.first() session.close() return result @staticmethod def by_id(user_id): session = db_session() query = session.query(User).filter_by(id=user_id).order_by( desc(User.creation_date)) result = query.first() session.close() return result
class EmailVerificationRequest(db.Model): """EmailVerificationRequest object """ key = db.Column(db.String(64), primary_key=True) fk_user = db.Column(db.Integer, db.ForeignKey('user.id')) create_ts = db.Column(db.DateTime, default=datetime.datetime.utcnow, \ nullable=False)
class Site(db.Model): __tablename__ = 'site' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100)) url = db.Column(db.String(100)) meta = db.Column(db.String(100)) chat = db.Column(db.String(100)) api_name = db.Column(db.String(100), unique=True) launch_date = db.Column(db.DateTime, nullable=False) creation_date = db.Column(db.DateTime, nullable=False) language = db.Column(db.String(100), unique=True) def __init__(self, name, url, meta, chat, api_name, launch_date, language): self.creation_date = datetime.datetime.now() self.name = name self.url = url self.meta = meta self.chat = chat self.api_name = api_name self.launch_date = launch_date self.language = language @staticmethod def is_exist(adder, api_name): return True if adder.session.query(func.count(Site.id)).filter_by( api_name=api_name).scalar() > 0 else False @staticmethod def by_language(language): session = db_session() query = session.query(Site).filter_by(language=language) result = query.first() session.close() return result @staticmethod def by_api_name(api_name): session = db_session() query = session.query(Site).filter_by(api_name=api_name) result = query.first() session.close() return result @staticmethod def all(): return Site.query.all() def __repr__(self): return '<Site %r>' % str(self.id)
class Question(db.Model): __tablename__ = 'question' question_type_most_viewed = 'most_viewed' question_type_suggested = 'suggested' id = db.Column(db.Integer, primary_key=True) question_id = db.Column(db.Integer) view_count = db.Column(db.Integer) is_associated = db.Column(db.Boolean, default=False) question_type = db.Column(String(50)) # Is used in case of question_type is "suggested" suggested_user_id = Column(Integer, ForeignKey('user.id'), nullable=True) # Allows to remove a question from the list manually can_be_associated = db.Column(db.Boolean, default=True) # Currenly these are not in use. # It seems it could be a good idea # to add some kind of cache and search tags = db.Column(db.String(500)) # In order to track changes we need to know when we updated # a record last time. last_update_date = db.Column(db.DateTime, default=datetime.datetime.now) def __init__(self, question_type, question_id, view_count, suggested_user_id=None, is_associated=False): self.question_type = question_type self.question_id = question_id self.view_count = view_count self.is_associated = is_associated self.can_be_associated = True self.suggested_user_id = suggested_user_id self.last_update_date = datetime.datetime.now() def __repr__(self): return '<Question %s>' % str(self.id)
class Post(db.Model): __tablename__ = 'post' post_type_question = 1 post_type_answer = 2 id = db.Column(db.Integer, primary_key=True) site_id = db.Column(db.Integer, ForeignKey('site.id')) post_id = db.Column(db.Integer) creation_date = db.Column(db.DateTime, nullable=False) post_type = db.Column(db.Integer) owner_id = db.Column(db.Integer) score = db.Column(db.Integer) down_vote_count = db.Column(db.Integer) up_vote_count = db.Column(db.Integer) view_count = db.Column(db.Integer) answer_count = db.Column(db.Integer) is_answered = db.Column(db.Boolean, default=False) tags = db.Column(db.String(500)) is_accepted = db.Column(db.Boolean, default=False) parent_id = db.Column(db.Integer) def __init__(self, site_id, post_id, post_type, creation_date, owner_id, score, down_vote_count, up_vote_count): self.site_id = site_id self.post_id = post_id self.post_type = post_type self.creation_date = creation_date self.owner_id = owner_id self.score = score self.down_vote_count = down_vote_count self.up_vote_count = up_vote_count @staticmethod def question(site_id, question_id, creation_date, score, down_vote_count, up_vote_count, view_count, answer_count, is_answered, tags, owner_id): q = Post(site_id, question_id, Post.post_type_question, creation_date, owner_id, score, down_vote_count, up_vote_count) q.view_count = view_count q.answer_count = answer_count q.is_answered = is_answered q.tags = tags return q @staticmethod def answer(site_id, answer_id, question_id, creation_date, score, down_vote_count, up_vote_count, owner_id, is_accepted): a = Post(site_id, answer_id, Post.post_type_answer, creation_date, owner_id, score, down_vote_count, up_vote_count) a.is_accepted = is_accepted a.parent_id = question_id return a @staticmethod def last(site_id): session = db_session() query = session.query(Post).filter_by(site_id=site_id).order_by( desc(Post.creation_date)) result = query.first() session.close() return result @staticmethod def is_exist(adder, site_id, post_id): return True if adder.session.query(func.count(Post.id)).filter_by( site_id=site_id).filter_by(post_id=post_id).scalar() > 0 else False def __repr__(self): return '<Post %r>' % str(self.id)