2011-07-31 23:55:18 +02:00
|
|
|
from collections import defaultdict
|
|
|
|
|
2011-08-19 23:44:54 +02:00
|
|
|
from flask import *
|
2011-07-30 15:46:53 +02:00
|
|
|
from flaskext.mail import Mail, Message
|
2011-08-19 23:44:54 +02:00
|
|
|
from werkzeug.routing import RequestRedirect
|
2011-07-23 18:45:40 +02:00
|
|
|
|
|
|
|
# local modules
|
|
|
|
from models import db, Project, Person, Bill
|
2011-07-30 15:46:53 +02:00
|
|
|
from forms import ProjectForm, AuthenticationForm, BillForm, MemberForm, InviteForm
|
2011-07-23 18:45:40 +02:00
|
|
|
from utils import get_billform_for, requires_auth
|
|
|
|
|
|
|
|
# create the application, initialize stuff
|
|
|
|
app = Flask(__name__)
|
2011-08-09 19:22:09 +02:00
|
|
|
app.config.from_object("default_settings")
|
2011-07-30 15:46:53 +02:00
|
|
|
mail = Mail()
|
2011-07-23 18:45:40 +02:00
|
|
|
|
2011-08-10 00:53:15 +02:00
|
|
|
# db
|
|
|
|
db.init_app(app)
|
|
|
|
db.app = app
|
|
|
|
db.create_all()
|
|
|
|
|
|
|
|
# mail
|
|
|
|
mail.init_app(app)
|
|
|
|
|
|
|
|
|
2011-08-21 01:42:10 +02:00
|
|
|
@app.url_defaults
|
|
|
|
def add_project_id(endpoint, values):
|
|
|
|
if 'project_id' in values or not hasattr(g, 'project'):
|
|
|
|
return
|
|
|
|
if app.url_map.is_endpoint_expecting(endpoint, 'project_id'):
|
|
|
|
values['project_id'] = g.project.id
|
|
|
|
|
|
|
|
@app.url_value_preprocessor
|
|
|
|
def pull_project(endpoint, values):
|
2011-08-21 02:23:53 +02:00
|
|
|
if endpoint == "authenticate":
|
|
|
|
return
|
2011-08-21 01:42:10 +02:00
|
|
|
if not values:
|
|
|
|
values = {}
|
|
|
|
project_id = values.pop('project_id', None)
|
|
|
|
if project_id:
|
|
|
|
project = Project.query.get(project_id)
|
|
|
|
if not project:
|
|
|
|
raise RequestRedirect(url_for("create_project", project_id=project_id))
|
|
|
|
if project.id in session and session[project.id] == project.password:
|
|
|
|
# add project into kwargs and call the original function
|
|
|
|
g.project = project
|
|
|
|
else:
|
|
|
|
# redirect to authentication page
|
|
|
|
raise RequestRedirect(
|
|
|
|
url_for("authenticate", redirect_url=request.url,
|
|
|
|
project_id=project_id))
|
2011-07-29 15:44:15 +02:00
|
|
|
|
|
|
|
@app.route("/authenticate", methods=["GET", "POST"])
|
2011-08-20 00:24:00 +02:00
|
|
|
def authenticate(redirect_url=None, project_id=None):
|
2011-07-23 18:45:40 +02:00
|
|
|
form = AuthenticationForm()
|
2011-08-20 00:24:00 +02:00
|
|
|
project_id = form.id.data or request.args['project_id']
|
2011-07-30 01:32:55 +02:00
|
|
|
project = Project.query.get(project_id)
|
2011-08-09 17:29:44 +02:00
|
|
|
create_project = False # We don't want to create the project by default
|
2011-07-30 01:32:55 +02:00
|
|
|
if not project:
|
2011-08-09 17:29:44 +02:00
|
|
|
# But if the user try to connect to an unexisting project, we will
|
|
|
|
# propose him a link to the creation form.
|
|
|
|
create_project = project_id
|
2011-07-30 01:32:55 +02:00
|
|
|
|
2011-08-09 19:28:50 +02:00
|
|
|
else:
|
|
|
|
# if credentials are already in session, redirect
|
|
|
|
if project_id in session and project.password == session[project_id]:
|
2011-08-20 00:28:58 +02:00
|
|
|
setattr(g, 'project', project)
|
2011-08-19 23:44:54 +02:00
|
|
|
redirect_url = redirect_url or url_for("list_bills")
|
2011-08-09 19:28:50 +02:00
|
|
|
return redirect(redirect_url)
|
|
|
|
|
|
|
|
# else process the form
|
|
|
|
if request.method == "POST":
|
|
|
|
if form.validate():
|
|
|
|
if not form.password.data == project.password:
|
|
|
|
form.errors['password'] = ["The password is not the right one"]
|
|
|
|
else:
|
|
|
|
# maintain a list of visited projects
|
|
|
|
if "projects" not in session:
|
|
|
|
session["projects"] = []
|
|
|
|
# add the project on the top of the list
|
|
|
|
session["projects"].insert(0, (project_id, project.name))
|
|
|
|
session[project_id] = form.password.data
|
|
|
|
session.update()
|
2011-08-19 23:44:54 +02:00
|
|
|
setattr(g, 'project', project)
|
|
|
|
redirect_url = redirect_url or url_for("list_bills")
|
2011-08-09 19:28:50 +02:00
|
|
|
return redirect(redirect_url)
|
2011-07-29 15:44:15 +02:00
|
|
|
|
2011-08-09 17:29:44 +02:00
|
|
|
return render_template("authenticate.html", form=form,
|
|
|
|
create_project=create_project)
|
2011-07-23 19:11:24 +02:00
|
|
|
|
2011-08-21 01:42:10 +02:00
|
|
|
@app.route("/")
|
|
|
|
def home():
|
|
|
|
project_form = ProjectForm()
|
|
|
|
auth_form = AuthenticationForm()
|
|
|
|
return render_template("home.html", project_form=project_form,
|
|
|
|
auth_form=auth_form, session=session)
|
|
|
|
|
2011-07-23 19:11:24 +02:00
|
|
|
@app.route("/create", methods=["GET", "POST"])
|
|
|
|
def create_project():
|
2011-07-26 16:03:00 +02:00
|
|
|
form = ProjectForm()
|
2011-07-23 19:11:24 +02:00
|
|
|
if request.method == "GET" and 'project_id' in request.values:
|
|
|
|
form.name.data = request.values['project_id']
|
|
|
|
|
|
|
|
if request.method == "POST":
|
|
|
|
if form.validate():
|
|
|
|
# save the object in the db
|
|
|
|
project = form.save()
|
|
|
|
db.session.add(project)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
# create the session object (authenticate)
|
|
|
|
session[project.id] = project.password
|
|
|
|
session.update()
|
|
|
|
|
|
|
|
# redirect the user to the next step (invite)
|
|
|
|
return redirect(url_for("invite", project_id=project.id))
|
|
|
|
|
|
|
|
return render_template("create_project.html", form=form)
|
|
|
|
|
2011-08-09 23:48:13 +02:00
|
|
|
@app.route("/exit")
|
|
|
|
def exit():
|
2011-07-29 15:44:15 +02:00
|
|
|
# delete the session
|
2011-07-30 01:32:55 +02:00
|
|
|
session.clear()
|
|
|
|
return redirect(url_for("home"))
|
2011-07-29 15:44:15 +02:00
|
|
|
|
2011-08-19 23:44:54 +02:00
|
|
|
@app.route("/<project_id>/invite", methods=["GET", "POST"])
|
|
|
|
def invite():
|
2011-07-30 15:46:53 +02:00
|
|
|
|
|
|
|
form = InviteForm()
|
|
|
|
|
|
|
|
if request.method == "POST":
|
|
|
|
if form.validate():
|
|
|
|
# send the email
|
|
|
|
|
2011-08-19 23:44:54 +02:00
|
|
|
message_body = render_template("invitation_mail")
|
2011-07-30 15:46:53 +02:00
|
|
|
|
2011-08-09 23:48:13 +02:00
|
|
|
message_title = "You have been invited to share your"\
|
2011-08-19 23:44:54 +02:00
|
|
|
+ " expenses for %s" % g.project.name
|
2011-07-30 15:46:53 +02:00
|
|
|
msg = Message(message_title,
|
|
|
|
body=message_body,
|
2011-08-09 23:48:13 +02:00
|
|
|
recipients=[email.strip()
|
|
|
|
for email in form.emails.data.split(",")])
|
2011-07-30 15:46:53 +02:00
|
|
|
mail.send(msg)
|
2011-08-19 23:44:54 +02:00
|
|
|
return redirect(url_for("list_bills"))
|
2011-07-30 15:46:53 +02:00
|
|
|
|
2011-08-19 23:44:54 +02:00
|
|
|
return render_template("send_invites.html", form=form)
|
2011-07-23 19:11:24 +02:00
|
|
|
|
2011-08-19 23:44:54 +02:00
|
|
|
@app.route("/<project_id>/")
|
|
|
|
def list_bills():
|
2011-08-09 18:05:55 +02:00
|
|
|
bills = Bill.query.join(Person, Project)\
|
|
|
|
.filter(Bill.payer_id == Person.id)\
|
|
|
|
.filter(Person.project_id == Project.id)\
|
2011-08-19 23:44:54 +02:00
|
|
|
.filter(Project.id == g.project.id)\
|
2011-08-09 18:05:55 +02:00
|
|
|
.order_by(Bill.date.desc())
|
2011-07-23 19:11:24 +02:00
|
|
|
return render_template("list_bills.html",
|
2011-08-19 23:44:54 +02:00
|
|
|
bills=bills, member_form=MemberForm(g.project),
|
|
|
|
bill_form=get_billform_for(g.project)
|
2011-07-31 23:55:18 +02:00
|
|
|
)
|
2011-07-23 20:36:13 +02:00
|
|
|
|
2011-08-19 23:44:54 +02:00
|
|
|
@app.route("/<project_id>/members/add", methods=["GET", "POST"])
|
|
|
|
def add_member():
|
2011-07-23 20:36:13 +02:00
|
|
|
# FIXME manage form errors on the list_bills page
|
2011-08-19 23:44:54 +02:00
|
|
|
form = MemberForm(g.project)
|
2011-07-23 20:36:13 +02:00
|
|
|
if request.method == "POST":
|
|
|
|
if form.validate():
|
2011-08-19 23:44:54 +02:00
|
|
|
db.session.add(Person(name=form.name.data, project=g.project))
|
2011-07-23 20:36:13 +02:00
|
|
|
db.session.commit()
|
2011-08-19 23:44:54 +02:00
|
|
|
return redirect(url_for("list_bills"))
|
|
|
|
return render_template("add_member.html", form=form)
|
2011-07-23 18:45:40 +02:00
|
|
|
|
2011-08-19 23:44:54 +02:00
|
|
|
@app.route("/<project_id>/members/<member_id>/delete", methods=["GET", "POST"])
|
|
|
|
def remove_member(member_id):
|
2011-07-31 23:55:18 +02:00
|
|
|
person = Person.query.get_or_404(member_id)
|
2011-08-19 23:44:54 +02:00
|
|
|
if person.project == g.project:
|
2011-08-10 17:49:35 +02:00
|
|
|
if not person.has_bills():
|
2011-08-09 19:34:46 +02:00
|
|
|
db.session.delete(person)
|
|
|
|
db.session.commit()
|
|
|
|
flash("User '%s' has been removed" % person.name)
|
|
|
|
else:
|
|
|
|
person.activated = False
|
|
|
|
db.session.commit()
|
|
|
|
flash("User '%s' has been desactivated" % person.name)
|
2011-08-19 23:44:54 +02:00
|
|
|
return redirect(url_for("list_bills"))
|
2011-07-31 23:55:18 +02:00
|
|
|
|
2011-08-19 23:44:54 +02:00
|
|
|
@app.route("/<project_id>/add", methods=["GET", "POST"])
|
|
|
|
def add_bill():
|
|
|
|
form = get_billform_for(g.project)
|
2011-07-23 18:45:40 +02:00
|
|
|
if request.method == 'POST':
|
|
|
|
if form.validate():
|
2011-08-10 12:59:30 +02:00
|
|
|
bill = Bill()
|
|
|
|
db.session.add(form.save(bill))
|
2011-07-23 18:45:40 +02:00
|
|
|
db.session.commit()
|
2011-07-31 00:41:28 +02:00
|
|
|
|
2011-08-10 01:05:25 +02:00
|
|
|
flash("The bill has been added")
|
2011-08-19 23:44:54 +02:00
|
|
|
return redirect(url_for('list_bills'))
|
2011-07-23 18:45:40 +02:00
|
|
|
|
2011-08-19 23:44:54 +02:00
|
|
|
return render_template("add_bill.html", form=form)
|
2011-07-23 18:45:40 +02:00
|
|
|
|
|
|
|
|
2011-08-19 23:44:54 +02:00
|
|
|
@app.route("/<project_id>/delete/<int:bill_id>")
|
|
|
|
def delete_bill(bill_id):
|
2011-08-10 12:59:30 +02:00
|
|
|
bill = Bill.query.get_or_404(bill_id)
|
|
|
|
db.session.delete(bill)
|
|
|
|
db.session.commit()
|
|
|
|
flash("The bill has been deleted")
|
|
|
|
|
2011-08-19 23:44:54 +02:00
|
|
|
return redirect(url_for('list_bills'))
|
2011-08-10 12:59:30 +02:00
|
|
|
|
|
|
|
|
2011-08-19 23:44:54 +02:00
|
|
|
@app.route("/<project_id>/edit/<int:bill_id>", methods=["GET", "POST"])
|
|
|
|
def edit_bill(bill_id):
|
2011-08-10 12:59:30 +02:00
|
|
|
bill = Bill.query.get_or_404(bill_id)
|
2011-08-19 23:44:54 +02:00
|
|
|
form = get_billform_for(g.project, set_default=False)
|
2011-08-10 12:59:30 +02:00
|
|
|
if request.method == 'POST' and form.validate():
|
|
|
|
form.save(bill)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
flash("The bill has been modified")
|
2011-08-19 23:44:54 +02:00
|
|
|
return redirect(url_for('list_bills'))
|
2011-08-10 12:59:30 +02:00
|
|
|
|
|
|
|
form.fill(bill)
|
2011-08-21 01:42:10 +02:00
|
|
|
return render_template("add_bill.html", form=form, edit=True)
|
2011-08-10 12:59:30 +02:00
|
|
|
|
2011-08-19 23:44:54 +02:00
|
|
|
@app.route("/<project_id>/compute")
|
|
|
|
def compute_bills():
|
2011-07-23 18:45:40 +02:00
|
|
|
"""Compute the sum each one have to pay to each other and display it"""
|
2011-08-19 23:44:54 +02:00
|
|
|
return render_template("compute_bills.html")
|
2011-07-23 18:45:40 +02:00
|
|
|
|
|
|
|
|
2011-08-19 23:44:54 +02:00
|
|
|
@app.route("/<project_id>/reset")
|
|
|
|
def reset_bills():
|
2011-07-23 18:45:40 +02:00
|
|
|
"""Reset the list of bills"""
|
2011-07-23 19:11:24 +02:00
|
|
|
# FIXME replace with the archive feature
|
2011-07-23 18:45:40 +02:00
|
|
|
# get all the bills which are not processed
|
|
|
|
bills = Bill.query.filter(Bill.processed == False)
|
|
|
|
for bill in bills:
|
|
|
|
bill.processed = True
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
return redirect(url_for('list_bills'))
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
app.run(host="0.0.0.0", debug=True)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|