Skip to content

Commit 1ff39c2

Browse files
committed
Simplify the show_content_type_selection_widget template tag a bit
1 parent 2ac104d commit 1ff39c2

File tree

3 files changed

+27
-34
lines changed

3 files changed

+27
-34
lines changed

feincms/admin/item_editor.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ def get_extra_context(self, request):
134134
""" Return extra context parameters for add/change views. """
135135

136136
extra_context = {
137+
'request': request,
137138
'model': self.model,
138139
'available_templates': getattr(
139140
self.model, '_feincms_templates', ()),
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
{% load i18n %}
22
<select name="order-machine-add-select">
33
<option selected value>{% trans 'Insert new content:' %}</option>
4-
{% for v,n in ungrouped %}<option value="{{ v }}">{{ n|capfirst }}</option> {% endfor %}
5-
{% for label,cts in grouped.items %}
6-
<optgroup label="{{ label }}">
7-
{% for v,n in cts %}<option value="{{ v }}">{{ n|capfirst }}</option> {% endfor %}
8-
</optgroup>
9-
{% endfor %}
4+
{% for group, cts in types.items %}
5+
{% if group %}<optgroup label="{{ group }}">{% endif %}
6+
{% for v, n in cts %}
7+
<option value="{{ v }}">{{ n|capfirst }}</option>
8+
{% endfor %}
9+
{% if group %}</optgroup>{% endif %}
10+
{% endfor %}
1011
</select>

feincms/templatetags/feincms_admin_tags.py

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from __future__ import absolute_import, unicode_literals
22

3+
from collections import OrderedDict
4+
35
from django import template
46
from django.contrib.auth import get_permission_codename
57

@@ -62,31 +64,20 @@ def show_content_type_selection_widget(context, region):
6264
"""
6365
{% show_content_type_selection_widget region %}
6466
"""
65-
if 'request' in context:
66-
user = context['request'].user
67-
elif 'user' in context:
68-
user = context['user']
69-
else:
70-
user = None
71-
72-
grouped = {}
73-
ungrouped = []
74-
75-
if user:
76-
for ct in region._content_types:
77-
# Skip cts that we shouldn't be adding anyway
78-
opts = ct._meta
79-
perm = opts.app_label + "." + get_permission_codename('add', opts)
80-
if not user.has_perm(perm):
81-
continue
82-
83-
ct_info = (ct.__name__.lower(), ct._meta.verbose_name)
84-
if hasattr(ct, 'optgroup'):
85-
if ct.optgroup in grouped:
86-
grouped[ct.optgroup].append(ct_info)
87-
else:
88-
grouped[ct.optgroup] = [ct_info]
89-
else:
90-
ungrouped.append(ct_info)
91-
92-
return {'grouped': grouped, 'ungrouped': ungrouped}
67+
user = context['request'].user
68+
types = OrderedDict((None, []))
69+
70+
for ct in region._content_types:
71+
# Skip cts that we shouldn't be adding anyway
72+
opts = ct._meta
73+
perm = opts.app_label + "." + get_permission_codename('add', opts)
74+
if not user.has_perm(perm):
75+
continue
76+
77+
ct_info = (ct.__name__.lower(), ct._meta.verbose_name)
78+
types.setdefault(
79+
getattr(ct, 'optgroup', None),
80+
[],
81+
).append((ct.__name__.lower, ct._meta.verbose_name))
82+
83+
return {'types': types}

0 commit comments

Comments
 (0)