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 • 9.01 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
favourites = db.relationship("RepoFavourite", back_populates="user")
75
76
commits = db.relationship("Commit", back_populates="owner")
77
posts = db.relationship("Post", back_populates="owner")
78
79
def __init__(self, username, password, email=None, displayName=None):
80
self.username = username
81
self.passwordHashed = bcrypt.generate_password_hash(password, config.HASHING_ROUNDS).decode("utf-8")
82
self.email = email
83
self.displayName = displayName
84
85
# Create the user's directory
86
if not os.path.exists(os.path.join(config.REPOS_PATH, username)):
87
os.makedirs(os.path.join(config.REPOS_PATH, username))
88
if not os.path.exists(os.path.join(config.USERDATA_PATH, username)):
89
os.makedirs(os.path.join(config.USERDATA_PATH, username))
90
91
avatarName = random.choice(os.listdir(config.DEFAULT_AVATARS_PATH))
92
if os.path.join(config.DEFAULT_AVATARS_PATH, avatarName).endswith(".svg"):
93
cairosvg.svg2png(url=os.path.join(config.DEFAULT_AVATARS_PATH, avatarName),
94
write_to="/tmp/roundabout-avatar.png")
95
avatar = Image.open("/tmp/roundabout-avatar.png")
96
else:
97
avatar = Image.open(os.path.join(config.DEFAULT_AVATARS_PATH, avatarName))
98
avatar.thumbnail(config.AVATAR_SIZE)
99
avatar.save(os.path.join(config.USERDATA_PATH, username, "avatar.png"))
100
101
102
class Repo(db.Model):
103
route = db.Column(db.String(98), unique=True, nullable=False, primary_key=True)
104
ownerName = db.Column(db.String(32), db.ForeignKey("user.username"), nullable=False)
105
name = db.Column(db.String(64), nullable=False)
106
owner = db.relationship("User", back_populates="repositories")
107
visibility = db.Column(db.SmallInteger(), nullable=False)
108
info = db.Column(db.Unicode(512), nullable=True)
109
URL = db.Column(db.String(256), nullable=True)
110
creationDate = db.Column(db.DateTime, default=datetime.utcnow)
111
112
defaultBranch = db.Column(db.String(64), nullable=True, default="")
113
114
commits = db.relationship("Commit", back_populates="repo")
115
posts = db.relationship("Post", back_populates="repo")
116
repoAccess = db.relationship("RepoAccess", back_populates="repo")
117
favourites = db.relationship("RepoFavourite", back_populates="repo")
118
119
lastPostID = db.Column(db.Integer, nullable=False, default=0)
120
121
def __init__(self, owner, name, visibility):
122
self.route = f"/{owner.username}/{name}"
123
self.name = name
124
self.ownerName = owner.username
125
self.owner = owner
126
self.visibility = visibility
127
128
# Add the owner as an admin
129
repoAccess = RepoAccess(owner, self, 2)
130
db.session.add(repoAccess)
131
132
133
class Commit(db.Model):
134
identifier = db.Column(db.String(227), unique=True, nullable=False, primary_key=True)
135
sha = db.Column(db.String(128), nullable=False)
136
repoName = db.Column(db.String(98), db.ForeignKey("repo.route"), nullable=False)
137
ownerName = db.Column(db.String(32), db.ForeignKey("user.username"), nullable=False)
138
ownerIdentity = db.Column(db.String(321))
139
receiveDate = db.Column(db.DateTime, default=datetime.now)
140
authorDate = db.Column(db.DateTime)
141
message = db.Column(db.UnicodeText)
142
repo = db.relationship("Repo", back_populates="commits")
143
owner = db.relationship("User", back_populates="commits")
144
145
def __init__(self, sha, owner, repo, date, message, ownerIdentity):
146
self.identifier = f"{repo.route}/{sha}"
147
self.sha = sha
148
self.repoName = repo.route
149
self.repo = repo
150
self.ownerName = owner.username
151
self.owner = owner
152
self.authorDate = datetime.fromtimestamp(int(date))
153
self.message = message
154
self.ownerIdentity = ownerIdentity
155
156
157
class Post(db.Model):
158
identifier = db.Column(db.String(109), unique=True, nullable=False, primary_key=True)
159
number = db.Column(db.Integer, nullable=False)
160
repoName = db.Column(db.String(98), db.ForeignKey("repo.route"), nullable=False)
161
ownerName = db.Column(db.String(32), db.ForeignKey("user.username"), nullable=False)
162
votes = db.relationship("PostVote", back_populates="post")
163
voteSum = db.Column(db.Integer, nullable=False, default=0)
164
165
parentID = db.Column(db.String(109), db.ForeignKey("post.identifier"), nullable=True)
166
state = db.Column(db.SmallInteger, nullable=True, default=1)
167
168
date = db.Column(db.DateTime, default=datetime.now)
169
lastUpdated = db.Column(db.DateTime, default=datetime.now)
170
subject = db.Column(db.Unicode(384))
171
message = db.Column(db.UnicodeText)
172
repo = db.relationship("Repo", back_populates="posts")
173
owner = db.relationship("User", back_populates="posts")
174
parent = db.relationship("Post", back_populates="children", remote_side="Post.identifier")
175
children = db.relationship("Post", back_populates="parent", remote_side="Post.parentID")
176
177
def __init__(self, owner, repo, parent, subject, message):
178
self.identifier = f"{repo.route}/{repo.lastPostID}"
179
self.number = repo.lastPostID
180
self.repoName = repo.route
181
self.repo = repo
182
self.ownerName = owner.username
183
self.owner = owner
184
self.subject = subject
185
self.message = message
186
self.parent = parent
187
repo.lastPostID += 1
188
189
def updateDate(self):
190
self.lastUpdated = datetime.now()
191
with db.session.no_autoflush:
192
if self.parent is not None:
193
self.parent.updateDate()
194