Skip to content

Commit fa35905

Browse files
committed
'Refactored by Sourcery'
1 parent e923d04 commit fa35905

File tree

16 files changed

+139
-156
lines changed

16 files changed

+139
-156
lines changed

feincms/admin/tree_editor.py

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,7 @@ def django_boolean_icon(field_val, alt_text=None, title=None):
4949
# Origin: contrib/admin/templatetags/admin_list.py
5050
BOOLEAN_MAPPING = {True: "yes", False: "no", None: "unknown"}
5151
alt_text = alt_text or BOOLEAN_MAPPING[field_val]
52-
if title is not None:
53-
title = 'title="%s" ' % title
54-
else:
55-
title = ""
52+
title = 'title="%s" ' % title if title is not None else ""
5653
icon_url = static("feincms/img/icon-%s.gif" % BOOLEAN_MAPPING[field_val])
5754
return mark_safe('<img src="%s" alt="%s" %s/>' % (icon_url, alt_text, title))
5855

@@ -533,34 +530,33 @@ def delete_selected_tree(self, modeladmin, request, queryset):
533530
trigger the post_delete hooks.)
534531
"""
535532
# If this is True, the confirmation page has been displayed
536-
if request.POST.get("post"):
537-
n = 0
538-
# TODO: The disable_mptt_updates / rebuild is a work around
539-
# for what seems to be a mptt problem when deleting items
540-
# in a loop. Revisit this, there should be a better solution.
541-
with queryset.model.objects.disable_mptt_updates():
542-
for obj in queryset:
543-
if self.has_delete_permission(request, obj):
544-
obj.delete()
545-
n += 1
546-
obj_display = force_text(obj)
547-
self.log_deletion(request, obj, obj_display)
548-
else:
549-
logger.warning(
550-
'Denied delete request by "%s" for object #%s',
551-
request.user,
552-
obj.id,
553-
)
554-
if n > 0:
555-
queryset.model.objects.rebuild()
556-
self.message_user(
557-
request, _("Successfully deleted %(count)d items.") % {"count": n}
558-
)
559-
# Return None to display the change list page again
560-
return None
561-
else:
533+
if not request.POST.get("post"):
562534
# (ab)using the built-in action to display the confirmation page
563535
return delete_selected(self, request, queryset)
536+
n = 0
537+
# TODO: The disable_mptt_updates / rebuild is a work around
538+
# for what seems to be a mptt problem when deleting items
539+
# in a loop. Revisit this, there should be a better solution.
540+
with queryset.model.objects.disable_mptt_updates():
541+
for obj in queryset:
542+
if self.has_delete_permission(request, obj):
543+
obj.delete()
544+
n += 1
545+
obj_display = force_text(obj)
546+
self.log_deletion(request, obj, obj_display)
547+
else:
548+
logger.warning(
549+
'Denied delete request by "%s" for object #%s',
550+
request.user,
551+
obj.id,
552+
)
553+
if n > 0:
554+
queryset.model.objects.rebuild()
555+
self.message_user(
556+
request, _("Successfully deleted %(count)d items.") % {"count": n}
557+
)
558+
# Return None to display the change list page again
559+
return None
564560

565561
def get_actions(self, request):
566562
actions = super(TreeEditor, self).get_actions(request)

feincms/content/application/models.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ def _update_response_headers(self, request, response, headers):
414414
# them and let the client worry about that.
415415
cc_headers = set(("must-revalidate",))
416416
for x in (cc.split(",") for cc in headers.get("Cache-Control", ())):
417-
cc_headers |= set((s.strip() for s in x))
417+
cc_headers |= {s.strip() for s in x}
418418

419419
if len(cc_headers):
420420
response["Cache-Control"] = ", ".join(cc_headers)
@@ -423,12 +423,12 @@ def _update_response_headers(self, request, response, headers):
423423

424424
# Check all Last-Modified headers, choose the latest one
425425
lm_list = [parsedate(x) for x in headers.get("Last-Modified", ())]
426-
if len(lm_list) > 0:
426+
if lm_list:
427427
response["Last-Modified"] = http_date(mktime(max(lm_list)))
428428

429429
# Check all Expires headers, choose the earliest one
430430
lm_list = [parsedate(x) for x in headers.get("Expires", ())]
431-
if len(lm_list) > 0:
431+
if lm_list:
432432
response["Expires"] = http_date(mktime(min(lm_list)))
433433

434434
@classmethod

feincms/content/section/models.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,7 @@ def get_queryset(cls, filter_args):
7878
return cls.objects.select_related("parent", "mediafile").filter(filter_args)
7979

8080
def render(self, **kwargs):
81-
if self.mediafile:
82-
mediafile_type = self.mediafile.type
83-
else:
84-
mediafile_type = "nomedia"
85-
81+
mediafile_type = self.mediafile.type if self.mediafile else "nomedia"
8682
return ct_render_to_string(
8783
[
8884
"content/section/%s_%s.html" % (mediafile_type, self.type),

feincms/extensions/base.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,7 @@ def register_extensions(cls, *extensions):
5555
elif hasattr(extension, "register"):
5656
extension = extension.register
5757

58-
elif hasattr(extension, "__call__"):
59-
pass
60-
61-
else:
58+
elif not hasattr(extension, "__call__"):
6259
raise ImproperlyConfigured(
6360
"%s is not a valid extension for %s" % (ext, cls.__name__)
6461
)

feincms/extensions/ct_tracker.py

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -46,36 +46,35 @@ def _fetch_content_type_counts(self):
4646
empty _ct_inventory.
4747
"""
4848

