Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Currency model #2483

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/apps/banking/admin/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from apps.banking.admin.currency_rate import CurrencyRateAdmin

__all__ = [
"CurrencyRateAdmin",
]
32 changes: 32 additions & 0 deletions src/apps/banking/admin/currency_rate.py
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)
37 changes: 37 additions & 0 deletions src/apps/banking/migrations/0003_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Generated by Django 4.2.17 on 2024-12-24 18:15

from django.db import migrations, models

import core.models


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),
),
]
20 changes: 20 additions & 0 deletions src/apps/banking/models.py
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")
17 changes: 17 additions & 0 deletions src/apps/orders/migrations/0039_alter_order_ue_rate.py
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"),
),
]
2 changes: 1 addition & 1 deletion src/apps/orders/models/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Order(TimestampedModel):
shipped = models.DateTimeField(_("Date when order was shipped"), null=True, blank=True)

bank_id = models.CharField(_("User-requested bank string"), choices=BANK_CHOICES, blank=True, max_length=32)
ue_rate = models.IntegerField(_("Purchase-time UE rate"))
ue_rate = models.DecimalField(_("Purchase-time UE rate"), decimal_places=2, max_digits=6)
acquiring_percent = models.DecimalField(default=0, max_digits=4, decimal_places=2)

course = ItemField(to="products.Course", verbose_name=_("Course"), null=True, blank=True, on_delete=models.PROTECT)
Expand Down
29 changes: 29 additions & 0 deletions src/locale/ru/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,35 @@ msgstr "Да"
msgid "No"
msgstr "Нет"

#: apps/banking/models.py:10
msgid "RUB"
msgstr "Рубль"

#: apps/banking/models.py:11
msgid "USD"
msgstr "Доллар"

#: apps/banking/models.py:12
msgid "KZT"
msgstr "Тенге"

#: apps/banking/models.py:13
msgid "KIS (for zero-price orders)"
msgstr "KIS (для бесплатных заказов)"


#: apps/banking/models.py:15
msgid "Rate"
msgstr "Курс"

#: apps/banking/models.py:18
msgid "Currency"
msgstr "Курс валюты"

#: apps/banking/models.py:19
msgid "Currencies"
msgstr "Курсы валют"

#, python-brace-format
#~ msgid ""
#~ "Orders {non_refunded_orders_as_message} have not been refunded. Up to 5 "
Expand Down
Loading