Skip to content

Commit 0320c63

Browse files
authored
Add support for Django 1.11 & 2.x (#8)
* Blackify codebase * Add support for Django 1.11 & 2.x
1 parent d42a620 commit 0320c63

File tree

14 files changed

+320
-180
lines changed

14 files changed

+320
-180
lines changed

.travis.yml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@ sudo: false
44

55

66
env:
7-
- TOX_ENV=py27-django18
8-
- TOX_ENV=py27-django19
9-
- TOX_ENV=py27-django110
10-
- TOX_ENV=py34-django18
11-
- TOX_ENV=py34-django19
12-
- TOX_ENV=py34-django110
7+
- TOX_ENV=py27-django111
8+
- TOX_ENV=py36-django111
9+
- TOX_ENV=py36-django20
10+
- TOX_ENV=py36-django21
11+
- TOX_ENV=py36-django22
1312

1413

1514
install:
1615
- pip install tox
1716

1817
script:
19-
- tox -e $TOX_ENV
18+
- tox -e $TOX_ENV

custom_field/admin.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55

66

77
class CFAdmin(CustomFieldAdmin):
8-
list_display = ('content_type', 'name')
9-
list_filter = ('content_type',)
10-
search_fields = ('content_type__name', 'name')
8+
list_display = ("content_type", "name")
9+
list_filter = ("content_type",)
10+
search_fields = ("content_type__name", "name")
11+
12+
1113
admin.site.register(CustomField, CFAdmin)

custom_field/custom_field.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,39 @@ class CustomFieldModel(object):
1717
"""
1818
Abstract class adds some helper functions a Model
1919
"""
20+
2021
@property
2122
def get_custom_fields(self):
2223
""" Return a list of custom fields for this model """
2324
return CustomField.objects.filter(
24-
content_type=ContentType.objects.get_for_model(self))
25+
content_type=ContentType.objects.get_for_model(self)
26+
)
2527

2628
def get_model_custom_fields(self):
2729
""" Return a list of custom fields for this model, directly callable
2830
without an instance. Use like Foo.get_model_custom_fields(Foo)
2931
"""
3032
return CustomField.objects.filter(
31-
content_type=ContentType.objects.get_for_model(self))
33+
content_type=ContentType.objects.get_for_model(self)
34+
)
35+
3236
get_model_custom_fields = Callable(get_model_custom_fields)
3337

3438
def get_custom_field(self, field_name):
3539
""" Get a custom field object for this model
3640
field_name - Name of the custom field you want.
3741
"""
3842
content_type = ContentType.objects.get_for_model(self)
39-
return CustomField.objects.get(
40-
content_type=content_type, name=field_name)
43+
return CustomField.objects.get(content_type=content_type, name=field_name)
4144

4245
def get_custom_value(self, field_name):
4346
""" Get a value for a specified custom field
4447
field_name - Name of the custom field you want.
4548
"""
4649
custom_field = self.get_custom_field(field_name)
4750
return CustomFieldValue.objects.get_or_create(
48-
field=custom_field, object_id=self.id)[0].value
51+
field=custom_field, object_id=self.id
52+
)[0].value
4953

5054
def set_custom_value(self, field_name, value):
5155
""" Set a value for a specified custom field
@@ -54,7 +58,8 @@ def set_custom_value(self, field_name, value):
5458
"""
5559
custom_field = self.get_custom_field(field_name)
5660
custom_value = CustomFieldValue.objects.get_or_create(
57-
field=custom_field, object_id=self.id)[0]
61+
field=custom_field, object_id=self.id
62+
)[0]
5863
custom_value.value = value
5964
custom_value.save()
6065

@@ -64,7 +69,7 @@ def __init__(self, *args, **kwargs):
6469
super(CustomFieldValueForm, self).__init__(*args, **kwargs)
6570
if self.instance:
6671
try:
67-
self.fields['value'] = self.instance.get_form_field()
72+
self.fields["value"] = self.instance.get_form_field()
6873
except ObjectDoesNotExist:
6974
pass
7075

@@ -73,8 +78,8 @@ class CustomInline(admin.GenericTabularInline):
7378
model = CustomFieldValue
7479
form = CustomFieldValueForm
7580
can_delete = False
76-
readonly_fields = ('field',)
77-
fields = ('field', 'value')
81+
readonly_fields = ("field",)
82+
fields = ("field", "value")
7883
extra = 0
7984
max_num = 0
8085

@@ -87,6 +92,7 @@ class CustomFieldAdmin(ModelAdmin):
8792
""" Abstract class addes functionality to deal with custom fields in
8893
Django admin.
8994
"""
95+
9096
inlines = ()
9197