49-
if "counts" not in self._cache:
50-
if (
51-
self.item._ct_inventory
52-
and self.item._ct_inventory.get("_version_", -1) == INVENTORY_VERSION
53-
):
54-
55-
try:
56-
self._cache["counts"] = self._from_inventory(
57-
self.item._ct_inventory
58-
)
59-
except KeyError:
60-
# It's possible that the inventory does not fit together
61-
# with the current models anymore, f.e. because a content
62-
# type has been removed.
63-
pass
64-
65-
if "counts" not in self._cache:
66-
super(TrackerContentProxy, self)._fetch_content_type_counts()
67-
68-
self.item._ct_inventory = self._to_inventory(self._cache["counts"])
69-
70-
self.item.__class__.objects.filter(id=self.item.id).update(
71-
_ct_inventory=self.item._ct_inventory
49+
if "counts" not in self._cache and (
50+
self.item._ct_inventory
51+
and self.item._ct_inventory.get("_version_", -1) == INVENTORY_VERSION
52+
):
53+
54+
try:
55+
self._cache["counts"] = self._from_inventory(
56+
self.item._ct_inventory
7257
)
58+
except KeyError:
59+
# It's possible that the inventory does not fit together
60+
# with the current models anymore, f.e. because a content
61+
# type has been removed.
62+
pass
63+
64+
if "counts" not in self._cache:
65+
super(TrackerContentProxy, self)._fetch_content_type_counts()
66+
67+
self.item._ct_inventory = self._to_inventory(self._cache["counts"])
68+
69+
self.item.__class__.objects.filter(id=self.item.id).update(
70+
_ct_inventory=self.item._ct_inventory
71+
)
7372

74-
# Run post save handler by hand
75-
if hasattr(self.item, "get_descendants"):
76-
self.item.get_descendants(include_self=False).update(
77-
_ct_inventory=None
78-
)
73+
# Run post save handler by hand
74+
if hasattr(self.item, "get_descendants"):
75+
self.item.get_descendants(include_self=False).update(
76+
_ct_inventory=None
77+
)
7978
return self._cache["counts"]
8079

8180
def _translation_map(self):
@@ -104,19 +103,20 @@ def _from_inventory(self, inventory):
104103

105104
map = self._translation_map()
106105

107-
return dict(
108-
(region, [(pk, map[-ct]) for pk, ct in items])
106+
return {
107+
region: [(pk, map[-ct]) for pk, ct in items]
109108
for region, items in inventory.items()
110109
if region != "_version_"
111-
)
110+
}
112111

113112
def _to_inventory(self, counts):
114113
map = self._translation_map()
115114

116-
inventory = dict(
117-
(region, [(pk, map[ct]) for pk, ct in items])
115+
inventory = {
116+
region: [(pk, map[ct]) for pk, ct in items]
118117
for region, items in counts.items()
119-
)
118+
}
119+
120120
inventory["_version_"] = INVENTORY_VERSION
121121
return inventory
122122

feincms/extensions/translations.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@ def user_has_language_set(request):
5555
and request.session.get(LANGUAGE_SESSION_KEY) is not None
5656
):
5757
return True
58-
if LANGUAGE_COOKIE_NAME in request.COOKIES:
59-
return True
60-
return False
58+
return LANGUAGE_COOKIE_NAME in request.COOKIES
6159

6260

6361
# ------------------------------------------------------------------------

feincms/models.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,11 @@ def _fetch_content_type_counts(self):
137137
if "counts" not in self._cache:
138138
counts = self._fetch_content_type_count_helper(self.item.pk)
139139

140-
empty_inherited_regions = set()
141-
for region in self.item.template.regions:
142-
if region.inherited and not counts.get(region.key):
143-
empty_inherited_regions.add(region.key)
140+
empty_inherited_regions = {
141+
region.key
142+
for region in self.item.template.regions
143+
if region.inherited and not counts.get(region.key)
144+
}
144145

145146
if empty_inherited_regions:
146147
for parent in self._inherit_from():
@@ -240,10 +241,11 @@ def _fetch_regions(self):
240241
for instance in content_list:
241242
contents.setdefault(instance.region, []).append(instance)
242243

243-
self._cache["regions"] = dict(
244-
(region, sorted(instances, key=lambda c: c.ordering))
244+
self._cache["regions"] = {
245+
region: sorted(instances, key=lambda c: c.ordering)
245246
for region, instances in contents.items()
246-
)
247+
}
248+
247249

