From 29e5a9eb95689396ff8edbab42c0d8064c8c87d8 Mon Sep 17 00:00:00 2001 From: Sander Ferdinand Date: Wed, 4 Jul 2018 16:04:19 +0200 Subject: [PATCH] Implement feature proposal categories --- wowfunding/bin/utils_request.py | 12 ++++++++---- wowfunding/factory.py | 7 ------- wowfunding/routes.py | 16 ++++++++++++++-- wowfunding/templates/proposal_edit.html | 12 +++++++++++- 4 files changed, 33 insertions(+), 14 deletions(-) diff --git a/wowfunding/bin/utils_request.py b/wowfunding/bin/utils_request.py index e0b91bb..9a1850d 100644 --- a/wowfunding/bin/utils_request.py +++ b/wowfunding/bin/utils_request.py @@ -7,14 +7,18 @@ from wowfunding.orm.orm import Proposal, User @app.context_processor -def template_vars(): +def templating(): global summary_data - return dict(summary_data=summary_data[1]) + from flask.ext.login import current_user + return dict(logged_in=current_user.is_authenticated, + current_user=current_user, + funding_categories=settings.FUNDING_CATEGORIES, + summary_data=summary_data[1]) -def fetch_summary(): +def fetch_summary(purge=False): global summary_data - if summary_data: + if summary_data and not purge: if (datetime.now() - summary_data[0]).total_seconds() <= 120: return diff --git a/wowfunding/factory.py b/wowfunding/factory.py index 3d687a8..586cc77 100644 --- a/wowfunding/factory.py +++ b/wowfunding/factory.py @@ -48,13 +48,6 @@ def create_app(): app.session_interface = JsonRedis(key_prefix=app.config['SESSION_PREFIX'], use_signer=False) cache = WowCache() - # template vars - @app.context_processor - def _bootstrap_templating(): - from flask.ext.login import current_user - return dict(logged_in=current_user.is_authenticated, - current_user=current_user) - # import routes from wowfunding import routes from wowfunding import api diff --git a/wowfunding/routes.py b/wowfunding/routes.py index 4ea1f91..a46df8d 100644 --- a/wowfunding/routes.py +++ b/wowfunding/routes.py @@ -78,9 +78,10 @@ def proposal(pid): parameter('content', type=str, required=True, location='json'), parameter('pid', type=int, required=False, location='json'), parameter('funds_target', type=float, required=True, location='json'), - parameter('addr_receiving', type=str, required=True, location='json') + parameter('addr_receiving', type=str, required=True, location='json'), + parameter('category', type=str, required=True, location='json') ) -def proposal_api_add(title, content, pid, funds_target, addr_receiving): +def proposal_api_add(title, content, pid, funds_target, addr_receiving, category): import markdown2 if current_user.is_anonymous: @@ -91,6 +92,9 @@ def proposal_api_add(title, content, pid, funds_target, addr_receiving): if len(content) <= 20: return make_response(jsonify('content too short'), 500) + if category and category not in settings.FUNDING_CATEGORIES: + return make_response(jsonify('unknown category'), 500) + try: from wowfunding.bin.anti_xss import such_xss content_escaped = such_xss(content) @@ -111,6 +115,8 @@ def proposal_api_add(title, content, pid, funds_target, addr_receiving): p.html = html if addr_receiving: p.addr_receiving = addr_receiving + if category: + p.category = category p.last_edited = datetime.now() else: if funds_target <= 1: @@ -123,10 +129,16 @@ def proposal_api_add(title, content, pid, funds_target, addr_receiving): p.last_edited = datetime.now() p.funds_target = funds_target p.addr_receiving = addr_receiving + p.category = category db_session.add(p) db_session.commit() db_session.flush() + + # reset cached statistics + from wowfunding.bin import utils_request + utils_request.fetch_summary(purge=True) + return make_response(jsonify({'url': url_for('proposal', pid=p.id)})) diff --git a/wowfunding/templates/proposal_edit.html b/wowfunding/templates/proposal_edit.html index b604f8e..663c372 100644 --- a/wowfunding/templates/proposal_edit.html +++ b/wowfunding/templates/proposal_edit.html @@ -65,6 +65,15 @@ +
+ + +
+
@@ -118,7 +127,8 @@ 'title': document.getElementById('title').value, 'content': simplemde.value(), 'funds_target': parseFloat(document.getElementById('funds_target').value), - 'addr_receiving': document.getElementById('addr_receiving').value + 'addr_receiving': document.getElementById('addr_receiving').value, + 'category': document.getElementById('category').value }; if (pid) {