diff --git a/ihatemoney/forms.py b/ihatemoney/forms.py
index 3d80d319..57f809df 100644
--- a/ihatemoney/forms.py
+++ b/ihatemoney/forms.py
@@ -22,7 +22,8 @@ from jinja2 import Markup
import email_validator
from ihatemoney.models import Project, Person
-from ihatemoney.utils import slugify, eval_arithmetic_expression,CurrencyConverter
+from ihatemoney.utils import slugify, eval_arithmetic_expression, CurrencyConverter
+
def strip_filter(string):
try:
@@ -38,8 +39,10 @@ def get_billform_for(project, set_default=True, **kwargs):
display the default form, it will call set_default on it.
"""
- form = BillForm(project=project, **kwargs)
- form.original_currency.label = Label("original_currency", "Currency (Default: %s)" % (project.default_currency))
+ form = BillForm(**kwargs)
+ form.original_currency.label = Label(
+ "original_currency", "Currency (Default: %s)" % (project.default_currency)
+ )
active_members = [(m.id, m.name) for m in project.active_members]
form.payed_for.choices = form.payer.choices = active_members
@@ -90,7 +93,9 @@ class EditProjectForm(FlaskForm):
contact_email = StringField(_("Email"), validators=[DataRequired(), Email()])
currency_helper = CurrencyConverter()
default_currency = SelectField(
- _("Default Currency"), choices=currency_helper.get_currencies(), validators=[DataRequired()]
+ _("Default Currency"),
+ choices=currency_helper.get_currencies(),
+ validators=[DataRequired()],
)
def save(self):
@@ -103,7 +108,7 @@ class EditProjectForm(FlaskForm):
id=self.id.data,
password=generate_password_hash(self.password.data),
contact_email=self.contact_email.data,
- default_currency=self.default_currency.data
+ default_currency=self.default_currency.data,
)
return project
@@ -172,7 +177,9 @@ class BillForm(FlaskForm):
amount = CalculatorStringField(_("Amount paid"), validators=[DataRequired()])
currency_helper = CurrencyConverter()
original_currency = SelectField(
- _("Currency"), choices=currency_helper.get_currencies(), validators=[DataRequired()]
+ _("Currency"),
+ choices=currency_helper.get_currencies(),
+ validators=[DataRequired()],
)
external_link = URLField(
_("External link"),
@@ -193,7 +200,9 @@ class BillForm(FlaskForm):
bill.date = self.date.data
bill.owers = [Person.query.get(ower, project) for ower in self.payed_for.data]
bill.original_currency = self.original_currency.data
- bill.original_amount = currency_helper.exchange_currency(bill.amount, bill.original_currency, project.default_currency)
+ bill.original_amount = self.currency_helper.exchange_currency(
+ float(bill.amount), bill.original_currency, project.default_currency
+ )
return bill
diff --git a/ihatemoney/models.py b/ihatemoney/models.py
index 5cbc974d..0f4c19de 100644
--- a/ihatemoney/models.py
+++ b/ihatemoney/models.py
@@ -379,7 +379,6 @@ class Bill(db.Model):
"external_link": self.external_link,
"original_currency": self.original_currency,
"original_amount": self.original_amount,
-
}
def pay_each(self):
diff --git a/ihatemoney/templates/forms.html b/ihatemoney/templates/forms.html
index 739c2c37..fa1a24ac 100644
--- a/ihatemoney/templates/forms.html
+++ b/ihatemoney/templates/forms.html
@@ -97,8 +97,8 @@
{{ input(form.what, inline=True) }}
{{ input(form.payer, inline=True, class="form-control custom-select") }}
{{ input(form.amount, inline=True) }}
- {{ input(form.external_link, inline=True) }}
{{ input(form.original_currency, inline=True) }}
+ {{ input(form.external_link, inline=True) }}
diff --git a/ihatemoney/tests/tests.py b/ihatemoney/tests/tests.py
index fb96d358..a41044e6 100644
--- a/ihatemoney/tests/tests.py
+++ b/ihatemoney/tests/tests.py
@@ -414,6 +414,8 @@ class BudgetTestCase(IhatemoneyTestCase):
"payer": fred_id,
"payed_for": [fred_id],
"amount": "25",
+ "original_amount": "25",
+ "original_currency": "USD",
},
)
@@ -465,6 +467,8 @@ class BudgetTestCase(IhatemoneyTestCase):
"payer": alexis.id,
"payed_for": [alexis.id],
"amount": "25",
+ "original_amount": "25",
+ "original_currency": "USD",
},
)
@@ -619,6 +623,8 @@ class BudgetTestCase(IhatemoneyTestCase):
"payer": members_ids[0],
"payed_for": members_ids,
"amount": "25",
+ "original_amount": "25",
+ "original_currency": "USD",
},
)
models.Project.query.get("raclette")
@@ -634,6 +640,8 @@ class BudgetTestCase(IhatemoneyTestCase):
"payer": members_ids[0],
"payed_for": members_ids,
"amount": "10",
+ "original_amount": "10",
+ "original_currency": "USD",
},
)
@@ -653,6 +661,8 @@ class BudgetTestCase(IhatemoneyTestCase):
"payer": members_ids[0],
"payed_for": members_ids,
"amount": "19",
+ "original_amount": "19",
+ "original_currency": "USD",
},
)
@@ -664,6 +674,8 @@ class BudgetTestCase(IhatemoneyTestCase):
"payer": members_ids[1],
"payed_for": members_ids[0],
"amount": "20",
+ "original_amount": "20",
+ "original_currency": "USD",
},
)
@@ -675,6 +687,8 @@ class BudgetTestCase(IhatemoneyTestCase):
"payer": members_ids[1],
"payed_for": members_ids,
"amount": "17",
+ "original_amount": "17",
+ "original_currency": "USD",
},
)
@@ -690,6 +704,8 @@ class BudgetTestCase(IhatemoneyTestCase):
"payer": members_ids[0],
"payed_for": members_ids,
"amount": "-25",
+ "original_amount": "-25",
+ "original_currency": "USD",
},
)
bill = models.Bill.query.filter(models.Bill.date == "2011-08-12")[0]
@@ -704,6 +720,8 @@ class BudgetTestCase(IhatemoneyTestCase):
"payer": members_ids[0],
"payed_for": members_ids,
"amount": "25,02",
+ "original_amount": "25,02",
+ "original_currency": "USD",
},
)
bill = models.Bill.query.filter(models.Bill.date == "2011-08-01")[0]
@@ -729,6 +747,8 @@ class BudgetTestCase(IhatemoneyTestCase):
"payer": members_ids[0],
"payed_for": members_ids,
"amount": "10",
+ "original_amount": "10",
+ "original_currency": "USD",
},
)
@@ -740,6 +760,8 @@ class BudgetTestCase(IhatemoneyTestCase):
"payer": members_ids[1],
"payed_for": members_ids,
"amount": "10",
+ "original_amount": "10",
+ "original_currency": "USD",
},
)
@@ -804,6 +826,8 @@ class BudgetTestCase(IhatemoneyTestCase):
"payer": 1,
"payed_for": [1, 2, 3],
"amount": "24.36",
+ "original_amount": "24.36",
+ "original_currency": "USD",
},
)
@@ -815,6 +839,8 @@ class BudgetTestCase(IhatemoneyTestCase):
"payer": 2,
"payed_for": [1],
"amount": "19.12",
+ "original_amount": "19.12",
+ "original_currency": "USD",
},
)
@@ -826,6 +852,8 @@ class BudgetTestCase(IhatemoneyTestCase):
"payer": 1,
"payed_for": [1, 2],
"amount": "22",
+ "original_amount": "22",
+ "original_currency": "USD",
},
)
@@ -913,6 +941,8 @@ class BudgetTestCase(IhatemoneyTestCase):
"payer": 1,
"payed_for": [1, 2, 3],
"amount": "10.0",
+ "original_amount": "10",
+ "original_currency": "USD",
},
)
@@ -924,6 +954,8 @@ class BudgetTestCase(IhatemoneyTestCase):
"payer": 2,
"payed_for": [1],
"amount": "20",
+ "original_amount": "20",
+ "original_currency": "USD",
},
)
@@ -935,6 +967,8 @@ class BudgetTestCase(IhatemoneyTestCase):
"payer": 1,
"payed_for": [1, 2],
"amount": "10",
+ "original_amount": "10",
+ "original_currency": "USD",
},
)
@@ -992,6 +1026,8 @@ class BudgetTestCase(IhatemoneyTestCase):
"payer": 1,
"payed_for": [1, 2, 3],
"amount": "10.0",
+ "original_amount": "10.0",
+ "original_currency": "USD",
},
)
@@ -1003,6 +1039,8 @@ class BudgetTestCase(IhatemoneyTestCase):
"payer": 2,
"payed_for": [1],
"amount": "20",
+ "original_amount": "20",
+ "original_currency": "USD",
},
)
@@ -1014,6 +1052,8 @@ class BudgetTestCase(IhatemoneyTestCase):
"payer": 1,
"payed_for": [1, 2],
"amount": "10",
+ "original_amount": "10",
+ "original_currency": "USD",
},
)
project = models.Project.query.get("raclette")
@@ -1045,6 +1085,8 @@ class BudgetTestCase(IhatemoneyTestCase):
"payer": 1,
"payed_for": [1, 2, 3],
"amount": "10.0",
+ "original_amount": "10",
+ "original_currency": "USD",
},
)
@@ -1056,6 +1098,8 @@ class BudgetTestCase(IhatemoneyTestCase):
"payer": 2,
"payed_for": [1, 3],
"amount": "20",
+ "original_amount": "20",
+ "original_currency": "USD",
},
)
@@ -1067,6 +1111,8 @@ class BudgetTestCase(IhatemoneyTestCase):
"payer": 3,
"payed_for": [2],
"amount": "13.33",
+ "original_amount": "13.33",
+ "original_currency": "USD",
},
)
project = models.Project.query.get("raclette")
@@ -1099,6 +1145,7 @@ class BudgetTestCase(IhatemoneyTestCase):
"payer": 1,
"payed_for": [1, 2, 3, 4],
"amount": "10.0",
+ "original_currency": "USD",
},
)
@@ -1110,6 +1157,7 @@ class BudgetTestCase(IhatemoneyTestCase):
"payer": 2,
"payed_for": [1, 3],
"amount": "200",
+ "original_currency": "USD",
},
)
@@ -1121,6 +1169,7 @@ class BudgetTestCase(IhatemoneyTestCase):
"payer": 3,
"payed_for": [2],
"amount": "13.33",
+ "original_currency": "USD",
},
)
@@ -1729,7 +1778,7 @@ class APITestCase(IhatemoneyTestCase):
"id": id,
"external_link": "",
"original_currency": "USD",
- "original_amount": input_amount,
+ "original_amount": expected_amount,
}
got = json.loads(req.data.decode("utf-8"))
@@ -1758,6 +1807,8 @@ class APITestCase(IhatemoneyTestCase):
"payer": "1",
"payed_for": ["1", "2"],
"amount": amount,
+ "original_amount": amount,
+ "original_currency": "USD",
},
headers=self.get_auth("raclette"),
)
@@ -1780,6 +1831,8 @@ class APITestCase(IhatemoneyTestCase):
"payer": "1",
"payed_for": ["1", "2"],
"amount": "25",
+ "original_amount": "25",
+ "original_currency": "USD",
},
headers=self.get_auth("raclette"),
)
@@ -1847,6 +1900,8 @@ class APITestCase(IhatemoneyTestCase):
"payer": "1",
"payed_for": ["1", "2"],
"amount": "25",
+ "original_amount": "25",
+ "original_currency": "USD",
},
headers=self.get_auth("raclette"),
)
diff --git a/ihatemoney/utils.py b/ihatemoney/utils.py
index 4df76358..696db6bf 100644
--- a/ihatemoney/utils.py
+++ b/ihatemoney/utils.py
@@ -14,26 +14,31 @@ from datetime import datetime, timedelta
import csv
import requests
+
class CurrencyConverter(object):
- api_url = 'https://api.exchangerate-api.com/v4/latest/USD'
+ api_url = "https://api.exchangerate-api.com/v4/latest/USD"
response = []
def __init__(self):
- self.response = requests.get(self.api_url).json();
-
+ self.response = requests.get(self.api_url).json()
+
def get_currencies(self):
currencies = []
for rate in self.response["rates"]:
- currencies.append((rate,rate))
+ currencies.append((rate, rate))
return currencies
-
- def exchange_currency(self,amount,currency1,currency2):
+
+ def exchange_currency(self, amount, currency1, currency2):
+ if currency1 == currency2:
+ return amount
+
base = self.response["base"]
conversion_rate1 = self.response["rates"][currency1]
conversion_rate2 = self.response["rates"][currency2]
new_amount = (amount / conversion_rate1) * conversion_rate2
# round to two digits because we are dealing with money
- return round(new_amount,2)
+ return round(new_amount, 2)
+
def slugify(value):
"""Normalizes string, converts to lowercase, removes non-alpha characters,
diff --git a/ihatemoney/web.py b/ihatemoney/web.py
index eb61ae66..5a1ecd72 100644
--- a/ihatemoney/web.py
+++ b/ihatemoney/web.py
@@ -317,9 +317,7 @@ def create_project():
)
return redirect(url_for(".list_bills", project_id=project.id))
- return render_template(
- "create_project.html", form=form,
- )
+ return render_template("create_project.html", form=form,)
@main.route("/password-reminder", methods=["GET", "POST"])
@@ -391,9 +389,7 @@ def edit_project():
edit_form.contact_email.data = g.project.contact_email
return render_template(
- "edit_project.html",
- edit_form=edit_form,
- current_view="edit_project",
+ "edit_project.html", edit_form=edit_form, current_view="edit_project",
)