diff --git a/core/migrations/0031_alter_country_gdp.py b/core/migrations/0031_alter_country_gdp.py new file mode 100644 index 00000000..f3d13f9a --- /dev/null +++ b/core/migrations/0031_alter_country_gdp.py @@ -0,0 +1,20 @@ +# Generated by Django 4.2.5 on 2024-01-06 01:27 + +from decimal import Decimal +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("core", "0030_alter_profile_cropped_photo"), + ] + + operations = [ + migrations.AlterField( + model_name="country", + name="gdp", + field=models.DecimalField( + decimal_places=3, default=Decimal("50000"), max_digits=12 + ), + ), + ] diff --git a/core/models/countries.py b/core/models/countries.py index ed9b6129..7cf4c84b 100644 --- a/core/models/countries.py +++ b/core/models/countries.py @@ -10,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.DecimalField(max_digits=12, decimal_places=2, default=Decimal(50_000)) + gdp = models.DecimalField(max_digits=12, decimal_places=3, default=Decimal(50_000)) currency = models.CharField(max_length=8) wikipedia_url = models.URLField(max_length=128) diff --git a/core/templatetags/plan_pricing.py b/core/templatetags/plan_pricing.py index 1d7b4f81..ece33ef0 100644 --- a/core/templatetags/plan_pricing.py +++ b/core/templatetags/plan_pricing.py @@ -1,7 +1,7 @@ from decimal import Decimal from django import template -from django.db.models import Max, Min +from django.db.models import Max, Min, Avg from djmoney.contrib.exchange.models import convert_money @@ -194,25 +194,26 @@ def get_plan_price(request, plan): countries = Country.objects.all() gdp_min = countries.aggregate(Min("gdp"))["gdp__min"] gdp_max = countries.aggregate(Max("gdp"))["gdp__max"] + gdp_avg = countries.aggregate(Avg("gdp"))["gdp__avg"] p_min = plan.price_min.amount p_max = plan.price_max.amount - in_currency = plan.price_min.currency + in_currency = str(plan.price_min.currency) # Get the GDP and the desired currency try: country = countries.filter(code=request.country.code)[0] gdp, out_currency = country.gdp, country.currency except (IndexError, AttributeError): - gdp, out_currency = Decimal(50000), in_currency + gdp, out_currency = gdp_avg, in_currency price_amount = p_min + (gdp - gdp_min) / (gdp_max - gdp_min) * (p_max - p_min) in_money = Money(price_amount, in_currency) out_money = convert_money(in_money, out_currency) if out_currency in THREE_DECIMAL_CURRENCIES: - return Money(round5(int(out_money.amount)), out_currency) + return Money(round5(out_money.amount), out_currency) elif out_currency in ZERO_DECIMAL_CURRENCIES: - return Money(int(out_money.amount), out_currency) + return Money(round(out_money.amount), out_currency) elif out_currency in STANDARD_CURRENCIES: return out_money else: