From dfbe59076733a4c3f4324e7a0082c67ccdb7de4d Mon Sep 17 00:00:00 2001 From: "Alexander A. Sosnovskiy" Date: Sun, 23 Sep 2012 21:37:58 +0300 Subject: [PATCH 1/4] Templates can be overriden now. --- adminsortable/admin.py | 18 ++++++--- .../templates/adminsortable/change_form.html | 2 +- .../templatetags/adminsortable_tags.py | 40 +++++++++++-------- 3 files changed, 38 insertions(+), 22 deletions(-) diff --git a/adminsortable/admin.py b/adminsortable/admin.py index 2ee3ba8..aa3a239 100644 --- a/adminsortable/admin.py +++ b/adminsortable/admin.py @@ -16,8 +16,16 @@ STATIC_URL = settings.STATIC_URL class SortableAdmin(ModelAdmin): + """ + + """ ordering = ('order', 'id') + sortable_change_list_with_sort_link_template = 'adminsortable/change_list_with_sort_link.html' + sortable_change_form_template = 'adminsortable/change_form.html' + sortable_change_list_template = 'adminsortable/change_list.html' + sortable_javascript_includes_template = 'adminsortable/shared/javascript_includes.html' + class Meta: abstract = True @@ -118,7 +126,7 @@ class SortableAdmin(ModelAdmin): 'sortable_by_class_is_sortable' : sortable_by_class_is_sortable, 'sortable_by_class_display_name' : sortable_by_class_display_name } - return render(request, 'adminsortable/change_list.html', context) + return render(request, self.sortable_change_list_template, context) def changelist_view(self, request, extra_context=None): """ @@ -127,13 +135,14 @@ class SortableAdmin(ModelAdmin): block to take people to the view to change the sorting. """ if self.model.is_sortable(): - self.change_list_template = 'adminsortable/change_list_with_sort_link.html' + self.change_list_template = self.sortable_change_list_with_sort_link_template return super(SortableAdmin, self).changelist_view(request, extra_context=extra_context) def change_view(self, request, object_id, extra_context=None): if self.has_sortable_tabular_inlines or self.has_sortable_stacked_inlines: - self.change_form_template = 'adminsortable/change_form.html' + self.change_form_template = self.sortable_change_form_template extra_context = { + 'sortable_javascript_includes_template': self.sortable_javascript_includes_template, 'has_sortable_tabular_inlines' : self.has_sortable_tabular_inlines, 'has_sortable_stacked_inlines' : self.has_sortable_stacked_inlines } @@ -170,8 +179,7 @@ class SortableAdmin(ModelAdmin): pass else: response = {'objects_sorted' : False} - return HttpResponse(json.dumps(response, ensure_ascii=False), - mimetype='application/json') + return HttpResponse(json.dumps(response, ensure_ascii=False), mimetype='application/json') class SortableInlineBase(InlineModelAdmin): diff --git a/adminsortable/templates/adminsortable/change_form.html b/adminsortable/templates/adminsortable/change_form.html index e8f0c17..8eaa2f4 100644 --- a/adminsortable/templates/adminsortable/change_form.html +++ b/adminsortable/templates/adminsortable/change_form.html @@ -7,7 +7,7 @@ {% url 'admin:jsi18n' as jsi18nurl %} {% if has_sortable_tabular_inlines or has_sortable_stacked_inlines %} - {% include 'adminsortable/shared/javascript_includes.html' %} + {% include sortable_javascript_includes_template %} {% endif %} {% if has_sortable_tabular_inlines %} diff --git a/adminsortable/templatetags/adminsortable_tags.py b/adminsortable/templatetags/adminsortable_tags.py index ca53798..9bcbd73 100644 --- a/adminsortable/templatetags/adminsortable_tags.py +++ b/adminsortable/templatetags/adminsortable_tags.py @@ -3,25 +3,33 @@ from django import template register = template.Library() -@register.inclusion_tag('adminsortable/shared/objects.html', takes_context=True) -def render_sortable_objects(context, objects): - return {'objects' : objects} +@register.simple_tag(takes_context=True) +def render_sortable_objects(context, objects, + sortable_objects_template='adminsortable/shared/objects.html'): + context.update({'objects': objects}) + tmpl = template.loader.get_template(sortable_objects_template) + return tmpl.render(context) -@register.inclusion_tag('adminsortable/shared/nested_objects.html', takes_context=True) -def render_nested_sortable_objects(context, objects, group_expression): - group_expression = context.get('group_expression') - sortable_on_class = context.get('sortable_on_class') - return {'objects' : objects, 'group_expression' : group_expression, - 'sortable_on_class' : sortable_on_class, - 'sortable_by_class_is_sortable' : context.get('sortable_by_class_is_sortable')} +@register.simple_tag(takes_context=True) +def render_nested_sortable_objects(context, objects, group_expression, + sortable_nested_objects_template = 'adminsortable/shared/nested_objects.html'): + context.update({'objects': objects, 'group_expression': group_expression}) + tmpl = template.loader.get_template(sortable_nested_objects_template) + return tmpl.render(context) -@register.inclusion_tag('adminsortable/shared/list_items.html', takes_context=True) -def render_list_items(context, list_objects): - return {'list_objects' : list_objects} +@register.simple_tag(takes_context=True) +def render_list_items(context, list_objects, + sortable_list_items_template='adminsortable/shared/list_items.html'): + context.update({'list_objects': list_objects}) + tmpl = template.loader.get_template(sortable_list_items_template) + return tmpl.render(context) -@register.inclusion_tag('adminsortable/shared/object_rep.html', takes_context=True) -def render_object_rep(context, object): - return {'object' : object} +@register.simple_tag(takes_context=True) +def render_object_rep(context, obj, sortable_object_rep_template='adminsortable/shared/object_rep.html'): + context.update({'object': obj}) + tmpl = template.loader.get_template(sortable_object_rep_template) + return tmpl.render(context) + From 22cd63a4ba34a58dee8010d39025c477c78dca4b Mon Sep 17 00:00:00 2001 From: "Alexander A. Sosnovskiy" Date: Mon, 24 Sep 2012 12:16:20 +0300 Subject: [PATCH 2/4] It was impossible to override sortable_javascript_includes_template. Fixed. --- adminsortable/admin.py | 6 +++--- adminsortable/templates/adminsortable/change_list.html | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/adminsortable/admin.py b/adminsortable/admin.py index aa3a239..9f11125 100644 --- a/adminsortable/admin.py +++ b/adminsortable/admin.py @@ -124,7 +124,8 @@ class SortableAdmin(ModelAdmin): 'group_expression' : sortable_by_expression, 'sortable_by_class' : sortable_by_class, 'sortable_by_class_is_sortable' : sortable_by_class_is_sortable, - 'sortable_by_class_display_name' : sortable_by_class_display_name + 'sortable_by_class_display_name' : sortable_by_class_display_name, + 'sortable_javascript_includes_template': self.sortable_javascript_includes_template } return render(request, self.sortable_change_list_template, context) @@ -173,9 +174,8 @@ class SortableAdmin(ModelAdmin): setattr(obj, 'order', start_index) obj.save() start_index += step - response = {'objects_sorted' : True} - except (Key, IndexError, klass.DoesNotExist, AttributeError): + except (KeyError, IndexError, klass.DoesNotExist, AttributeError): pass else: response = {'objects_sorted' : False} diff --git a/adminsortable/templates/adminsortable/change_list.html b/adminsortable/templates/adminsortable/change_list.html index 10a270e..2c88beb 100644 --- a/adminsortable/templates/adminsortable/change_list.html +++ b/adminsortable/templates/adminsortable/change_list.html @@ -8,7 +8,7 @@ {% block extrahead %} {{ block.super }} - {% include 'adminsortable/shared/javascript_includes.html' %} + {% include sortable_javascript_includes_template %} {% endblock %} {% block breadcrumbs %} From 1c2964c566e8c0573c5db1c503c757637b6390a3 Mon Sep 17 00:00:00 2001 From: "Alexander A. Sosnovskiy" Date: Mon, 24 Sep 2012 15:12:07 +0300 Subject: [PATCH 3/4] Unable to sort in more than one application in project - fixed. --- adminsortable/admin.py | 4 ++-- .../templates/adminsortable/edit_inline/stacked.html | 4 ++-- .../templates/adminsortable/edit_inline/tabular.html | 4 ++-- adminsortable/templates/adminsortable/shared/object_rep.html | 4 +++- adminsortable/templatetags/adminsortable_tags.py | 5 +++++ 5 files changed, 14 insertions(+), 7 deletions(-) diff --git a/adminsortable/admin.py b/adminsortable/admin.py index 9f11125..e4d5212 100644 --- a/adminsortable/admin.py +++ b/adminsortable/admin.py @@ -55,9 +55,9 @@ class SortableAdmin(ModelAdmin): admin_urls = patterns('', url(r'^sorting/do-sorting/(?P\d+)/$', self.admin_site.admin_view(self.do_sorting_view), - name='admin_do_sorting'), #this view changes the order + name=('%s_do_sorting' % self.model._meta.app_label)), #this view changes the order url(r'^sort/$', self.admin_site.admin_view(self.sort_view), - name='admin_sort'), #this view shows a link to the drag-and-drop view + name=('%s_sort' % self.model._meta.app_label)), #this view shows a link to the drag-and-drop view ) return admin_urls + urls diff --git a/adminsortable/templates/adminsortable/edit_inline/stacked.html b/adminsortable/templates/adminsortable/edit_inline/stacked.html index bb930b3..9a62bf8 100644 --- a/adminsortable/templates/adminsortable/edit_inline/stacked.html +++ b/adminsortable/templates/adminsortable/edit_inline/stacked.html @@ -1,4 +1,4 @@ -{% load i18n adminmedia %} +{% load i18n adminmedia adminsortable_tags %}

{{ inline_admin_formset.opts.verbose_name_plural|title }} {% if inline_admin_formset.opts.is_sortable %} - drag and drop to change order{% endif %}

{{ inline_admin_formset.formset.management_form }} @@ -17,7 +17,7 @@ {% if inline_admin_form.has_auto_field %}{{ inline_admin_form.pk_field.field }}{% endif %} {{ inline_admin_form.fk_field.field }} {% if inline_admin_form.original %} - + {% endif %}
{% endfor %} diff --git a/adminsortable/templates/adminsortable/edit_inline/tabular.html b/adminsortable/templates/adminsortable/edit_inline/tabular.html index e4f2b77..eed1365 100644 --- a/adminsortable/templates/adminsortable/edit_inline/tabular.html +++ b/adminsortable/templates/adminsortable/edit_inline/tabular.html @@ -1,4 +1,4 @@ -{% load i18n adminmedia admin_modify %} +{% load i18n adminmedia admin_modify adminsortable_tags %}