Skip to content

Commit 3d629b1

Browse files
committed
Merge pull request jazzband#49 from treyhunner/flake8
Check code style in tests
2 parents f35f315 + 1d4f7ad commit 3d629b1

File tree

10 files changed

+171
-132
lines changed

10 files changed

+171
-132
lines changed

simple_history/admin.py

+44-26
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ def get_urls(self):
3737
admin_site = self.admin_site
3838
opts = self.model._meta
3939
info = opts.app_label, opts.module_name,
40-
history_urls = patterns("",
40+
history_urls = patterns(
41+
"",
4142
url("^([^/]+)/history/([^/]+)/$",
4243
admin_site.admin_view(self.history_form_view),
43-
name='%s_%s_simple_history' % info),)
44+
name='%s_%s_simple_history' % info),
45+
)
4446
return history_urls + urls
4547

4648
def history_view(self, request, object_id, extra_context=None):
@@ -54,7 +56,8 @@ def history_view(self, request, object_id, extra_context=None):
5456
# If no history was found, see whether this object even exists.
5557
obj = get_object_or_404(model, pk=unquote(object_id))
5658
content_type = ContentType.objects.get_for_model(User)
57-
admin_user_view = 'admin:%s_%s_change' % (content_type.app_label, content_type.model)
59+
admin_user_view = 'admin:%s_%s_change' % (content_type.app_label,
60+
content_type.model)
5861
context = {
5962
'title': _('Change history: %s') % force_text(obj),
6063
'action_list': action_list,
@@ -66,56 +69,67 @@ def history_view(self, request, object_id, extra_context=None):
6669
'admin_user_view': admin_user_view
6770
}
6871
context.update(extra_context or {})
69-
context_instance = template.RequestContext(request, current_app=self.admin_site.name)
70-
return render_to_response(self.object_history_template, context, context_instance=context_instance)
72+
context_instance = template.RequestContext(
73+
request, current_app=self.admin_site.name)
74+
return render_to_response(self.object_history_template, context,
75+
context_instance=context_instance)
7176

7277
def history_form_view(self, request, object_id, version_id):
7378
original_model = self.model
7479
original_opts = original_model._meta
75-
history = getattr(self.model, self.model._meta.simple_history_manager_attribute)
80+
history = getattr(self.model,
81+
self.model._meta.simple_history_manager_attribute)
7682
model = history.model
7783
opts = model._meta
7884
pk_name = original_opts.pk.attname
79-
record = get_object_or_404(model, **{pk_name: object_id, 'history_id': version_id})
85+
record = get_object_or_404(model, **{
86+
pk_name: object_id,
87+
'history_id': version_id,
88+
})
8089
obj = record.instance
8190
obj._state.adding = False
8291

8392
if not self.has_change_permission(request, obj):
8493
raise PermissionDenied
8594

8695
formsets = []
87-
ModelForm = self.get_form(request, obj)
96+
form_class = self.get_form(request, obj)
8897
if request.method == 'POST':
89-
form = ModelForm(request.POST, request.FILES, instance=obj)
98+
form = form_class(request.POST, request.FILES, instance=obj)
9099
if form.is_valid():
91100
form_validated = True
92101
new_object = self.save_form(request, form, change=True)
93102
else:
94103
form_validated = False
95104
new_object = obj
96-
prefixes = {}
97105

98106
if form_validated:
99107
self.save_model(request, new_object, form, change=True)
100108
form.save_m2m()
101109

102-
change_message = self.construct_change_message(request, form, formsets)
110+
change_message = self.construct_change_message(request, form,
111+
formsets)
103112
self.log_change(request, new_object, change_message)
104113
return self.response_change(request, new_object)
105114

106115
else:
107-
form = ModelForm(instance=obj)
116+
form = form_class(instance=obj)
108117

109-
adminForm = helpers.AdminForm(form, self.get_fieldsets(request, obj),
110-
self.prepopulated_fields, self.get_readonly_fields(request, obj),
111-
model_admin=self)
112-
media = self.media + adminForm.media
118+
admin_form = helpers.AdminForm(
119+
form,
120+
self.get_fieldsets(request, obj),
121+
self.prepopulated_fields,
122+
self.get_readonly_fields(request, obj),
123+
model_admin=self,
124+
)
125+
media = self.media + admin_form.media
113126

