Skip to content

Commit bb36adb

Browse files
committed
no linting errors
1 parent f265e1a commit bb36adb

File tree

17 files changed

+504
-170
lines changed

17 files changed

+504
-170
lines changed

README.rst

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,21 @@ type for the given enumeration based on its value type and range. For example,
7171
int_enum = EnumField(IntEnum)
7272
7373
74-
``EnumField`` is more than just an alias. The fields are now assignable and
75-
accessible as their enumeration type rather than by-value:
74+
``EnumField`` *is more than just an alias. The fields are now assignable and
75+
accessible as their enumeration type rather than by-value:*
7676

7777
.. code:: python
7878
7979
instance = MyModel.objects.create(
80-
txt_enum=TextEnum.VALUE1,
80+
txt_enum=MyModel.TextEnum.VALUE1,
8181
int_enum=3 # by-value assignment also works
8282
)
83+
instance.refresh_from_db()
8384
84-
instance.txt_enum == TextEnum('V1')
85+
instance.txt_enum == MyModel.TextEnum('V1')
8586
instance.txt_enum.label == 'Value 1'
8687
87-
instance.int_enum == IntEnum['THREE']
88+
instance.int_enum == MyModel.IntEnum['THREE']
8889
instance.int_enum.value == 3
8990
9091
@@ -96,7 +97,7 @@ possible very rich enumeration fields.
9697
.. code:: python
9798
9899
from enum_properties import s
99-
from django_enum import TextChoices # use instead of django's TextChoices
100+
from django_enum import TextChoices # use instead of Django's TextChoices
100101
from django.db import models
101102
102103
class MyModel(models.Model):
@@ -114,13 +115,14 @@ possible very rich enumeration fields.
114115
115116
color = EnumField(Color)
116117
117-
instance = MyModel.objects.create(color=Color('FF0000'))
118-
instance.color == Color('Red') == Color('R') == Color((1, 0, 0))
118+
instance = MyModel.objects.create(color=MyModel.Color('FF0000'))
119+
instance.color == MyModel.Color('Red') == MyModel.Color('R') == MyModel.Color((1, 0, 0))
119120
120121
# save back by any symmetric value
121122
instance.color = 'FF0000'
122-
instance.save()
123+
instance.full_clean()
123124
instance.color.hex == 'ff0000'
125+
instance.save()
124126
125127
126128
.. note::

django_enum/__init__.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,22 @@
88
99
*******************************************************************************
1010
"""
11+
from django_enum.choices import (
12+
DjangoEnumPropertiesMeta,
13+
FloatChoices,
14+
IntegerChoices,
15+
TextChoices,
16+
)
1117
from django_enum.fields import (
18+
EnumBigIntegerField,
19+
EnumCharField,
1220
EnumField,
1321
EnumFloatField,
14-
EnumCharField,
15-
EnumSmallIntegerField,
1622
EnumIntegerField,
17-
EnumBigIntegerField,
18-
EnumPositiveSmallIntegerField,
23+
EnumPositiveBigIntegerField,
1924
EnumPositiveIntegerField,
20-
EnumPositiveBigIntegerField
21-
)
22-
from django_enum.choices import (
23-
TextChoices,
24-
IntegerChoices,
25-
FloatChoices,
26-
DjangoEnumPropertiesMeta
25+
EnumPositiveSmallIntegerField,
26+
EnumSmallIntegerField,
2727
)
2828
from django_enum.forms import EnumChoiceField
2929

django_enum/choices.py

Lines changed: 60 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,78 @@
33
types. These choices types are drop in replacements for the Django
44
IntegerChoices and TextChoices.
55
"""
6-
from enum_properties import (
7-
SymmetricMixin,
8-
EnumPropertiesMeta
9-
)
106
from django.db.models import Choices
117
from django.db.models import IntegerChoices as DjangoIntegerChoices
128
from django.db.models import TextChoices as DjangoTextChoices
139
from django.db.models.enums import ChoicesMeta
1410

11+
try:
12+
from enum_properties import EnumPropertiesMeta, SymmetricMixin
1513

16-
class DjangoEnumPropertiesMeta(EnumPropertiesMeta, ChoicesMeta):
17-
"""Derive """
18-
pass
1914

15+
class DjangoEnumPropertiesMeta(EnumPropertiesMeta, ChoicesMeta):
16+
"""
17+
A composite meta class that combines Django's Choices metaclass with
18+
enum-properties metaclass. This metaclass will add Django's expected
19+
choices attribute and label properties to enumerations and
20+
enum-properties' generic property support.
21+
"""
2022

21-
class DjangoSymmetricMixin(SymmetricMixin):
22-
_symmetric_builtins_ = ['name', 'label']
2323

24+
class DjangoSymmetricMixin(SymmetricMixin):
25+
"""
26+
An enumeration mixin that makes Django's Choices type label field
27+
symmetric.
28+
"""
29+
_symmetric_builtins_ = ['name', 'label']
2430

25-
class TextChoices(
26-
DjangoSymmetricMixin,
27-
DjangoTextChoices,
28-
metaclass=DjangoEnumPropertiesMeta
29-
):
30-
pass
3131

32+
class TextChoices(
33+
DjangoSymmetricMixin,
34+
DjangoTextChoices,
35+
metaclass=DjangoEnumPropertiesMeta
36+
):
37+
"""
38+
A character enumeration type that extends Django's TextChoices and
39+
accepts enum-properties property lists.
40+
"""
3241

33-
class IntegerChoices(
34-
DjangoSymmetricMixin,
35-
DjangoIntegerChoices,
36-
metaclass=DjangoEnumPropertiesMeta
37-
):
38-
pass
3942

43+
class IntegerChoices(
44+
DjangoSymmetricMixin,
45+
DjangoIntegerChoices,
46+
metaclass=DjangoEnumPropertiesMeta
47+
):
48+
"""
49+
An integer enumeration type that extends Django's IntegerChoices and
50+
accepts enum-properties property lists.
51+
"""
4052

41-
class FloatChoices(
42-
DjangoSymmetricMixin,
43-
float,
44-
Choices,
45-
metaclass=DjangoEnumPropertiesMeta
46-
):
47-
pass
4853

54+
class FloatChoices(
55+
DjangoSymmetricMixin,
56+
float,
57+
Choices,
58+
metaclass=DjangoEnumPropertiesMeta
59+
):
60+
"""
61+
A floating point enumeration type that accepts enum-properties
62+
property lists.
63+
"""
64+
65+
except (ImportError, ModuleNotFoundError):
66+
67+
class MissingEnumProperties:
68+
"""Throw error if choice types are used without enum-properties"""
69+
70+
def __init__(self, *args, **kwargs):
71+
raise ImportError(
72+
f'{self.__class__.__name__} requires enum-properties to be '
73+
f'installed.'
74+
)
75+
76+
DjangoEnumPropertiesMeta = MissingEnumProperties
77+
DjangoSymmetricMixin = MissingEnumProperties
78+
TextChoices = MissingEnumProperties
79+
IntegerChoices = MissingEnumProperties
80+
FloatChoices = MissingEnumProperties

0 commit comments

Comments
 (0)