Skip to content

Commit 0f522f9

Browse files
author
Ross Mechanic
authored
GH-446: add black auto-formatter (#480)
* initial commit for adding black autoformatter * Accepting defeat on chaining style * Line length issue * Added checklist item for running make format
1 parent cc3a2c1 commit 0f522f9

40 files changed

+1414
-1258
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ charset = utf-8
88
indent_style = space
99
end_of_line = lf
1010
insert_final_newline = true
11-
max_line_length = 79
11+
max_line_length = 88
1212
trim_trailing_whitespace = true
1313

1414
[*.{py,rst,ini}]

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ install:
1717
- pip install -U coverage codecov
1818
- pip install -U flake8==3.6.0
1919
- pip install -U $DJANGO
20+
- if [[ $TRAVIS_PYTHON_VERSION == '3.6' ]]; then pip install black; fi
2021
- pip freeze
2122

2223
script:
2324
- flake8 simple_history
25+
- if [[ $TRAVIS_PYTHON_VERSION == '3.6' ]]; then black --check simple_history; fi
2426
- coverage run setup.py test
2527

2628
matrix:

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ Authors
7171
- Matheus Cansian (@mscansian)
7272
- Jim Gomez
7373
- Hanyin Zhang
74+
- James Muranga (@jamesmura)
7475

7576
Background
7677
==========

CONTRIBUTING.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,14 @@ To run tox and generate an HTML code coverage report (available in the
6363
To quickly run the tests against a single version of Python and Django (note: you must ``pip install django`` beforehand)::
6464

6565
python setup.py test
66+
67+
Code Formatting
68+
---------------
69+
We make use of `black`_ for code formatting.
70+
71+
.. _black: https://black.readthedocs.io/en/stable/installation_and_usage.html
72+
73+
Once it is installed you can make sure the code is properly formatted by running::
74+
75+
make format
76+

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,5 @@ release: dist
4343
gpg --detach-sign -a dist/*
4444
twine upload dist/*
4545

46+
format:
47+
black simple_history

PULL_REQUEST_TEMPLATE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
## Checklist:
2929
<!--- Go over all the following points, and put an `x` in all the boxes that apply. -->
3030
<!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! -->
31-
- [ ] My code follows the code style of this project.
31+
- [ ] I have run the `make format` command to format my code
3232
- [ ] My change requires a change to the documentation.
3333
- [ ] I have updated the documentation accordingly.
3434
- [ ] I have read the **CONTRIBUTING** document.

simple_history/__init__.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
from __future__ import unicode_literals
22

3-
__version__ = '2.5.1'
3+
__version__ = "2.5.1"
44

55

66
def register(
7-
model, app=None, manager_name='history', records_class=None,
8-
table_name=None, **records_config):
7+
model,
8+
app=None,
9+
manager_name="history",
10+
records_class=None,
11+
table_name=None,
12+
**records_config
13+
):
914
"""
1015
Create historical model for `model` and attach history manager to `model`.
1116

simple_history/admin.py

Lines changed: 70 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@
1515
from django.utils.text import capfirst
1616
from django.utils.translation import ugettext as _
1717

18-
USER_NATURAL_KEY = tuple(
19-
key.lower() for key in settings.AUTH_USER_MODEL.split('.', 1))
18+
USER_NATURAL_KEY = tuple(key.lower() for key in settings.AUTH_USER_MODEL.split(".", 1))
2019

21-
SIMPLE_HISTORY_EDIT = getattr(settings, 'SIMPLE_HISTORY_EDIT', False)
20+
SIMPLE_HISTORY_EDIT = getattr(settings, "SIMPLE_HISTORY_EDIT", False)
2221

2322

2423
class SimpleHistoryAdmin(admin.ModelAdmin):
@@ -32,9 +31,11 @@ def get_urls(self):
3231
opts = self.model._meta
3332
info = opts.app_label, opts.model_name
3433
history_urls = [
35-
url("^([^/]+)/history/([^/]+)/$",
34+
url(
35+
"^([^/]+)/history/([^/]+)/$",
3636
admin_site.admin_view(self.history_form_view),
37-
name='%s_%s_simple_history' % info),
37+
name="%s_%s_simple_history" % info,
38+
)
3839
]
3940
return history_urls + urls
4041

@@ -54,7 +55,7 @@ def history_view(self, request, object_id, extra_context=None):
5455
obj = self.get_queryset(request).get(**{pk_name: object_id})
5556
except model.DoesNotExist:
5657
try:
57-
obj = action_list.latest('history_date').instance
58+
obj = action_list.latest("history_date").instance
5859
except action_list.model.DoesNotExist:
5960
raise http.Http404
6061

@@ -66,57 +67,55 @@ def history_view(self, request, object_id, extra_context=None):
6667
value_for_entry = getattr(self, history_list_entry, None)
6768
if value_for_entry and callable(value_for_entry):
6869
for list_entry in action_list:
69-
setattr(list_entry, history_list_entry,
70-
value_for_entry(list_entry))
70+
setattr(list_entry, history_list_entry, value_for_entry(list_entry))
7171

72-
content_type = ContentType.objects.get_by_natural_key(
73-
*USER_NATURAL_KEY)
74-
admin_user_view = 'admin:%s_%s_change' % (content_type.app_label,
75-
content_type.model)
72+
content_type = ContentType.objects.get_by_natural_key(*USER_NATURAL_KEY)
73+
admin_user_view = "admin:%s_%s_change" % (
74+
content_type.app_label,
75+
content_type.model,
76+
)
7677
context = {
77-
'title': _('Change history: %s') % force_text(obj),
78-
'action_list': action_list,
79-
'module_name': capfirst(force_text(opts.verbose_name_plural)),
80-
'object': obj,
81-
'root_path': getattr(self.admin_site, 'root_path', None),
82-
'app_label': app_label,
83-
'opts': opts,
84-
'admin_user_view': admin_user_view,
85-
'history_list_display': history_list_display,
78+
"title": _("Change history: %s") % force_text(obj),
79+
"action_list": action_list,
80+
"module_name": capfirst(force_text(opts.verbose_name_plural)),
81+
"object": obj,
82+
"root_path": getattr(self.admin_site, "root_path", None),
83+
"app_label": app_label,
84+
"opts": opts,
85+
"admin_user_view": admin_user_view,
86+
"history_list_display": history_list_display,
8687
}
8788
context.update(self.admin_site.each_context(request))
8889
context.update(extra_context or {})
8990
extra_kwargs = {}
90-
return render(request, self.object_history_template, context,
91-
**extra_kwargs)
91+
return render(request, self.object_history_template, context, **extra_kwargs)
9292

9393
def response_change(self, request, obj):
94-
if '_change_history' in request.POST and SIMPLE_HISTORY_EDIT:
94+
if "_change_history" in request.POST and SIMPLE_HISTORY_EDIT:
9595
verbose_name = obj._meta.verbose_name
9696

9797
msg = _('The %(name)s "%(obj)s" was changed successfully.') % {
98-
'name': force_text(verbose_name),
99-
'obj': force_text(obj)
98+
"name": force_text(verbose_name),
99+
"obj": force_text(obj),
100100
}
101101

102102
self.message_user(
103-
request, "%s - %s" % (msg, _("You may edit it again below")))
103+
request, "%s - %s" % (msg, _("You may edit it again below"))
104+
)
104105

105106
return http.HttpResponseRedirect(request.path)
106107
else:
107-
return super(SimpleHistoryAdmin, self).response_change(
108-
request, obj)
108+
return super(SimpleHistoryAdmin, self).response_change(request, obj)
109109

110110
def history_form_view(self, request, object_id, version_id):
111111
request.current_app = self.admin_site.name
112112
original_opts = self.model._meta
113113
model = getattr(
114-
self.model,
115-
self.model._meta.simple_history_manager_attribute).model
116-
obj = get_object_or_404(model, **{
117-
original_opts.pk.attname: object_id,
118-
'history_id': version_id,
119-
}).instance
114+
self.model, self.model._meta.simple_history_manager_attribute
115+
).model
116+
obj = get_object_or_404(
117+
model, **{original_opts.pk.attname: object_id, "history_id": version_id}
118+
).instance
120119
obj._state.adding = False
121120

122121
if not self.has_change_permission(request, obj):
@@ -127,21 +126,23 @@ def history_form_view(self, request, object_id, version_id):
127126
else:
128127
change_history = False
129128

130-
if '_change_history' in request.POST and SIMPLE_HISTORY_EDIT:
129+
if "_change_history" in request.POST and SIMPLE_HISTORY_EDIT:
131130
obj = obj.history.get(pk=version_id).instance
132131

133132
formsets = []
134133
form_class = self.get_form(request, obj)
135-
if request.method == 'POST':
134+
if request.method == "POST":
136135
form = form_class(request.POST, request.FILES, instance=obj)
137136
if form.is_valid():
138137
new_object = self.save_form(request, form, change=True)
139138
self.save_model(request, new_object, form, change=True)
140139
form.save_m2m()
141140

142-
self.log_change(request, new_object,
143-
self.construct_change_message(
144-
request, form, formsets))
141+
self.log_change(
142+
request,
143+
new_object,
144+
self.construct_change_message(request, form, formsets),
145+
)
145146
return self.response_change(request, new_object)
146147

147148
else:
@@ -158,42 +159,39 @@ def history_form_view(self, request, object_id, version_id):
158159
model_name = original_opts.model_name
159160
url_triplet = self.admin_site.name, original_opts.app_label, model_name
160161
context = {
161-
'title': _('Revert %s') % force_text(obj),
162-
'adminform': admin_form,
163-
'object_id': object_id,
164-
'original': obj,
165-
'is_popup': False,
166-
'media': mark_safe(self.media + admin_form.media),
167-
'errors': helpers.AdminErrorList(form, formsets),
168-
'app_label': original_opts.app_label,
169-
'original_opts': original_opts,
170-
'changelist_url': reverse('%s:%s_%s_changelist' % url_triplet),
171-
'change_url': reverse('%s:%s_%s_change' % url_triplet,
172-
args=(obj.pk,)),
173-
'history_url': reverse('%s:%s_%s_history' % url_triplet,
174-
args=(obj.pk,)),
175-
'change_history': change_history,
176-
162+
"title": _("Revert %s") % force_text(obj),
163+
"adminform": admin_form,
164+
"object_id": object_id,
165+
"original": obj,
166+
"is_popup": False,
167+
"media": mark_safe(self.media + admin_form.media),
168+
"errors": helpers.AdminErrorList(form, formsets),
169+
"app_label": original_opts.app_label,
170+
"original_opts": original_opts,
171+
"changelist_url": reverse("%s:%s_%s_changelist" % url_triplet),
172+
"change_url": reverse("%s:%s_%s_change" % url_triplet, args=(obj.pk,)),
173+
"history_url": reverse("%s:%s_%s_history" % url_triplet, args=(obj.pk,)),
174+
"change_history": change_history,
177175
# Context variables copied from render_change_form
178-
'add': False,
179-
'change': True,
180-
'has_add_permission': self.has_add_permission(request),
181-
'has_change_permission': self.has_change_permission(request, obj),
182-
'has_delete_permission': self.has_delete_permission(request, obj),
183-
'has_file_field': True,
184-
'has_absolute_url': False,
185-
'form_url': '',
186-
'opts': model._meta,
187-
'content_type_id': ContentType.objects.get_for_model(
188-
self.model).id,
189-
'save_as': self.save_as,
190-
'save_on_top': self.save_on_top,
191-
'root_path': getattr(self.admin_site, 'root_path', None),
176+
"add": False,
177+
"change": True,
178+
"has_add_permission": self.has_add_permission(request),
179+
"has_change_permission": self.has_change_permission(request, obj),
180+
"has_delete_permission": self.has_delete_permission(request, obj),
181+
"has_file_field": True,
182+
"has_absolute_url": False,
183+
"form_url": "",
184+
"opts": model._meta,
185+
"content_type_id": ContentType.objects.get_for_model(self.model).id,
186+
"save_as": self.save_as,
187+
"save_on_top": self.save_on_top,
188+
"root_path": getattr(self.admin_site, "root_path", None),
192189
}
193190
context.update(self.admin_site.each_context(request))
194191
extra_kwargs = {}
195-
return render(request, self.object_history_form_template, context,
196-
**extra_kwargs)
192+
return render(
193+
request, self.object_history_form_template, context, **extra_kwargs
194+
)
197195

198196
def save_model(self, request, obj, form, change):
199197
"""Set special model attribute to user for reference after save"""

simple_history/exceptions.py

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

66
class MultipleRegistrationsError(Exception):
77
"""The model has been registered to have history tracking more than once"""
8+
89
pass
910

1011

1112
class NotHistoricalModelError(TypeError):
1213
"""No related history model found."""
14+
1315
pass

0 commit comments

Comments
 (0)