Allow changing collection URL name

This commit is contained in:
rubenwardy 2023-08-16 00:52:10 +01:00
parent 2ad25f1aa9
commit f470357a42
4 changed files with 31 additions and 11 deletions

@ -22,7 +22,7 @@ from flask_babel import lazy_gettext, gettext
from flask_login import current_user, login_required
from flask_wtf import FlaskForm
from wtforms import StringField, BooleanField, SubmitField, FieldList, HiddenField
from wtforms.validators import InputRequired, Length, Optional
from wtforms.validators import InputRequired, Length, Optional, Regexp
from app.models import Collection, db, Package, Permission, CollectionPackage, User, UserRank, AuditSeverity
from app.utils import is_package_page, nonempty_or_none, add_audit_log
@ -70,6 +70,8 @@ def view(author, name):
class CollectionForm(FlaskForm):
title = StringField(lazy_gettext("Title"), [InputRequired(), Length(3, 100)])
name = StringField("URL", [Optional(), Length(1, 20), Regexp("^[a-z0-9_]", 0,
"Lower case letters (a-z), digits (0-9), and underscores (_) only")])
short_description = StringField(lazy_gettext("Short Description"), [Optional(), Length(0, 200)])
private = BooleanField(lazy_gettext("Private"))
descriptions = FieldList(
@ -113,6 +115,8 @@ def create_edit(author=None, name=None):
for item in collection.items:
form.descriptions.append_entry(item.description)
form.package_ids.append_entry(item.package.id)
else:
form.name = None
if form.validate_on_submit():
ret = handle_create_edit(collection, form, initial_package, author)
@ -127,9 +131,9 @@ def handle_create_edit(collection: Collection, form: CollectionForm,
initial_package: typing.Optional[Package], author: User):
severity = AuditSeverity.NORMAL if author == current_user else AuditSeverity.EDITOR
name = collection.name if collection else regex_invalid_chars.sub("", form.title.data.lower().replace(" ", "_"))
name = form.name.data if collection else regex_invalid_chars.sub("", form.title.data.lower().replace(" ", "_"))
if collection is None:
if collection is None or name != collection.name:
if Collection.query \
.filter(Collection.name == name, Collection.author == author) \
.count() > 0:
@ -142,6 +146,7 @@ def handle_create_edit(collection: Collection, form: CollectionForm,
flash(gettext("Unable to create collection as a package with that name already exists"), "danger")
return
if collection is None:
collection = Collection()
collection.name = name
collection.author = author

@ -9,13 +9,22 @@
{% endblock %}
{% block content %}
{% from "macros/forms.html" import render_field, render_submit_field, render_checkbox_field %}
{% from "macros/forms.html" import render_field, render_submit_field, render_checkbox_field, render_field_prefix_button %}
<form method="POST" action="" enctype="multipart/form-data">
{{ render_submit_field(form.submit, class_="btn btn-primary float-right") }}
<h1>{{ self.title() }}</h1>
{{ form.hidden_tag() }}
{{ render_field(form.title) }}
<div class="row">
{{ render_field(form.title, class_="col-sm-6") }}
{% if form.name %}
{{ render_field_prefix_button(form.name, class_="col-sm-6", pattern="[a-z0-9_]+",
prefix="/collections/" + collection.author.username + "/",
hint=_("Users won't be redirected when going to the old URL")) }}
{% endif %}
</div>
{{ render_field(form.short_description) }}
{{ render_checkbox_field(form.private, class_="my-3") }}

@ -36,24 +36,29 @@
</div>
{%- endmacro %}
{% macro render_field_prefix_button(field, label=None, prefix="@", label_visible=true, right_url=None, right_label=None, fieldclass=None) -%}
{% macro render_field_prefix_button(field, label=None, prefix="@", label_visible=true, right_url=None, right_label=None, fieldclass=None, has_view=False, hint=None) -%}
<div class="form-group {% if field.errors %}has-danger{% endif %} {{ kwargs.pop('class_', '') }}">
{% if field.type != 'HiddenField' and label_visible %}
{% if not label and label != "" %}{% set label=field.label.text %}{% endif %}
{% if label %}<label for="{{ field.id }}">{{ label|safe }}</label>{% endif %}
{% endif %}
<div class="input-group mb-3">
<div class="input-group mb-1">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon1">{{ prefix }}</span>
</div>
{{ field(class_=fieldclass or 'form-control', **kwargs) }}
<a class="btn btn-secondary" id="{{ field.name }}-button">
{{ _("View") }}
</a>
{% if has_view %}
<a class="btn btn-secondary" id="{{ field.name }}-button">
{{ _("View") }}
</a>
{% endif %}
</div>
{{ render_errors(field) }}
{% if hint %}
<small class="form-text text-muted">{{ hint | safe }}</small>
{% endif %}
</div>
{%- endmacro %}

@ -116,7 +116,8 @@
{{ render_field_prefix_button(form.forums, class_="pkg_meta",
pattern="[0-9]+",
prefix="forum.minetest.net/viewtopic.php?t=",
placeholder=_("Tip: paste in a forum topic URL")) }}
placeholder=_("Tip: paste in a forum topic URL"),
has_view=True) }}
{{ render_field(form.video_url, class_="pkg_meta", hint=_("YouTube videos will be shown in an embed.")) }}
{{ render_field(form.donate_url, class_="pkg_meta", hint=_("If blank, the author's donation URL will be used instead.")) }}
</fieldset>