-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathrepo.py
72 lines (61 loc) · 2.64 KB
/
repo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from sqlalchemy.dialects.mysql import SMALLINT
from sqlalchemy.sql import expression
from gitmostwanted.app import db
from datetime import datetime
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(20))
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)
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:
if value not in ('promising', 'new', 'unknown', 'deleted', 'hopeless'):
raise ValueError('Unknown status provided')
self.status_updated_at = datetime.now()
if key == 'homepage':
value = value.strip() if value else None
if value and value.find('http') != 0:
value = 'http://' + value
if key == 'description':
value = value[:250] if value else None
super().__setattr__(key, value)
@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', q.all())
return getattr(Repo.language_distinct, 'memoize')
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
)