Add EditRequest creation

This commit is contained in:
rubenwardy 2018-03-23 19:08:55 +00:00
parent 269c8c0eb4
commit dcfd2b0003
3 changed files with 157 additions and 19 deletions

@ -73,6 +73,7 @@ class User(db.Model, UserMixin):
# Content
packages = db.relationship("Package", backref="author", lazy="dynamic")
requests = db.relationship("EditRequest", backref="author", lazy="dynamic")
def __init__(self, username):
import datetime
@ -104,6 +105,19 @@ class PackageType(enum.Enum):
def coerce(cls, item):
return item if type(item) == PackageType else PackageType[item]
class PackagePropertyKey(enum.Enum):
name = "name"
title = "title"
shortDesc = "shortDesc"
desc = "desc"
type = "type"
repo = "repo"
website = "website"
issueTracker = "issueTracker"
forums = "forums"
class Package(db.Model):
id = db.Column(db.Integer, primary_key=True)
@ -127,6 +141,10 @@ class Package(db.Model):
releases = db.relationship("PackageRelease", backref="package",
lazy="dynamic", order_by=db.desc("package_release_releaseDate"))
requests = db.relationship("EditRequest", backref="package",
lazy="dynamic")
def getAsDictionary(self, base_url):
return {
"name": self.name,
@ -224,6 +242,37 @@ class PackageRelease(db.Model):
def __init__(self):
self.releaseDate = datetime.now()
class EditRequest(db.Model):
id = db.Column(db.Integer, primary_key=True)
package_id = db.Column(db.Integer, db.ForeignKey("package.id"))
author_id = db.Column(db.Integer, db.ForeignKey("user.id"))
title = db.Column(db.String(100), nullable=False)
desc = db.Column(db.String(1000), nullable=True)
changes = db.relationship("EditRequestChange", backref="request",
lazy="dynamic")
def applyAll(self, package):
for change in self.changes:
change.apply(package)
class EditRequestChange(db.Model):
id = db.Column(db.Integer, primary_key=True)
request_id = db.Column(db.Integer, db.ForeignKey("edit_request.id"))
key = db.Column(db.Enum(PackagePropertyKey), nullable=False)
# TODO: make diff instead
oldValue = db.Column(db.Text, nullable=False)
newValue = db.Column(db.Text, nullable=False)
def apply(self, package):
prop = PackagePropertyKey[self.key]
# Setup Flask-User
db_adapter = SQLAlchemyAdapter(db, User) # Register the User model
user_manager = UserManager(db_adapter, app) # Initialize Flask-User

@ -0,0 +1,29 @@
{% extends "base.html" %}
{% block title %}
{{ package.title or "Create Package" }}
{% 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) }}
<hr />
{{ render_field(form.edit_title) }}
{{ render_field(form.edit_desc) }}
{{ render_submit_field(form.submit) }}
<input type="submit" value="Go">
</form>
{% endblock %}

@ -186,6 +186,66 @@ def approve_package_page(type=None, author=None, name=None):
return redirect(package.getDetailsURL())
class EditRequestForm(PackageForm):
edit_title = StringField("Edit Title", [InputRequired(), Length(1, 100)])
edit_desc = TextField("Edit Description", [Optional()])
class UnresolvedPackage(Package):
edit_title = ""
edit_desc = ""
@app.route("/<ptype>s/<author>/<name>/requests/new/", methods=["GET","POST"])
@login_required
def create_editrequest_page(ptype=None, author=None, name=None):
package = getPageByInfo(ptype, author, name)
form = EditRequestForm(request.form, obj=package)
if request.method == "POST" and form.validate():
editedPackage = UnresolvedPackage()
form.populate_obj(editedPackage)
erequest = EditRequest()
erequest.package = package
erequest.author = current_user
erequest.title = editedPackage.edit_title
erequest.desc = editedPackage.edit_desc
db.session.add(erequest)
wasChangeMade = False
for e in PackagePropertyKey:
newValue = getattr(editedPackage, e.name)
oldValue = getattr(package, e.name)
if newValue == "":
newValue = None
newValueComp = newValue
oldValueComp = oldValue
if type(newValue) is str:
newValue = newValue.replace("\r\n", "\n")
newValueComp = newValue.strip()
oldValueComp = oldValue.strip()
if newValueComp != oldValueComp:
change = EditRequestChange()
change.request = erequest
change.key = e
change.oldValue = oldValue
change.newValue = newValue
db.session.add(change)
wasChangeMade = True
if wasChangeMade:
db.session.commit()
return redirect(package.getDetailsURL())
else:
flash("No changes detected", "warning")
return render_template("packages/create_editrequest.html", package=package, form=form)
class CreatePackageReleaseForm(FlaskForm):
name = StringField("Name")
title = StringField("Title")