9298
def change_view(self, request, object_id, *args, **kwargs):
@@ -95,26 +101,23 @@ def change_view(self, request, object_id, *args, **kwargs):
95101
inlines.append(CustomInline)
96102
self.inlines = inlines
97103
return super(CustomFieldAdmin, self).change_view(
98-
request, object_id, *args, **kwargs)
104+
request, object_id, *args, **kwargs
105+
)
99106

100107
def get_form(self, request, obj=None, **kwargs):
101108
if obj:
102109
content_type = ContentType.objects.get_for_model(obj)
103-
custom_fields = CustomField.objects.filter(
104-
content_type=content_type)
110+
custom_fields = CustomField.objects.filter(content_type=content_type)
105111
for custom_field in custom_fields:
106112
try:
107113
field_value, created = CustomFieldValue.objects.get_or_create(
108-
content_type=content_type,
109-
object_id=obj.id,
110-
field=custom_field,
114+
content_type=content_type, object_id=obj.id, field=custom_field,
111115
)
112116
except IntegrityError:
113117
# This can happen because content_type is really a
114118
# cache field and didn't always exist
115119
field_value, created = CustomFieldValue.objects.get_or_create(
116-
object_id=obj.id,
117-
field=custom_field,
120+
object_id=obj.id, field=custom_field,
118121
)
119122
field_value.content_type = content_type
120123
field_value.save()
Lines changed: 72 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,98 @@
11
# -*- coding: utf-8 -*-
22
from __future__ import unicode_literals
33

4-
from django.db import models, migrations
4+
from django.db import migrations, models
55

66

77
class Migration(migrations.Migration):
88

99
dependencies = [
10-
('contenttypes', '0001_initial'),
10+
("contenttypes", "0001_initial"),
1111
]
1212

1313
operations = [
1414
migrations.CreateModel(
15-
name='CustomField',
15+
name="CustomField",
1616
fields=[
17-
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
18-
('name', models.CharField(max_length=75)),
19-
('field_type', models.CharField(default=b't', max_length=1, choices=[(b't', b'Text'), (b'i', b'Integer'), (b'b', b'Boolean (Yes/No)')])),
20-
('default_value', models.CharField(help_text=b'You may leave blank. For Boolean use True or False', max_length=255, blank=True)),
21-
('content_type', models.ForeignKey(to='contenttypes.ContentType')),
17+
(
18+
"id",
19+
models.AutoField(
20+
verbose_name="ID",
21+
serialize=False,
22+
auto_created=True,
23+
primary_key=True,
24+
),
25+
),
26+
("name", models.CharField(max_length=75)),
27+
(
28+
"field_type",
29+
models.CharField(
30+
default=b"t",
31+
max_length=1,
32+
choices=[
33+
(b"t", b"Text"),
34+
(b"i", b"Integer"),
35+
(b"b", b"Boolean (Yes/No)"),
36+
],
37+
),
38+
),
39+
(
40+
"default_value",
41+
models.CharField(
42+
help_text=b"You may leave blank. For Boolean use True or False",
43+
max_length=255,
44+
blank=True,
45+
),
46+
),
47+
(
48+
"content_type",
49+
models.ForeignKey(
50+
to="contenttypes.ContentType", on_delete=models.CASCADE
51+
),
52+
),
2253
],
23-
options={
24-
},
54+
options={},
2555
bases=(models.Model,),
2656
),
2757
migrations.CreateModel(
28-
name='CustomFieldValue',
58+
name="CustomFieldValue",
2959
fields=[
30-
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
31-
('value', models.CharField(max_length=255, null=True, blank=True)),
32-
('object_id', models.PositiveIntegerField()),
33-
('content_type', models.ForeignKey(blank=True, to='contenttypes.ContentType', null=True)),
34-
('field', models.ForeignKey(related_name='instance', to='custom_field.CustomField')),
60+
(
61+
"id",
62+
models.AutoField(
63+
verbose_name="ID",
64+
serialize=False,
65+
auto_created=True,
66+
primary_key=True,
67+
),
68+
),
69+
("value", models.CharField(max_length=255, null=True, blank=True)),
70+
("object_id", models.PositiveIntegerField()),
71+
(
72+
"content_type",
73+
models.ForeignKey(
74+
blank=True,
75+
to="contenttypes.ContentType",
76+
null=True,
77+
on_delete=models.CASCADE,
78+
),
79+
),
80+
(
81+
"field",
82+
models.ForeignKey(
83+
related_name="instance",
84+
to="custom_field.CustomField",
85+
on_delete=models.CASCADE,
86+
),
87+
),
3588
],
36-
options={
37-
},
89+
options={},
3890
bases=(models.Model,),
3991
),
4092
migrations.AlterUniqueTogether(
41-
name='customfieldvalue',
42-
unique_together=set([('field', 'object_id')]),
93+
name="customfieldvalue", unique_together=set([("field", "object_id")]),
4394
),
4495
migrations.AlterUniqueTogether(
45-
name='customfield',
46-
unique_together=set([('name', 'content_type')]),
96+
name="customfield", unique_together=set([("name", "content_type")]),
4797
),
4898
]

