From 1095a049a63e4286da620b914c23411ea7a02e64 Mon Sep 17 00:00:00 2001 From: François Schmidts Date: Sat, 10 Oct 2015 12:59:32 +0200 Subject: base category creation/edition --- AUTHORS.rst | 2 +- pyaggr3g470r/templates/edit_category.html | 23 ++++++++++++ pyaggr3g470r/views/category.py | 62 +++++++++++++++++++++++++++++++ src/runserver.py | 2 + src/web/controllers/__init__.py | 5 ++- src/web/forms.py | 20 +++++----- src/web/views/__init__.py | 1 + 7 files changed, 103 insertions(+), 12 deletions(-) create mode 100644 pyaggr3g470r/templates/edit_category.html create mode 100644 pyaggr3g470r/views/category.py diff --git a/AUTHORS.rst b/AUTHORS.rst index 23ff750e..5a6f2cc0 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -3,4 +3,4 @@ by Cédric Bonhomme https://www.cedricbonhomme.org Contributors ```````````` -- François Schmidts https://bitbucket.org/jaesivsm +- François Schmidts http://1pxsolidblack.pl/ diff --git a/pyaggr3g470r/templates/edit_category.html b/pyaggr3g470r/templates/edit_category.html new file mode 100644 index 00000000..93c952d6 --- /dev/null +++ b/pyaggr3g470r/templates/edit_category.html @@ -0,0 +1,23 @@ +{% extends "layout.html" %} +{% block content %} +
+
+

{{ action }}

