Removed titled proposal table and better peformance on comment queries

This commit is contained in:
Sander Ferdinand 2018-07-04 01:17:23 +02:00
parent eb03b8a7cb
commit c5ef1699ad
4 changed files with 45 additions and 32 deletions

View file

@ -143,20 +143,26 @@ class Proposal(base):
if not result.addr_donation: if not result.addr_donation:
Proposal.generate_donation_addr(result) 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
def get_comments(self):
from wowfunding.factory import db_session
q = db_session.query(Comment) q = db_session.query(Comment)
q = q.filter(Comment.proposal_id == result.id) q = q.filter(Comment.proposal_id == self.id)
q = q.filter(Comment.replied_to == None) q = q.filter(Comment.replied_to == None)
comments = q.all() comments = q.all()
for c in comments: for c in comments:
q = db_session.query(Comment) q = db_session.query(Comment)
q = q.filter(Comment.proposal_id == result.id) q = q.filter(Comment.proposal_id == self.id)
q = q.filter(Comment.replied_to == c.id) q = q.filter(Comment.replied_to == c.id)
_c = q.all() _c = q.all()
setattr(c, 'comments', _c) setattr(c, 'comments', _c)
setattr(result, '_comments', comments) setattr(self, '_comments', comments)
return result return self
@property @property
def balance(self): def balance(self):
@ -226,6 +232,7 @@ class Proposal(base):
@classmethod @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: if status is None or not status >= 0 or not status <= 2:
raise NotImplementedError('missing status') raise NotImplementedError('missing status')
@ -238,7 +245,12 @@ class Proposal(base):
if isinstance(offset, int): if isinstance(offset, int):
q = q.offset(offset) q = q.offset(offset)
return q.all() 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
@classmethod @classmethod
def search(cls, key: str): def search(cls, key: str):

View file

@ -66,6 +66,7 @@ def propsal_comment_reply(cid, pid):
@app.route('/proposal/<int:pid>') @app.route('/proposal/<int:pid>')
def proposal(pid): def proposal(pid):
p = Proposal.find_by_id(pid=pid) p = Proposal.find_by_id(pid=pid)
p.get_comments()
if not p: if not p:
return make_response(redirect(url_for('proposals'))) return make_response(redirect(url_for('proposals')))
return make_response(render_template(('proposal.html'), proposal=p)) return make_response(render_template(('proposal.html'), proposal=p))

BIN
wowfunding/static/msg.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

View file

@ -36,9 +36,7 @@
<div class="row"> <div class="row">
<div class="col-lg-8"> <div class="col-lg-8">
{% if proposals %} {% if proposals %}
{% for p in proposals %} <table class="table table-proposal table-hover" style="margin-bottom:6px;">
{% if loop.index == 1 %}
<table class="table table-proposal table-hover table-tilted" style="margin-bottom:6px;">
<thead> <thead>
<tr> <tr>
<th style="font-size: x-large;">Proposal</th> <th style="font-size: x-large;">Proposal</th>
@ -49,33 +47,35 @@
{% else %} {% else %}
<th></th> <th></th>
{% endif %} {% endif %}
<th style="display: table-cell;text-align: center;">
<img width="24" src="/static/msg.png">
</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{% else %} {% for p in proposals %}
{% if loop.index % 3 == 0 %} <tr>
</tbody> <td><b><a href="/proposal/{{ p.id }}">{{ p.headline }}</a></b></td>
</table> <td><a href="/user/{{ p.user.username }}">{{ p.user.username }}</a></td>
<table class="table table-proposal table-hover {% if (loop.index/3) % 2 == 0 %}table-tilted{% else %}table-tilted-v{% endif %}"> <td id="date"><small>{{ p.date_added.strftime('%Y-%m-%d %H:%M') }}</small></td>
<tbody> <td>
{% endif %} <span style="float:right;">
{% endif %} {% if p.funds_progress >= 0.1 and status == 0 %}
<tr> {{p.funds_progress|int}}%
<td><b><a href="/proposal/{{ p.id }}">{{ p.headline }}</a></b></td> {% else %}
<td><a href="/user/{{ p.user.username }}">{{ p.user.username }}</a></td> -
<td id="date"><small>{{ p.date_added.strftime('%Y-%m-%d %H:%M') }}</small></td> {% endif %}
<td> </span>
<span style="float:right;"> </td>
{% if p.funds_progress >= 0.1 and status == 0 %} <td style="text-align:center;">
{{p.funds_progress|int}}% {% if p.comment_count %}
{% else %} {{p.comment_count}}
- {% else %}
{% endif %} -
</span> {% endif %}
</td> </td>
</tr> </tr>
{% endfor %}
{% endfor %}
</tbody> </tbody>
</table> </table>
{% else %} {% else %}