From af22696a016e97f032a46d8bd2c2bbcff99c55a6 Mon Sep 17 00:00:00 2001 From: Glandos Date: Sun, 26 Jul 2020 21:53:37 +0200 Subject: [PATCH] New algorithm when switching currencies Fixes #599 Be careful, when switching to no currency, all bills are first converted to the last known currency, and currency is erased --- ihatemoney/forms.py | 2 +- ihatemoney/models.py | 29 +++++++++++++++++++++++++++++ ihatemoney/web.py | 24 ------------------------ 3 files changed, 30 insertions(+), 25 deletions(-) diff --git a/ihatemoney/forms.py b/ihatemoney/forms.py index 9f8eefa3..fcb9f9a4 100644 --- a/ihatemoney/forms.py +++ b/ihatemoney/forms.py @@ -152,7 +152,7 @@ class EditProjectForm(FlaskForm): project.contact_email = self.contact_email.data project.logging_preference = self.logging_preference - project.default_currency = self.default_currency.data + project.switch_currency(self.default_currency.data) return project diff --git a/ihatemoney/models.py b/ihatemoney/models.py index 7984ab76..85e2e064 100644 --- a/ihatemoney/models.py +++ b/ihatemoney/models.py @@ -17,6 +17,7 @@ from sqlalchemy_continuum import make_versioned, version_class from sqlalchemy_continuum.plugins import FlaskPlugin from werkzeug.security import generate_password_hash +from ihatemoney.currency_convertor import CurrencyConverter from ihatemoney.patch_sqlalchemy_continuum import PatchedBuilder from ihatemoney.versioning import ( ConditionalVersioningManager, @@ -263,6 +264,34 @@ class Project(db.Model): ) return pretty_bills + def switch_currency(self, new_currency): + # Update converted currency + if new_currency != self.default_currency: + for bill in self.get_bills(): + if bill.original_currency == new_currency: + continue + + if new_currency == CurrencyConverter.no_currency: + # Use old currency to flatten all amount before stripping + bill.converted_amount = CurrencyConverter().exchange_currency( + bill.amount, bill.original_currency, self.default_currency + ) + # Strip currency + bill.amount = bill.converted_amount + bill.original_currency = CurrencyConverter.no_currency + else: + # Switch to new currency for everyone + bill.converted_amount = CurrencyConverter().exchange_currency( + bill.amount, bill.original_currency, new_currency + ) + # Add the currency for previously un-currency-ied + if bill.original_currency == CurrencyConverter.no_currency: + bill.original_currency = new_currency + db.session.add(bill) + self.default_currency = new_currency + db.session.add(self) + db.session.commit() + def remove_member(self, member_id): """Remove a member from the project. diff --git a/ihatemoney/web.py b/ihatemoney/web.py index 90371695..928c9f40 100644 --- a/ihatemoney/web.py +++ b/ihatemoney/web.py @@ -414,31 +414,7 @@ def edit_project(): # Edit form if edit_form.validate_on_submit(): - old_currency = g.project.default_currency project = edit_form.update(g.project) - # Update converted currency - if project.default_currency != old_currency: - for bill in project.get_bills(): - if bill.original_currency == project.default_currency: - continue - - if project.default_currency == CurrencyConverter.no_currency: - # Use old currency to flatten all amount before stripping - bill.converted_amount = CurrencyConverter().exchange_currency( - bill.amount, bill.original_currency, old_currency - ) - # Strip currency - bill.amount = bill.converted_amount - bill.original_currency = CurrencyConverter.no_currency - else: - # Switch to new currency for everyone - bill.converted_amount = CurrencyConverter().exchange_currency( - bill.amount, bill.original_currency, project.default_currency - ) - # Add the currency for previously un-currency-ied - if bill.original_currency == CurrencyConverter.no_currency: - bill.original_currency = project.default_currency - db.session.add(bill) db.session.add(project) db.session.commit()