Add EditRequest approval and rejection

This commit is contained in:
rubenwardy 2018-03-23 19:49:57 +00:00
parent a5042a986a
commit bb9d589fb5
5 changed files with 88 additions and 14 deletions

@ -251,6 +251,7 @@ class EditRequest(db.Model):
title = db.Column(db.String(100), nullable=False)
desc = db.Column(db.String(1000), nullable=True)
status = db.Column(db.Integer, nullable=False, default=0)
changes = db.relationship("EditRequestChange", backref="request",
lazy="dynamic")
@ -262,6 +263,20 @@ class EditRequest(db.Model):
name=self.package.name,
id=self.id)
def getApproveURL(self):
return url_for("approve_editrequest_page",
ptype=self.package.type.toName(),
author=self.package.author.username,
name=self.package.name,
id=self.id)
def getRejectURL(self):
return url_for("reject_editrequest_page",
ptype=self.package.type.toName(),
author=self.package.author.username,
name=self.package.name,
id=self.id)
def applyAll(self, package):
for change in self.changes:
change.apply(package)
@ -275,12 +290,11 @@ class EditRequestChange(db.Model):
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)
oldValue = db.Column(db.Text, nullable=True)
newValue = db.Column(db.Text, nullable=True)
def apply(self, package):
prop = PackagePropertyKey[self.key]
setattr(package, self.key.name, self.newValue)
# Setup Flask-User
db_adapter = SQLAlchemyAdapter(db, User) # Register the User model

@ -147,6 +147,11 @@ a:hover {
border: 1px solid #c96;
}
.alert-success {
background: #161;
border: 1px solid #393;
}
/* Nav */
nav, main, #alerts {

@ -16,6 +16,21 @@
<i>No description given</i>
{% endif %}
</p>
{% if request.status == 1 %}
<div class="box box_grey alert alert-success">
This edit request was merged.
</div>
{% elif request.status == 2 %}
<div class="box box_grey alert alert-error">
This edit request was rejected.
</div>
{% elif package.checkPerm(current_user, "APPROVE_CHANGES") %}
<div class="box box_grey">
To resolve this request, either
<a href="{{ request.getApproveURL() }}">Approve and Apply</a> or
<a href="{{ request.getRejectURL() }}">Reject</a> it.
</div>
{% endif %}
<table>
<tr>

@ -87,11 +87,11 @@
{% endfor %}
</ul>
{% if current_user.is_authenticated or package.requests %}
{% if current_user.is_authenticated or requests %}
<h3>Edit Requests</h3>
<ul>
{% for r in package.requests %}
{% for r in requests %}
<li>
<a href="{{ r.getURL() }}">{{ r.title }}</a>
by

@ -107,7 +107,8 @@ def package_page(type, author, name):
return jsonify(package.getAsDictionary(request.url_root))
else:
releases = getReleases(package)
return render_template("packages/view.html", package=package, releases=releases)
requests = [r for r in package.requests if r.status == 0]
return render_template("packages/view.html", package=package, releases=releases, requests=requests)
@app.route("/<type>s/<author>/<name>/download/")
@ -216,30 +217,27 @@ def create_editrequest_page(ptype, author, name):
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()
oldValueComp = "" if oldValue is None else oldValue.strip()
if newValueComp != oldValueComp:
change = EditRequestChange()
change.request = erequest
change.key = e
change.oldValue = oldValue
change.newValue = newValue
change.oldValue = str(oldValue)
change.newValue = str(newValue)
db.session.add(change)
wasChangeMade = True
if wasChangeMade:
db.session.commit()
return redirect(package.getDetailsURL())
return redirect(erequest.getURL())
else:
flash("No changes detected", "warning")
@ -257,6 +255,48 @@ def view_editrequest_page(ptype, author, name, id):
return render_template("packages/editrequest_view.html", package=package, request=erequest)
@app.route("/<ptype>s/<author>/<name>/requests/<id>/approve/")
def approve_editrequest_page(ptype, author, name, id):
package = getPageByInfo(ptype, author, name)
if not package.checkPerm(current_user, Permission.APPROVE_CHANGES):
flash("You don't have permission to do that.", "error")
return redirect(package.getDetailsURL())
erequest = EditRequest.query.filter_by(id=id).first()
if erequest is None:
abort(404)
if erequest.status != 0:
flash("Edit request has already been resolved", "error")
else:
erequest.status = 1
erequest.applyAll(package)
db.session.commit()
return redirect(package.getDetailsURL())
@app.route("/<ptype>s/<author>/<name>/requests/<id>/reject/")
def reject_editrequest_page(ptype, author, name, id):
package = getPageByInfo(ptype, author, name)
if not package.checkPerm(current_user, Permission.APPROVE_CHANGES):
flash("You don't have permission to do that.", "error")
return redirect(package.getDetailsURL())
erequest = EditRequest.query.filter_by(id=id).first()
if erequest is None:
abort(404)
if erequest.status != 0:
flash("Edit request has already been resolved", "error")
else:
erequest.status = 2
db.session.commit()
return redirect(package.getDetailsURL())
class CreatePackageReleaseForm(FlaskForm):
name = StringField("Name")
title = StringField("Title")