mirror of
https://github.com/YunoHost-Apps/ihatemoney_ynh.git
synced 2024-09-03 19:26:15 +02:00
Move some logic to the models and add comments.
This commit is contained in:
parent
801802836a
commit
45dc6edacb
2 changed files with 51 additions and 15 deletions
|
@ -36,6 +36,33 @@ class Project(db.Model):
|
||||||
|
|
||||||
return balances
|
return balances
|
||||||
|
|
||||||
|
def get_bills(self):
|
||||||
|
"""Return the list of bills related to this project"""
|
||||||
|
return Bill.query.join(Person, Project)\
|
||||||
|
.filter(Bill.payer_id == Person.id)\
|
||||||
|
.filter(Person.project_id == Project.id)\
|
||||||
|
.filter(Project.id == self.id)\
|
||||||
|
.order_by(Bill.date.desc())
|
||||||
|
|
||||||
|
def remove_member(self, member_id):
|
||||||
|
"""Remove a member from the project.
|
||||||
|
|
||||||
|
If the member is not bound to a bill, then he is deleted, otherwise
|
||||||
|
he is only deactivated.
|
||||||
|
|
||||||
|
This method returns the status DELETED or DEACTIVATED regarding the
|
||||||
|
changes made.
|
||||||
|
"""
|
||||||
|
person = Person.query.get_or_404(member_id)
|
||||||
|
if person.project == self:
|
||||||
|
if not person.has_bills():
|
||||||
|
db.session.delete(person)
|
||||||
|
db.session.commit()
|
||||||
|
else:
|
||||||
|
person.activated = False
|
||||||
|
db.session.commit()
|
||||||
|
return person
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "<Project %s>" % self.name
|
return "<Project %s>" % self.name
|
||||||
|
|
||||||
|
|
|
@ -25,6 +25,10 @@ mail.init_app(app)
|
||||||
|
|
||||||
@app.url_defaults
|
@app.url_defaults
|
||||||
def add_project_id(endpoint, values):
|
def add_project_id(endpoint, values):
|
||||||
|
"""Add the project id to the url calls if it is expected.
|
||||||
|
|
||||||
|
This is to not carry it everywhere in the templates.
|
||||||
|
"""
|
||||||
if 'project_id' in values or not hasattr(g, 'project'):
|
if 'project_id' in values or not hasattr(g, 'project'):
|
||||||
return
|
return
|
||||||
if app.url_map.is_endpoint_expecting(endpoint, 'project_id'):
|
if app.url_map.is_endpoint_expecting(endpoint, 'project_id'):
|
||||||
|
@ -32,6 +36,11 @@ def add_project_id(endpoint, values):
|
||||||
|
|
||||||
@app.url_value_preprocessor
|
@app.url_value_preprocessor
|
||||||
def pull_project(endpoint, values):
|
def pull_project(endpoint, values):
|
||||||
|
"""When a request contains a project_id value, transform it directly
|
||||||
|
into a project by checking the credentials are stored in session.
|
||||||
|
|
||||||
|
If not, redirect the user to an authentication form
|
||||||
|
"""
|
||||||
if endpoint == "authenticate":
|
if endpoint == "authenticate":
|
||||||
return
|
return
|
||||||
if not values:
|
if not values:
|
||||||
|
@ -51,6 +60,7 @@ def pull_project(endpoint, values):
|
||||||
|
|
||||||
@app.route("/authenticate", methods=["GET", "POST"])
|
@app.route("/authenticate", methods=["GET", "POST"])
|
||||||
def authenticate(project_id=None):
|
def authenticate(project_id=None):
|
||||||
|
"""Authentication form"""
|
||||||
form = AuthenticationForm()
|
form = AuthenticationForm()
|
||||||
if not form.id.data and request.args['project_id']:
|
if not form.id.data and request.args['project_id']:
|
||||||
form.id.data = request.args['project_id']
|
form.id.data = request.args['project_id']
|
||||||
|
@ -124,6 +134,12 @@ def exit():
|
||||||
|
|
||||||
@app.route("/demo")
|
@app.route("/demo")
|
||||||
def demo():
|
def demo():
|
||||||
|
"""
|
||||||
|
Authenticate the user for the demonstration project and redirect him to
|
||||||
|
the bills list for this project.
|
||||||
|
|
||||||
|
Create a demo project if it doesnt exists yet (or has been deleted)
|
||||||
|
"""
|
||||||
project = Project.query.get("demo")
|
project = Project.query.get("demo")
|
||||||
if not project:
|
if not project:
|
||||||
project = Project(id="demo", name=u"demonstration", password="demo",
|
project = Project(id="demo", name=u"demonstration", password="demo",
|
||||||
|
@ -135,6 +151,7 @@ def demo():
|
||||||
|
|
||||||
@app.route("/<project_id>/invite", methods=["GET", "POST"])
|
@app.route("/<project_id>/invite", methods=["GET", "POST"])
|
||||||
def invite():
|
def invite():
|
||||||
|
"""Send invitations for this particular project"""
|
||||||
|
|
||||||
form = InviteForm()
|
form = InviteForm()
|
||||||
|
|
||||||
|
@ -158,11 +175,7 @@ def invite():
|
||||||
|
|
||||||
@app.route("/<project_id>/")
|
@app.route("/<project_id>/")
|
||||||
def list_bills():
|
def list_bills():
|
||||||
bills = Bill.query.join(Person, Project)\
|
bills = g.project.get_bills()
|
||||||
.filter(Bill.payer_id == Person.id)\
|
|
||||||
.filter(Person.project_id == Project.id)\
|
|
||||||
.filter(Project.id == g.project.id)\
|
|
||||||
.order_by(Bill.date.desc())
|
|
||||||
return render_template("list_bills.html",
|
return render_template("list_bills.html",
|
||||||
bills=bills, member_form=MemberForm(g.project),
|
bills=bills, member_form=MemberForm(g.project),
|
||||||
bill_form=get_billform_for(g.project)
|
bill_form=get_billform_for(g.project)
|
||||||
|
@ -201,16 +214,12 @@ def reactivate(member_id):
|
||||||
|
|
||||||
@app.route("/<project_id>/members/<member_id>/delete", methods=["GET", "POST"])
|
@app.route("/<project_id>/members/<member_id>/delete", methods=["GET", "POST"])
|
||||||
def remove_member(member_id):
|
def remove_member(member_id):
|
||||||
person = Person.query.get_or_404(member_id)
|
member = g.project.remove_member(member_id)
|
||||||
if person.project == g.project:
|
if member.activated == False:
|
||||||
if not person.has_bills():
|
flash("User '%s' has been desactivated" % member.name)
|
||||||
db.session.delete(person)
|
else:
|
||||||
db.session.commit()
|
flash("User '%s' has been removed" % member.name)
|
||||||
flash("User '%s' has been removed" % person.name)
|
|
||||||
else:
|
|
||||||
person.activated = False
|
|
||||||
db.session.commit()
|
|
||||||
flash("User '%s' has been desactivated" % person.name)
|
|
||||||
return redirect(url_for("list_bills"))
|
return redirect(url_for("list_bills"))
|
||||||
|
|
||||||
@app.route("/<project_id>/add", methods=["GET", "POST"])
|
@app.route("/<project_id>/add", methods=["GET", "POST"])
|
||||||
|
|
Loading…
Reference in a new issue