114127
url_triplet = (self.admin_site.name, original_opts.app_label,
115-
original_opts.module_name)
128+
original_opts.module_name)
129+
content_type_id = ContentType.objects.get_for_model(self.model).id
116130
context = {
117131
'title': _('Revert %s') % force_text(obj),
118-
'adminform': adminForm,
132+
'adminform': admin_form,
119133
'object_id': object_id,
120134
'original': obj,
121135
'is_popup': False,
@@ -124,8 +138,10 @@ def history_form_view(self, request, object_id, version_id):
124138
'app_label': opts.app_label,
125139
'original_opts': original_opts,
126140
'changelist_url': reverse('%s:%s_%s_changelist' % url_triplet),
127-
'change_url': reverse('%s:%s_%s_change' % url_triplet, args=(obj.pk,)),
128-
'history_url': reverse('%s:%s_%s_history' % url_triplet, args=(obj.pk,)),
141+
'change_url': reverse('%s:%s_%s_change' % url_triplet,
142+
args=(obj.pk,)),
143+
'history_url': reverse('%s:%s_%s_history' % url_triplet,
144+
args=(obj.pk,)),
129145
# Context variables copied from render_change_form
130146
'add': False,
131147
'change': True,
@@ -136,17 +152,19 @@ def history_form_view(self, request, object_id, version_id):
136152
'has_absolute_url': False,
137153
'form_url': '',
138154
'opts': opts,
139-
'content_type_id': ContentType.objects.get_for_model(self.model).id,
155+
'content_type_id': content_type_id,
140156
'save_as': self.save_as,
141157
'save_on_top': self.save_on_top,
142158
'root_path': getattr(self.admin_site, 'root_path', None),
143159
}
144-
context_instance = template.RequestContext(request, current_app=self.admin_site.name)
145-
return render_to_response(self.object_history_form_template, context, context_instance)
160+
context_instance = template.RequestContext(
161+
request,
162+
current_app=self.admin_site.name,
163+
)
164+
return render_to_response(self.object_history_form_template, context,
165+
context_instance)
146166

147167
def save_model(self, request, obj, form, change):
148-
"""
149-
Add the admin user to a special model attribute for reference after save
150-
"""
168+
"""Set special model attribute to user for reference after save"""
151169
obj._history_user = request.user
152170
super(SimpleHistoryAdmin, self).save_model(request, obj, form, change)

simple_history/models.py

+93-90
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@
2121
# copy of django function without use of six
2222
def python_2_unicode_compatible(klass):
2323
"""
24-
A decorator that defines __unicode__ and __str__ methods under Python 2.
25-
Under Python 3 it does nothing.
24+
Decorator defining __unicode__ and __str__ as appropriate for Py2/3
2625
27-
To support Python 2 and 3 with a single code base, define a __str__ method
28-
returning text and apply this decorator to the class.
26+
Usage: define __str__ method and apply this decorator to the class.
2927
"""
3028
if sys.version_info[0] != 3:
3129
klass.__unicode__ = klass.__str__
@@ -43,14 +41,19 @@ def contribute_to_class(self, cls, name):
4341
models.signals.class_prepared.connect(self.finalize, sender=cls)
4442

4543
def save_without_historical_record(self, *args, **kwargs):
46-
"""Caution! Make sure you know what you're doing before you use this method."""
44+
"""
45+
Save model without saving a historical record
46+
47+
Make sure you know what you're doing before you use this method.
48+
"""
4749
self.skip_history_when_saving = True
4850
try:
4951
ret = self.save(*args, **kwargs)
5052
finally:
5153
del self.skip_history_when_saving
5254
return ret
53-
setattr(cls, 'save_without_historical_record', save_without_historical_record)
55+
setattr(cls, 'save_without_historical_record',
56+
save_without_historical_record)
5457

