-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Currency model * Bank currency rate from db * Add currency choices * Create currencies in migration * Rename ue to currency_rate
- Loading branch information
1 parent
623f574
commit 0ebb6e7
Showing
24 changed files
with
221 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from apps.banking.admin.currency_rate import CurrencyRateAdmin | ||
|
||
__all__ = [ | ||
"CurrencyRateAdmin", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from typing import Any | ||
|
||
from django.contrib.admin.models import CHANGE, LogEntry | ||
from django.http import HttpRequest | ||
|
||
from apps.banking.models import CurrencyRate | ||
from core.admin import ModelAdmin, admin | ||
from core.tasks import write_admin_log | ||
|
||
|
||
@admin.register(CurrencyRate) | ||
class CurrencyRateAdmin(ModelAdmin): | ||
list_display = ["name", "rate"] | ||
|
||
def get_object(self, request: HttpRequest, object_id: str, from_field: str | None = None) -> CurrencyRate | None: | ||
obj = super().get_object(request, object_id, from_field) | ||
if obj: | ||
obj.__original_rate = obj.rate | ||
return obj | ||
|
||
def log_change(self, request: HttpRequest, obj: CurrencyRate, message: Any) -> LogEntry: | ||
if obj.rate != obj.__original_rate: # type: ignore[attr-defined] | ||
return write_admin_log( | ||
action_flag=CHANGE, | ||
app="banking", | ||
change_message=f"Currency rate was changed from {obj.__original_rate} to {obj.rate}", # type: ignore[attr-defined] | ||
model="CurrencyRate", | ||
object_id=obj.id, | ||
user_id=request.user.id, | ||
) | ||
else: | ||
return super().log_change(request, obj, message) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from typing import Any | ||
|
||
from apps.banking.models import CurrencyRate | ||
from core.test.factory import register | ||
|
||
|
||
@register | ||
def currency_rate(self: Any, **kwargs: dict[str, Any]) -> CurrencyRate: | ||
return self.mixer.blend("banking.CurrencyRate", **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Generated by Django 4.2.17 on 2024-12-24 18:15 | ||
from decimal import Decimal | ||
|
||
from django.db import migrations, models | ||
|
||
import core.models | ||
|
||
|
||
def create_currencies(apps, schema_editor): | ||
del schema_editor | ||
|
||
CurrencyRate = apps.get_model("banking.CurrencyRate") | ||
|
||
CurrencyRate.objects.create(name="RUB", rate=Decimal(1)) | ||
CurrencyRate.objects.create(name="USD", rate=Decimal(90)) | ||
CurrencyRate.objects.create(name="KZT", rate=Decimal("0.18")) | ||
CurrencyRate.objects.create(name="KIS", rate=Decimal(1)) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
initial = True | ||
|
||
dependencies = [ | ||
("banking", "0002_drop_recipient_model"), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="CurrencyRate", | ||
fields=[ | ||
("id", models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")), | ||
( | ||
"name", | ||
models.CharField( | ||
choices=[("RUB", "RUB"), ("USD", "USD"), ("KZT", "KZT"), ("KIS", "KIS (for zero-price orders)")], | ||
db_index=True, | ||
max_length=4, | ||
unique=True, | ||
), | ||
), | ||
("rate", models.DecimalField(decimal_places=2, max_digits=6, verbose_name="Rate")), | ||
], | ||
options={ | ||
"verbose_name": "Currency", | ||
"verbose_name_plural": "Currencies", | ||
}, | ||
bases=(core.models.TestUtilsMixin, models.Model), | ||
), | ||
migrations.RunPython(create_currencies), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from django.db import models | ||
from django.db.models import TextChoices | ||
from django.utils.translation import gettext_lazy as _ | ||
|
||
from core.models import DefaultModel | ||
|
||
|
||
class CurrencyRate(DefaultModel): | ||
class Currency(TextChoices): | ||
RUB = "RUB", _("RUB") | ||
USD = "USD", _("USD") | ||
KZT = "KZT", _("KZT") | ||
KIS = "KIS", _("KIS (for zero-price orders)") | ||
|
||
name = models.CharField(max_length=4, unique=True, choices=Currency.choices, db_index=True) | ||
rate = models.DecimalField(max_digits=6, decimal_places=2, verbose_name=_("Rate")) | ||
|
||
class Meta: | ||
verbose_name = _("Currency") | ||
verbose_name_plural = _("Currencies") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import pytest | ||
|
||
from apps.banking.base import Bank | ||
from apps.banking.exceptions import CurrencyRateDoesNotExist | ||
from apps.banking.models import CurrencyRate | ||
|
||
pytestmark = [pytest.mark.django_db] | ||
|
||
|
||
def test_get_currency_rate(): | ||
assert Bank.get_currency_rate() == 1 | ||
|
||
|
||
def test_raise_if_no_currency_rate(): | ||
CurrencyRate.objects.all().delete() | ||
with pytest.raises(CurrencyRateDoesNotExist): | ||
Bank.get_currency_rate() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Generated by Django 4.2.17 on 2024-12-24 11:29 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("orders", "0038_alter_order_bank_id_alter_refund_bank_id"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name="order", | ||
name="ue_rate", | ||
field=models.DecimalField(decimal_places=2, max_digits=6, verbose_name="Purchase-time UE rate"), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,8 +35,8 @@ def _adjust_settings(settings, mocker): | |
"[email protected]", | ||
"[email protected]", | ||
] | ||
mocker.patch("apps.stripebank.bank.StripeBankUSD.ue", Decimal(80)) | ||
mocker.patch("apps.stripebank.bank.StripeBankKZT.ue", Decimal("0.18")) | ||
mocker.patch("apps.stripebank.bank.StripeBankUSD.get_currency_rate", return_value=Decimal(80)) | ||
mocker.patch("apps.stripebank.bank.StripeBankKZT.get_currency_rate", return_value=Decimal("0.18")) | ||
|
||
|
||
@pytest.fixture | ||
|
@@ -66,7 +66,6 @@ def mock_stripe_refund(mocker): | |
|
||
@pytest.fixture | ||
def mock_stripe_kz_refund(mocker): | ||
mocker.patch("apps.stripebank.bank.StripeBankUSD.ue", 70) | ||
return mocker.patch("apps.stripebank.bank.StripeBankKZT.refund") | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.