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 • 11.12 kiB
Python script, ASCII text executable
        
            
1
from app import app, db
2
import git
3
from datetime import datetime
4
from enum import Enum
5
from PIL import Image
6
from cairosvg import svg2png
7
8
with app.app_context():
9
class RepoAccess(db.Model):
10
id = db.Column(db.Integer, primary_key=True)
11
userUsername = db.Column(db.String(32), db.ForeignKey("user.username"), nullable=False)
12
repoRoute = db.Column(db.String(98), db.ForeignKey("repo.route"), nullable=False)
13
accessLevel = db.Column(db.SmallInteger(), nullable=False) # 0 read-only, 1 read-write, 2 admin
14
15
user = db.relationship("User", back_populates="repoAccess")
16
repo = db.relationship("Repo", back_populates="repoAccess")
17
18
__table_args__ = (db.UniqueConstraint("userUsername", "repoRoute", name="_user_repo_uc"),)
19
20
def __init__(self, user, repo, level):
21
self.userUsername = user.username
22
self.repoRoute = repo.route
23
self.accessLevel = level
24
25
26
class RepoFavourite(db.Model):
27
id = db.Column(db.Integer, primary_key=True)
28
userUsername = db.Column(db.String(32), db.ForeignKey("user.username"), nullable=False)
29
repoRoute = db.Column(db.String(98), db.ForeignKey("repo.route"), nullable=False)
30
31
user = db.relationship("User", back_populates="favourites")
32
repo = db.relationship("Repo", back_populates="favourites")
33
34
__table_args__ = (db.UniqueConstraint("userUsername", "repoRoute", name="_user_repo_uc1"),)
35
36
def __init__(self, user, repo):
37
self.userUsername = user.username
38
self.repoRoute = repo.route
39
40
41
class PostVote(db.Model):
42
id = db.Column(db.Integer, primary_key=True)
43
userUsername = db.Column(db.String(32), db.ForeignKey("user.username"), nullable=False)
44
postIdentifier = db.Column(db.String(109), db.ForeignKey("post.identifier"), nullable=False)
45
voteScore = db.Column(db.SmallInteger(), nullable=False)
46
47
user = db.relationship("User", back_populates="votes")
48
post = db.relationship("Post", back_populates="votes")
49
50
__table_args__ = (db.UniqueConstraint("userUsername", "postIdentifier", name="_user_post_uc"),)
51
52
def __init__(self, user, post, score):
53
self.userUsername = user.username
54
self.postIdentifier = post.identifier
55
self.voteScore = score
56
57
58
class User(db.Model):
59
username = db.Column(db.String(32), unique=True, nullable=False, primary_key=True)
60
displayName = db.Column(db.Unicode(128), unique=False, nullable=True)
61
bio = db.Column(db.Unicode(512), unique=False, nullable=True)
62
passwordHashed = db.Column(db.String(60), nullable=False)
63
email = db.Column(db.String(254), nullable=True)
64
company = db.Column(db.Unicode(64), nullable=True)
65
companyURL = db.Column(db.String(256), nullable=True)
66
URL = db.Column(db.String(256), nullable=True)
67
showMail = db.Column(db.Boolean, default=False, nullable=False)
68
location = db.Column(db.Unicode(64), nullable=True)
69
creationDate = db.Column(db.DateTime, default=datetime.utcnow)
70
71
repositories = db.relationship("Repo", back_populates="owner")
72
repoAccess = db.relationship("RepoAccess", back_populates="user")
73
votes = db.relationship("PostVote", back_populates="user")
74
generatedNotifications = db.relationship("Notification", back_populates="author")
75
favourites = db.relationship("RepoFavourite", back_populates="user")
76
77
commits = db.relationship("Commit", back_populates="owner")
78
posts = db.relationship("Post", back_populates="owner")
79
notifications = db.relationship("UserNotification", back_populates="user")
80
81
def __init__(self, username, password, email=None, displayName=None):
82
self.username = username
83
self.passwordHashed = bcrypt.generate_password_hash(password, config.HASHING_ROUNDS).decode("utf-8")
84
self.email = email
85
self.displayName = displayName
86
87
# Create the user's directory
88
if not os.path.exists(os.path.join(config.REPOS_PATH, username)):
89
os.makedirs(os.path.join(config.REPOS_PATH, username))
90
if not os.path.exists(os.path.join(config.USERDATA_PATH, username)):
91
os.makedirs(os.path.join(config.USERDATA_PATH, username))
92
93
avatarName = random.choice(os.listdir(config.DEFAULT_AVATARS_PATH))
94
if os.path.join(config.DEFAULT_AVATARS_PATH, avatarName).endswith(".svg"):
95
cairosvg.svg2png(url=os.path.join(config.DEFAULT_AVATARS_PATH, avatarName),
96
write_to="/tmp/roundabout-avatar.png")
97
avatar = Image.open("/tmp/roundabout-avatar.png")
98
else:
99
avatar = Image.open(os.path.join(config.DEFAULT_AVATARS_PATH, avatarName))
100
avatar.thumbnail(config.AVATAR_SIZE)
101
avatar.save(os.path.join(config.USERDATA_PATH, username, "avatar.png"))
102
103
104
class Repo(db.Model):
105
route = db.Column(db.String(98), unique=True, nullable=False, primary_key=True)
106
ownerName = db.Column(db.String(32), db.ForeignKey("user.username"), nullable=False)
107
name = db.Column(db.String(64), nullable=False)
108
owner = db.relationship("User", back_populates="repositories")
109
visibility = db.Column(db.SmallInteger(), nullable=False)
110
info = db.Column(db.Unicode(512), nullable=True)
111
URL = db.Column(db.String(256), nullable=True)
112
creationDate = db.Column(db.DateTime, default=datetime.utcnow)
113
114
defaultBranch = db.Column(db.String(64), nullable=True, default="")
115
116
commits = db.relationship("Commit", back_populates="repo")
117
posts = db.relationship("Post", back_populates="repo")
118
repoAccess = db.relationship("RepoAccess", back_populates="repo")
119
favourites = db.relationship("RepoFavourite", back_populates="repo")
120
121
lastPostID = db.Column(db.Integer, nullable=False, default=0)
122
123
def __init__(self, owner, name, visibility):
124
self.route = f"/{owner.username}/{name}"
125
self.name = name
126
self.ownerName = owner.username
127
self.owner = owner
128
self.visibility = visibility
129
130
# Add the owner as an admin
131
repoAccess = RepoAccess(owner, self, 2)
132
db.session.add(repoAccess)
133
134
135
class Commit(db.Model):
136
identifier = db.Column(db.String(227), unique=True, nullable=False, primary_key=True)
137
sha = db.Column(db.String(128), nullable=False)
138
repoName = db.Column(db.String(98), db.ForeignKey("repo.route"), nullable=False)
139
ownerName = db.Column(db.String(32), db.ForeignKey("user.username"), nullable=False)
140
ownerIdentity = db.Column(db.String(321))
141
receiveDate = db.Column(db.DateTime, default=datetime.now)
142
authorDate = db.Column(db.DateTime)
143
message = db.Column(db.UnicodeText)
144
repo = db.relationship("Repo", back_populates="commits")
145
owner = db.relationship("User", back_populates="commits")
146
147
def __init__(self, sha, owner, repo, date, message, ownerIdentity):
148
self.identifier = f"{repo.route}/{sha}"
149
self.sha = sha
150
self.repoName = repo.route
151
self.repo = repo
152
self.ownerName = owner.username
153
self.owner = owner
154
self.authorDate = datetime.fromtimestamp(int(date))
155
self.message = message
156
self.ownerIdentity = ownerIdentity
157
158
159
class Post(db.Model):
160
identifier = db.Column(db.String(109), unique=True, nullable=False, primary_key=True)
161
number = db.Column(db.Integer, nullable=False)
162
repoName = db.Column(db.String(98), db.ForeignKey("repo.route"), nullable=False)
163
ownerName = db.Column(db.String(32), db.ForeignKey("user.username"), nullable=False)
164
votes = db.relationship("PostVote", back_populates="post")
165
voteSum = db.Column(db.Integer, nullable=False, default=0)
166
167
parentID = db.Column(db.String(109), db.ForeignKey("post.identifier"), nullable=True)
168
state = db.Column(db.SmallInteger, nullable=True, default=1)
169
170
date = db.Column(db.DateTime, default=datetime.now)
171
lastUpdated = db.Column(db.DateTime, default=datetime.now)
172
subject = db.Column(db.Unicode(384))
173
message = db.Column(db.UnicodeText)
174
repo = db.relationship("Repo", back_populates="posts")
175
owner = db.relationship("User", back_populates="posts")
176
parent = db.relationship("Post", back_populates="children", remote_side="Post.identifier")
177
children = db.relationship("Post", back_populates="parent", remote_side="Post.parentID")
178
179
def __init__(self, owner, repo, parent, subject, message):
180
self.identifier = f"{repo.route}/{repo.lastPostID}"
181
self.number = repo.lastPostID
182
self.repoName = repo.route
183
self.repo = repo
184
self.ownerName = owner.username
185
self.owner = owner
186
self.subject = subject
187
self.message = message
188
self.parent = parent
189
repo.lastPostID += 1
190
191
def updateDate(self):
192
self.lastUpdated = datetime.now()
193
with db.session.no_autoflush:
194
if self.parent is not None:
195
self.parent.updateDate()
196
197
198
class UserNotification(db.Model):
199
id = db.Column(db.Integer, primary_key=True)
200
userUsername = db.Column(db.String(32), db.ForeignKey("user.username"), nullable=False)
201
notificationID = db.Column(db.BigInteger, db.ForeignKey("notification.id"))
202
attentionLevel = db.Column(db.SmallInteger, nullable=False) # 0 is read
203
readTime = db.Column(db.DateTime, nullable=True)
204
205
user = db.relationship("User", back_populates="notifications")
206
notification = db.relationship("Notification", back_populates="notifications")
207
208
__table_args__ = (db.UniqueConstraint("userUsername", "notificationID", name="_user_notification_uc"),)
209
210
def __init__(self, user, notification, level):
211
self.userUsername = user.username
212
self.notificationID = notification.id
213
self.attentionLevel = level
214
215
def read(self):
216
self.readTime = datetime.utcnow
217
self.attentionLevel = 0
218
219
220
class Notification(db.Model):
221
id = db.Column(db.BigInteger, primary_key=True, autoincrement=True)
222
message = db.Column(db.UnicodeText)
223
repoName = db.Column(db.String(98), nullable=True)
224
authorName = db.Column(db.String(32), db.ForeignKey("user.username"), nullable=True)
225
timestamp = db.Column(db.DateTime, nullable=False, default=datetime.now)
226
link = db.Column(db.String(256), nullable=True)
227
228
author = db.relationship("User", back_populates="generatedNotifications")
229
notifications = db.relationship("UserNotification", back_populates="notification")
230
231
def __init__(self, message, author, repoLink, link):
232
self.message = message
233
self.link = link
234
235
if author is not None:
236
self.authorName = author.username
237
238
self.repoLink = repoLink
239
240
def sendTo(self, users, level):
241
for user in users:
242
db.session.add(UserNotification(user, self, level))
243
244