|
1 | 1 | from __future__ import absolute_import, unicode_literals |
2 | 2 |
|
| 3 | +from collections import OrderedDict |
| 4 | + |
3 | 5 | from django import template |
4 | 6 | from django.contrib.auth import get_permission_codename |
5 | 7 |
|
@@ -62,31 +64,20 @@ def show_content_type_selection_widget(context, region): |
62 | 64 | """ |
63 | 65 | {% show_content_type_selection_widget region %} |
64 | 66 | """ |
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