-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
chenyong
committed
Jul 9, 2013
1 parent
b14515b
commit 29cadd3
Showing
7 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
include README | ||
recursive-include valueinlist_filter/templates * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
17
valueinlist_filter/templates/valueinlist_filter/filter.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |