From 3fe668c1e119363bc9c0f0a82c98f55ce4bc5a83 Mon Sep 17 00:00:00 2001 From: Nicolas Vanvyve Date: Tue, 26 Nov 2019 08:45:38 +0100 Subject: [PATCH] New tab upload --- .gitignore | 3 ++- ihatemoney/forms.py | 8 ++++++++ ihatemoney/templates/forms.html | 10 ++++++++++ ihatemoney/templates/layout.html | 1 + ihatemoney/templates/upload_json.html | 10 ++++++++++ ihatemoney/web.py | 16 +++++++++++++++- 6 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 ihatemoney/templates/upload_json.html diff --git a/.gitignore b/.gitignore index b7e8680f..f0668257 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,5 @@ dist build .vscode .env -.pytest_cache \ No newline at end of file +.pytest_cache +.idea/ diff --git a/ihatemoney/forms.py b/ihatemoney/forms.py index eb1bf2b9..dfe8a17e 100644 --- a/ihatemoney/forms.py +++ b/ihatemoney/forms.py @@ -10,6 +10,8 @@ from wtforms.validators import ( NumberRange, Optional, ) +from flask_wtf.file import FileField, FileAllowed, FileRequired + from flask_babel import lazy_gettext as _ from flask import request from werkzeug.security import generate_password_hash @@ -109,6 +111,12 @@ class EditProjectForm(FlaskForm): return project +class UploadForm(FlaskForm): + file = FileField('JSON', validators=[ + FileRequired(), + FileAllowed(['json', 'JSON'], 'JSON only!') + ]) + class ProjectForm(EditProjectForm): id = StringField(_("Project identifier"), validators=[DataRequired()]) diff --git a/ihatemoney/templates/forms.html b/ihatemoney/templates/forms.html index 12cc1651..48b55e96 100644 --- a/ihatemoney/templates/forms.html +++ b/ihatemoney/templates/forms.html @@ -85,6 +85,16 @@ {% endmacro %} +{% macro upload_json(form) %} + {% include "display_errors.html" %} + {{ form.hidden_tag() }} + {{ form.file }} + {{ form.csrf_token() }} +
+ +
+{% endmacro %} + {% macro add_bill(form, edit=False, title=True) %}
diff --git a/ihatemoney/templates/layout.html b/ihatemoney/templates/layout.html index 7ff72969..5af4ae23 100644 --- a/ihatemoney/templates/layout.html +++ b/ihatemoney/templates/layout.html @@ -42,6 +42,7 @@ + {% endblock %} {% endif %} diff --git a/ihatemoney/templates/upload_json.html b/ihatemoney/templates/upload_json.html new file mode 100644 index 00000000..a391b7ac --- /dev/null +++ b/ihatemoney/templates/upload_json.html @@ -0,0 +1,10 @@ +{% extends "layout.html" %} + +{% block content %} +

{{ _("Upload JSON") }}

+

+

+ {{ forms.upload_json(form) }} +
+

+{% endblock %} diff --git a/ihatemoney/web.py b/ihatemoney/web.py index fc12e9d5..7692673a 100644 --- a/ihatemoney/web.py +++ b/ihatemoney/web.py @@ -43,7 +43,7 @@ from ihatemoney.forms import ( ResetPasswordForm, ProjectForm, get_billform_for, -) + UploadForm) from ihatemoney.utils import ( Redirect303, list_of_dicts2json, @@ -391,6 +391,20 @@ def edit_project(): ) +@main.route('//upload_json', methods=['GET', 'POST']) +def upload_json(): + form = UploadForm() + pid = g.project.id + if form.validate_on_submit(): + filename = pid+"_uploaded_bills.json" + form.file.data.save(filename) + return redirect(url_for('upload')) + os.remove(filename) + return redirect(url_for('main.list_bills')) + + return render_template('upload_json.html', form=form) + + @main.route("//delete") def delete_project(): g.project.remove_project()