From b34d06d49e662b8697a33c6350145ae5b2004882 Mon Sep 17 00:00:00 2001 From: Sander Ferdinand Date: Wed, 4 Jul 2018 19:50:36 +0200 Subject: [PATCH] Introduced a new proposal status: proposed. Moved some templates around and created macros. --- settings.py_example | 10 ++ wowfunding/api.py | 2 +- wowfunding/bin/utils_request.py | 3 +- wowfunding/orm/orm.py | 40 ++++--- wowfunding/routes.py | 24 ++-- wowfunding/static/css/wow.css | 5 + wowfunding/static/js/app.js | 9 +- .../edit.html} | 2 +- .../templates/{ => proposal}/proposal.html | 0 wowfunding/templates/proposal/proposals.html | 23 ++++ wowfunding/templates/proposals.html | 104 ------------------ 11 files changed, 88 insertions(+), 134 deletions(-) rename wowfunding/templates/{proposal_edit.html => proposal/edit.html} (97%) rename wowfunding/templates/{ => proposal}/proposal.html (100%) create mode 100644 wowfunding/templates/proposal/proposals.html delete mode 100644 wowfunding/templates/proposals.html diff --git a/settings.py_example b/settings.py_example index a8610cb..fa85871 100644 --- a/settings.py_example +++ b/settings.py_example @@ -1,5 +1,6 @@ import logging import socket +import collections import os @@ -33,3 +34,12 @@ FUNDING_CATEGORIES = [ 'misc', 'design' ] + +FUNDING_STATUSES = collections.OrderedDict() +FUNDING_STATUSES[0] = 'disabled' +FUNDING_STATUSES[1] = 'proposal' +FUNDING_STATUSES[2] = 'funding' +FUNDING_STATUSES[3] = 'wip' +FUNDING_STATUSES[4] = 'completed' + +USER_REG_DISABLED = False \ No newline at end of file diff --git a/wowfunding/api.py b/wowfunding/api.py index 40920f1..fa3f3e5 100644 --- a/wowfunding/api.py +++ b/wowfunding/api.py @@ -10,7 +10,7 @@ from wowfunding.orm.orm import Proposal, User @app.route('/api/1/proposals') @endpoint.api( - parameter('status', type=int, location='args', default=0), + parameter('status', type=int, location='args', default=1), parameter('cat', type=str, location='args'), parameter('limit', type=int, location='args', default=20), parameter('offset', type=int, location='args', default=0) diff --git a/wowfunding/bin/utils_request.py b/wowfunding/bin/utils_request.py index 9a1850d..55bfd5c 100644 --- a/wowfunding/bin/utils_request.py +++ b/wowfunding/bin/utils_request.py @@ -13,6 +13,7 @@ def templating(): return dict(logged_in=current_user.is_authenticated, current_user=current_user, funding_categories=settings.FUNDING_CATEGORIES, + funding_statuses=settings.FUNDING_STATUSES, summary_data=summary_data[1]) @@ -24,7 +25,7 @@ def fetch_summary(purge=False): data = {} categories = settings.FUNDING_CATEGORIES - statuses = [0, 1, 2] + statuses = settings.FUNDING_STATUSES.keys() for cat in categories: q = db_session.query(Proposal) diff --git a/wowfunding/orm/orm.py b/wowfunding/orm/orm.py index 9c715ea..f095df5 100644 --- a/wowfunding/orm/orm.py +++ b/wowfunding/orm/orm.py @@ -93,11 +93,12 @@ class Proposal(base): addr_receiving = sa.Column(sa.VARCHAR) # proposal status: - # -1: disabled - # 0: proposed - # 1: wip - # 2: completed - status = sa.Column(sa.INTEGER, default=0) + # 0: disabled + # 1: proposed + # 2: funding required + # 3: wip + # 4: completed + status = sa.Column(sa.INTEGER, default=1) user_id = sa.Column(sa.Integer, sa.ForeignKey('users.user_id')) user = relationship("User", back_populates="proposals") @@ -140,13 +141,18 @@ class Proposal(base): return # check if we have a valid addr_donation generated. if not, make one. - if not result.addr_donation: + if not result.addr_donation and result.status >= 2: Proposal.generate_donation_addr(result) - comment_count = db_session.query(sa.func.count(Comment.id)).filter(Comment.proposal_id == result.id).scalar() - setattr(result, 'comment_count', comment_count) return result + @property + def comment_count(self): + from wowfunding.factory import db_session + q = db_session.query(sa.func.count(Comment.id)) + q = q.filter(Comment.proposal_id == self.id) + return q.scalar() + def get_comments(self): from wowfunding.factory import db_session q = db_session.query(Comment) @@ -231,13 +237,16 @@ class Proposal(base): return addr_donation['address'] @classmethod - def find_by_args(cls, status:int = None, cat: str = None, limit: int = 20, offset=0): + def find_by_args(cls, status: int = None, cat: str = None, limit: int = 20, offset=0): from wowfunding.factory import db_session - if status is None or not status >= 0 or not status <= 2: - raise NotImplementedError('missing status') + if isinstance(status, int) and status not in settings.FUNDING_STATUSES.keys(): + raise NotImplementedError('invalid status') + if isinstance(cat, str) and cat not in settings.FUNDING_CATEGORIES: + raise NotImplementedError('invalid cat') q = cls.query - q = q.filter(Proposal.status == status) + if isinstance(status, int): + q = q.filter(Proposal.status == status) if cat: q = q.filter(Proposal.category == cat) q = q.order_by(Proposal.date_added.desc()) @@ -245,12 +254,7 @@ class Proposal(base): if isinstance(offset, int): q = q.offset(offset) - results = q.all() - for result in results: - comment_count = db_session.query(sa.func.count(Comment.id)).filter( - Comment.proposal_id == result.id).scalar() - setattr(result, 'comment_count', comment_count) - return results + return q.all() @classmethod def search(cls, key: str): diff --git a/wowfunding/routes.py b/wowfunding/routes.py index a46df8d..fc2e81c 100644 --- a/wowfunding/routes.py +++ b/wowfunding/routes.py @@ -130,6 +130,7 @@ def proposal_api_add(title, content, pid, funds_target, addr_receiving, category p.funds_target = funds_target p.addr_receiving = addr_receiving p.category = category + p.status = 1 db_session.add(p) db_session.commit() @@ -169,21 +170,30 @@ def user(name): user = q.first() return render_template('user.html', user=user) - @app.route('/proposals') @endpoint.api( - parameter('status', type=int, location='args', default=0), - parameter('page', type=int, location='args'), - parameter('cat', type=str, location='args') + parameter('status', type=int, location='args', required=False), + parameter('page', type=int, location='args', required=False), + parameter('cat', type=str, location='args', required=False) ) def proposals(status, page, cat): + if not isinstance(status, int) and not isinstance(page, int) and not cat: + # no args, render overview + proposals = { + 'proposed': Proposal.find_by_args(status=1, limit=10), + 'funding': Proposal.find_by_args(status=2, limit=10), + 'wip': Proposal.find_by_args(status=3, limit=4)} + return make_response(render_template('proposal/overview.html', proposals=proposals)) + try: + if not isinstance(status, int): + status = 1 proposals = Proposal.find_by_args(status=status, cat=cat) except: - return make_response(redirect(url_for('proposals') + '?status=0')) - - return make_response(render_template('proposals.html', proposals=proposals, status=status, cat=cat)) + return make_response(redirect(url_for('proposals'))) + return make_response(render_template('proposal/proposals.html', + proposals=proposals, status=status, cat=cat)) @app.route('/register', methods=['GET', 'POST']) def register(): diff --git a/wowfunding/static/css/wow.css b/wowfunding/static/css/wow.css index d82b887..7e8a990 100644 --- a/wowfunding/static/css/wow.css +++ b/wowfunding/static/css/wow.css @@ -99,6 +99,11 @@ body { font-size: 15px; } +.table-proposal tbody td:first-child{ + word-break: break-word; + width: 40%; +} + .table-hover tbody tr:hover { background-color: rgba(0,0,0,.075); } diff --git a/wowfunding/static/js/app.js b/wowfunding/static/js/app.js index 7982027..f1dfcd0 100644 --- a/wowfunding/static/js/app.js +++ b/wowfunding/static/js/app.js @@ -1,5 +1,10 @@ -function clickDataHref(d){ - window.location.href = d.getAttribute("data-href"); +function proposalNavbarclickDataHref(d){ + if(d.classList.contains('active')){ + // if the navbar button is already active, remove filter, go to proposals page + window.location.href = '/proposals' + } else { + window.location.href = d.getAttribute("data-href"); + } } function hideShow(element_id) { diff --git a/wowfunding/templates/proposal_edit.html b/wowfunding/templates/proposal/edit.html similarity index 97% rename from wowfunding/templates/proposal_edit.html rename to wowfunding/templates/proposal/edit.html index 663c372..66fb3df 100644 --- a/wowfunding/templates/proposal_edit.html +++ b/wowfunding/templates/proposal/edit.html @@ -69,7 +69,7 @@ diff --git a/wowfunding/templates/proposal.html b/wowfunding/templates/proposal/proposal.html similarity index 100% rename from wowfunding/templates/proposal.html rename to wowfunding/templates/proposal/proposal.html diff --git a/wowfunding/templates/proposal/proposals.html b/wowfunding/templates/proposal/proposals.html new file mode 100644 index 0000000..ef4f4ff --- /dev/null +++ b/wowfunding/templates/proposal/proposals.html @@ -0,0 +1,23 @@ +{% extends "base.html" %} +{% block content %} +{% from 'proposal/macros/table.html' import proposal_table %} + +
+ {% include 'proposal/macros/navbar.html' %} + +
+
+ {% if proposals %} + {{ proposal_table(title='', _proposals=proposals) }} + {% else %} + No proposals here yet. + {% endif %} +
+ {% include 'sidebar.html' %} +
+ +
+ +
+ +{% endblock %} diff --git a/wowfunding/templates/proposals.html b/wowfunding/templates/proposals.html deleted file mode 100644 index aa89368..0000000 --- a/wowfunding/templates/proposals.html +++ /dev/null @@ -1,104 +0,0 @@ -{% extends "base.html" %} -{% block content %} - - -
-
-
-
-
-
- - - -
-
- - wow -
-
-
- {% if cat %} - - Results limited by category '{{cat}}'. Undo filter. - -
- {% endif %} -
-
- -
-
- {% if proposals %} - - - - - - - {% if status == 0 %} - - {% else %} - - {% endif %} - - - - - {% for p in proposals %} - - - - - - - - {% endfor %} - -
ProposalUsernameDateFunding - -
{{ p.headline }}{{ p.user.username }}{{ p.date_added.strftime('%Y-%m-%d %H:%M') }} - - {% if p.funds_progress >= 0.1 and status == 0 %} - {{p.funds_progress|int}}% - {% else %} - - - {% endif %} - - - {% if p.comment_count %} - {{p.comment_count}} - {% else %} - - - {% endif %} -
- {% else %} - No proposals here yet. - {% endif %} -
- {% include 'sidebar.html' %} -
- - - - - - - - - - - - - -
- -
- -{% endblock %}