5558
def finalize(self, sender, **kwargs):
5659
history_model = self.create_history_model(sender)
@@ -99,100 +102,24 @@ def copy_fields(self, model):
99102
a dictionary mapping field name to copied field object.
100103
"""
101104
fields = {}
102-
103-
def customize_field(field):
104-
field.name = field.attname
105-
if isinstance(field, models.AutoField):
106-
# The historical model gets its own AutoField, so any
107-
# existing one must be replaced with an IntegerField.
108-
field.__class__ = models.IntegerField
109-
elif isinstance(field, models.FileField):
110-
# Don't copy file, just path.
111-
field.__class__ = models.TextField
112-
113-
# The historical instance should not change creation/modification timestamps.
114-
field.auto_now = False
115-
field.auto_now_add = False
116-
117-
if field.primary_key or field.unique:
118-
# Unique fields can no longer be guaranteed unique,
119-
# but they should still be indexed for faster lookups.
120-
field.primary_key = False
121-
field._unique = False
122-
field.db_index = True
123-
field.serialize = True
124-
125105
for field in model._meta.fields:
126106
field = copy.copy(field)
127-
128107
if isinstance(field, models.ForeignKey):
129-
class CustomKey(type(field)):
130-
131-
def get_attname(self):
132-
return self.name
133-
134-
def do_related_class(self, other, cls):
135-
# this hooks into contribute_to_class() and this is
136-
# called specifically after the class_prepared signal
137-
to_field = copy.copy(self.rel.to._meta.pk)
138-
field = self
139-
if isinstance(to_field, models.AutoField):
140-
field.__class__ = models.IntegerField
141-
else:
142-
field.__class__ = to_field.__class__
143-
excluded_prefixes = ("_","__")
144-
excluded_attributes = (
145-
"rel",
146-
"creation_counter",
147-
"validators",
148-
"error_messages",
149-
"attname",
150-
"column",
151-
"help_text",
152-
"name",
153-
"model",
154-
"unique_for_year",
155-
"unique_for_date",
156-
"unique_for_month",
157-
"db_tablespace",
158-
"db_index",
159-
"db_column",
160-
"default",
161-
"auto_created",
162-
"null",
163-
"blank",
164-
)
165-
for key, val in to_field.__dict__.items():
166-
if (isinstance(key, basestring)
167-
and not key.startswith(excluded_prefixes)
168-
and not key in excluded_attributes):
169-
setattr(field, key, val)
170-
171-
customize_field(field)
172-
field.rel = None
173-
174-
def contribute_to_class(self, cls, name):
175-
# HACK: remove annoying descriptor (don't super())
176-
RelatedField.contribute_to_class(self, cls, name)
177-
178108
# Don't allow reverse relations.
179109
# ForeignKey knows best what datatype to use for the column
180110
# we'll used that as soon as it's finalized by copying rel.to
181-
field.__class__ = CustomKey
111+
field.__class__ = get_custom_fk_class(type(field))
182112
field.rel.related_name = '+'
183113
field.null = True
184114
field.blank = True
185-
186-
customize_field(field)
115+
transform_field(field)
187116
fields[field.name] = field
188-
189117
return fields
190118

191119
def get_extra_fields(self, model, fields):
192-
"""
193-
Returns a dictionary of fields that will be added to the historical
194-
record model, in addition to the ones returned by copy_fields below.
195-
"""
120+
"""Return dict of extra fields added to the historical record model"""
121+
122+
user_model = getattr(settings, 'AUTH_USER_MODEL', 'auth.User')
196123

197124
@models.permalink
198125
def revert_url(self):
@@ -207,8 +134,7 @@ def get_instance(self):
207134
return {
208135
'history_id': models.AutoField(primary_key=True),
209136
'history_date': models.DateTimeField(auto_now_add=True),
210-
'history_user': models.ForeignKey(
211-
getattr(settings, 'AUTH_USER_MODEL', 'auth.User'), null=True),
137+
'history_user': models.ForeignKey(user_model, null=True),
212138
'history_type': models.CharField(max_length=1, choices=(
213139
('+', 'Created'),
214140
('~', 'Changed'),
@@ -248,10 +174,87 @@ def create_historical_record(self, instance, type):
248174
manager.create(history_type=type, history_user=history_user, **attrs)
249175

250176

177+
def get_custom_fk_class(parent_type):
178+
class CustomForeignKey(parent_type):
179+
180+
def get_attname(self):
181+
return self.name
182+
183+
def do_related_class(self, other, cls):
184+
# this hooks into contribute_to_class() and this is
185+
# called specifically after the class_prepared signal
186+
to_field = copy.copy(self.rel.to._meta.pk)
187+
field = self
188+
if isinstance(to_field, models.AutoField):
189+
field.__class__ = models.IntegerField
190+
else:
191+
field.__class__ = to_field.__class__
192+
excluded_prefixes = ("_", "__")
193+
excluded_attributes = (
194+
"rel",
195+
"creation_counter",
196+
"validators",
197+
"error_messages",
198+
"attname",
199+
"column",
200+
"help_text",
201+
"name",
202+
"model",
203+
"unique_for_year",
204+
"unique_for_date",
205+
"unique_for_month",
206+
"db_tablespace",
207+
"db_index",
208+
"db_column",
209+
"default",
210+
"auto_created",
211+
"null",
212+
"blank",
213+
)
214+
for key, val in to_field.__dict__.items():
215+
if (isinstance(key, basestring)
216+
and not key.startswith(excluded_prefixes)
217+
and not key in excluded_attributes):
218+
setattr(field, key, val)
219+
transform_field(field)
220+
field.rel = None
221+
222+
def contribute_to_class(self, cls, name):
223+
# HACK: remove annoying descriptor (don't super())
224+
RelatedField.contribute_to_class(self, cls, name)
225+
226+
return CustomForeignKey
227+
228+
229+
def transform_field(field):
230+
"""Customize field appropriately for use in historical model"""
231+
field.name = field.attname
232+
if isinstance(field, models.AutoField):
233+
# The historical model gets its own AutoField, so any
234+
# existing one must be replaced with an IntegerField.
235+
field.__class__ = models.IntegerField
236+
elif isinstance(field, models.FileField):
237+
# Don't copy file, just path.
238+
field.__class__ = models.TextField
239+
240+
# Historical instance shouldn't change create/update timestamps
241+
field.auto_now = False
242+
field.auto_now_add = False
243+
244+
if field.primary_key or field.unique:
245+
# Unique fields can no longer be guaranteed unique,
246+
# but they should still be indexed for faster lookups.
247+
field.primary_key = False
248+
field._unique = False
249+
field.db_index = True
250+
field.serialize = True
251+
252+
251253
class HistoricalObjectDescriptor(object):
252254
def __init__(self, model):
253255
self.model = model
254256

255257
def __get__(self, instance, owner):
256-
values = (getattr(instance, f.attname) for f in self.model._meta.fields)
258+
values = (getattr(instance, f.attname)
259+
for f in self.model._meta.fields)
257260
return self.model(*values)

simple_history/tests/custom_user/models.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
class CustomUser(AbstractUser):
99
pass
1010

11-
admin.site.register(CustomUser, UserAdmin)
11+
admin.site.register(CustomUser, UserAdmin)

0 commit comments

Comments
 (0)