Skip to content

Commit

Permalink
decimals better according to @willmcgugan
Browse files Browse the repository at this point in the history
  • Loading branch information
ramibch committed Jan 5, 2024
1 parent 24009ca commit 8812d9f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
20 changes: 20 additions & 0 deletions core/migrations/0029_alter_country_gdp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Generated by Django 4.2.5 on 2024-01-05 13:38

from decimal import Decimal
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("core", "0028_freeplan_name_de_freeplan_name_en_freeplan_name_es"),
]

operations = [
migrations.AlterField(
model_name="country",
name="gdp",
field=models.DecimalField(
decimal_places=2, default=Decimal("50000"), max_digits=12
),
),
]
5 changes: 4 additions & 1 deletion core/models/countries.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from decimal import Decimal


import requests
from bs4 import BeautifulSoup

Expand All @@ -7,7 +10,7 @@
class Country(models.Model):
code = models.CharField(max_length=8, unique=True, db_index=True)
name = models.CharField(max_length=64)
gdp = models.PositiveIntegerField(default=50_000)
gdp = models.DecimalField(max_digits=12, decimal_places=2, default=Decimal(50_000))
currency = models.CharField(max_length=8)
wikipedia_url = models.URLField(max_length=128)

Expand Down
17 changes: 9 additions & 8 deletions core/templatetags/plan_pricing.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from decimal import Decimal

from django import template
from django.db.models import Avg, Max, Min, Sum
from django.db.models import Max, Min


from djmoney.contrib.exchange.models import convert_money
from djmoney.money import Money
Expand All @@ -13,17 +16,15 @@
def get_plan_price(request, plan):
"""Get price depending on the GDP per capita"""
countries = Country.objects.all()
p_min = float(plan.price_min.amount)
p_max = float(plan.price_max.amount)
p_min = plan.price_min.amount
p_max = plan.price_max.amount
gdp_min = countries.aggregate(Min("gdp"))["gdp__min"]
gdp_max = countries.aggregate(Max("gdp"))["gdp__max"]
try:
country = countries.filter(code=request.country.code)[0]
gdp = country.gdp
out_currency = country.currency
except KeyError:
gdp = 50000
out_currency = "EUR"
gdp, out_currency = country.gdp, country.currency
except (IndexError, AttributeError):
gdp, out_currency = Decimal(50000), "EUR"

price_amount = p_min + (gdp - gdp_min) / (gdp_max - gdp_min) * (p_max - p_min)
in_currency = plan.price_min.currency
Expand Down

0 comments on commit 8812d9f

Please sign in to comment.