Add tag editor

This commit is contained in:
rubenwardy 2018-05-27 17:11:02 +01:00
parent 6ce2eb7133
commit 19848a154d
No known key found for this signature in database
GPG Key ID: A1E29D52FF81513C
6 changed files with 105 additions and 1 deletions

@ -7,6 +7,7 @@
{% block content %} {% block content %}
<ul> <ul>
<li><a href="{{ url_for('user_list_page') }}">User list</a></li> <li><a href="{{ url_for('user_list_page') }}">User list</a></li>
<li><a href="{{ url_for('tag_list_page') }}">Tag Editor</a></li>
<li><a href="{{ url_for('switch_user_page') }}">Sign in as another user</a></li> <li><a href="{{ url_for('switch_user_page') }}">Sign in as another user</a></li>
</ul> </ul>

@ -63,6 +63,9 @@
{% if current_user.rank == current_user.rank.ADMIN %} {% if current_user.rank == current_user.rank.ADMIN %}
<li><a href="{{ url_for('admin_page') }}">Admin</a></li> <li><a href="{{ url_for('admin_page') }}">Admin</a></li>
{% endif %} {% endif %}
{% if current_user.rank == current_user.rank.MODERATOR %}
<li><a href="{{ url_for('tag_list_page') }}">Tag Editor</a></li>
{% endif %}
<li><a href="{{ url_for('user.logout') }}">Sign out</a></li> <li><a href="{{ url_for('user.logout') }}">Sign out</a></li>
</ul> </ul>
</li> </li>

@ -0,0 +1,27 @@
{% extends "base.html" %}
{% block title %}
{% if tag %}
Edit {{ tag.title }}
{% else %}
New tag
{% endif %}
{% endblock %}
{% block content %}
<p>
<a href="{{ url_for('tag_list_page') }}">Back to list</a> |
<a href="{{ url_for('createedit_tag_page') }}">New Tag</a>
</p>
{% from "macros/forms.html" import render_field, render_submit_field %}
<form method="POST" action="" enctype="multipart/form-data">
{{ form.hidden_tag() }}
{{ render_field(form.title) }}
{% if tag %}
{{ render_field(form.name) }}
{% endif %}
{{ render_submit_field(form.submit) }}
</form>
{% endblock %}

@ -0,0 +1,16 @@
{% extends "base.html" %}
{% block title %}
Tags
{% endblock %}
{% block content %}
<p>
<a href="{{ url_for('createedit_tag_page') }}">New Tag</a>
</p>
<ul>
{% for t in tags %}
<li><a href="{{ url_for('createedit_tag_page', name=t.name) }}">{{ t.title }}</a> [{{ t.packages | count }} packages]</li>
{% endfor %}
</ul>
{% endblock %}

@ -43,7 +43,7 @@ def home_page():
packages = query.order_by(db.desc(Package.created_at)).limit(15).all() packages = query.order_by(db.desc(Package.created_at)).limit(15).all()
return render_template("index.html", packages=packages, count=count) return render_template("index.html", packages=packages, count=count)
from . import users, githublogin, packages, sass, tasks, admin, notifications from . import users, githublogin, packages, sass, tasks, admin, notifications, tagseditor
@menu.register_menu(app, ".help", "Help", order=19, endpoint_arguments_constructor=lambda: { 'path': 'help' }) @menu.register_menu(app, ".help", "Help", order=19, endpoint_arguments_constructor=lambda: { 'path': 'help' })
@app.route('/<path:path>/') @app.route('/<path:path>/')

57
app/views/tagseditor.py Normal file

@ -0,0 +1,57 @@
# Content DB
# Copyright (C) 2018 rubenwardy
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from flask import *
from flask_user import *
from app import app
from app.models import *
from flask_wtf import FlaskForm
from wtforms import *
from wtforms.validators import *
from app.utils import rank_required
@app.route("/tags/")
@rank_required(UserRank.MODERATOR)
def tag_list_page():
return render_template("tags/list.html", tags=Tag.query.all())
class TagForm(FlaskForm):
title = StringField("Title", [InputRequired(), Length(3,100)])
name = StringField("Name", [Optional(), Length(1, 20), Regexp("^[a-z0-9_]", 0, "Lower case letters (a-z), digits (0-9), and underscores (_) only")])
submit = SubmitField("Save")
@app.route("/tags/new/", methods=["GET", "POST"])
@app.route("/tags/<name>/edit/", methods=["GET", "POST"])
@rank_required(UserRank.MODERATOR)
def createedit_tag_page(name=None):
tag = None
if name is not None:
tag = Tag.query.filter_by(name=name).first()
if tag is None:
abort(404)
form = TagForm(formdata=request.form, obj=tag)
if request.method == "POST" and form.validate():
if tag is None:
tag = Tag(form.title.data)
db.session.add(tag)
else:
form.populate_obj(tag)
db.session.commit()
return redirect(url_for("createedit_tag_page", name=tag.name))
return render_template("tags/edit.html", tag=tag, form=form)