custom_field/migrations/0002_auto_20150119_2032.py

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,63 @@
77
class Migration(migrations.Migration):
88

99
dependencies = [
10-
('custom_field', '0001_initial'),
10+
("custom_field", "0001_initial"),
1111
]
1212

1313
operations = [
1414
migrations.AddField(
15-
model_name='customfield',
16-
name='field_choices',
17-
field=models.CharField(help_text=b'List the choices you want displayed, seperated by commas. This is only valid for Dropdown, Multiple, and Checkbox field types', max_length=2000, blank=True),
15+
model_name="customfield",
16+
name="field_choices",
17+
field=models.CharField(
18+
help_text=b"List the choices you want displayed, seperated by commas. This is only valid for Dropdown, Multiple, and Checkbox field types",
19+
max_length=2000,
20+
blank=True,
21+
),
1822
preserve_default=True,
1923
),
2024
migrations.AddField(
21-
model_name='customfield',
22-
name='is_required',
25+
model_name="customfield",
26+
name="is_required",
2327
field=models.BooleanField(default=False),
2428
preserve_default=True,
2529
),
2630
migrations.AlterField(
27-
model_name='customfield',
28-
name='default_value',
29-
field=models.CharField(help_text=b'You may leave blank. For Boolean use True or False', max_length=5000, blank=True),
31+
model_name="customfield",
32+
name="default_value",
33+
field=models.CharField(
34+
help_text=b"You may leave blank. For Boolean use True or False",
35+
max_length=5000,
36+
blank=True,
37+
),
3038
preserve_default=True,
3139
),
3240
migrations.AlterField(
33-
model_name='customfield',
34-
name='field_type',
35-
field=models.CharField(default=b't', max_length=1, choices=[(b't', b'Text'), (b'a', b'Large Text Field'), (b'i', b'Integer'), (b'f', b'Floating point decimal'), (b'b', b'Boolean (Yes/No)'), (b'm', b'Dropdown Choices'), (b'd', b'Date')]),
41+
model_name="customfield",
42+
name="field_type",
43+
field=models.CharField(
44+
default=b"t",
45+
max_length=1,
46+
choices=[
47+
(b"t", b"Text"),
48+
(b"a", b"Large Text Field"),
49+
(b"i", b"Integer"),
50+
(b"f", b"Floating point decimal"),
51+
(b"b", b"Boolean (Yes/No)"),
52+
(b"m", b"Dropdown Choices"),
53+
(b"d", b"Date"),
54+
],
55+
),
3656
preserve_default=True,
3757
),
3858
migrations.AlterField(
39-
model_name='customfield',
40-
name='name',
59+
model_name="customfield",
60+
name="name",
4161
field=models.CharField(max_length=150),
4262
preserve_default=True,
4363
),
4464
migrations.AlterField(
45-
model_name='customfieldvalue',
46-
name='value',
65+
model_name="customfieldvalue",
66+
name="value",
4767
field=models.CharField(max_length=5000, null=True, blank=True),
4868
preserve_default=True,
4969
),

custom_field/migrations/0003_customfield_mask.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@
77
class Migration(migrations.Migration):
88

99
dependencies = [
10-
('custom_field', '0002_auto_20150119_2032'),
10+
("custom_field", "0002_auto_20150119_2032"),
1111
]
1212

1313
operations = [
1414
migrations.AddField(
15-
model_name='customfield',
16-
name='mask',
17-
field=models.CharField(help_text=b"You may leave blank. For user Jquery Mask, ex: '00/00/0000' for date.", max_length=5000, blank=True),
15+
model_name="customfield",
16+
name="mask",
17+
field=models.CharField(
18+
help_text=b"You may leave blank. For user Jquery Mask, ex: '00/00/0000' for date.",
19+
max_length=5000,
20+
blank=True,
21+
),
1822
),
1923
]

0 commit comments

Comments
 (0)