2018-03-18 18:43:30 +01:00
|
|
|
from flask import Flask, url_for
|
2018-03-18 19:05:53 +01:00
|
|
|
from flask_sqlalchemy import SQLAlchemy
|
2018-03-18 18:43:30 +01:00
|
|
|
from app import app
|
|
|
|
from datetime import datetime
|
|
|
|
from sqlalchemy.orm import validates
|
|
|
|
from flask_user import login_required, UserManager, UserMixin, SQLAlchemyAdapter
|
2018-03-20 01:44:47 +01:00
|
|
|
import enum
|
2018-03-18 18:43:30 +01:00
|
|
|
|
|
|
|
# Initialise database
|
|
|
|
db = SQLAlchemy(app)
|
|
|
|
|
|
|
|
|
2018-03-20 04:16:46 +01:00
|
|
|
class UserRank(enum.Enum):
|
2018-03-20 05:03:13 +01:00
|
|
|
NOT_JOINED = 0
|
|
|
|
NEW_MEMBER = 1
|
|
|
|
MEMBER = 2
|
|
|
|
EDITOR = 3
|
|
|
|
MODERATOR = 4
|
|
|
|
ADMIN = 5
|
2018-03-20 04:16:46 +01:00
|
|
|
|
|
|
|
def atLeast(self, min):
|
|
|
|
return self.value >= min.value
|
|
|
|
|
2018-03-20 04:32:14 +01:00
|
|
|
def getTitle(self):
|
|
|
|
return self.name.replace("_", " ").title()
|
|
|
|
|
2018-03-20 05:11:50 +01:00
|
|
|
|
|
|
|
class Permission(enum.Enum):
|
|
|
|
EDIT_PACKAGE = "EDIT_PACKAGE"
|
|
|
|
APPROVE_CHANGES = "APPROVE_CHANGES"
|
|
|
|
DELETE_PACKAGE = "DELETE_PACKAGE"
|
|
|
|
CHANGE_AUTHOR = "CHANGE_AUTHOR"
|
|
|
|
APPROVE_RELEASE = "APPROVE_RELEASE"
|
|
|
|
APPROVE_NEW = "APPROVE_NEW"
|
|
|
|
|
|
|
|
|
2018-03-18 18:43:30 +01:00
|
|
|
class User(db.Model, UserMixin):
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
|
|
|
|
# User authentication information
|
|
|
|
username = db.Column(db.String(50), nullable=False, unique=True)
|
|
|
|
password = db.Column(db.String(255), nullable=False, server_default='')
|
|
|
|
reset_password_token = db.Column(db.String(100), nullable=False, server_default='')
|
|
|
|
|
2018-03-20 04:16:46 +01:00
|
|
|
rank = db.Column(db.Enum(UserRank))
|
|
|
|
|
2018-03-18 19:05:53 +01:00
|
|
|
# Account linking
|
|
|
|
github_username = db.Column(db.String(50), nullable=True, unique=True)
|
|
|
|
forums_username = db.Column(db.String(50), nullable=True, unique=True)
|
|
|
|
|
2018-03-18 18:43:30 +01:00
|
|
|
# User email information
|
|
|
|
email = db.Column(db.String(255), nullable=True, unique=True)
|
|
|
|
confirmed_at = db.Column(db.DateTime())
|
|
|
|
|
|
|
|
# User information
|
|
|
|
active = db.Column('is_active', db.Boolean, nullable=False, server_default='0')
|
|
|
|
display_name = db.Column(db.String(100), nullable=False, server_default='')
|
|
|
|
|
|
|
|
# Content
|
2018-03-19 19:25:13 +01:00
|
|
|
packages = db.relationship('Package', backref='author', lazy='dynamic')
|
2018-03-18 18:43:30 +01:00
|
|
|
|
|
|
|
def __init__(self, username):
|
|
|
|
import datetime
|
|
|
|
|
|
|
|
self.username = username
|
|
|
|
self.confirmed_at = datetime.datetime.now() - datetime.timedelta(days=6000)
|
2018-03-18 19:05:53 +01:00
|
|
|
self.display_name = username
|
2018-03-20 05:03:13 +01:00
|
|
|
self.rank = UserRank.NOT_JOINED
|
2018-03-18 18:43:30 +01:00
|
|
|
|
|
|
|
def isClaimed(self):
|
|
|
|
return self.password is not None and self.password != ""
|
|
|
|
|
2018-03-20 01:44:47 +01:00
|
|
|
class PackageType(enum.Enum):
|
|
|
|
MOD = "Mod"
|
|
|
|
GAME = "Game"
|
|
|
|
TXP = "Texture Pack"
|
|
|
|
|
2018-03-20 04:32:14 +01:00
|
|
|
def toName(self):
|
2018-03-20 05:11:50 +01:00
|
|
|
return self.name.lower()
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
2018-03-20 01:58:44 +01:00
|
|
|
|
2018-03-20 03:58:31 +01:00
|
|
|
@classmethod
|
|
|
|
def choices(cls):
|
|
|
|
return [(choice, choice.value) for choice in cls]
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def coerce(cls, item):
|
2018-03-20 05:11:50 +01:00
|
|
|
return item if type(item) == PackageType else PackageType[item]
|
2018-03-20 03:58:31 +01:00
|
|
|
|
2018-03-19 19:25:13 +01:00
|
|
|
class Package(db.Model):
|
2018-03-20 01:44:47 +01:00
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
2018-03-18 18:43:30 +01:00
|
|
|
|
|
|
|
# Basic details
|
2018-03-20 01:44:47 +01:00
|
|
|
author_id = db.Column(db.Integer, db.ForeignKey('user.id'))
|
|
|
|
name = db.Column(db.String(100), nullable=False)
|
|
|
|
title = db.Column(db.String(100), nullable=False)
|
2018-03-20 03:17:33 +01:00
|
|
|
shortDesc = db.Column(db.Text, nullable=True)
|
2018-03-20 01:44:47 +01:00
|
|
|
desc = db.Column(db.Text, nullable=True)
|
|
|
|
type = db.Column(db.Enum(PackageType))
|
2018-03-18 18:43:30 +01:00
|
|
|
|
|
|
|
# Downloads
|
2018-03-20 01:44:47 +01:00
|
|
|
repo = db.Column(db.String(200), nullable=True)
|
|
|
|
website = db.Column(db.String(200), nullable=True)
|
2018-03-18 18:43:30 +01:00
|
|
|
issueTracker = db.Column(db.String(200), nullable=True)
|
2018-03-20 01:44:47 +01:00
|
|
|
forums = db.Column(db.String(200), nullable=False)
|
2018-03-18 18:43:30 +01:00
|
|
|
|
2018-03-20 03:58:31 +01:00
|
|
|
def getDetailsURL(self):
|
|
|
|
return url_for("package_page",
|
2018-03-20 04:32:14 +01:00
|
|
|
type=self.type.toName(),
|
2018-03-20 03:58:31 +01:00
|
|
|
author=self.author.username, name=self.name)
|
|
|
|
|
|
|
|
def getEditURL(self):
|
|
|
|
return url_for("edit_package_page",
|
2018-03-20 04:32:14 +01:00
|
|
|
type=self.type.toName(),
|
2018-03-20 03:58:31 +01:00
|
|
|
author=self.author.username, name=self.name)
|
|
|
|
|
|
|
|
def checkPerm(self, user, perm):
|
2018-03-20 04:45:01 +01:00
|
|
|
if not user.is_authenticated:
|
|
|
|
return False
|
|
|
|
|
2018-03-20 04:16:46 +01:00
|
|
|
if type(perm) == str:
|
|
|
|
perm = Permission[perm]
|
2018-03-20 04:54:38 +01:00
|
|
|
elif type(perm) != Permission:
|
|
|
|
raise Exception("Unknown permission given to Package.checkPerm()")
|
2018-03-20 04:16:46 +01:00
|
|
|
|
|
|
|
isOwner = user == self.author
|
2018-03-20 04:45:01 +01:00
|
|
|
|
|
|
|
# Members can edit their own packages, and editors can edit any packages
|
|
|
|
if perm == Permission.EDIT_PACKAGE or perm == Permission.APPROVE_CHANGES:
|
2018-03-20 04:16:46 +01:00
|
|
|
return user.rank.atLeast(UserRank.MEMBER if isOwner else UserRank.EDITOR)
|
2018-03-20 04:45:01 +01:00
|
|
|
|
|
|
|
# Editors can change authors, approve new packages, and approve releases
|
|
|
|
elif perm == Permission.CHANGE_AUTHOR or perm == Permission.APPROVE_NEW \
|
|
|
|
or perm == Permission.APPROVE_RELEASE:
|
2018-03-20 04:16:46 +01:00
|
|
|
return user.rank.atLeast(UserRank.EDITOR)
|
2018-03-20 04:45:01 +01:00
|
|
|
|
|
|
|
# Moderators can delete packages
|
|
|
|
elif perm == Permission.DELETE_PACKAGE:
|
|
|
|
return user.rank.atLeast(UserRank.MODERATOR)
|
|
|
|
|
2018-03-20 04:16:46 +01:00
|
|
|
else:
|
2018-03-20 04:54:38 +01:00
|
|
|
raise Exception("Permission {} is not related to packages".format(perm.name))
|
2018-03-20 03:58:31 +01:00
|
|
|
|
2018-03-18 18:43:30 +01:00
|
|
|
# Setup Flask-User
|
|
|
|
db_adapter = SQLAlchemyAdapter(db, User) # Register the User model
|
|
|
|
user_manager = UserManager(db_adapter, app) # Initialize Flask-User
|