mirror of
https://github.com/spiral-project/ihatemoney.git
synced 2025-05-06 13:01:50 +02:00
Extract data from JSON
This commit is contained in:
parent
3fe668c1e1
commit
1785da36f0
2 changed files with 54 additions and 3 deletions
|
@ -234,3 +234,22 @@ def eval_arithmetic_expression(expr):
|
||||||
raise ValueError("Error evaluating expression: {}".format(expr))
|
raise ValueError("Error evaluating expression: {}".format(expr))
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
def get_members(file):
|
||||||
|
members_list = list()
|
||||||
|
for item in file:
|
||||||
|
if (item['payer_name'], item['payer_weight']) not in members_list:
|
||||||
|
members_list.append((item['payer_name'], item['payer_weight']))
|
||||||
|
for item in file:
|
||||||
|
for ower in item['owers']:
|
||||||
|
if ower not in [i[0] for i in members_list]:
|
||||||
|
members_list.append((ower,1))
|
||||||
|
|
||||||
|
return members_list
|
||||||
|
|
||||||
|
def same_bill(bill1,bill2):
|
||||||
|
attr = ["what", "payer_name", "payer_weight", "amount", "date", "owers"]
|
||||||
|
for a in attr:
|
||||||
|
if bill1[a] != bill2[a]:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
|
@ -8,7 +8,7 @@ Basically, this blueprint takes care of the authentication and provides
|
||||||
some shortcuts to make your life better when coding (see `pull_project`
|
some shortcuts to make your life better when coding (see `pull_project`
|
||||||
and `add_project_id` for a quick overview)
|
and `add_project_id` for a quick overview)
|
||||||
"""
|
"""
|
||||||
|
import json
|
||||||
import os
|
import os
|
||||||
from flask import (
|
from flask import (
|
||||||
abort,
|
abort,
|
||||||
|
@ -49,6 +49,8 @@ from ihatemoney.utils import (
|
||||||
list_of_dicts2json,
|
list_of_dicts2json,
|
||||||
list_of_dicts2csv,
|
list_of_dicts2csv,
|
||||||
LoginThrottler,
|
LoginThrottler,
|
||||||
|
get_members,
|
||||||
|
same_bill
|
||||||
)
|
)
|
||||||
|
|
||||||
main = Blueprint("main", __name__)
|
main = Blueprint("main", __name__)
|
||||||
|
@ -396,15 +398,45 @@ def upload_json():
|
||||||
form = UploadForm()
|
form = UploadForm()
|
||||||
pid = g.project.id
|
pid = g.project.id
|
||||||
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)
|
||||||
return redirect(url_for('upload'))
|
import_project(filename)
|
||||||
os.remove(filename)
|
os.remove(filename)
|
||||||
|
flash(_("Project successfully uploaded"))
|
||||||
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)
|
||||||
|
|
||||||
|
|
||||||
|
def import_project(file):
|
||||||
|
# From json : export list of members
|
||||||
|
json_file = json.load(open(file))
|
||||||
|
members = get_members(json_file)
|
||||||
|
active_members = g.project.active_members
|
||||||
|
members_already_here = list()
|
||||||
|
for m in active_members:
|
||||||
|
members_already_here.append(str(m))
|
||||||
|
|
||||||
|
# List all members not in the project and weight associated
|
||||||
|
# List of tuples (name,weight)
|
||||||
|
members_to_add = list()
|
||||||
|
for i in members:
|
||||||
|
if str(i[0]) not in members_already_here:
|
||||||
|
members_to_add.append(i)
|
||||||
|
|
||||||
|
# List bills not in the project
|
||||||
|
# Same format than JSON element
|
||||||
|
project_bills = g.project.get_pretty_bills()
|
||||||
|
bill_to_add = list()
|
||||||
|
for j in json_file:
|
||||||
|
same = False
|
||||||
|
for p in project_bills:
|
||||||
|
if same_bill(p, j):
|
||||||
|
same = True
|
||||||
|
break
|
||||||
|
if not same:
|
||||||
|
bill_to_add.append(j)
|
||||||
|
|
||||||
@main.route("/<project_id>/delete")
|
@main.route("/<project_id>/delete")
|
||||||
def delete_project():
|
def delete_project():
|
||||||
g.project.remove_project()
|
g.project.remove_project()
|
||||||
|
|
Loading…
Reference in a new issue