From dfbe59076733a4c3f4324e7a0082c67ccdb7de4d Mon Sep 17 00:00:00 2001 From: "Alexander A. Sosnovskiy" Date: Sun, 23 Sep 2012 21:37:58 +0300 Subject: [PATCH] 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) +