mirror of
https://github.com/spiral-project/ihatemoney.git
synced 2025-05-14 08:21:49 +02:00
Moved the check function to the model
This commit is contained in:
parent
d6d9fd2180
commit
72320c19d7
2 changed files with 22 additions and 16 deletions
|
@ -752,6 +752,22 @@ class Bill(db.Model):
|
||||||
else:
|
else:
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
def involves_deactivated_members(self, project):
|
||||||
|
"""Check whether the bill contains deactivated member.
|
||||||
|
Return:
|
||||||
|
True if it contains deactivated member,
|
||||||
|
False if not.
|
||||||
|
"""
|
||||||
|
owers_id = [int(m.id) for m in self.owers]
|
||||||
|
bill_member_id_list = owers_id + [self.payer_id]
|
||||||
|
deactivated_member_number = (
|
||||||
|
Person.query.filter(Person.id.in_(bill_member_id_list))
|
||||||
|
.filter(Person.project_id == project.id)
|
||||||
|
.filter(Person.activated == False)
|
||||||
|
.count()
|
||||||
|
)
|
||||||
|
return deactivated_member_number != 0
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.what
|
return self.what
|
||||||
|
|
||||||
|
|
|
@ -800,13 +800,8 @@ def delete_bill(bill_id):
|
||||||
if not bill:
|
if not bill:
|
||||||
return redirect(url_for(".list_bills"))
|
return redirect(url_for(".list_bills"))
|
||||||
|
|
||||||
# Prevent deleting if the bill involves deactivated user.
|
# Check if the bill contains deactivated member. If yes, stop deleting.
|
||||||
active_member_id = [m.id for m in g.project.active_members]
|
if bill.involves_deactivated_members(g.project):
|
||||||
owers_id = [int(m.id) for m in bill.owers]
|
|
||||||
deactivated_members = set([bill.payer_id] + owers_id).difference(
|
|
||||||
set(active_member_id)
|
|
||||||
)
|
|
||||||
if len(deactivated_members):
|
|
||||||
flash(
|
flash(
|
||||||
_("Deactivated users involved. This bill cannot be deleted."),
|
_("Deactivated users involved. This bill cannot be deleted."),
|
||||||
category="warning",
|
category="warning",
|
||||||
|
@ -827,21 +822,16 @@ def edit_bill(bill_id):
|
||||||
if not bill:
|
if not bill:
|
||||||
raise NotFound()
|
raise NotFound()
|
||||||
|
|
||||||
form = get_billform_for(g.project, set_default=False)
|
# Check if the bill contains deactivated member. If yes, stop editing.
|
||||||
|
if bill.involves_deactivated_members(g.project):
|
||||||
# Prevent editing if the bill involves deactivated user.
|
|
||||||
active_member_id = [m.id for m in g.project.active_members]
|
|
||||||
owers_id = [int(m.id) for m in bill.owers]
|
|
||||||
deactivated_members = set([bill.payer_id] + owers_id).difference(
|
|
||||||
set(active_member_id)
|
|
||||||
)
|
|
||||||
if len(deactivated_members):
|
|
||||||
flash(
|
flash(
|
||||||
_("Deactivated users involved. This bill cannot be edited."),
|
_("Deactivated users involved. This bill cannot be edited."),
|
||||||
category="warning",
|
category="warning",
|
||||||
)
|
)
|
||||||
return redirect(url_for(".list_bills"))
|
return redirect(url_for(".list_bills"))
|
||||||
|
|
||||||
|
form = get_billform_for(g.project, set_default=False)
|
||||||
|
|
||||||
if request.method == "POST" and form.validate():
|
if request.method == "POST" and form.validate():
|
||||||
form.save(bill, g.project)
|
form.save(bill, g.project)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
Loading…
Reference in a new issue