Django

Code

Ticket #8330 (closed: duplicate)

Opened 4 months ago

Last modified 4 months ago

Cannot add new filterspecs without modifining filterspecs.py

Reported by: elwaywitvac <elwaywitvac@gmail.com> Assigned to: nobody
Milestone: Component: django.contrib.admin
Version: SVN Keywords: Filters
Cc: Triage Stage: Unreviewed
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

Adding your own filter specs works easily by simply using this code within an applications admin.py

from django.contrib.admin.filterspecs import FilterSpec

class MyFilterSpec(FilterSpec):
...

FilterSpec.register(...)

However, since the last default FilterSpec? is always evaluated as True, and new FilterSpec?'s never get tested. It doesn't seem right to require a library file be modified in order to add a a feature to an application (for example, django-tagging which is what I was messing with when I found this.)

Since I don't know exactly what channels I should be going through to submit this as a patch, I'll just put the solution I found here. It's easy to fix if you add the following the FilterSpec?.

class FilterSpec(object):
    filter_specs = []
    default = None  #added
...
    def create(cls, f, request, params, model, model_admin):
        for test, factory in cls.filter_specs:
            if test(f):
                return factory(f, request, params, model, model_admin)
        ### added after this point
        if callable(default):
            return default(f, request, params, model, model_admin)
    create = classmethod(create)

    # new method
    def set_default(cls, factory):
	if callable(factory):
		cls.default = factory
    set_default = classmethod(set_default)

Then, just replace the last line (which registers AllValuesFilterSpec?) with:

FilterSpec.set_default(AllValuesFilterSpec)

Attachments

filterspecs.py (7.8 kB) - added by elwaywitvac <elwaywitvac@gmail.com> on 08/14/08 21:40:57.
New version of filterspecs.py

Change History

08/14/08 21:40:57 changed by elwaywitvac <elwaywitvac@gmail.com>

  • attachment filterspecs.py added.

New version of filterspecs.py

08/14/08 21:45:29 changed by Alex

  • status changed from new to closed.
  • needs_better_patch changed.
  • resolution set to duplicate.
  • needs_tests changed.
  • needs_docs changed.

Closing as a dupe of #5833.


Add/Change #8330 (Cannot add new filterspecs without modifining filterspecs.py)




Change Properties
Action