Ticket #17515: filterspec_custom_templates_0.1.diff

File filterspec_custom_templates_0.1.diff, 5.4 KB (added by Stefano Apostolico, 13 years ago)

patch with docs and tests also renamed template attribute as 'template_name'

  • docs/ref/contrib/admin/index.txt

     
    695695                  ('is_staff', BooleanFieldListFilter),
    696696              )
    697697
    698       .. note::
    699698
    700           The ``FieldListFilter`` API is currently considered internal
    701           and prone to refactoring.
     699    .. note::
    702700
     701        .. versionadded:: 1.4
     702
     703        It's possible customize the template used to display the filter, for example::
     704
     705            class AdvancedDecadeBornListFilter(DecadeBornListFilter):
     706                template_name = "custom_template.html"
     707
     708
     709
     710    .. note::
     711
     712      The ``FieldListFilter`` API is currently considered internal
     713      and prone to refactoring.
     714
    703715.. attribute:: ModelAdmin.list_max_show_all
    704716
    705717    .. versionadded:: 1.4
     
    20272039:class:`ModelAdmin` instances described above. The ``opts`` variable can be any
    20282040object which has an ``app_label`` and ``module_name`` and is usually supplied
    20292041by the admin views for the current model.
     2042
     2043
  • tests/regressiontests/admin_views/admin.py

     
    1313from django.db import models
    1414from django.forms.models import BaseModelFormSet
    1515from django.http import HttpResponse
     16from django.contrib.admin import BooleanFieldListFilter
    1617
    1718from .models import (Article, Chapter, Account, Media, Child, Parent, Picture,
    1819    Widget, DooHickey, Grommet, Whatsit, FancyDoodad, Category, Link,
     
    2526    CoverLetter, Story, OtherStory, Book, Promo, ChapterXtra1, Pizza, Topping,
    2627    Album, Question, Answer, ComplexSortedPerson, PrePopulatedPostLargeSlug,
    2728    AdminOrderedField, AdminOrderedModelMethod, AdminOrderedAdminMethod,
    28     AdminOrderedCallable, Report)
     29    AdminOrderedCallable, Report, Color2)
    2930
    3031
    3132def callable_year(dt_value):
     
    512513                name='cable_extra'),
    513514        )
    514515
     516class CustomTemplateBooleanFieldListFilter(BooleanFieldListFilter):
     517    template_name = 'custom_filter_template.html'
     518
     519class CustomTemplateFilterColorAdmin(admin.ModelAdmin):
     520    list_filter = (('warm', CustomTemplateBooleanFieldListFilter),)
     521
    515522site = admin.AdminSite(name="admin")
    516523site.register(Article, ArticleAdmin)
    517524site.register(CustomArticle, CustomArticleAdmin)
     
    581588site.register(AdminOrderedModelMethod, AdminOrderedModelMethodAdmin)
    582589site.register(AdminOrderedAdminMethod, AdminOrderedAdminMethodAdmin)
    583590site.register(AdminOrderedCallable, AdminOrderedCallableAdmin)
     591site.register(Color2, CustomTemplateFilterColorAdmin)
    584592
    585593# Register core models we need in our tests
    586594from django.contrib.auth.models import User, Group
  • tests/regressiontests/admin_views/tests.py

     
    573573        except SuspiciousOperation:
    574574            self.fail("Filters should be allowed if they are defined on a ForeignKey pointing to this model")
    575575
     576    def test_filter_with_custom_template(self):
     577        response = self.client.get("/test_admin/admin/admin_views/color2/")
     578        self.assertTrue('custom_filter_template.html' in [t.name for t in response.template])
     579
     580
    576581class AdminJavaScriptTest(AdminViewBasicTest):
    577582    urls = "regressiontests.admin_views.urls"
    578583
  • tests/regressiontests/admin_views/models.py

     
    106106    def __unicode__(self):
    107107        return self.value
    108108
     109# we replicate Color to register with another ModelAdmin
     110class Color2(Color):
     111    class Meta:
     112        proxy = True
    109113
    110114class Thing(models.Model):
    111115    title = models.CharField(max_length=20)
  • django/contrib/admin/templatetags/admin_list.py

     
    1313from django.utils.translation import ugettext as _
    1414from django.utils.encoding import smart_unicode, force_unicode
    1515from django.template import Library
     16from django.template.loader import get_template
     17from django.template.context import Context
    1618
    17 
    1819register = Library()
    1920
    2021DOT = '.'
     
    360361        'search_var': SEARCH_VAR
    361362    }
    362363
    363 @register.inclusion_tag('admin/filter.html')
     364@register.simple_tag()
    364365def admin_list_filter(cl, spec):
    365     return {'title': spec.title, 'choices' : list(spec.choices(cl))}
     366    if hasattr(spec, 'template_name') and spec.template_name:
     367        t = get_template(spec.template_name)
     368    else:
     369        t = get_template('admin/filter.html')
     370    ctx = Context({'title': spec.title, 'choices' : list(spec.choices(cl)), 'spec': spec})
     371    return t.render(ctx)
    366372
    367373@register.inclusion_tag('admin/actions.html', takes_context=True)
    368374def admin_actions(context):
Back to Top