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