Import bills

This commit is contained in:
Nicolas Vanvyve 2019-12-16 23:27:22 +01:00
parent 3e414dfabe
commit 2b99dc846c
6 changed files with 57 additions and 17 deletions

View file

@ -189,6 +189,15 @@ class BillForm(FlaskForm):
bill.external_link = self.external_link.data bill.external_link = self.external_link.data
bill.date = self.date.data bill.date = self.date.data
bill.owers = [Person.query.get(ower, project) for ower in self.payed_for.data] bill.owers = [Person.query.get(ower, project) for ower in self.payed_for.data]
return bill
def fake_form(self, bill, project):
bill.payer_id = self.payer
bill.amount = self.amount
bill.what = self.what
bill.external_link = u""
bill.date = self.date
bill.owers = [Person.query.get(ower, project) for ower in self.payed_for]
return bill return bill

View file

@ -91,7 +91,7 @@
{{ form.file }} {{ form.file }}
{{ form.csrf_token() }} {{ form.csrf_token() }}
<div class="actions"> <div class="actions">
<button class="btn btn-primary">{{ _("BlaBlaBla") }}</button> <button class="btn btn-primary">{{ _("Import") }}</button>
</div> </div>
{% endmacro %} {% endmacro %}

View file

@ -42,7 +42,7 @@
<li class="nav-item{% if current_view == 'settle_bill' %} active{% endif %}"><a class="nav-link" href="{{ url_for("main.settle_bill") }}">{{ _("Settle") }}</a></li> <li class="nav-item{% if current_view == 'settle_bill' %} active{% endif %}"><a class="nav-link" href="{{ url_for("main.settle_bill") }}">{{ _("Settle") }}</a></li>
<li class="nav-item{% if current_view == 'statistics' %} active{% endif %}"><a class="nav-link" href="{{ url_for("main.statistics") }}">{{ _("Statistics") }}</a></li> <li class="nav-item{% if current_view == 'statistics' %} active{% endif %}"><a class="nav-link" href="{{ url_for("main.statistics") }}">{{ _("Statistics") }}</a></li>
<li class="nav-item{% if current_view == 'edit_project' %} active{% endif %}"><a class="nav-link" href="{{ url_for("main.edit_project") }}">{{ _("Settings") }}</a></li> <li class="nav-item{% if current_view == 'edit_project' %} active{% endif %}"><a class="nav-link" href="{{ url_for("main.edit_project") }}">{{ _("Settings") }}</a></li>
<li class="nav-item{% if current_view == 'upload_json' %} active{% endif %}"><a class="nav-link" href="{{ url_for("main.upload_json") }}">{{ _("Upload") }}</a></li> <li class="nav-item{% if current_view == 'upload_json' %} active{% endif %}"><a class="nav-link" href="{{ url_for("main.upload_json") }}">{{ _("Import") }}</a></li>
{% endblock %} {% endblock %}
{% endif %} {% endif %}
</ul> </ul>

View file

@ -1,7 +1,7 @@
{% extends "layout.html" %} {% extends "layout.html" %}
{% block content %} {% block content %}
<h2>{{ _("Upload JSON") }}</h2> <h2>{{ _("Import JSON") }}</h2>
<p> <p>
<form class="form-horizontal" method="post" enctype="multipart/form-data"> <form class="form-horizontal" method="post" enctype="multipart/form-data">
{{ forms.upload_json(form) }} {{ forms.upload_json(form) }}

View file

@ -255,3 +255,8 @@ def same_bill(bill1, bill2):
if bill1[a] != bill2[a]: if bill1[a] != bill2[a]:
return False return False
return True return True
def parse_date(string_date):
date = string_date.split("-")
return datetime(int(date[0]), int(date[1]), int(date[2]), 0, 0, 0)

View file

@ -10,6 +10,9 @@ and `add_project_id` for a quick overview)
""" """
import json import json
import os import os
from functools import wraps
from smtplib import SMTPRecipientsRefused
from flask import ( from flask import (
abort, abort,
Blueprint, Blueprint,
@ -24,15 +27,12 @@ from flask import (
send_file, send_file,
send_from_directory, send_from_directory,
) )
from flask_mail import Message
from flask_babel import get_locale, gettext as _ from flask_babel import get_locale, gettext as _
from werkzeug.security import check_password_hash, generate_password_hash from flask_mail import Message
from smtplib import SMTPRecipientsRefused
from werkzeug.exceptions import NotFound
from sqlalchemy import orm from sqlalchemy import orm
from functools import wraps from werkzeug.exceptions import NotFound
from werkzeug.security import check_password_hash, generate_password_hash
from ihatemoney.models import db, Project, Person, Bill
from ihatemoney.forms import ( from ihatemoney.forms import (
AdminAuthenticationForm, AdminAuthenticationForm,
AuthenticationForm, AuthenticationForm,
@ -45,6 +45,7 @@ from ihatemoney.forms import (
get_billform_for, get_billform_for,
UploadForm, UploadForm,
) )
from ihatemoney.models import db, Project, Person, Bill
from ihatemoney.utils import ( from ihatemoney.utils import (
Redirect303, Redirect303,
list_of_dicts2json, list_of_dicts2json,
@ -52,6 +53,7 @@ from ihatemoney.utils import (
LoginThrottler, LoginThrottler,
get_members, get_members,
same_bill, same_bill,
parse_date,
) )
main = Blueprint("main", __name__) main = Blueprint("main", __name__)
@ -401,9 +403,13 @@ def upload_json():
if form.validate_on_submit(): if form.validate_on_submit():
filename = pid + "_uploaded_bills.json" filename = pid + "_uploaded_bills.json"
form.file.data.save(filename) form.file.data.save(filename)
import_project(filename) try:
os.remove(filename) import_project(filename)
flash(_("Project successfully uploaded")) flash(_("Project successfully uploaded"))
except ValueError:
flash(_("Invalid JSON"), category="error")
finally:
os.remove(filename)
return redirect(url_for("main.list_bills")) return redirect(url_for("main.list_bills"))
return render_template("upload_json.html", form=form) return render_template("upload_json.html", form=form)
@ -438,15 +444,35 @@ def import_project(file):
if not same: if not same:
bill_to_add.append(j) bill_to_add.append(j)
# Add to DB # Add users to DB
for m in members_to_add: for m in members_to_add:
Person(name=m[0], project=g.project, weight=m[1]) Person(name=m[0], project=g.project, weight=m[1])
bill = Bill(
what="TestBill", amount=12, date="2019-12-04", payer_id=9, owers=[7, 8, 9], id=7
)
db.session.commit() db.session.commit()
id_dict = {}
for i in g.project.members:
id_dict[i.name] = i.id
# Create bills
for b in bill_to_add:
owers_id = list()
for ower in b["owers"]:
owers_id.append(id_dict[ower])
bill = Bill()
form = get_billform_for(g.project)
form.what = b["what"]
form.amount = b["amount"]
form.date = parse_date(b["date"])
form.payer = id_dict[b["payer_name"]]
form.payed_for = owers_id
db.session.add(form.fake_form(bill, g.project))
# Add bills to DB
db.session.commit()
@main.route("/<project_id>/delete") @main.route("/<project_id>/delete")
def delete_project(): def delete_project():
g.project.remove_project() g.project.remove_project()