Coder Social home page Coder Social logo

reshmaingavale / django-admin-filtrate Goto Github PK

View Code? Open in Web Editor NEW

This project forked from runekaagaard/django-admin-filtrate

0.0 1.0 0.0 663 KB

Custom filter framework for the Django Admin.

Python 3.68% CSS 6.79% JavaScript 50.89% PHP 6.19% HTML 32.44% Shell 0.01%

django-admin-filtrate's Introduction

About

This Django app makes it easier to create custom filters in the change list of Django Admin and supplies a TreeFilter and a DateRangeFilter too. Se below.

Tested on Django 1.2.3 and from @15ea9a9 1.3.1.

Updating to 1.4-1.7 support

There are no release yet, but a version tested with 1.4 and 1.7 can be installed with pip like:

pip install git+git://github.com/runekaagaard/django-admin-filtrate@19f56144bfff2180cdc4e3f89770d7423c2b0318

I still need to test it with 1.5 and 1.6, update the documentation and then coax PyPi into making a release. Help could be great :) Major differences in this version is:

  • lookup_allowed() no longer needed.
  • Uses the new builtin Django Filter classes.
  • No need to register with filtrate.register_filter.

NOTE: The docs below and the test app are not corret for this new version.

Updating to 1.3 support

I will write proper docs, things are getting messy here, but here is the lowdown.

New settings required

As I found out, you can't reliably convert the django date formats to Datepicker formats. So this commit introduces these two new settings:

FILTRATE = {
	# See http://jqueryui.com/demos/datepicker/#localization.
	'datepicker_region': 'en-GB',
	# See http://docs.jquery.com/UI/Datepicker/formatDate.
	'datepicker_date_format': 'yy-mm-dd',
}

So if the above defaults does not suit you, you have to change them your self. Check out the Datepicker documentation to see how to use them.

lookup_allowed()

Django 1.2.4 introduces restrictions on which lookups that can be queried in the url, so at the moment the end user are responsible for checking for those, as in this example:

class CaseAdmin(admin.ModelAdmin):
	list_filter = ['client']

	def lookup_allowed(self, key, *args, **kwargs):
		if 'client__start_date' in key:
			return True
		else:
			return super(CaseAdmin, self).lookup_allowed(key, *args, **kwargs)

Undefined Media() class bug

Time and my Python meta-fu is running out, and I couldn't fix it so its not neccessary to define an empty Media() class as in:

class CaseAdmin(admin.ModelAdmin):
	class Media():
		pass

The FiltrateFilter

The base class that adds support for custom html in the content of the filter and for using Media() classes.

TreeFilter

A recursive tree filter using the excellent library http://www.jstree.com/.

Example

# The Filter.
from filtrate.filters import TreeFilter
from itertools import groupby
class CompanyDepartmentFilter(TreeFilter):
    field_name = "client__department__id__in"
    
    def get_title(self):
        return 'By Department'
    
    def get_tree(self):
        from company.models import Department
        qs = Department.objects.all().order_by('company_order', 'company')
        return groupby(qs, lambda obj: getattr(obj, 'company'))

# The model.
from filtrate import register_filter
class Case(Model):
    ...
    client = models.ForeignKey(Client)
    register_filter(client, CompanyDepartmentFilter)
	...

DateRangeFilter

Filters results in a given date range using the jQueryUI datepicker plugin.

Example

# The Filter.
from filtrate.filters import DateRangeFilter

class CaseLicenseStartDateFilter(DateRangeFilter):
    field_name = 'caselicense__start_date'
    
    def get_title(self):
        return "By license start date"

# The model.
from filtrate import register_filter
class Case(Model):
	...
    caselicense = models.ForeignKey(Licence)
    register_filter(caselicense, CaseLicenseStartDateFilter)
	...

Installation

  • Clone the repo and symlink or copy the "filtrate" folder to your apps folder.
  • Add filtrate to your installed apps.
  • Add the "filtrate/templates" folder to your template folders.

Static files

FlexSelect requires "django.contrib.staticfiles" installed to work out of the box. If it is not then the js and css files must be installed manually. Read more about "django.contrib.staticfiles" at https://docs.djangoproject.com/en/1.3/ref/contrib/staticfiles/.

django-admin-filtrate's People

Contributors

runekaagaard avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.