1 | "wrapped generic list view adding searching parameters "
|
---|
2 | from django import models
|
---|
3 | from django.core.meta import FieldDoesNotExist
|
---|
4 | from django.views.generic.list_detail import object_list
|
---|
5 |
|
---|
6 | def wrapped(generic):
|
---|
7 |
|
---|
8 | def _wrapper(request, app_label, module_name, *args, **kw):
|
---|
9 | direction = request.GET.get('direction', None)
|
---|
10 | if direction in ('newest_top', 'oldest_top'):
|
---|
11 | request.session['direction'] = direction
|
---|
12 | else:
|
---|
13 | direction = request.session.get('direction', 'oldest_top')
|
---|
14 |
|
---|
15 | if not kw.has_key('extra_lookup_kwargs'):
|
---|
16 | kw['extra_lookup_kwargs'] = {}
|
---|
17 |
|
---|
18 | keys = request.GET.keys()
|
---|
19 | if keys:
|
---|
20 | mod = models.get_module(app_label, module_name)
|
---|
21 | meta = mod.Klass._meta
|
---|
22 | for key in keys:
|
---|
23 | try:
|
---|
24 | meta.get_field(key)
|
---|
25 | value = request.GET.get(key,None)
|
---|
26 |
|
---|
27 | if value:
|
---|
28 | print key+'__contains', '=', value
|
---|
29 | kw['extra_lookup_kwargs'][key+'__contains'] = value
|
---|
30 | except FieldDoesNotExist, msg:
|
---|
31 | print msg
|
---|
32 |
|
---|
33 | if not kw.has_key('extra_context'):
|
---|
34 | kw['extra_context'] = {}
|
---|
35 |
|
---|
36 | return generic(request, app_label, module_name, *args, **kw)
|
---|
37 |
|
---|
38 | return _wrapper
|
---|
39 |
|
---|
40 | object_list = wrapped(object_list)
|
---|
41 |
|
---|