mirror of
https://github.com/minetest/contentdb.git
synced 2025-01-08 22:17:34 +01:00
Add ability to edit provides
This commit is contained in:
parent
f4c9348b7f
commit
5e4613a6ef
@ -410,6 +410,39 @@ class MetaPackage(db.Model):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
@staticmethod
|
||||
def ListToSpec(list):
|
||||
return ",".join([str(x) for x in list])
|
||||
|
||||
@staticmethod
|
||||
def SpecToList(spec, cache={}):
|
||||
retval = []
|
||||
arr = spec.split(",")
|
||||
|
||||
import re
|
||||
pattern = re.compile("^([a-z0-9_]+)$")
|
||||
|
||||
for x in arr:
|
||||
x = x.strip()
|
||||
if x == "":
|
||||
continue
|
||||
|
||||
if not pattern.match(x):
|
||||
continue
|
||||
|
||||
mp = cache.get(x)
|
||||
if mp is None:
|
||||
mp = MetaPackage.query.filter_by(name=x).first()
|
||||
|
||||
if mp is None:
|
||||
mp = MetaPackage(x)
|
||||
db.session.add(mp)
|
||||
|
||||
cache[x] = mp
|
||||
retval.append(mp)
|
||||
|
||||
return retval
|
||||
|
||||
class Tag(db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
name = db.Column(db.String(100), unique=True, nullable=False)
|
||||
|
@ -39,6 +39,25 @@
|
||||
</div>
|
||||
{% endmacro %}
|
||||
|
||||
{% macro render_mpackage_field(field, label=None, label_visible=true, right_url=None, right_label=None) -%}
|
||||
<div class="form-group {% if field.errors %}has-error{% endif %} {{ kwargs.pop('class_', '') }}">
|
||||
{% if field.type != 'HiddenField' and label_visible %}
|
||||
{% if not label %}{% set label=field.label.text %}{% endif %}
|
||||
<label for="{{ field.id }}" class="control-label">{{ label|safe }}</label>
|
||||
{% endif %}
|
||||
<div class="metapackage_selector">
|
||||
<input type="text" placeholder="Start typing to see suggestions">
|
||||
<div class="clearboth"></div>
|
||||
</div>
|
||||
{{ field(class_='form-control', **kwargs) }}
|
||||
{% if field.errors %}
|
||||
{% for e in field.errors %}
|
||||
<p class="help-block">{{ e }}</p>
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
</div>
|
||||
{% endmacro %}
|
||||
|
||||
{% macro render_checkbox_field(field, label=None) -%}
|
||||
{% if not label %}{% set label=field.label.text %}{% endif %}
|
||||
<div class="checkbox">
|
||||
|
@ -10,7 +10,7 @@
|
||||
{% block content %}
|
||||
<h2>Create Package</h2>
|
||||
|
||||
{% from "macros/forms.html" import render_field, render_submit_field, form_includes, render_multiselect_field %}
|
||||
{% from "macros/forms.html" import render_field, render_submit_field, form_includes, render_multiselect_field, render_mpackage_field %}
|
||||
{{ form_includes() }}
|
||||
|
||||
<form method="POST" action="" class="tableform">
|
||||
@ -22,6 +22,7 @@
|
||||
{{ render_field(form.desc, class_="pkg_meta") }}
|
||||
{{ render_field(form.type, class_="pkg_meta") }}
|
||||
{{ render_field(form.license, class_="pkg_meta") }}
|
||||
{{ render_mpackage_field(form.provides_str, class_="pkg_meta", placeholder="Comma separated list") }}
|
||||
{{ render_multiselect_field(form.tags, class_="pkg_meta") }}
|
||||
|
||||
<div class="pkg_wiz_1">
|
||||
|
@ -106,6 +106,7 @@ class PackageForm(FlaskForm):
|
||||
desc = TextAreaField("Long Description", [Optional(), Length(0,10000)])
|
||||
type = SelectField("Type", [InputRequired()], choices=PackageType.choices(), coerce=PackageType.coerce, default=PackageType.MOD)
|
||||
license = QuerySelectField("License", [InputRequired()], query_factory=lambda: License.query, get_pk=lambda a: a.id, get_label=lambda a: a.name)
|
||||
provides_str = StringField("Provides", [InputRequired(), Length(1,1000)])
|
||||
tags = QuerySelectMultipleField('Tags', query_factory=lambda: Tag.query.order_by(db.asc(Tag.name)), get_pk=lambda a: a.id, get_label=lambda a: a.title)
|
||||
repo = StringField("Repo URL", [Optional(), URL()])
|
||||
website = StringField("Website URL", [Optional(), URL()])
|
||||
@ -144,6 +145,9 @@ def create_edit_package_page(author=None, name=None):
|
||||
form = PackageForm(formdata=request.form, obj=package)
|
||||
|
||||
# Initial form class from post data and default data
|
||||
if request.method == "GET" and package is not None:
|
||||
form.provides_str.data = MetaPackage.ListToSpec(package.provides)
|
||||
|
||||
if request.method == "POST" and form.validate():
|
||||
wasNew = False
|
||||
if not package:
|
||||
@ -164,6 +168,12 @@ def create_edit_package_page(author=None, name=None):
|
||||
|
||||
form.populate_obj(package) # copy to row
|
||||
|
||||
mpackage_cache = {}
|
||||
package.provides.clear()
|
||||
mpackages = MetaPackage.SpecToList(form.provides_str.data, mpackage_cache)
|
||||
for m in mpackages:
|
||||
package.provides.append(m)
|
||||
|
||||
package.tags.clear()
|
||||
for tag in form.tags.raw_data:
|
||||
package.tags.append(Tag.query.get(tag))
|
||||
|
Loading…
Reference in New Issue
Block a user