* Made the forms template more readable

* Removed FIXME within edit_bill function, no bill will be returned if the bill_id is not associated with that particular project
* Resolved FIXME within the pay_each function, now using a sql query for sum rather than for loop
This commit is contained in:
DavidRThrashJr 2020-02-07 23:55:01 -05:00
parent 9aa7e62d0f
commit b68692621c
3 changed files with 11 additions and 4 deletions

View file

@ -6,6 +6,7 @@ from flask import g, current_app
from debts import settle
from sqlalchemy import orm
from sqlalchemy.sql import func
from itsdangerous import (
TimedJSONWebSignatureSerializer,
URLSafeSerializer,
@ -376,8 +377,10 @@ class Bill(db.Model):
def pay_each(self):
"""Compute what each share has to pay"""
if self.owers:
# FIXME: SQL might do that more efficiently
weights = sum(i.weight for i in self.owers)
weights = (db.session.query(func.sum(Person.weight))
.join(billowers, Bill)
.filter(Bill.id == self.id))\
.scalar()
return self.amount / weights
else:
return 0

View file

@ -112,7 +112,12 @@
<ul id="payed_for" class="inputs-list">
<p><a href="#" id="selectall" onclick="selectCheckboxes(true)">{{ _("Select all") }}</a> | <a href="#" id="selectnone" onclick="selectCheckboxes(false)">{{_("Select none")}}</a></p>
{% for key, value, checked in form.payed_for.iter_choices() | sort(attribute='1') %}
<p class="form-check"><label for="payed_for-{{key}}" class="form-check-label"><input name="payed_for" type="checkbox" {% if checked %}checked{% endif %} class="form-check-input" value="{{key}}" id="payed_for-{{key}}"/><span>{{value}}</span></label></p>
<p class="form-check">
<label for="payed_for-{{key}}" class="form-check-label">
<input name="payed_for" type="checkbox" {% if checked %}checked{% endif %} class="form-check-input" value="{{key}}" id="payed_for-{{key}}"/>
<span>{{value}}</span>
</label>
</p>
{% endfor %}
</ul>
</div>

View file

@ -699,7 +699,6 @@ def delete_bill(bill_id):
@main.route("/<project_id>/edit/<int:bill_id>", methods=["GET", "POST"])
def edit_bill(bill_id):
# FIXME: Test this bill belongs to this project !
bill = Bill.query.get(g.project, bill_id)
if not bill:
raise NotFound()