Skip to content

Commit 14e1cf5

Browse files
authored
Merge pull request #59 from akx/csty
PEP8, isort and cleanup
2 parents 5a2d5d0 + 158aa0f commit 14e1cf5

15 files changed

+97
-63
lines changed

enumfields/admin.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from __future__ import print_function
2+
23
from django.contrib.admin.filters import ChoicesFieldListFilter
34
from django.utils.encoding import force_text
45
from django.utils.translation import ugettext_lazy as _

enumfields/compat.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -- encoding: UTF-8 --
2-
from django.forms import TypedChoiceField, CharField
2+
from django.forms import CharField, TypedChoiceField
33
from django.utils.text import capfirst
44

55
__all__ = ["formfield"]
@@ -11,6 +11,7 @@
1111
# would make this compatible with our enums,
1212
# but it's best to go all the way to the freshest code, I think.
1313

14+
1415
def formfield(db_field, form_class=None, choices_form_class=None, **kwargs):
1516
"""
1617
Returns a django.forms.Field instance for this database Field.
@@ -59,8 +60,8 @@ def formfield(db_field, form_class=None, choices_form_class=None, **kwargs):
5960
from django.utils.module_loading import import_by_path as import_string
6061
except ImportError:
6162
from django.utils.importlib import import_module
63+
6264
def import_string(dotted_path):
6365
module_path, class_name = dotted_path.rsplit('.', 1)
6466
module = import_module(module_path)
6567
return getattr(module, class_name)
66-

enumfields/enums.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import inspect
2+
from enum import Enum as BaseEnum
3+
from enum import EnumMeta as BaseEnumMeta
4+
from enum import _EnumDict
5+
26
from django.utils.encoding import python_2_unicode_compatible
3-
from enum import Enum as BaseEnum, EnumMeta as BaseEnumMeta, _EnumDict
47

58
try:
69
from django.utils.encoding import force_text

enumfields/fields.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1+
from enum import Enum
2+
13
import django
4+
import six
25
from django.core.exceptions import ValidationError
36
from django.db import models
7+
from django.db.models.fields import BLANK_CHOICE_DASH, NOT_PROVIDED
48
from django.utils.functional import cached_property
5-
from enum import Enum
6-
from .forms import EnumChoiceField
7-
import six
8-
from django.db.models.fields import NOT_PROVIDED, BLANK_CHOICE_DASH
99

1010
from .compat import import_string
11+
from .forms import EnumChoiceField
1112

1213
metaclass = models.SubfieldBase if django.VERSION < (1, 8) else type
1314

15+
1416
class EnumFieldMixin(six.with_metaclass(metaclass)):
1517
def __init__(self, enum, **options):
1618
if isinstance(enum, six.string_types):
@@ -146,7 +148,6 @@ def enum_value(an_enum):
146148
raise ValueError("%s is not a enum" % an_enum)
147149

148150

149-
150151
rules = [
151152
(
152153
[EnumFieldMixin],

enumfields/forms.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
__all__ = ["EnumChoiceField", "EnumMultipleChoiceField"]
77

8+
89
class EnumChoiceFieldMixin(object):
910
def prepare_value(self, value):
1011
# Widgets expect to get strings as values.
@@ -25,5 +26,6 @@ def valid_value(self, value):
2526
class EnumChoiceField(EnumChoiceFieldMixin, TypedChoiceField):
2627
pass
2728

29+
2830
class EnumMultipleChoiceField(EnumChoiceFieldMixin, TypedMultipleChoiceField):
2931
pass

setup.cfg

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[pep8]
2+
max-line-length = 120
3+
exclude = *migrations*
4+
ignore = E309
5+
6+
[flake8]
7+
exclude = migrations
8+
max-line-length = 120
9+
max-complexity = 10
10+
11+
[pytest]
12+
DJANGO_SETTINGS_MODULE = tests.settings
13+
norecursedirs = .git .tox .eggs .cache htmlcov venv*
14+
doctest_optionflags = NORMALIZE_WHITESPACE IGNORE_EXCEPTION_DETAIL ALLOW_UNICODE
15+
16+
[isort]
17+
atomic=true
18+
combine_as_imports=false
19+
indent=4
20+
known_standard_library=token,tokenize,enum,importlib
21+
known_third_party=django,six
22+
length_sort=false
23+
line_length=120
24+
multi_line_output=5
25+
not_skip=__init__.py
26+
order_by_type=false
27+
skip=migrations
28+
wrap_length=120

tests/admin.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from django.contrib import admin
55

66
from enumfields.admin import EnumFieldListFilter
7+
78
from .models import MyModel
89

910

tests/enums.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# coding=utf-8
22
from __future__ import unicode_literals
3+
34
from django.utils.translation import ugettext_lazy
45

56
from enumfields import Enum, IntEnum

tests/models.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from django.db import models
22

33
from enumfields import EnumField, EnumIntegerField
4-
from .enums import Color, Taste, IntegerEnum, LabeledEnum, ZeroEnum
4+
5+
from .enums import Color, IntegerEnum, LabeledEnum, Taste, ZeroEnum
56

67

78
class MyModel(models.Model):

tests/settings.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,22 @@
2929
DEBUG = True
3030

3131
STATIC_URL = "/static/"
32+
33+
TEMPLATES = [
34+
{
35+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
36+
'DIRS': [],
37+
'APP_DIRS': True,
38+
'OPTIONS': {
39+
'context_processors': [
40+
'django.contrib.auth.context_processors.auth',
41+
'django.template.context_processors.debug',
42+
'django.template.context_processors.i18n',
43+
'django.template.context_processors.media',
44+
'django.template.context_processors.static',
45+
'django.template.context_processors.tz',
46+
'django.contrib.messages.context_processors.messages',
47+
],
48+
},
49+
},
50+
]

tests/test_django_admin.py

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,18 @@
22
import re
33
import uuid
44

5-
try:
6-
from django.contrib.auth import get_user_model
7-
except ImportError: # `get_user_model` only exists from Django 1.5 on.
8-
from django.contrib.auth.models import User
9-
10-
get_user_model = lambda: User
11-
12-
from django.core.urlresolvers import reverse
13-
from django.test import Client
145
import pytest
15-
from enumfields import EnumIntegerField
16-
from .enums import Color, Taste, ZeroEnum, IntegerEnum
17-
from .models import MyModel
18-
19-
20-
@pytest.fixture
21-
def client():
22-
return Client()
23-
24-
25-
SUPERUSER_USERNAME = "superuser"
26-
SUPERUSER_PASS = "superpass"
27-
28-
29-
@pytest.fixture
30-
def superuser():
31-
return get_user_model().objects.create_superuser(
32-
username=SUPERUSER_USERNAME,
33-
password=SUPERUSER_PASS,
34-
35-
)
6+
from django.core.urlresolvers import reverse
367

8+
from enumfields import EnumIntegerField
379

38-
@pytest.fixture
39-
def superuser_client(client, superuser):
40-
client.login(username=SUPERUSER_USERNAME, password=SUPERUSER_PASS)
41-
return client
10+
from .enums import Color, IntegerEnum, Taste, ZeroEnum
11+
from .models import MyModel
4212

4313

4414
@pytest.mark.django_db
4515
@pytest.mark.urls('tests.urls')
46-
def test_model_admin_post(superuser_client):
16+
def test_model_admin_post(admin_client):
4717
url = reverse("admin:tests_mymodel_add")
4818
secret_uuid = str(uuid.uuid4())
4919
post_data = {
@@ -53,7 +23,7 @@ def test_model_admin_post(superuser_client):
5323
'random_code': secret_uuid,
5424
'zero2': ZeroEnum.ZERO.value,
5525
}
56-
response = superuser_client.post(url, follow=True, data=post_data)
26+
response = admin_client.post(url, follow=True, data=post_data)
5727
response.render()
5828
text = response.content
5929

@@ -73,7 +43,7 @@ def test_model_admin_post(superuser_client):
7343
@pytest.mark.parametrize('q_color', (None, Color.BLUE, Color.RED))
7444
@pytest.mark.parametrize('q_taste', (None, Taste.SWEET, Taste.SOUR))
7545
@pytest.mark.parametrize('q_int_enum', (None, IntegerEnum.A, IntegerEnum.B))
76-
def test_model_admin_filter(superuser_client, q_color, q_taste, q_int_enum):
46+
def test_model_admin_filter(admin_client, q_color, q_taste, q_int_enum):
7747
"""
7848
Test that various combinations of Enum filters seem to do the right thing in the change list.
7949
"""
@@ -94,7 +64,7 @@ def test_model_admin_filter(superuser_client, q_color, q_taste, q_int_enum):
9464
# Build the query string (this is assuming things, sort of)
9565
qs = dict(('%s__exact' % k, v.value) for (k, v) in lookup.items())
9666
# Run the request!
97-
response = superuser_client.get(reverse('admin:tests_mymodel_changelist'), data=qs)
67+
response = admin_client.get(reverse('admin:tests_mymodel_changelist'), data=qs)
9868
response.render()
9969

10070
# Look for the paginator line that lists how many results we found...

tests/test_django_models.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# -- encoding: UTF-8 --
22

3+
from enum import IntEnum
4+
35
import pytest
46
from django.db import connection
5-
from enum import IntEnum
67

78
from .enums import Color, IntegerEnum, LabeledEnum, Taste, ZeroEnum
89
from .models import MyModel

tests/test_enums.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
# -- encoding: UTF-8 --
22
from __future__ import unicode_literals
3-
from django.core.exceptions import ValidationError
43

5-
from django.forms import BaseForm
6-
from enumfields import EnumField
74
import pytest
85
import six
6+
from django.core.exceptions import ValidationError
7+
from django.forms import BaseForm
8+
9+
from enumfields import EnumField
910

1011
from .enums import Color, IntegerEnum
1112

13+
1214
def test_choice_ordering():
1315
EXPECTED_CHOICES = (
1416
('r', 'Reddish'),
@@ -19,23 +21,27 @@ def test_choice_ordering():
1921
assert key == ex_key
2022
assert six.text_type(val) == six.text_type(ex_val)
2123

24+
2225
def test_custom_labels():
2326
# Custom label
2427
assert Color.RED.label == 'Reddish'
2528
assert six.text_type(Color.RED) == 'Reddish'
2629
assert six.text_type(IntegerEnum.A) == 'foo'
2730

31+
2832
def test_automatic_labels():
2933
# Automatic label
3034
assert Color.GREEN.label == 'Green'
3135
assert six.text_type(Color.GREEN) == 'Green'
3236
assert six.text_type(IntegerEnum.B) == 'B'
3337

38+
3439
def test_lazy_labels():
3540
# Lazy label
3641
assert isinstance(six.text_type(Color.BLUE), six.string_types)
3742
assert six.text_type(Color.BLUE) == 'bluë'
3843

44+
3945
def test_formfield_labels():
4046
# Formfield choice label
4147
form_field = EnumField(Color).formfield()
@@ -44,6 +50,7 @@ def test_formfield_labels():
4450
if value:
4551
assert text == expectations[value]
4652

53+
4754
def test_formfield_functionality():
4855
form_cls = type(str("FauxForm"), (BaseForm,), {
4956
"base_fields": {"color": EnumField(Color).formfield()}
@@ -52,10 +59,12 @@ def test_formfield_functionality():
5259
assert not form.errors
5360
assert form.cleaned_data["color"] == Color.RED
5461

62+
5563
def test_invalid_to_python_fails():
5664
with pytest.raises(ValidationError) as ve:
5765
EnumField(Color).to_python("invalid")
5866
assert ve.value.code == "invalid_enum_value"
5967

68+
6069
def test_import_by_string():
6170
assert EnumField("tests.test_enums.Color").enum == Color

tests/test_form_fields.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# -- encoding: UTF-8 --
2+
import pytest
3+
import six
24
from django.db.models import BLANK_CHOICE_DASH
35
from django.forms.models import modelform_factory
4-
import pytest
6+
57
from .enums import Color
68
from .models import MyModel
7-
import six
89

910

1011
def get_form(**kwargs):

tests/urls.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
# -*- coding: utf-8 -*-
22
from django.conf import settings
3-
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
4-
5-
__author__ = 'mikhailturilin'
6-
7-
from django.conf.urls import *
3+
from django.conf.urls import include, url
84
from django.contrib import admin
9-
5+
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
106

117
admin.autodiscover()
128

13-
urlpatterns = patterns(
14-
'',
9+
urlpatterns = [
1510
url(r'^admin/', include(admin.site.urls)),
16-
)
11+
]
1712

1813
if settings.DEBUG:
19-
urlpatterns = staticfiles_urlpatterns() + urlpatterns
14+
urlpatterns = staticfiles_urlpatterns() + urlpatterns

0 commit comments

Comments
 (0)