diff options
| author | Alexis Metaireau <alexis@notmyidea.org> | 2017-03-11 01:27:41 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-03-11 01:27:41 +0100 |
| commit | 42854bd064404931eaccc3580ce505611162108b (patch) | |
| tree | b80cbf5ae0ddee631cb0c0aa31940f71d94d5855 /budget/web.py | |
| parent | 8c412b391f9402f3840728ad1e6b8043e4ed8b7b (diff) | |
| parent | e35480d6b7adf76f5bc4d46153b621abd722c681 (diff) | |
| download | ihatemoney-mirror-42854bd064404931eaccc3580ce505611162108b.zip ihatemoney-mirror-42854bd064404931eaccc3580ce505611162108b.tar.gz ihatemoney-mirror-42854bd064404931eaccc3580ce505611162108b.tar.bz2 | |
Merge pull request #176 from 0livd/master
Add export feature
Diffstat (limited to 'budget/web.py')
| -rw-r--r-- | budget/web.py | 45 |
1 files changed, 34 insertions, 11 deletions
diff --git a/budget/web.py b/budget/web.py index 28ed344..1c58a62 100644 --- a/budget/web.py +++ b/budget/web.py @@ -10,7 +10,7 @@ and `add_project_id` for a quick overview) """ from flask import Blueprint, current_app, flash, g, redirect, \ - render_template, request, session, url_for + render_template, request, session, url_for, send_file from flask_mail import Mail, Message from flask_babel import get_locale, gettext as _ from smtplib import SMTPRecipientsRefused @@ -20,9 +20,9 @@ from sqlalchemy import orm # local modules from models import db, Project, Person, Bill from forms import AuthenticationForm, CreateArchiveForm, EditProjectForm, \ - InviteForm, MemberForm, PasswordReminder, ProjectForm, get_billform_for -from utils import Redirect303 - + InviteForm, MemberForm, PasswordReminder, ProjectForm, get_billform_for, \ + ExportForm +from utils import Redirect303, list_of_dicts2json, list_of_dicts2csv main = Blueprint("main", __name__) mail = Mail() @@ -197,20 +197,43 @@ def remind_password(): @main.route("/<project_id>/edit", methods=["GET", "POST"]) def edit_project(): - form = EditProjectForm() + edit_form = EditProjectForm() + export_form = ExportForm() if request.method == "POST": - if form.validate(): - project = form.update(g.project) + if edit_form.validate(): + project = edit_form.update(g.project) db.session.commit() session[project.id] = project.password return redirect(url_for(".list_bills")) + + if export_form.validate(): + export_format = export_form.export_format.data + export_type = export_form.export_type.data + + if export_type == 'transactions': + export = g.project.get_transactions_to_settle_bill( + pretty_output=True) + if export_type == "bills": + export = g.project.get_pretty_bills( + export_format=export_format) + + if export_format == "json": + file2export = list_of_dicts2json(export) + if export_format == "csv": + file2export = list_of_dicts2csv(export) + + return send_file(file2export, + attachment_filename="%s-%s.%s" % + (g.project.name, export_type, export_format), + as_attachment=True + ) else: - form.name.data = g.project.name - form.password.data = g.project.password - form.contact_email.data = g.project.contact_email + edit_form.name.data = g.project.name + edit_form.password.data = g.project.password + edit_form.contact_email.data = g.project.contact_email - return render_template("edit_project.html", form=form) + return render_template("edit_project.html", edit_form=edit_form, export_form=export_form) @main.route("/<project_id>/delete") |
