By using this site, you agree to have cookies stored on your device, strictly for functional purposes, such as storing your session and preferences.

Dismiss

 models.py

View raw Download
text/x-script.python • 13.19 kiB
Python script, ASCII text executable
        
            
1
from app import app, db, bcrypt
2
import git
3
from datetime import datetime
4
from enum import Enum
5
from PIL import Image
6
from cairosvg import svg2png
7
import os
8
import config
9
import cairosvg
10
import random
11
12
__all__ = [
13
"RepoAccess",
14
"RepoFavourite",
15
"Repo",
16
"UserFollow",
17
"UserNotification",
18
"User",
19
"Notification",
20
"PostVote",
21
"Post",
22
"Commit",
23
"PullRequest",
24
]
25
26
with (app.app_context()):
27
class RepoAccess(db.Model):
28
id = db.Column(db.Integer, primary_key=True)
29
user_username = db.Column(db.String(32), db.ForeignKey("user.username"), nullable=False)
30
repo_route = db.Column(db.String(98), db.ForeignKey("repo.route"), nullable=False)
31
access_level = db.Column(db.SmallInteger(), nullable=False) # 0 read-only, 1 read-write, 2 admin
32
33
user = db.relationship("User", back_populates="repo_access")
34
repo = db.relationship("Repo", back_populates="repo_access")
35
36
__table_args__ = (db.UniqueConstraint("user_username", "repo_route", name="_user_repo_uc"),)
37
38
def __init__(self, user, repo, level):
39
self.user_username = user.username
40
self.repo_route = repo.route
41
self.access_level = level
42
43
44
class RepoFavourite(db.Model):
45
id = db.Column(db.Integer, primary_key=True)
46
user_username = db.Column(db.String(32), db.ForeignKey("user.username"), nullable=False)
47
repo_route = db.Column(db.String(98), db.ForeignKey("repo.route"), nullable=False)
48
49
notify_commit = db.Column(db.Boolean)
50
51
user = db.relationship("User", back_populates="favourites")
52
repo = db.relationship("Repo", back_populates="favourites")
53
54
__table_args__ = (db.UniqueConstraint("user_username", "repo_route", name="_user_repo_uc1"),)
55
56
def __init__(self, user, repo):
57
self.user_username = user.username
58
self.repo_route = repo.route
59
60
61
class PostVote(db.Model):
62
id = db.Column(db.Integer, primary_key=True)
63
user_username = db.Column(db.String(32), db.ForeignKey("user.username"), nullable=False)
64
post_identifier = db.Column(db.String(109), db.ForeignKey("post.identifier"), nullable=False)
65
vote_score = db.Column(db.SmallInteger(), nullable=False)
66
67
user = db.relationship("User", back_populates="votes")
68
post = db.relationship("Post", back_populates="votes")
69
70
__table_args__ = (db.UniqueConstraint("user_username", "post_identifier", name="_user_post_uc"),)
71
72
def __init__(self, user, post, score):
73
self.user_username = user.username
74
self.post_identifier = post.identifier
75
self.vote_score = score
76
77
78
class User(db.Model):
79
username = db.Column(db.String(32), unique=True, nullable=False, primary_key=True)
80
display_name = db.Column(db.Unicode(128), unique=False, nullable=True)
81
bio = db.Column(db.Unicode(16384), unique=False, nullable=True)
82
password_hashed = db.Column(db.String(60), nullable=False)
83
email = db.Column(db.String(254), nullable=True)
84
company = db.Column(db.Unicode(64), nullable=True)
85
company_url = db.Column(db.String(256), nullable=True)
86
url = db.Column(db.String(256), nullable=True)
87
show_mail = db.Column(db.Boolean, default=False, nullable=False)
88
location = db.Column(db.Unicode(64), nullable=True)
89
creation_date = db.Column(db.DateTime, default=datetime.utcnow)
90
91
repositories = db.relationship("Repo", back_populates="owner")
92
followers = db.relationship("UserFollow", back_populates="followed", foreign_keys="[UserFollow.followed_username]")
93
follows = db.relationship("UserFollow", back_populates="follower", foreign_keys="[UserFollow.follower_username]")
94
repo_access = db.relationship("RepoAccess", back_populates="user")
95
votes = db.relationship("PostVote", back_populates="user")
96
favourites = db.relationship("RepoFavourite", back_populates="user")
97
98
commits = db.relationship("Commit", back_populates="owner")
99
posts = db.relationship("Post", back_populates="owner")
100
prs = db.relationship("PullRequest", back_populates="owner")
101
notifications = db.relationship("UserNotification", back_populates="user")
102
103
def __init__(self, username, password, email=None, display_name=None):
104
self.username = username
105
self.password_hashed = bcrypt.generate_password_hash(password, config.HASHING_ROUNDS).decode("utf-8")
106
self.email = email
107
self.display_name = display_name
108
109
# Create the user's directory
110
if not os.path.exists(os.path.join(config.REPOS_PATH, username)):
111
os.makedirs(os.path.join(config.REPOS_PATH, username))
112
if not os.path.exists(os.path.join(config.USERDATA_PATH, username)):
113
os.makedirs(os.path.join(config.USERDATA_PATH, username))
114
115
avatar_name = random.choice(os.listdir(config.DEFAULT_AVATARS_PATH))
116
if os.path.join(config.DEFAULT_AVATARS_PATH, avatar_name).endswith(".svg"):
117
cairosvg.svg2png(url=os.path.join(config.DEFAULT_AVATARS_PATH, avatar_name),
118
write_to="/tmp/roundabout-avatar.png")
119
avatar = Image.open("/tmp/roundabout-avatar.png")
120
else:
121
avatar = Image.open(os.path.join(config.DEFAULT_AVATARS_PATH, avatar_name))
122
avatar.thumbnail(config.AVATAR_SIZE)
123
avatar.save(os.path.join(config.USERDATA_PATH, username, "avatar.png"))
124
125
126
class Repo(db.Model):
127
route = db.Column(db.String(98), unique=True, nullable=False, primary_key=True)
128
owner_name = db.Column(db.String(32), db.ForeignKey("user.username"), nullable=False)
129
name = db.Column(db.String(64), nullable=False)
130
owner = db.relationship("User", back_populates="repositories")
131
visibility = db.Column(db.SmallInteger(), nullable=False)
132
info = db.Column(db.Unicode(512), nullable=True)
133
url = db.Column(db.String(256), nullable=True)
134
creation_date = db.Column(db.DateTime, default=datetime.utcnow)
135
136
default_branch = db.Column(db.String(64), nullable=True, default="")
137
138
commits = db.relationship("Commit", back_populates="repo")
139
posts = db.relationship("Post", back_populates="repo")
140
repo_access = db.relationship("RepoAccess", back_populates="repo")
141
favourites = db.relationship("RepoFavourite", back_populates="repo")
142
heads = db.relationship("PullRequest", back_populates="head", foreign_keys="[PullRequest.head_route]")
143
bases = db.relationship("PullRequest", back_populates="base", foreign_keys="[PullRequest.base_route]")
144
145
last_post_id = db.Column(db.Integer, nullable=False, default=0)
146
147
def __init__(self, owner, name, visibility):
148
self.route = f"/{owner.username}/{name}"
149
self.name = name
150
self.owner_name = owner.username
151
self.owner = owner
152
self.visibility = visibility
153
154
# Add the owner as an admin
155
repo_access = RepoAccess(owner, self, 2)
156
db.session.add(repo_access)
157
158
159
class Commit(db.Model):
160
identifier = db.Column(db.String(227), unique=True, nullable=False, primary_key=True)
161
sha = db.Column(db.String(128), nullable=False)
162
repo_name = db.Column(db.String(98), db.ForeignKey("repo.route"), nullable=False)
163
owner_name = db.Column(db.String(32), db.ForeignKey("user.username"), nullable=False)
164
owner_identity = db.Column(db.String(321))
165
receive_date = db.Column(db.DateTime, default=datetime.now)
166
author_date = db.Column(db.DateTime)
167
message = db.Column(db.UnicodeText)
168
repo = db.relationship("Repo", back_populates="commits")
169
owner = db.relationship("User", back_populates="commits")
170
171
def __init__(self, sha, owner, repo, date, message, owner_identity):
172
self.identifier = f"{repo.route}/{sha}"
173
self.sha = sha
174
self.repo_name = repo.route
175
self.repo = repo
176
self.owner_name = owner.username
177
self.owner = owner
178
self.author_date = datetime.fromtimestamp(int(date))
179
self.message = message
180
self.owner_identity = owner_identity
181
182
183
class Post(db.Model):
184
identifier = db.Column(db.String(109), unique=True, nullable=False, primary_key=True)
185
number = db.Column(db.Integer, nullable=False)
186
repo_name = db.Column(db.String(98), db.ForeignKey("repo.route"), nullable=False)
187
owner_name = db.Column(db.String(32), db.ForeignKey("user.username"), nullable=False)
188
votes = db.relationship("PostVote", back_populates="post")
189
vote_sum = db.Column(db.Integer, nullable=False, default=0)
190
191
parent_id = db.Column(db.String(109), db.ForeignKey("post.identifier"), nullable=True)
192
state = db.Column(db.SmallInteger, nullable=True, default=1)
193
194
date = db.Column(db.DateTime, default=datetime.now)
195
last_updated = db.Column(db.DateTime, default=datetime.now)
196
subject = db.Column(db.Unicode(384))
197
message = db.Column(db.UnicodeText)
198
repo = db.relationship("Repo", back_populates="posts")
199
owner = db.relationship("User", back_populates="posts")
200
parent = db.relationship("Post", back_populates="children", remote_side="Post.identifier")
201
children = db.relationship("Post", back_populates="parent", remote_side="Post.parent_id")
202
203
def __init__(self, owner, repo, parent, subject, message):
204
self.identifier = f"{repo.route}/{repo.last_post_id}"
205
self.number = repo.last_post_id
206
self.repo_name = repo.route
207
self.repo = repo
208
self.owner_name = owner.username
209
self.owner = owner
210
self.subject = subject
211
self.message = message
212
self.parent = parent
213
repo.last_post_id += 1
214
215
def update_date(self):
216
self.last_updated = datetime.now()
217
with db.session.no_autoflush:
218
if self.parent is not None:
219
self.parent.update_date()
220
221
222
class UserNotification(db.Model):
223
id = db.Column(db.Integer, primary_key=True)
224
user_username = db.Column(db.String(32), db.ForeignKey("user.username"), nullable=False)
225
notification_id = db.Column(db.BigInteger, db.ForeignKey("notification.id"))
226
attention_level = db.Column(db.SmallInteger, nullable=False) # 0 is read
227
read_time = db.Column(db.DateTime, nullable=True)
228
229
user = db.relationship("User", back_populates="notifications")
230
notification = db.relationship("Notification", back_populates="notifications")
231
232
__table_args__ = (db.UniqueConstraint("user_username", "notification_id", name="_user_notification_uc"),)
233
234
def __init__(self, user, notification, level):
235
self.user_username = user.username
236
self.notification_id = notification.id
237
self.attention_level = level
238
239
def read(self):
240
self.read_time = datetime.utcnow
241
self.attention_level = 0
242
243
244
class UserFollow(db.Model):
245
id = db.Column(db.Integer, primary_key=True)
246
follower_username = db.Column(db.String(32), db.ForeignKey("user.username", ondelete="CASCADE"), nullable=False)
247
followed_username = db.Column(db.String(32), db.ForeignKey("user.username", ondelete="CASCADE"), nullable=False)
248
249
follower = db.relationship("User", back_populates="followers", foreign_keys=[follower_username])
250
followed = db.relationship("User", back_populates="follows", foreign_keys=[followed_username])
251
252
def __init__(self, follower_username, followed_username):
253
self.follower_username = follower_username
254
self.followed_username = followed_username
255
256
257
class Notification(db.Model):
258
id = db.Column(db.BigInteger, primary_key=True, autoincrement=True)
259
data = db.Column(db.dialects.postgresql.JSONB, nullable=False, default={})
260
notifications = db.relationship("UserNotification", back_populates="notification")
261
timestamp = db.Column(db.DateTime, nullable=False, default=datetime.now)
262
263
def __init__(self, json):
264
self.data = json
265
266
267
class PullRequest(db.Model):
268
id = db.Column(db.BigInteger, primary_key=True, autoincrement=True)
269
head_route = db.Column(db.String(98), db.ForeignKey("repo.route", ondelete="CASCADE"), nullable=False)
270
base_route = db.Column(db.String(98), db.ForeignKey("repo.route", ondelete="CASCADE"), nullable=False)
271
owner_name = db.Column(db.String(32), db.ForeignKey("user.username"), nullable=False)
272
273
head = db.relationship("Repo", back_populates="heads", foreign_keys=[head_route])
274
base = db.relationship("Repo", back_populates="bases", foreign_keys=[base_route])
275
276
head_branch = db.Column(db.String(64), nullable=False)
277
base_branch = db.Column(db.String(64), nullable=False)
278
279
owner = db.relationship("User", back_populates="prs")
280
timestamp = db.Column(db.DateTime, nullable=False, default=datetime.now)
281
282
def __init__(self, head, head_branch, base, base_branch, owner):
283
self.head = head
284
self.base = base
285
self.head_branch = head_branch
286
self.base_branch = base_branch
287
self.owner = owner
288