Ticket #450: dynamic_generics.diff
File dynamic_generics.diff, 6.5 KB (added by , 19 years ago) |
---|
-
generic/date_based.py
2 2 from django.core.exceptions import Http404, ObjectDoesNotExist 3 3 from django.core.extensions import DjangoContext as Context 4 4 from django.core.xheaders import populate_xheaders 5 from django.views.generic import get_dynamic_lookup 5 6 from django.models import get_module 6 7 from django.utils.httpwrappers import HttpResponse 7 8 import datetime, time 8 9 9 10 def archive_index(request, app_label, module_name, date_field, num_latest=15, 10 template_name=None, extra_lookup_kwargs={}, extra_context={} ):11 template_name=None, extra_lookup_kwargs={}, extra_context={}, **keywds): 11 12 """ 12 13 Generic top-level archive of date-based objects. 13 14 … … 21 22 mod = get_module(app_label, module_name) 22 23 lookup_kwargs = {'%s__lte' % date_field: datetime.datetime.now()} 23 24 lookup_kwargs.update(extra_lookup_kwargs) 25 lookup_kwargs.update(get_dynamic_lookup(keywds)) 26 24 27 date_list = getattr(mod, "get_%s_list" % date_field)('year', **lookup_kwargs)[::-1] 25 28 if not date_list: 26 29 raise Http404("No %s.%s available" % (app_label, module_name)) … … 49 52 return HttpResponse(t.render(c)) 50 53 51 54 def archive_year(request, year, app_label, module_name, date_field, 52 template_name=None, extra_lookup_kwargs={}, extra_context={} ):55 template_name=None, extra_lookup_kwargs={}, extra_context={},**keywds): 53 56 """ 54 57 Generic yearly archive view. 55 58 … … 67 70 if int(year) >= now.year: 68 71 lookup_kwargs['%s__lte' % date_field] = now 69 72 lookup_kwargs.update(extra_lookup_kwargs) 73 lookup_kwargs.update(get_dynamic_lookup(keywds)) 74 70 75 date_list = getattr(mod, "get_%s_list" % date_field)('month', **lookup_kwargs) 71 76 if not date_list: 72 77 raise Http404 … … 86 91 87 92 def archive_month(request, year, month, app_label, module_name, date_field, 88 93 month_format='%b', template_name=None, extra_lookup_kwargs={}, 89 extra_context={} ):94 extra_context={},**keywds): 90 95 """ 91 96 Generic monthly archive view. 92 97 … … 119 124 if last_day >= now.date(): 120 125 lookup_kwargs['%s__lte' % date_field] = now 121 126 lookup_kwargs.update(extra_lookup_kwargs) 127 lookup_kwargs.update(get_dynamic_lookup(keywds)) 122 128 object_list = mod.get_list(**lookup_kwargs) 123 129 if not object_list: 124 130 raise Http404 … … 138 144 139 145 def archive_day(request, year, month, day, app_label, module_name, date_field, 140 146 month_format='%b', day_format='%d', template_name=None, 141 extra_lookup_kwargs={}, extra_context={}, allow_empty=False ):147 extra_lookup_kwargs={}, extra_context={}, allow_empty=False,**keywds): 142 148 """ 143 149 Generic daily archive view. 144 150 … … 167 173 if date >= now.date(): 168 174 lookup_kwargs['%s__lte' % date_field] = now 169 175 lookup_kwargs.update(extra_lookup_kwargs) 176 lookup_kwargs.update(get_dynamic_lookup(keywds)) 170 177 object_list = mod.get_list(**lookup_kwargs) 171 178 if not allow_empty and not object_list: 172 179 raise Http404 … … 201 208 def object_detail(request, year, month, day, app_label, module_name, date_field, 202 209 month_format='%b', day_format='%d', object_id=None, slug=None, 203 210 slug_field=None, template_name=None, template_name_field=None, 204 extra_lookup_kwargs={}, extra_context={} ):211 extra_lookup_kwargs={}, extra_context={},**keywds): 205 212 """ 206 213 Generic detail view from year/month/day/slug or year/month/day/id structure. 207 214 … … 230 237 else: 231 238 raise AttributeError("Generic detail view must be called with either an object_id or a slug/slugfield") 232 239 lookup_kwargs.update(extra_lookup_kwargs) 240 lookup_kwargs.update(get_dynamic_lookup(keywds)) 233 241 try: 234 242 object = mod.get_object(**lookup_kwargs) 235 243 except ObjectDoesNotExist: -
generic/list_detail.py
5 5 from django.core.extensions import DjangoContext as Context 6 6 from django.core.paginator import ObjectPaginator, InvalidPage 7 7 from django.core.exceptions import Http404, ObjectDoesNotExist 8 from django.views.generic import get_dynamic_lookup 8 9 9 10 def object_list(request, app_label, module_name, paginate_by=None, allow_empty=False, 10 template_name=None, extra_lookup_kwargs={}, extra_context={} ):11 template_name=None, extra_lookup_kwargs={}, extra_context={},**kwargs): 11 12 """ 12 13 Generic list of objects. 13 14 … … 34 35 """ 35 36 mod = models.get_module(app_label, module_name) 36 37 lookup_kwargs = extra_lookup_kwargs.copy() 38 lookup_kwargs.update(get_dynamic_lookup(keywds)) 39 37 40 if paginate_by: 38 41 paginator = ObjectPaginator(mod, lookup_kwargs, paginate_by) 39 42 page = request.GET.get('page', 0) … … 73 76 74 77 def object_detail(request, app_label, module_name, object_id=None, slug=None, 75 78 slug_field=None, template_name=None, template_name_field=None, 76 extra_lookup_kwargs={}, extra_context={} ):79 extra_lookup_kwargs={}, extra_context={},**kwargs): 77 80 """ 78 81 Generic list of objects. 79 82 … … 91 94 else: 92 95 raise AttributeError("Generic detail view must be called with either an object_id or a slug/slug_field") 93 96 lookup_kwargs.update(extra_lookup_kwargs) 97 lookup_kwargs.update(get_dynamic_lookup(keywds)) 98 94 99 try: 95 100 object = mod.get_object(**lookup_kwargs) 96 101 except ObjectDoesNotExist: -
generic/__init__.py
1 def get_dynamic_lookup(keywords): 2 lookups = {} 3 4 if keywords.has_key("dynamic_lookup_kwargs"): 5 dynamic_lookup_kwargs = keywords["dynamic_lookup_kwargs"] 6 7 del keywords["dynamic_lookup_kwargs"] 8 9 for lookup, url_keyword in dynamic_lookup_kwargs.items(): 10 if keywords.has_key(url_keyword): 11 lookups[lookup] = keywords[url_keyword] 12 else: 13 raise Exception, "dynamic variable %s, was needed but is not defined in the url pattern" % (url_keyword) 14 15 return lookups