class User(db.Model): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(120), unique=True) username = db.Column(db.String(80)) github_id = db.Column(db.BigInteger, unique=True)
class UserAttitude(db.Model): __tablename__ = 'users_attitude' user = db.relationship(User) user_id = db.Column(db.Integer, db.ForeignKey('users.id', name='fk_users_id', ondelete='CASCADE'), primary_key=True) repo = db.relationship(Repo, lazy=False) repo_id = db.Column(db.BigInteger, db.ForeignKey('repos.id', name='fk_repos_id', ondelete='CASCADE'), primary_key=True, index=True) attitude = db.Column(db.Enum('like', 'dislike', 'neutral'), nullable=False) @classmethod def join_by_user_and_repo(cls, query, user_id: int, repo_id: int): return query.outerjoin(cls, (cls.user_id == user_id) & (cls.repo_id == repo_id)) @classmethod def list_liked_by_user(cls, user_id: int): """ @type user_id: int @rtype: list """ return cls.query.filter(cls.user_id == user_id).filter( cls.attitude == 'like').all() @staticmethod def like(user_id: int, repo_id: int): return UserAttitude(user_id=user_id, repo_id=repo_id, attitude='like')
class RepoMean(db.Model): __tablename__ = 'repos_mean' repo = db.relationship(Repo) repo_id = db.Column( db.BigInteger, db.ForeignKey('repos.id', name='fk_repos_mean_repo_id', ondelete='CASCADE'), primary_key=True ) created_at = db.Column( db.Date, default=datetime.today().strftime('%Y-%m-%d'), nullable=False, primary_key=True ) value = db.Column(db.Float(), nullable=False)
class RepoStars(db.Model): __tablename__ = 'repos_stars' repo_id = db.Column(db.BigInteger, db.ForeignKey('repos.id', name='fk_repos_stars_repo_id', ondelete='CASCADE'), primary_key=True) stars = db.Column(SMALLINT(display_width=4, unsigned=True), nullable=False) year = db.Column(SMALLINT(display_width=4, unsigned=True), autoincrement=False, nullable=False, primary_key=True) day = db.Column(SMALLINT(display_width=3, unsigned=True), autoincrement=False, nullable=False, primary_key=True)
class ReportBase(db.Model): __abstract__ = True @declared_attr def id(self): return db.Column(db.BigInteger, db.ForeignKey('repos.id'), primary_key=True) @declared_attr def repo(self): return db.relationship(Repo, uselist=False, lazy='joined') cnt_watch = db.Column(db.Integer, nullable=False)
class Repo(db.Model): __tablename__ = 'repos' id = db.Column(db.BigInteger, primary_key=True) checked_at = db.Column(db.DateTime, index=True) created_at = db.Column(db.DateTime, nullable=False, index=True) description = db.Column(db.String(250)) forks_count = db.Column(INTEGER(unsigned=True), nullable=False, server_default='0', index=True) full_name = db.Column(db.String(120), nullable=False) homepage = db.Column(db.String(150)) html_url = db.Column(db.String(150), nullable=False) language = db.Column(db.String(25)) last_reset_at = db.Column(db.DateTime, index=True) mature = db.Column(db.Boolean, nullable=False, server_default=expression.false(), index=True) name = db.Column(db.String(80), nullable=False) open_issues_count = db.Column(INTEGER(unsigned=True), nullable=False, server_default='0') size = db.Column(INTEGER(unsigned=True), nullable=False, server_default='0') stargazers_count = db.Column(INTEGER(unsigned=True), nullable=False, server_default='0', index=True) status = db.Column(db.Enum('promising', 'new', 'unknown', 'deleted', 'hopeless'), server_default='new', nullable=False, index=True) status_updated_at = db.Column(db.DateTime) subscribers_count = db.Column(INTEGER(unsigned=True), nullable=False, server_default='0', index=True) worth = db.Column(SMALLINT(display_width=2), index=True, nullable=False, server_default=str( app.config['REPOSITORY_WORTH_DEFAULT'])) worth_max = db.Column(SMALLINT(display_width=2), nullable=False, server_default='0') def __setattr__(self, key, value): if key == 'status' and self.status != value: value = str(Status(value)) self.status_updated_at = datetime.now() if key == 'homepage': value = str(Url(value)) if value else None if key == 'description': value = str(TextWithoutSmilies(str(TextNormalized( value[:250])))) if value else None if key == 'worth' and self.worth_max < value: self.worth_max = value super().__setattr__(key, value) @classmethod def filter_by_args(cls, q, args: ImmutableMultiDict): lang = args.get('lang') if lang != 'All' and (lang, ) in cls.language_distinct(): q = q.filter(cls.language == lang) status = args.get('status') if status in ('promising', 'hopeless'): q = q.filter(cls.status == status) if bool(args.get('mature')): q = q.filter(cls.mature.is_(True)) try: q = q.filter( cls.full_name.like(str(SearchTerm(args.get('term', ''))))) except ValueError: pass return q @staticmethod def language_distinct(): if not hasattr(Repo.language_distinct, 'memoize'): q = db.session.query(Repo.language).distinct().filter( Repo.language.isnot(None)) setattr(Repo.language_distinct, 'memoize', sorted(q.all())) return getattr(Repo.language_distinct, 'memoize') @classmethod def get_one_by_full_name(cls, name): return cls.query.filter(cls.full_name == name).first()
def id(self): return db.Column(db.BigInteger, db.ForeignKey('repos.id'), primary_key=True)
class Repo(db.Model): __tablename__ = 'repos' __table_args__ = { 'mysql_engine': 'InnoDB', 'mysql_charset': 'utf8', 'mysql_collate': 'utf8_general_ci' } id = db.Column(db.BigInteger, primary_key=True) name = db.Column(db.String(80), nullable=False) language = db.Column(db.String(25)) full_name = db.Column(db.String(120), nullable=False) description = db.Column(db.String(250)) html_url = db.Column(db.String(150), nullable=False) homepage = db.Column(db.String(150)) created_at = db.Column(db.DateTime, nullable=False, index=True) checked_at = db.Column(db.DateTime, index=True) mature = db.Column(db.Boolean, nullable=False, server_default=expression.false(), index=True) worth = db.Column(SMALLINT(display_width=1), nullable=False, server_default='3', index=True) stargazers_count = db.Column(db.Integer, nullable=False, server_default='0') status_updated_at = db.Column(db.DateTime) status = db.Column(db.Enum('promising', 'new', 'unknown', 'deleted', 'hopeless'), server_default='new', nullable=False, index=True) def __setattr__(self, key, value): if key == 'status' and self.status != value: value = str(Status(value)) self.status_updated_at = datetime.now() if key == 'homepage': value = str(Url(value)) if value else None if key == 'description': value = str(TextWithoutSmilies(value[:250])) if value else None super().__setattr__(key, value) @classmethod def filter_by_args(cls, q, args: ImmutableMultiDict): lang = args.get('lang') if lang != 'All' and (lang, ) in cls.language_distinct(): q = q.filter(cls.language == lang) status = args.get('status') if status in ('promising', 'hopeless'): q = q.filter(cls.status == status) if bool(args.get('mature')): q = q.filter(cls.mature.is_(True)) try: q = q.filter( cls.full_name.like(str(SearchTerm(args.get('term', ''))))) except ValueError: pass return q @staticmethod def language_distinct(): if not hasattr(Repo.language_distinct, 'memoize'): q = db.session.query(Repo.language).distinct().filter( Repo.language.isnot(None)) setattr(Repo.language_distinct, 'memoize', sorted(q.all())) return getattr(Repo.language_distinct, 'memoize') @classmethod def get_one_by_full_name(cls, name): return cls.query.filter(cls.full_name == name).first()