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") 72followers = db.relationship("UserFollow", back_populates="followed", foreign_keys="[UserFollow.followedUsername]") 73follows = db.relationship("UserFollow", back_populates="follower", foreign_keys="[UserFollow.followerUsername]") 74repoAccess = db.relationship("RepoAccess", back_populates="user") 75votes = db.relationship("PostVote", back_populates="user") 76favourites = db.relationship("RepoFavourite", back_populates="user") 77 78commits = db.relationship("Commit", back_populates="owner") 79posts = db.relationship("Post", back_populates="owner") 80notifications = db.relationship("UserNotification", back_populates="user") 81 82def __init__(self, username, password, email=None, displayName=None): 83self.username = username 84self.passwordHashed = bcrypt.generate_password_hash(password, config.HASHING_ROUNDS).decode("utf-8") 85self.email = email 86self.displayName = displayName 87 88# Create the user's directory 89if not os.path.exists(os.path.join(config.REPOS_PATH, username)): 90os.makedirs(os.path.join(config.REPOS_PATH, username)) 91if not os.path.exists(os.path.join(config.USERDATA_PATH, username)): 92os.makedirs(os.path.join(config.USERDATA_PATH, username)) 93 94avatarName = random.choice(os.listdir(config.DEFAULT_AVATARS_PATH)) 95if os.path.join(config.DEFAULT_AVATARS_PATH, avatarName).endswith(".svg"): 96cairosvg.svg2png(url=os.path.join(config.DEFAULT_AVATARS_PATH, avatarName), 97write_to="/tmp/roundabout-avatar.png") 98avatar = Image.open("/tmp/roundabout-avatar.png") 99else: 100avatar = Image.open(os.path.join(config.DEFAULT_AVATARS_PATH, avatarName)) 101avatar.thumbnail(config.AVATAR_SIZE) 102avatar.save(os.path.join(config.USERDATA_PATH, username, "avatar.png")) 103 104 105class Repo(db.Model): 106route = db.Column(db.String(98), unique=True, nullable=False, primary_key=True) 107ownerName = db.Column(db.String(32), db.ForeignKey("user.username"), nullable=False) 108name = db.Column(db.String(64), nullable=False) 109owner = db.relationship("User", back_populates="repositories") 110visibility = db.Column(db.SmallInteger(), nullable=False) 111info = db.Column(db.Unicode(512), nullable=True) 112URL = db.Column(db.String(256), nullable=True) 113creationDate = db.Column(db.DateTime, default=datetime.utcnow) 114 115defaultBranch = db.Column(db.String(64), nullable=True, default="") 116 117commits = db.relationship("Commit", back_populates="repo") 118posts = db.relationship("Post", 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")) 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 UserFollow(db.Model): 222id = db.Column(db.Integer, primary_key=True) 223followerUsername = db.Column(db.String(32), db.ForeignKey("user.username", ondelete="CASCADE"), nullable=False) 224followedUsername = db.Column(db.String(32), db.ForeignKey("user.username", ondelete="CASCADE"), nullable=False) 225 226follower = db.relationship("User", back_populates="follows", foreign_keys=[followedUsername]) 227followed = db.relationship("User", back_populates="followers", foreign_keys=[followerUsername]) 228 229def __init__(self, followerUsername, followedUsername): 230self.followerUsername = followerUsername 231self.followedUsername = followedUsername 232 233 234class Notification(db.Model): 235id = db.Column(db.BigInteger, primary_key=True, autoincrement=True) 236data = db.Column(db.dialects.postgresql.JSONB, nullable=False, default={}) 237notifications = db.relationship("UserNotification", back_populates="notification") 238timestamp = db.Column(db.DateTime, nullable=False, default=datetime.now) 239 240def __init__(self, json): 241self.data = json 242 243def sendTo(self, users, level): 244for user in users: 245db.session.add(UserNotification(user, self, level)) 246 247