mirror of
https://github.com/minetest/contentdb.git
synced 2024-12-23 06:22:24 +01:00
Add ability to edit editrequests
This commit is contained in:
parent
e372bb810d
commit
4508d6f486
@ -49,6 +49,7 @@ class Permission(enum.Enum):
|
|||||||
APPROVE_NEW = "APPROVE_NEW"
|
APPROVE_NEW = "APPROVE_NEW"
|
||||||
CHANGE_RELEASE_URL = "CHANGE_RELEASE_URL"
|
CHANGE_RELEASE_URL = "CHANGE_RELEASE_URL"
|
||||||
CHANGE_RANK = "CHANGE_RANK"
|
CHANGE_RANK = "CHANGE_RANK"
|
||||||
|
EDIT_EDITREQUEST = "EDIT_EDITREQUEST"
|
||||||
|
|
||||||
# Only return true if the permission is valid for *all* contexts
|
# Only return true if the permission is valid for *all* contexts
|
||||||
# See Package.checkPerm for package-specific contexts
|
# See Package.checkPerm for package-specific contexts
|
||||||
@ -248,7 +249,7 @@ class Package(db.Model):
|
|||||||
author=self.author.username, name=self.name)
|
author=self.author.username, name=self.name)
|
||||||
|
|
||||||
def getCreateEditRequestURL(self):
|
def getCreateEditRequestURL(self):
|
||||||
return url_for("create_editrequest_page",
|
return url_for("create_edit_editrequest_page",
|
||||||
ptype=self.type.toName(),
|
ptype=self.type.toName(),
|
||||||
author=self.author.username, name=self.name)
|
author=self.author.username, name=self.name)
|
||||||
|
|
||||||
@ -353,6 +354,9 @@ class EditRequest(db.Model):
|
|||||||
title = db.Column(db.String(100), nullable=False)
|
title = db.Column(db.String(100), nullable=False)
|
||||||
desc = db.Column(db.String(1000), nullable=True)
|
desc = db.Column(db.String(1000), nullable=True)
|
||||||
|
|
||||||
|
# 0 - open
|
||||||
|
# 1 - merged
|
||||||
|
# 2 - rejected
|
||||||
status = db.Column(db.Integer, nullable=False, default=0)
|
status = db.Column(db.Integer, nullable=False, default=0)
|
||||||
|
|
||||||
changes = db.relationship("EditRequestChange", backref="request",
|
changes = db.relationship("EditRequestChange", backref="request",
|
||||||
@ -379,11 +383,38 @@ class EditRequest(db.Model):
|
|||||||
name=self.package.name,
|
name=self.package.name,
|
||||||
id=self.id)
|
id=self.id)
|
||||||
|
|
||||||
|
def getEditURL(self):
|
||||||
|
return url_for("create_edit_editrequest_page",
|
||||||
|
ptype=self.package.type.toName(),
|
||||||
|
author=self.package.author.username,
|
||||||
|
name=self.package.name,
|
||||||
|
id=self.id)
|
||||||
|
|
||||||
def applyAll(self, package):
|
def applyAll(self, package):
|
||||||
for change in self.changes:
|
for change in self.changes:
|
||||||
change.apply(package)
|
change.apply(package)
|
||||||
|
|
||||||
|
|
||||||
|
def checkPerm(self, user, perm):
|
||||||
|
if not user.is_authenticated:
|
||||||
|
return False
|
||||||
|
|
||||||
|
if type(perm) == str:
|
||||||
|
perm = Permission[perm]
|
||||||
|
elif type(perm) != Permission:
|
||||||
|
raise Exception("Unknown permission given to EditRequest.checkPerm()")
|
||||||
|
|
||||||
|
isOwner = user == self.author
|
||||||
|
|
||||||
|
# Members can edit their own packages, and editors can edit any packages
|
||||||
|
if perm == Permission.EDIT_EDITREQUEST:
|
||||||
|
return isOwner or user.rank.atLeast(UserRank.EDITOR)
|
||||||
|
|
||||||
|
else:
|
||||||
|
raise Exception("Permission {} is not related to packages".format(perm.name))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class EditRequestChange(db.Model):
|
class EditRequestChange(db.Model):
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
|
2
app/templates/packages/editrequest_create.html → app/templates/packages/editrequest_create_edit.html
2
app/templates/packages/editrequest_create.html → app/templates/packages/editrequest_create_edit.html
@ -1,7 +1,7 @@
|
|||||||
{% extends "base.html" %}
|
{% extends "base.html" %}
|
||||||
|
|
||||||
{% block title %}
|
{% block title %}
|
||||||
{{ package.title or "Create Package" }}
|
{{ package.title or "Edit Request" }}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
@ -7,6 +7,9 @@
|
|||||||
{% block content %}
|
{% block content %}
|
||||||
<h1>{{ request.title }} by {{ request.author.display_name }}</h1>
|
<h1>{{ request.title }} by {{ request.author.display_name }}</h1>
|
||||||
<p>
|
<p>
|
||||||
|
{% if request.status != 1 and request.status != 2 and request.checkPerm(current_user, 'EDIT_EDITREQUEST') %}
|
||||||
|
<a href="{{ request.getEditURL() }}">Edit</a> |
|
||||||
|
{% endif %}
|
||||||
Package: <a href="{{ package.getDetailsURL() }}">{{ package.title }}</a>
|
Package: <a href="{{ package.getDetailsURL() }}">{{ package.title }}</a>
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
|
@ -249,19 +249,43 @@ class EditRequestForm(PackageForm):
|
|||||||
edit_desc = TextField("Edit Description", [Optional()])
|
edit_desc = TextField("Edit Description", [Optional()])
|
||||||
|
|
||||||
@app.route("/<ptype>s/<author>/<name>/requests/new/", methods=["GET","POST"])
|
@app.route("/<ptype>s/<author>/<name>/requests/new/", methods=["GET","POST"])
|
||||||
|
@app.route("/<ptype>s/<author>/<name>/requests/<id>/edit/", methods=["GET","POST"])
|
||||||
@login_required
|
@login_required
|
||||||
def create_editrequest_page(ptype, author, name):
|
def create_edit_editrequest_page(ptype, author, name, id=None):
|
||||||
package = getPageByInfo(ptype, author, name)
|
package = getPageByInfo(ptype, author, name)
|
||||||
|
edited_package = package
|
||||||
|
|
||||||
form = EditRequestForm(request.form, obj=package)
|
erequest = None
|
||||||
|
if id is not None:
|
||||||
|
erequest = EditRequest.query.get(id)
|
||||||
|
if erequest.package != package:
|
||||||
|
abort(404)
|
||||||
|
|
||||||
|
from copy import copy
|
||||||
|
edited_package = copy(package)
|
||||||
|
erequest.applyAll(edited_package)
|
||||||
|
|
||||||
|
if not erequest.checkPerm(current_user, Permission.EDIT_EDITREQUEST):
|
||||||
|
abort(403)
|
||||||
|
|
||||||
|
if erequest.status != 0:
|
||||||
|
flash("Can't edit EditRequest, it has already been merged or rejected", "error")
|
||||||
|
return redirect(erequest.getURL())
|
||||||
|
|
||||||
|
|
||||||
|
form = EditRequestForm(request.form, obj=edited_package)
|
||||||
if request.method == "POST" and form.validate():
|
if request.method == "POST" and form.validate():
|
||||||
erequest = EditRequest()
|
if erequest is None:
|
||||||
erequest.package = package
|
erequest = EditRequest()
|
||||||
erequest.author = current_user
|
erequest.package = package
|
||||||
|
erequest.author = current_user
|
||||||
|
|
||||||
erequest.title = form["edit_title"].data
|
erequest.title = form["edit_title"].data
|
||||||
erequest.desc = form["edit_desc"].data
|
erequest.desc = form["edit_desc"].data
|
||||||
db.session.add(erequest)
|
db.session.add(erequest)
|
||||||
|
|
||||||
|
EditRequestChange.query.filter_by(request=erequest).delete()
|
||||||
|
|
||||||
wasChangeMade = False
|
wasChangeMade = False
|
||||||
for e in PackagePropertyKey:
|
for e in PackagePropertyKey:
|
||||||
newValue = form[e.name].data
|
newValue = form[e.name].data
|
||||||
@ -288,8 +312,11 @@ def create_editrequest_page(ptype, author, name):
|
|||||||
return redirect(erequest.getURL())
|
return redirect(erequest.getURL())
|
||||||
else:
|
else:
|
||||||
flash("No changes detected", "warning")
|
flash("No changes detected", "warning")
|
||||||
|
elif erequest is not None:
|
||||||
|
form["edit_title"].data = erequest.title
|
||||||
|
form["edit_desc"].data = erequest.desc
|
||||||
|
|
||||||
return render_template("packages/editrequest_create.html", package=package, form=form)
|
return render_template("packages/editrequest_create_edit.html", package=package, form=form)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/<ptype>s/<author>/<name>/requests/<id>/")
|
@app.route("/<ptype>s/<author>/<name>/requests/<id>/")
|
||||||
|
Loading…
Reference in New Issue
Block a user