Add edit package

This commit is contained in:
rubenwardy 2018-03-20 02:58:31 +00:00
parent 7c628ca093
commit 5a3764f178
4 changed files with 101 additions and 3 deletions

@ -58,6 +58,12 @@ class UserRoles(db.Model):
user_id = db.Column(db.Integer(), db.ForeignKey('user.id', ondelete='CASCADE')) user_id = db.Column(db.Integer(), db.ForeignKey('user.id', ondelete='CASCADE'))
role_id = db.Column(db.Integer(), db.ForeignKey('role.id', ondelete='CASCADE')) role_id = db.Column(db.Integer(), db.ForeignKey('role.id', ondelete='CASCADE'))
class Permission(enum.Enum):
EDIT_PACKAGE = "EDIT_PACKAGE"
APPROVE = "APPROVE"
DELETE_PACKAGE = "DELETE_PACKAGE"
CHANGE_AUTHOR = "CHANGE_AUTHOR"
class PackageType(enum.Enum): class PackageType(enum.Enum):
MOD = "Mod" MOD = "Mod"
GAME = "Game" GAME = "Game"
@ -82,6 +88,23 @@ class PackageType(enum.Enum):
else: else:
return None return None
@classmethod
def choices(cls):
return [(choice, choice.value) for choice in cls]
@classmethod
def coerce(cls, item):
"""item will be both type(enum) AND type(unicode).
"""
if item == 'PackageType.MOD' or item == PackageType.MOD:
return PackageType.MOD
elif item == 'PackageType.GAME' or item == PackageType.GAME:
return PackageType.GAME
elif item == 'PackageType.TXP' or item == PackageType.TXP:
return PackageType.TXP
else:
print("Can't coerce", item, type(item))
class Package(db.Model): class Package(db.Model):
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
@ -99,6 +122,19 @@ class Package(db.Model):
issueTracker = db.Column(db.String(200), nullable=True) issueTracker = db.Column(db.String(200), nullable=True)
forums = db.Column(db.String(200), nullable=False) forums = db.Column(db.String(200), nullable=False)
def getDetailsURL(self):
return url_for("package_page",
type=self.type.getTitle().lower(),
author=self.author.username, name=self.name)
def getEditURL(self):
return url_for("edit_package_page",
type=self.type.getTitle().lower(),
author=self.author.username, name=self.name)
def checkPerm(self, user, perm):
return user == self.author
# Setup Flask-User # Setup Flask-User
db_adapter = SQLAlchemyAdapter(db, User) # Register the User model db_adapter = SQLAlchemyAdapter(db, User) # Register the User model
user_manager = UserManager(db_adapter, app) # Initialize Flask-User user_manager = UserManager(db_adapter, app) # Initialize Flask-User

@ -20,6 +20,7 @@
</table> </table>
<ul class="buttonset linedbuttonset"> <ul class="buttonset linedbuttonset">
{% if package.checkPerm(current_user, "EDIT_PACKAGE") %}<li><a href="{{ package.getEditURL() }}">Edit</a></li>{% endif %}
{% if package.repo %}<li><a href="{{ package.repo }}">View Source</a></li>{% endif %} {% if package.repo %}<li><a href="{{ package.repo }}">View Source</a></li>{% endif %}
{% if package.forums %}<li><a href="{{ package.forums }}">Forums</a></li>{% endif %} {% if package.forums %}<li><a href="{{ package.forums }}">Forums</a></li>{% endif %}
{% if package.issueTracker %}<li><a href="{{ package.issueTracker }}">Issue Tracker</a></li>{% endif %} {% if package.issueTracker %}<li><a href="{{ package.issueTracker }}">Issue Tracker</a></li>{% endif %}

@ -0,0 +1,24 @@
{% extends "base.html" %}
{% block title %}
{{ package.title }}
{% endblock %}
{% block content %}
{% from "macros/forms.html" import render_field, render_submit_field %}
<form method="POST" action="">
{{ form.hidden_tag() }}
{{ render_field(form.name) }}
{{ render_field(form.title) }}
{{ render_field(form.shortDesc) }}
{{ render_field(form.desc) }}
{{ render_field(form.type) }}
{{ render_field(form.repo) }}
{{ render_field(form.website) }}
{{ render_field(form.issueTracker) }}
{{ render_field(form.forums) }}
{{ render_submit_field(form.submit) }}
<input type="submit" value="Go">
</form>
{% endblock %}

@ -4,6 +4,9 @@ from flask.ext import menu
from app import app from app import app
from app.models import * from app.models import *
from flask_wtf import FlaskForm
from wtforms import *
# TODO: the following could be made into one route, except I'm not sure how # TODO: the following could be made into one route, except I'm not sure how
# to do the menu # to do the menu
@ -27,9 +30,7 @@ def txp_page():
return render_template('packages.html', title="Texture Packs", packages=packages) return render_template('packages.html', title="Texture Packs", packages=packages)
def getPageByInfo(type, author, name):
@app.route("/<type>s/<author>/<name>/")
def package_page(type, author, name):
user = User.query.filter_by(username=author).first() user = User.query.filter_by(username=author).first()
if user is None: if user is None:
abort(404) abort(404)
@ -39,4 +40,40 @@ def package_page(type, author, name):
if package is None: if package is None:
abort(404) abort(404)
return package
@app.route("/<type>s/<author>/<name>/")
def package_page(type, author, name):
package = getPageByInfo(type, author, name)
return render_template('package_details.html', package=package) return render_template('package_details.html', package=package)
class PackageForm(FlaskForm):
name = StringField("Name")
title = StringField("Title")
shortDesc = StringField("Short Description")
desc = StringField("Long Description")
type = SelectField("Type", choices=PackageType.choices(), coerce=PackageType.coerce)
repo = StringField("Repo URL")
website = StringField("Website URL")
issueTracker = StringField("Issue Tracker URL")
forums = StringField("Forum Topic ID")
submit = SubmitField('Save')
@app.route("/<type>s/<author>/<name>/edit/", methods=['GET', 'POST'])
@login_required
def edit_package_page(type, author, name):
package = getPageByInfo(type, author, name)
if not package.checkPerm(current_user, Permission.EDIT_PACKAGE):
return redirect(package.getDetailsURL())
# Initial form class from post data and default data
form = PackageForm(formdata=request.form, obj=package)
if request.method == "POST" and form.validate():
# Successfully submitted!
form.populate_obj(package) # copy to row
db.session.commit() # save
return redirect(package.getDetailsURL()) # redirect
return render_template('package_edit.html', package=package, form=form)