248250
return self._cache["regions"]
249251

feincms/module/medialibrary/models.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,11 @@ def save(self, *args, **kwargs):
214214

215215
# User uploaded a new file. Try to get rid of the old file in
216216
# storage, to avoid having orphaned files hanging around.
217-
if getattr(self, "_original_file_name", None):
218-
if self.file.name != self._original_file_name:
219-
self.delete_mediafile(self._original_file_name)
217+
if (
218+
getattr(self, "_original_file_name", None)
219+
and self.file.name != self._original_file_name
220+
):
221+
self.delete_mediafile(self._original_file_name)
220222

221223
self.purge_translation_cache()
222224

feincms/module/page/modeladmins.py

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,13 @@ def __init__(self, model, admin_site):
9696

9797
def get_readonly_fields(self, request, obj=None):
9898
readonly = super(PageAdmin, self).get_readonly_fields(request, obj=obj)
99-
if not settings.FEINCMS_SINGLETON_TEMPLATE_CHANGE_ALLOWED:
100-
if obj and obj.template and obj.template.singleton:
101-
return tuple(readonly) + ("template_key",)
99+
if (
100+
not settings.FEINCMS_SINGLETON_TEMPLATE_CHANGE_ALLOWED
101+
and obj
102+
and obj.template
103+
and obj.template.singleton
104+
):
105+
return tuple(readonly) + ("template_key",)
102106
return readonly
103107

104108
def get_form(self, *args, **kwargs):
@@ -114,20 +118,19 @@ def _actions_column(self, page):
114118
)
115119
actions = super(PageAdmin, self)._actions_column(page)
116120

117-
if addable:
118-
if not page.template.enforce_leaf:
119-
actions.insert(
120-
0,
121-
'<a href="add/?parent=%s" title="%s">'
122-
'<img src="%s" alt="%s" />'
123-
"</a>"
124-
% (
125-
page.pk,
126-
_("Add child page"),
127-
static("feincms/img/icon_addlink.gif"),
128-
_("Add child page"),
129-
),
130-
)
121+
if addable and not page.template.enforce_leaf:
122+
actions.insert(
123+
0,
124+
'<a href="add/?parent=%s" title="%s">'
125+
'<img src="%s" alt="%s" />'
126+
"</a>"
127+
% (
128+
page.pk,
129+
_("Add child page"),
130+
static("feincms/img/icon_addlink.gif"),
131+
_("Add child page"),
132+
),
133+
)
131134
actions.insert(
132135
0,
133136
'<a href="%s" title="%s">'
@@ -216,9 +219,12 @@ def change_view(self, request, object_id, **kwargs):
216219
return HttpResponseRedirect(reverse("admin:page_page_changelist"))
217220

218221
def has_delete_permission(self, request, obj=None):
219-
if not settings.FEINCMS_SINGLETON_TEMPLATE_DELETION_ALLOWED:
220-
if obj and obj.template.singleton:
221-
return False
222+
if (
223+
not settings.FEINCMS_SINGLETON_TEMPLATE_DELETION_ALLOWED
224+
and obj
225+
and obj.template.singleton
226+
):
227+
return False
222228
return super(PageAdmin, self).has_delete_permission(request, obj=obj)
223229

224230
def changelist_view(self, request, *args, **kwargs):
@@ -260,10 +266,10 @@ def is_visible_recursive(self, page):
260266
self.model.objects.active().values_list("id", flat=True)
261267
)
262268

263-
retval = []
264-
for c in page.get_descendants(include_self=True):
265-
retval.append(self.is_visible_admin(c))
266-
return retval
269+
return [
270+
self.is_visible_admin(c)
271+
for c in page.get_descendants(include_self=True)
272+
]
267273

268274
is_visible_admin.editable_boolean_result = is_visible_recursive
269275

feincms/module/page/models.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -301,15 +301,17 @@ def save(self, *args, **kwargs):
301301
save.alters_data = True
302302

303303
def delete(self, *args, **kwargs):
304-
if not settings.FEINCMS_SINGLETON_TEMPLATE_DELETION_ALLOWED:
305-
if self.template.singleton:
306-
raise PermissionDenied(
307-
_(
308-
"This %(page_class)s uses a singleton template, and "
309-
"FEINCMS_SINGLETON_TEMPLATE_DELETION_ALLOWED=False"
310-
% {"page_class": self._meta.verbose_name}
311-
)
304+
if (
305+
not settings.FEINCMS_SINGLETON_TEMPLATE_DELETION_ALLOWED
306+
and self.template.singleton
307+
):
308+
raise PermissionDenied(
309+
_(
310+
"This %(page_class)s uses a singleton template, and "
311+
"FEINCMS_SINGLETON_TEMPLATE_DELETION_ALLOWED=False"
312+
% {"page_class": self._meta.verbose_name}
312313
)
314+
)
313315
super(BasePage, self).delete(*args, **kwargs)
314316

315317
delete.alters_data = True

0 commit comments

Comments
 (0)