diff --git a/azbankgateways/default_settings.py b/azbankgateways/default_settings.py index 72d5401..f0436dd 100755 --- a/azbankgateways/default_settings.py +++ b/azbankgateways/default_settings.py @@ -1,18 +1,10 @@ """Default settings for messaging.""" -import django from django.conf import settings from azbankgateways.apps import AZIranianBankGatewaysConfig -if django.__version__ >= "3.0": - from django.db import models - TEXT_CHOICES = models.TextChoices -else: - from .models.enum_django import TextChoices - - TEXT_CHOICES = TextChoices BANK_CLASS = getattr( settings, "CLASS", @@ -24,7 +16,7 @@ "ZIBAL": "azbankgateways.banks.Zibal", "BAHAMTA": "azbankgateways.banks.Bahamta", "MELLAT": "azbankgateways.banks.Mellat", - "PAYV1": "azbankgateways.banks.PayV1", + "PAYV1": "azbankgateways.banks.PayV1", }, ) _AZ_IRANIAN_BANK_GATEWAYS = getattr(settings, "AZ_IRANIAN_BANK_GATEWAYS", {}) @@ -39,16 +31,18 @@ TRACKING_CODE_LENGTH = _AZ_IRANIAN_BANK_GATEWAYS.get("TRACKING_CODE_LENGTH", 16) IS_SAMPLE_FORM_ENABLE = _AZ_IRANIAN_BANK_GATEWAYS.get("IS_SAMPLE_FORM_ENABLE", False) IS_SAFE_GET_GATEWAY_PAYMENT = _AZ_IRANIAN_BANK_GATEWAYS.get("IS_SAFE_GET_GATEWAY_PAYMENT", False) -CUSTOM_APP = _AZ_IRANIAN_BANK_GATEWAYS.get("CUSTOM_APP", None) +CUSTOM_APP = _AZ_IRANIAN_BANK_GATEWAYS.get("CUSTOM_APP") if CUSTOM_APP: CALLBACK_NAMESPACE = f"{CUSTOM_APP}:{AZIranianBankGatewaysConfig.name}:callback" GO_TO_BANK_GATEWAY_NAMESPACE = f"{CUSTOM_APP}:{AZIranianBankGatewaysConfig.name}:go-to-bank-gateway" SAMPLE_RESULT_NAMESPACE = f"{CUSTOM_APP}:{AZIranianBankGatewaysConfig.name}:sample-result" else: - CALLBACK_NAMESPACE = _AZ_IRANIAN_BANK_GATEWAYS.get("CALLBACK_NAMESPACE", f"{AZIranianBankGatewaysConfig.name}:callback") + CALLBACK_NAMESPACE = _AZ_IRANIAN_BANK_GATEWAYS.get( + "CALLBACK_NAMESPACE", f"{AZIranianBankGatewaysConfig.name}:callback" + ) GO_TO_BANK_GATEWAY_NAMESPACE = _AZ_IRANIAN_BANK_GATEWAYS.get( "GO_TO_BANK_GATEWAY_NAMESPACE", f"{AZIranianBankGatewaysConfig.name}:go-to-bank-gateway" ) SAMPLE_RESULT_NAMESPACE = _AZ_IRANIAN_BANK_GATEWAYS.get( "SAMPLE_RESULT_NAMESPACE", f"{AZIranianBankGatewaysConfig.name}:sample-result" - ) \ No newline at end of file + ) diff --git a/azbankgateways/models/enum.py b/azbankgateways/models/enum.py index e705a6a..ce0f1e5 100644 --- a/azbankgateways/models/enum.py +++ b/azbankgateways/models/enum.py @@ -1,9 +1,8 @@ +from django.db import models from django.utils.translation import gettext_lazy as _ -import azbankgateways.default_settings as settings - -class BankType(settings.TEXT_CHOICES): +class BankType(models.TextChoices): BMI = "BMI", _("BMI") SEP = "SEP", _("SEP") ZARINPAL = "ZARINPAL", _("Zarinpal") @@ -14,7 +13,7 @@ class BankType(settings.TEXT_CHOICES): PAYV1 = "PAYV1", _("PayV1") -class CurrencyEnum(settings.TEXT_CHOICES): +class CurrencyEnum(models.TextChoices): IRR = "IRR", _("Rial") IRT = "IRT", _("Toman") @@ -27,7 +26,7 @@ def toman_to_rial(cls, amount): return amount * 10 -class PaymentStatus(settings.TEXT_CHOICES): +class PaymentStatus(models.TextChoices): WAITING = "WAITING", _("Waiting") REDIRECT_TO_BANK = "REDIRECT_TO_BANK", _("Redirect to bank") RETURN_FROM_BANK = "RETURN_FROM_BANK", _("Return from bank") diff --git a/azbankgateways/models/enum_django.py b/azbankgateways/models/enum_django.py deleted file mode 100644 index e18f1ab..0000000 --- a/azbankgateways/models/enum_django.py +++ /dev/null @@ -1,77 +0,0 @@ -import enum - -from django.utils.functional import Promise - - -class ChoicesMeta(enum.EnumMeta): - """A metaclass for creating a enum choices.""" - - def __new__(metacls, classname, bases, classdict): - labels = [] - for key in classdict._member_names: - value = classdict[key] - if isinstance(value, (list, tuple)) and len(value) > 1 and isinstance(value[-1], (Promise, str)): - *value, label = value - value = tuple(value) - else: - label = key.replace("_", " ").title() - labels.append(label) - # Use dict.__setitem__() to suppress defenses against double - # assignment in enum's classdict. - dict.__setitem__(classdict, key, value) - cls = super().__new__(metacls, classname, bases, classdict) - cls._value2label_map_ = dict(zip(cls._value2member_map_, labels)) - # Add a label property to instances of enum which uses the enum member - # that is passed in as "self" as the value to use when looking up the - # label in the choices. - cls.label = property(lambda self: cls._value2label_map_.get(self.value)) - cls.do_not_call_in_templates = True - return enum.unique(cls) - - def __contains__(cls, member): - if not isinstance(member, enum.Enum): - # Allow non-enums to match against member values. - return any(x.value == member for x in cls) - return super().__contains__(member) - - @property - def names(cls): - empty = ["__empty__"] if hasattr(cls, "__empty__") else [] - return empty + [member.name for member in cls] - - @property - def choices(cls): - empty = [(None, cls.__empty__)] if hasattr(cls, "__empty__") else [] - return empty + [(member.value, member.label) for member in cls] - - @property - def labels(cls): - return [label for _, label in cls.choices] - - @property - def values(cls): - return [value for value, _ in cls.choices] - - -class Choices(enum.Enum, metaclass=ChoicesMeta): - """Class for creating enumerated choices.""" - - def __str__(self): - """ - Use value when cast to str, so that Choices set as model instance - attributes are rendered as expected in templates and similar contexts. - """ - return str(self.value) - - -class IntegerChoices(int, Choices): - """Class for creating enumerated integer choices.""" - - pass - - -class TextChoices(str, Choices): - """Class for creating enumerated string choices.""" - - def _generate_next_value_(name, start, count, last_values): - return name