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") 118repoAccess = db.relationship("RepoAccess", back_populates="repo") 119favourites = db.relationship("RepoFavourite", back_populates="repo") 120 121lastPostID = db.Column(db.Integer, nullable=False, default=0) 122 123def __init__(self, owner, name, visibility): 124self.route = f"/{owner.username}/{name}" 125self.name = name 126self.ownerName = owner.username 127self.owner = owner 128self.visibility = visibility 129 130# Add the owner as an admin 131repoAccess = RepoAccess(owner, self, 2) 132db.session.add(repoAccess) 133 134 135class Commit(db.Model): 136identifier = db.Column(db.String(227), unique=True, nullable=False, primary_key=True) 137sha = db.Column(db.String(128), nullable=False) 138repoName = db.Column(db.String(98), db.ForeignKey("repo.route"), nullable=False) 139ownerName = db.Column(db.String(32), db.ForeignKey("user.username"), nullable=False) 140ownerIdentity = db.Column(db.String(321)) 141receiveDate = db.Column(db.DateTime, default=datetime.now) 142authorDate = db.Column(db.DateTime) 143message = db.Column(db.UnicodeText) 144repo = db.relationship("Repo", back_populates="commits") 145owner = db.relationship("User", back_populates="commits") 146 147def __init__(self, sha, owner, repo, date, message, ownerIdentity): 148self.identifier = f"{repo.route}/{sha}" 149self.sha = sha 150self.repoName = repo.route 151self.repo = repo 152self.ownerName = owner.username 153self.owner = owner 154self.authorDate = datetime.fromtimestamp(int(date)) 155self.message = message 156self.ownerIdentity = ownerIdentity 157 158 159class Post(db.Model): 160identifier = db.Column(db.String(109), unique=True, nullable=False, primary_key=True) 161number = db.Column(db.Integer, nullable=False) 162repoName = db.Column(db.String(98), db.ForeignKey("repo.route"), nullable=False) 163ownerName = db.Column(db.String(32), db.ForeignKey("user.username"), nullable=False) 164votes = db.relationship("PostVote", back_populates="post") 165voteSum = db.Column(db.Integer, nullable=False, default=0) 166 167parentID = db.Column(db.String(109), db.ForeignKey("post.identifier"), nullable=True) 168state = db.Column(db.SmallInteger, nullable=True, default=1) 169 170date = db.Column(db.DateTime, default=datetime.now) 171lastUpdated = db.Column(db.DateTime, default=datetime.now) 172subject = db.Column(db.Unicode(384)) 173message = db.Column(db.UnicodeText) 174repo = db.relationship("Repo", back_populates="posts") 175owner = db.relationship("User", back_populates="posts") 176parent = db.relationship("Post", back_populates="children", remote_side="Post.identifier") 177children = db.relationship("Post", back_populates="parent", remote_side="Post.parentID") 178 179def __init__(self, owner, repo, parent, subject, message): 180self.identifier = f"{repo.route}/{repo.lastPostID}" 181self.number = repo.lastPostID 182self.repoName = repo.route 183self.repo = repo 184self.ownerName = owner.username 185self.owner = owner 186self.subject = subject 187self.message = message 188self.parent = parent 189repo.lastPostID += 1 190 191def updateDate(self): 192self.lastUpdated = datetime.now() 193with db.session.no_autoflush: 194if self.parent is not None: 195self.parent.updateDate() 196 197 198class UserNotification(db.Model): 199id = db.Column(db.Integer, primary_key=True) 200userUsername = db.Column(db.String(32), db.ForeignKey("user.username"), nullable=False) 201notificationID = db.Column(db.BigInteger, db.ForeignKey("notification.id")) 202attentionLevel = db.Column(db.SmallInteger, nullable=False) # 0 is read 203readTime = db.Column(db.DateTime, nullable=True) 204 205user = db.relationship("User", back_populates="notifications") 206notification = db.relationship("Notification", back_populates="notifications") 207 208__table_args__ = (db.UniqueConstraint("userUsername", "notificationID", name="_user_notification_uc"),) 209 210def __init__(self, user, notification, level): 211self.userUsername = user.username 212self.notificationID = notification.id 213self.attentionLevel = level 214 215def read(self): 216self.readTime = datetime.utcnow 217self.attentionLevel = 0 218 219 220class Notification(db.Model): 221id = db.Column(db.BigInteger, primary_key=True, autoincrement=True) 222message = db.Column(db.UnicodeText) 223repoName = db.Column(db.String(98), nullable=True) 224authorName = db.Column(db.String(32), db.ForeignKey("user.username"), nullable=True) 225timestamp = db.Column(db.DateTime, nullable=False, default=datetime.now) 226link = db.Column(db.String(256), nullable=True) 227 228author = db.relationship("User", back_populates="generatedNotifications") 229notifications = db.relationship("UserNotification", back_populates="notification") 230 231def __init__(self, message, author, repoLink, link): 232self.message = message 233self.link = link 234 235if author is not None: 236self.authorName = author.username 237 238self.repoLink = repoLink 239 240def sendTo(self, users, level): 241for user in users: 242db.session.add(UserNotification(user, self, level)) 243 244