-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.py
293 lines (212 loc) · 10.6 KB
/
model.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
"""Models and database functions for git_data db."""
import datetime
from flask_sqlalchemy import SQLAlchemy
import config
db = SQLAlchemy()
class Repo(db.Model):
"""Repository model."""
__tablename__ = "repos"
repo_id = db.Column(db.Integer, primary_key=True)
owner_id = db.Column(db.ForeignKey("users.user_id"), nullable=False)
name = db.Column(db.Text, nullable=False)
description = db.Column(db.Text, nullable=True)
created_at = db.Column(db.DateTime(), nullable=True)
updated_at = db.Column(db.DateTime(), nullable=True)
pushed_at = db.Column(db.DateTime(), nullable=True)
last_updated = db.Column(db.DateTime(), nullable=True,
default=datetime.datetime.utcnow)
last_crawled = db.Column(db.DateTime(), nullable=True)
last_crawled_depth = db.Column(db.Integer, nullable=True)
url = db.Column(db.Text, nullable=True)
stargazers_count = db.Column(db.Integer, nullable=True)
owner = db.relationship("User",
backref=db.backref("repos",
order_by="desc(Repo.pushed_at)"))
stargazers = db.relationship("User",
secondary="stargazers",
order_by="User.user_id",
backref=db.backref("stars",
order_by="desc(Repo.repo_id)"))
dislikes = db.relationship("User",
secondary="dislikes",
order_by="User.user_id",
backref=db.backref("dislikes",
order_by="desc(Repo.repo_id)"))
watchers = db.relationship("User",
secondary="watchers",
order_by="User.user_id",
backref=db.backref("watches",
order_by="desc(Repo.repo_id)"))
contributors = db.relationship("User",
secondary="contributors",
order_by="User.user_id",
backref=db.backref("contributions",
order_by="desc(Repo.repo_id)"))
languages = db.relationship("Language",
secondary="repo_languages",
order_by="Language.language_name",
backref=db.backref("repos",
order_by="desc(Repo.repo_id)"))
repo_langs = db.relationship("RepoLanguage",
order_by="desc(RepoLanguage.language_bytes)",
backref=db.backref("repos",
order_by="desc(Repo.repo_id)"))
def __repr__(self): # pragma: no cover
"""Provide helpful representation when printed."""
return ("<Repo {} owner={} name={}>"
.format(self.repo_id,
self.owner_id,
self.name))
class User(db.Model):
"""User model."""
__tablename__ = "users"
user_id = db.Column(db.Integer, primary_key=True)
login = db.Column(db.String(50), nullable=False)
name = db.Column(db.Text, nullable=True)
created_at = db.Column(db.DateTime(), nullable=True)
updated_at = db.Column(db.DateTime(), nullable=True,
default=datetime.datetime.utcnow)
last_crawled = db.Column(db.DateTime(), nullable=True)
last_crawled_depth = db.Column(db.Integer, nullable=True)
last_crawled_user_repos = db.Column(db.DateTime(), nullable=True)
followers = db.relationship("User",
secondary="followers",
order_by=user_id,
primaryjoin="User.user_id==followers.c.user_id",
secondaryjoin="User.user_id==followers.c.follower_id",
backref=db.backref("follows",
order_by=user_id))
def __repr__(self): # pragma: no cover
"""Provide helpful representation when printed."""
return ("<User {} login={} name={}>"
.format(self.user_id,
self.login,
self.name))
class Account(db.Model):
"""Account model."""
__tablename__ = "accounts"
account_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
user_id = db.Column(db.ForeignKey("users.user_id"), nullable=False)
access_token = db.Column(db.String(100), nullable=True)
last_login = db.Column(db.DateTime(), nullable=True,
default=datetime.datetime.utcnow)
user = db.relationship("User", backref=db.backref("account"))
def __repr__(self): # pragma: no cover
"""Provide helpful representation when printed."""
return ("<Account {} user_id={}>"
.format(self.account_id,
self.user_id))
class Follower(db.Model):
"""Watcher model."""
__tablename__ = "followers"
follow_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
user_id = db.Column(db.ForeignKey("users.user_id"), nullable=False)
follower_id = db.Column(db.ForeignKey("users.user_id"), nullable=False)
def __repr__(self): # pragma: no cover
"""Provide helpful representation when printed."""
return ("<Follower {} user_id={} follower_id={}>"
.format(self.follow_id,
self.user_id,
self.follower_id))
class Stargazer(db.Model):
"""Stargazer model."""
__tablename__ = "stargazers"
stargazer_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
repo_id = db.Column(db.ForeignKey("repos.repo_id"), nullable=False)
user_id = db.Column(db.ForeignKey("users.user_id"), nullable=False)
user = db.relationship("User", backref=db.backref("stars_sec"))
repo = db.relationship("Repo", backref=db.backref("stargazers_sec"))
__table_args__ = ( db.UniqueConstraint("user_id",
"repo_id",
name="uniq_star_user_repo_id"), )
def __repr__(self): # pragma: no cover
"""Provide helpful representation when printed."""
return ("<Stargazer {} repo_id={} user_id={}>"
.format(self.stargazer_id,
self.repo_id,
self.user_id))
class Watcher(db.Model):
"""Watcher model."""
__tablename__ = "watchers"
watcher_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
repo_id = db.Column(db.ForeignKey("repos.repo_id"), nullable=False)
user_id = db.Column(db.ForeignKey("users.user_id"), nullable=False)
def __repr__(self): # pragma: no cover
"""Provide helpful representation when printed."""
return ("<Watcher {} repo_id={} user_id={}>"
.format(self.watcher_id,
self.repo_id,
self.user_id))
class Contributor(db.Model):
"""Contributor model."""
__tablename__ = "contributors"
contributor_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
repo_id = db.Column(db.ForeignKey("repos.repo_id"), nullable=False)
user_id = db.Column(db.ForeignKey("users.user_id"), nullable=False)
def __repr__(self): # pragma: no cover
"""Provide helpful representation when printed."""
return ("<Contributor {} repo_id={} user_id={}>"
.format(self.contributor_id,
self.repo_id,
self.user_id))
class Dislike(db.Model):
"""Dislike model."""
__tablename__ = "dislikes"
dislike_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
repo_id = db.Column(db.ForeignKey("repos.repo_id"), nullable=False)
user_id = db.Column(db.ForeignKey("users.user_id"), nullable=False)
__table_args__ = ( db.UniqueConstraint("user_id",
"repo_id",
name="uniq_dislike_user_repo_id"), )
def __repr__(self): # pragma: no cover
"""Provide helpful representation when printed."""
return ("<Dislike {} repo_id={} user_id={}>"
.format(self.dislike_id,
self.repo_id,
self.user_id))
class Language(db.Model):
"""Languages model."""
__tablename__ = "languages"
language_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
language_name = db.Column(db.String(100), nullable=False, unique=True)
def __repr__(self): # pragma: no cover
"""Provide helpful representation when printed."""
return ("<Language {} name={}>"
.format(self.language_id,
self.language_name))
class RepoLanguage(db.Model):
"""Association table between Repository and Language."""
__tablename__ = "repo_languages"
repo_language_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
repo_id = db.Column(db.Integer,
db.ForeignKey("repos.repo_id"),
nullable=False)
language_id = db.Column(db.Integer,
db.ForeignKey("languages.language_id"),
nullable=False)
language_bytes = db.Column(db.Integer, nullable=True)
language = db.relationship("Language")
def __repr__(self): # pragma: no cover
"""Provide helpful representation when printed."""
return ("<RepoLanguage {} repo_id={} language_id={} bytes={}>"
.format(self.repo_language_id,
self.repo_id,
self.language_id,
self.language_bytes))
def connect_to_db(app, uri=config.DB_URI):
"""Connect the database to our Flask app."""
# Configure to use our database.
app.config["SQLALCHEMY_DATABASE_URI"] = uri
app.config["SQLALCHEMY_ECHO"] = False
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db.app = app
db.init_app(app)
db.create_all()
if __name__ == "__main__": # pragma: no cover
# As a convenience, if we run this module interactively, it will leave
# you in a state of being able to work with the database directly.
# So that we can use Flask-SQLAlchemy, we'll make a Flask app.
from flask import Flask
app = Flask(__name__)
connect_to_db(app)
print("Connected to DB {}.".format(config.DB_URI))