Skip to content

Commit

Permalink
that is for now
Browse files Browse the repository at this point in the history
  • Loading branch information
ramiboutas committed Jan 6, 2024
1 parent 66f1321 commit 0188a3c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
20 changes: 20 additions & 0 deletions core/migrations/0031_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-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
),
),
]
2 changes: 1 addition & 1 deletion core/models/countries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
11 changes: 6 additions & 5 deletions core/templatetags/plan_pricing.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 0188a3c

Please sign in to comment.