+
+ {{ form.hidden_tag() }} +
+ +
+ {{ form.name(class_="form-control", size="100%") }} +
+ {% for error in form.name.errors %} {{ error }}
{% endfor %} +
+
+
+ {{ form.submit(class_="btn btn-default") }} +
+
+
+
+
+{% endblock %} diff --git a/pyaggr3g470r/views/category.py b/pyaggr3g470r/views/category.py new file mode 100644 index 00000000..c5defb7f --- /dev/null +++ b/pyaggr3g470r/views/category.py @@ -0,0 +1,62 @@ +from flask import g, Blueprint, render_template, flash, redirect, url_for +from flask.ext.babel import gettext +from flask.ext.login import login_required + +from pyaggr3g470r.forms import AddCategoryForm +from pyaggr3g470r.lib.view_utils import etag_match +from pyaggr3g470r.controllers.category import CategoryController + +categories_bp = Blueprint('categories', __name__, url_prefix='/categories') +category_bp = Blueprint('category', __name__, url_prefix='/category') + + +@category_bp.route('/create', methods=['GET']) +@category_bp.route('/edit/', methods=['GET']) +@login_required +@etag_match +def form(category_id=None): + action = gettext("Add a category") + head_titles = [action] + if category_id is None: + return render_template('edit_category.html', action=action, + head_titles=head_titles, form=AddCategoryForm()) + category = CategoryController(g.user.id).get(id=category_id) + action = gettext('Edit category') + head_titles = [action] + if category.name: + head_titles.append(category.name) + return render_template('edit_category.html', action=action, + head_titles=head_titles, category=category, + form=AddCategoryForm(obj=category)) + + +@category_bp.route('/create', methods=['POST']) +@category_bp.route('/edit/', methods=['POST']) +@login_required +def process_form(category_id=None): + form = AddCategoryForm() + cat_contr = CategoryController(g.user.id) + + if not form.validate(): + return render_template('edit_category.html', form=form) + existing_cats = list(cat_contr.read(name=form.name.data)) + if existing_cats and category_id is None: + flash(gettext("Couldn't add category: already exists."), "warning") + return redirect(url_for('category.form', + category_id=existing_cats[0].id)) + # Edit an existing category + category_attr = {'name': form.name.data} + + if category_id is not None: + cat_contr.update({'id': category_id}, category_attr) + flash(gettext('Category %(cat_name)r successfully updated.', + cat_name=category_attr['name']), 'success') + return redirect(url_for('category.form', category_id=category_id)) + + # Create a new category + new_category = cat_contr.create(**category_attr) + + flash(gettext('Category %(category_name)r successfully created.', + category_name=new_category.name), 'success') + + return redirect(url_for('category.form', category_id=new_category.id)) diff --git a/src/runserver.py b/src/runserver.py index 8e1e2459..b355d7d1 100755 --- a/src/runserver.py +++ b/src/runserver.py @@ -51,6 +51,8 @@ with application.app_context(): application.register_blueprint(views.article_bp) application.register_blueprint(views.feeds_bp) application.register_blueprint(views.feed_bp) + application.register_blueprint(views.categories_bp) + application.register_blueprint(views.category_bp) application.register_blueprint(views.icon_bp) diff --git a/src/web/controllers/__init__.py b/src/web/controllers/__init__.py index ad77fa1d..a1b89ea8 100644 --- a/src/web/controllers/__init__.py +++ b/src/web/controllers/__init__.py @@ -1,8 +1,9 @@ from .feed import FeedController +from .category import CategoryController from .article import ArticleController from .user import UserController from .icon import IconController -__all__ = ['FeedController', 'ArticleController', 'UserController', - 'IconController'] +__all__ = ['FeedController', 'CategoryController', 'ArticleController', + 'UserController', 'IconController'] diff --git a/src/web/forms.py b/src/web/forms.py index 447a54e9..d901f260 100644 --- a/src/web/forms.py +++ b/src/web/forms.py @@ -27,7 +27,7 @@ __copyright__ = "Copyright (c) Cedric Bonhomme" __license__ = "GPLv3" -from flask import flash, request, url_for, redirect +from flask import flash, url_for, redirect from flask.ext.wtf import Form from flask.ext.babel import lazy_gettext from wtforms import TextField, TextAreaField, PasswordField, BooleanField, \ @@ -57,7 +57,9 @@ class SignupForm(Form): def validate(self): validated = super(SignupForm, self).validate() if self.nickname.data != User.make_valid_nickname(self.nickname.data): - self.nickname.errors.append(lazy_gettext('This nickname has invalid characters. Please use letters, numbers, dots and underscores only.')) + self.nickname.errors.append(lazy_gettext( + 'This nickname has invalid characters. ' + 'Please use letters, numbers, dots and underscores only.')) validated = False return validated @@ -102,7 +104,6 @@ class SigninForm(RedirectForm): return False else: flash(lazy_gettext('Invalid email or password'), 'danger') - #self.email.errors.append("Invalid email or password") return False @@ -124,7 +125,9 @@ class UserForm(Form): def validate(self): validated = super(UserForm, self).validate() if self.nickname.data != User.make_valid_nickname(self.nickname.data): - self.nickname.errors.append(lazy_gettext('This nickname has invalid characters. Please use letters, numbers, dots and underscores only.')) + self.nickname.errors.append(lazy_gettext( + 'This nickname has invalid characters. ' + 'Please use letters, numbers, dots and underscores only.')) validated = False return validated @@ -168,10 +171,10 @@ class AddFeedForm(Form): enabled = BooleanField(lazy_gettext("Check for updates"), default=True) submit = SubmitField(lazy_gettext("Save")) - def validate(self): - if not super(AddFeedForm, self).validate(): - return False - return True + +class AddCategoryForm(Form): + name = TextField(lazy_gettext("Name")) + submit = SubmitField(lazy_gettext("Sign up")) class InformationMessageForm(Form): @@ -201,5 +204,4 @@ class RecoverPasswordForm(Form): return False else: flash(lazy_gettext('Invalid email.'), 'danger') - #self.email.errors.append("Invalid email") return False diff --git a/src/web/views/__init__.py b/src/web/views/__init__.py index 36d382bd..1da9205b 100644 --- a/src/web/views/__init__.py +++ b/src/web/views/__init__.py @@ -3,4 +3,5 @@ from .api import * from .article import article_bp, articles_bp from .feed import feed_bp, feeds_bp +from .category import category_bp, categories_bp from .icon import icon_bp -- cgit