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