Simplify import code

Co-authored-by: Glandos <bugs-github@antipoul.fr>
This commit is contained in:
zorun 2021-08-01 16:46:50 +02:00
parent 4bf9308908
commit 3cbe276a69

View file

@ -452,12 +452,10 @@ def import_project(file, project):
attr.sort() attr.sort()
currencies = set() currencies = set()
for e in json_file: for e in json_file:
# Add compatibility with versions < 5 that did not have currencies # If currency is absent, empty, or explicitly set to XXX
if "currency" not in e: # set it to project default.
e["currency"] = CurrencyConverter.no_currency if e.get("currency", "") in ["", "XXX"]:
# Empty currency should be converted to "XXX" e["currency"] = project.default_currency
if e["currency"] == "":
e["currency"] = CurrencyConverter.no_currency
if len(e) != len(attr): if len(e) != len(attr):
raise ValueError(_("Invalid JSON")) raise ValueError(_("Invalid JSON"))
list_attr = [] list_attr = []
@ -466,12 +464,6 @@ def import_project(file, project):
list_attr.sort() list_attr.sort()
if list_attr != attr: if list_attr != attr:
raise ValueError(_("Invalid JSON")) raise ValueError(_("Invalid JSON"))
# If the project has a default currency, convert bills that have no currency
if (
project.default_currency != CurrencyConverter.no_currency
and e["currency"] == CurrencyConverter.no_currency
):
e["currency"] = project.default_currency
# Keep track of currencies # Keep track of currencies
currencies.add(e["currency"]) currencies.add(e["currency"])