From cda824ad56e915cdbb2c55b55c7cfc56c35b6c85 Mon Sep 17 00:00:00 2001 From: imperosol Date: Fri, 14 Mar 2025 16:04:16 +0100 Subject: [PATCH] move CurrencyField to counter --- accounting/migrations/0001_initial.py | 8 ++--- accounting/models.py | 30 +------------------ counter/fields.py | 30 +++++++++++++++++++ counter/migrations/0001_initial.py | 14 ++++----- counter/migrations/0002_auto_20160826_1342.py | 4 +-- counter/migrations/0020_auto_20221215_1709.py | 4 +-- ..._remove_product_parent_product_and_more.py | 6 ++-- counter/models.py | 2 +- counter/views/invoice.py | 2 +- eboutic/migrations/0001_initial.py | 6 ++-- eboutic/models.py | 2 +- 11 files changed, 55 insertions(+), 53 deletions(-) create mode 100644 counter/fields.py diff --git a/accounting/migrations/0001_initial.py b/accounting/migrations/0001_initial.py index 2f22da8d..29ce0739 100644 --- a/accounting/migrations/0001_initial.py +++ b/accounting/migrations/0001_initial.py @@ -4,7 +4,7 @@ import django.core.validators import django.db.models.deletion from django.db import migrations, models -import accounting.models +import counter.fields class Migration(migrations.Migration): @@ -142,7 +142,7 @@ class Migration(migrations.Migration): ), ( "amount", - accounting.models.CurrencyField( + counter.fields.CurrencyField( decimal_places=2, default=0, verbose_name="amount", @@ -151,7 +151,7 @@ class Migration(migrations.Migration): ), ( "effective_amount", - accounting.models.CurrencyField( + counter.fields.CurrencyField( decimal_places=2, default=0, verbose_name="effective_amount", @@ -176,7 +176,7 @@ class Migration(migrations.Migration): ("number", models.IntegerField(verbose_name="number")), ( "amount", - accounting.models.CurrencyField( + counter.fields.CurrencyField( decimal_places=2, max_digits=12, verbose_name="amount" ), ), diff --git a/accounting/models.py b/accounting/models.py index ba1024cf..4ddbb739 100644 --- a/accounting/models.py +++ b/accounting/models.py @@ -13,7 +13,6 @@ # # -from decimal import Decimal from django.conf import settings from django.core import validators @@ -23,34 +22,7 @@ from phonenumber_field.modelfields import PhoneNumberField from club.models import Club from core.models import SithFile - - -class CurrencyField(models.DecimalField): - """Custom database field used for currency.""" - - def __init__(self, *args, **kwargs): - kwargs["max_digits"] = 12 - kwargs["decimal_places"] = 2 - super().__init__(*args, **kwargs) - - def to_python(self, value): - try: - return super().to_python(value).quantize(Decimal("0.01")) - except AttributeError: - return None - - -if settings.TESTING: - from model_bakery import baker - - baker.generators.add( - CurrencyField, - lambda: baker.random_gen.gen_decimal(max_digits=8, decimal_places=2), - ) -else: # pragma: no cover - # baker is only used in tests, so we don't need coverage for this part - pass - +from counter.fields import CurrencyField # Accounting classes diff --git a/counter/fields.py b/counter/fields.py new file mode 100644 index 00000000..a212059d --- /dev/null +++ b/counter/fields.py @@ -0,0 +1,30 @@ +from decimal import Decimal + +from django.conf import settings +from django.db import models + + +class CurrencyField(models.DecimalField): + """Custom database field used for currency.""" + + def __init__(self, *args, **kwargs): + kwargs["max_digits"] = 12 + kwargs["decimal_places"] = 2 + super().__init__(*args, **kwargs) + + def to_python(self, value): + if value is None: + return None + return super().to_python(value).quantize(Decimal("0.01")) + + +if settings.TESTING: + from model_bakery import baker + + baker.generators.add( + CurrencyField, + lambda: baker.random_gen.gen_decimal(max_digits=8, decimal_places=2), + ) +else: # pragma: no cover + # baker is only used in tests, so we don't need coverage for this part + pass diff --git a/counter/migrations/0001_initial.py b/counter/migrations/0001_initial.py index 34381c89..0eb145da 100644 --- a/counter/migrations/0001_initial.py +++ b/counter/migrations/0001_initial.py @@ -4,7 +4,7 @@ import django.db.models.deletion from django.conf import settings from django.db import migrations, models -import accounting.models +import counter.fields class Migration(migrations.Migration): @@ -78,7 +78,7 @@ class Migration(migrations.Migration): ), ( "amount", - accounting.models.CurrencyField( + counter.fields.CurrencyField( decimal_places=2, max_digits=12, verbose_name="amount" ), ), @@ -145,19 +145,19 @@ class Migration(migrations.Migration): ), ( "purchase_price", - accounting.models.CurrencyField( + counter.fields.CurrencyField( decimal_places=2, max_digits=12, verbose_name="purchase price" ), ), ( "selling_price", - accounting.models.CurrencyField( + counter.fields.CurrencyField( decimal_places=2, max_digits=12, verbose_name="selling price" ), ), ( "special_selling_price", - accounting.models.CurrencyField( + counter.fields.CurrencyField( decimal_places=2, max_digits=12, verbose_name="special selling price", @@ -240,7 +240,7 @@ class Migration(migrations.Migration): ), ( "amount", - accounting.models.CurrencyField( + counter.fields.CurrencyField( decimal_places=2, max_digits=12, verbose_name="amount" ), ), @@ -324,7 +324,7 @@ class Migration(migrations.Migration): ("label", models.CharField(max_length=64, verbose_name="label")), ( "unit_price", - accounting.models.CurrencyField( + counter.fields.CurrencyField( decimal_places=2, max_digits=12, verbose_name="unit price" ), ), diff --git a/counter/migrations/0002_auto_20160826_1342.py b/counter/migrations/0002_auto_20160826_1342.py index 7655a480..a9709477 100644 --- a/counter/migrations/0002_auto_20160826_1342.py +++ b/counter/migrations/0002_auto_20160826_1342.py @@ -4,7 +4,7 @@ import django.db.models.deletion from django.conf import settings from django.db import migrations, models -import accounting.models +import counter.fields class Migration(migrations.Migration): @@ -67,7 +67,7 @@ class Migration(migrations.Migration): ), ( "value", - accounting.models.CurrencyField( + counter.fields.CurrencyField( max_digits=12, verbose_name="value", decimal_places=2 ), ), diff --git a/counter/migrations/0020_auto_20221215_1709.py b/counter/migrations/0020_auto_20221215_1709.py index 22db406b..4cf6a95d 100644 --- a/counter/migrations/0020_auto_20221215_1709.py +++ b/counter/migrations/0020_auto_20221215_1709.py @@ -2,7 +2,7 @@ from django.db import migrations -import accounting.models +import counter.fields class Migration(migrations.Migration): @@ -14,7 +14,7 @@ class Migration(migrations.Migration): migrations.AlterField( model_name="customer", name="amount", - field=accounting.models.CurrencyField( + field=counter.fields.CurrencyField( decimal_places=2, default=0, max_digits=12, verbose_name="amount" ), ), diff --git a/counter/migrations/0025_remove_product_parent_product_and_more.py b/counter/migrations/0025_remove_product_parent_product_and_more.py index 64a2129c..9a2d76a9 100644 --- a/counter/migrations/0025_remove_product_parent_product_and_more.py +++ b/counter/migrations/0025_remove_product_parent_product_and_more.py @@ -2,7 +2,7 @@ from django.db import migrations, models -import accounting.models +import counter.fields class Migration(migrations.Migration): @@ -18,7 +18,7 @@ class Migration(migrations.Migration): migrations.AlterField( model_name="product", name="purchase_price", - field=accounting.models.CurrencyField( + field=counter.fields.CurrencyField( decimal_places=2, help_text="Initial cost of purchasing the product", max_digits=12, @@ -28,7 +28,7 @@ class Migration(migrations.Migration): migrations.AlterField( model_name="product", name="special_selling_price", - field=accounting.models.CurrencyField( + field=counter.fields.CurrencyField( decimal_places=2, help_text="Price for barmen during their permanence", max_digits=12, diff --git a/counter/models.py b/counter/models.py index dc043509..a0f8681d 100644 --- a/counter/models.py +++ b/counter/models.py @@ -38,12 +38,12 @@ from django_countries.fields import CountryField from ordered_model.models import OrderedModel from phonenumber_field.modelfields import PhoneNumberField -from accounting.models import CurrencyField from club.models import Club from core.fields import ResizedImageField from core.models import Group, Notification, User from core.utils import get_start_of_semester from counter.apps import PAYMENT_METHOD +from counter.fields import CurrencyField from sith.settings import SITH_MAIN_CLUB from subscription.models import Subscription diff --git a/counter/views/invoice.py b/counter/views/invoice.py index dbd6e7cb..cabbccdb 100644 --- a/counter/views/invoice.py +++ b/counter/views/invoice.py @@ -19,7 +19,7 @@ from django.db.models import F from django.utils import timezone from django.views.generic import TemplateView -from accounting.models import CurrencyField +from counter.fields import CurrencyField from counter.models import Refilling, Selling from counter.views.mixins import CounterAdminMixin, CounterAdminTabsMixin diff --git a/eboutic/migrations/0001_initial.py b/eboutic/migrations/0001_initial.py index 3e3730f3..1fa8c248 100644 --- a/eboutic/migrations/0001_initial.py +++ b/eboutic/migrations/0001_initial.py @@ -4,7 +4,7 @@ import django.db.models.deletion from django.conf import settings from django.db import migrations, models -import accounting.models +import counter.fields class Migration(migrations.Migration): @@ -55,7 +55,7 @@ class Migration(migrations.Migration): ("type_id", models.IntegerField(verbose_name="product type id")), ( "product_unit_price", - accounting.models.CurrencyField( + counter.fields.CurrencyField( decimal_places=2, max_digits=12, verbose_name="unit price" ), ), @@ -120,7 +120,7 @@ class Migration(migrations.Migration): ("type_id", models.IntegerField(verbose_name="product type id")), ( "product_unit_price", - accounting.models.CurrencyField( + counter.fields.CurrencyField( decimal_places=2, max_digits=12, verbose_name="unit price" ), ), diff --git a/eboutic/models.py b/eboutic/models.py index 980137b5..cd55d0ae 100644 --- a/eboutic/models.py +++ b/eboutic/models.py @@ -25,8 +25,8 @@ from django.db.models import F, OuterRef, Subquery, Sum from django.utils.functional import cached_property from django.utils.translation import gettext_lazy as _ -from accounting.models import CurrencyField from core.models import User +from counter.fields import CurrencyField from counter.models import BillingInfo, Counter, Customer, Product, Refilling, Selling