Add tag selecting in edit package

Fixes #35
This commit is contained in:
rubenwardy 2018-03-26 12:42:35 +01:00
parent 39ef712817
commit 536da2add2
5 changed files with 11 additions and 4 deletions

@ -275,6 +275,7 @@ class Package(db.Model):
class Tag(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), unique=True, nullable=False)
title = db.Column(db.String(100), nullable=False)
backgroundColor = db.Column(db.String(6), nullable=False)
textColor = db.Column(db.String(6), nullable=False)
@ -284,10 +285,9 @@ class Tag(db.Model):
self.backgroundColor = backgroundColor
self.textColor = textColor
def getName(self):
import re
regex = re.compile('[^a-z_]')
return regex.sub("", self.title.lower().replace(" ", "_"))
self.name = regex.sub("", self.title.lower().replace(" ", "_"))
class PackageRelease(db.Model):
id = db.Column(db.Integer, primary_key=True)

@ -14,6 +14,7 @@
{{ render_field(form.shortDesc) }}
{{ render_field(form.desc) }}
{{ render_field(form.type) }}
{{ render_field(form.tags) }}
{{ render_field(form.repo) }}
{{ render_field(form.website) }}
{{ render_field(form.issueTracker) }}

@ -26,7 +26,7 @@
</div>
<ul>
{% for t in tags %}
<li><a href="{{ url_for('packages_page', q=(query or '')+' tag:'+t.getName()) }}">
<li><a href="{{ url_for('packages_page', q=(query or '')+' tag:'+t.name) }}">
{{ t.title }}
</a></li>
{% else %}

@ -9,6 +9,7 @@ from .utils import *
from flask_wtf import FlaskForm
from wtforms import *
from wtforms.validators import *
from wtforms.ext.sqlalchemy.fields import QuerySelectMultipleField
# TODO: the following could be made into one route, except I"m not sure how
@ -134,6 +135,7 @@ class PackageForm(FlaskForm):
shortDesc = StringField("Short Description", [InputRequired(), Length(1,200)])
desc = TextAreaField("Long Description", [Optional(), Length(0,10000)])
type = SelectField("Type", [InputRequired()], choices=PackageType.choices(), coerce=PackageType.coerce, default=PackageType.MOD)
tags = QuerySelectMultipleField('Tags', query_factory=lambda: Tag.query, get_pk=lambda a: a.id, get_label=lambda a: a.title)
repo = StringField("Repo URL", [Optional(), URL()])
website = StringField("Website URL", [Optional(), URL()])
issueTracker = StringField("Issue Tracker URL", [Optional(), URL()])
@ -164,6 +166,10 @@ def create_edit_package_page(type=None, author=None, name=None):
package.author = current_user
# package.approved = package.checkPerm(current_user, Permission.APPROVE_NEW)
package.tags.clear()
for tag in form.tags.raw_data:
package.tags.append(Tag.query.get(tag))
form.populate_obj(package) # copy to row
db.session.commit() # save
return redirect(package.getDetailsURL()) # redirect

@ -40,7 +40,7 @@ if not os.path.isfile("db.sqlite"):
"Environment", "Transport", "Maintenance", "Plants and farming", \
"PvP", "PvE", "Survival", "Creative", "Puzzle", "Multiplayer", "Singleplayer"]:
row = Tag(tag)
tags[row.getName()] = row
tags[row.name] = row
db.session.add(row)