Skip to content

Commit

Permalink
Init repo
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyong committed Jul 9, 2013
1 parent b14515b commit 29cadd3
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 0 deletions.
2 changes: 2 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include README
recursive-include valueinlist_filter/templates *
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,46 @@ django-valueinlist-filter
=========================

Queryset can be filtered by a given list on the django admin page, just like: qs.filter(field__in=[list]).

Installation
------------

.. code-block:: bash

cd django-valueinlist-filter
python setup.py install


Add valueinlist_filter to settings.INSTALLED_APPS:

.. code-block:: python

INSTALLED_APPS = (
...
'valueinlist_filter'
)

And then, set a CharField field in a model like this:

.. code-block:: python

class Model(models.Model):
...
serial_number = models.CharField(max_length=32)


If you want to filter the **serial_number** field by a given value list(split string by space), you can bind ValueInListFilter to this field:

.. code-block:: python

from valueinlist_filter.filter import ValueInListFilter
from django.contrib import admin
from models import Model

class ModelAdmin(admin.ModelAdmin):
list_filter = (
('serial_number', ValueInListFilter), # bind filter to CharField
...
)

Finally, you will get a textarea form on the filter side bar, the textarea's change event will trigger form.
23 changes: 23 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from setuptools import setup, find_packages

setup(
name='django-valueinlist-filter',
version='0.1',
description='Queryset can be filtered by a given list on the django admin page, just like: qs.filter(field__in=[list])',
long_description=open('README.md').read(-1),
author='Chris Chen',
author_email='[email protected]',
url='http://github.com/gzeronet/django-valueinlist-filter',
keywords=[
'django admin',
'django filter query',
'django value in list',
],
install_requires=[
'Django>=1.5',
],
packages=find_packages(),
include_package_data=True,
zip_safe=False,
license = 'BSD',
)
Empty file added valueinlist_filter/__init__.py
Empty file.
32 changes: 32 additions & 0 deletions valueinlist_filter/filter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from django.contrib import admin
from django.db import models
from .forms import ValueInListForm


class ValueInListFilter(admin.filters.FieldListFilter):
template = 'valueinlist_filter/filter.html'

def choices(self, cl):
return []

def __init__(self, field, request, params, model, model_admin, field_path):
self.field = field
self.field_path = field_path
self.title = getattr(field, 'verbose_name', field_path)
self.lookup_kwarg_in = '%s__in' % field_path
self.used_parameters = dict((key, params.pop(key)) for key in self.expected_parameters() if key in params)
self.form = self.get_form(request)

def expected_parameters(self):
return [self.lookup_kwarg_in]

def get_form(self, request):
return ValueInListForm(data=self.used_parameters, field_name=self.field_path)

def queryset(self, request, queryset):
if self.form.is_valid():
data = self.form.cleaned_data
return queryset.filter(**dict((key, data[key].split()) for key in data if data[key]))
return queryset

admin.filters.FieldListFilter.register(lambda f: isinstance(f, models.CharField), ValueInListFilter)
13 changes: 13 additions & 0 deletions valueinlist_filter/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from django import forms
from django.utils.translation import ugettext as _


class ValueInListForm(forms.Form):

def __init__(self, *args, **kwargs):
field_name = kwargs.pop('field_name')
super(ValueInListForm, self).__init__(*args, **kwargs)
self.fields['%s__in' % field_name] = forms.CharField(label=_('in'), required=False,
widget=forms.Textarea(attrs={
'placeholder': _('%(fn)sA %(fn)sB ... %(fn)sN') % {'fn': field_name}
}))
17 changes: 17 additions & 0 deletions valueinlist_filter/templates/valueinlist_filter/filter.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% load i18n %}

<h3>{% blocktrans with filter_title=title %} By {{ filter_title }} {% endblocktrans %}</h3>
<form class='valueinlist_form' method="GET" action="">
{{ spec.form.as_p }}
<script>
(function($){
$(function($){
$("form :input[name='{{ title }}__in']").change(function(){
if(confirm("{% trans 'Refresh results ?' %}")){
$(this).parents("form").submit();
}
}).css("width", "100%");
});
})(django.jQuery);
</script>
</form>

0 comments on commit 29cadd3

Please sign in to comment.