Ticket #667: generic_extra_callbacks_r1263.patch
File generic_extra_callbacks_r1263.patch, 8.0 KB (added by , 19 years ago) |
---|
-
views/generic/date_based.py
21 21 """ 22 22 mod = get_module(app_label, module_name) 23 23 lookup_kwargs = {'%s__lte' % date_field: datetime.datetime.now()} 24 lookup_kwargs.update(extra_lookup_kwargs) 24 if callable(extra_lookup_kwargs): 25 lookup_kwargs.update(extra_lookup_kwargs(request, lookup_kwargs)) 26 else: 27 lookup_kwargs.update(extra_lookup_kwargs) 25 28 date_list = getattr(mod, "get_%s_list" % date_field)('year', **lookup_kwargs)[::-1] 26 29 if not date_list: 27 30 raise Http404("No %s.%s available" % (app_label, module_name)) … … 42 45 'date_list' : date_list, 43 46 'latest' : latest, 44 47 }) 48 if callable(extra_context): 49 extra_context = extra_context(request, lookup_kwargs) 45 50 for key, value in extra_context.items(): 46 51 if callable(value): 47 52 c[key] = value() … … 68 73 # Only bother to check current date if the year isn't in the past. 69 74 if int(year) >= now.year: 70 75 lookup_kwargs['%s__lte' % date_field] = now 71 lookup_kwargs.update(extra_lookup_kwargs) 76 if callable(extra_lookup_kwargs): 77 lookup_kwargs.update(extra_lookup_kwargs(request, lookup_kwargs)) 78 else: 79 lookup_kwargs.update(extra_lookup_kwargs) 72 80 date_list = getattr(mod, "get_%s_list" % date_field)('month', **lookup_kwargs) 73 81 if not date_list: 74 82 raise Http404 … … 79 87 'date_list': date_list, 80 88 'year': year, 81 89 }) 90 if callable(extra_context): 91 extra_context = extra_context(request, lookup_kwargs) 82 92 for key, value in extra_context.items(): 83 93 if callable(value): 84 94 c[key] = value() … … 120 130 # Only bother to check current date if the month isn't in the past. 121 131 if last_day >= now.date(): 122 132 lookup_kwargs['%s__lte' % date_field] = now 123 lookup_kwargs.update(extra_lookup_kwargs) 133 if callable(extra_lookup_kwargs): 134 lookup_kwargs.update(extra_lookup_kwargs(request, lookup_kwargs)) 135 else: 136 lookup_kwargs.update(extra_lookup_kwargs) 124 137 object_list = mod.get_list(**lookup_kwargs) 125 138 if not object_list: 126 139 raise Http404 … … 131 144 'object_list': object_list, 132 145 'month': date, 133 146 }) 147 if callable(extra_context): 148 extra_context = extra_context(request, lookup_kwargs) 134 149 for key, value in extra_context.items(): 135 150 if callable(value): 136 151 c[key] = value() … … 169 184 # Only bother to check current date if the date isn't in the past. 170 185 if date >= now.date(): 171 186 lookup_kwargs['%s__lte' % date_field] = now 172 lookup_kwargs.update(extra_lookup_kwargs) 187 if callable(extra_lookup_kwargs): 188 lookup_kwargs.update(extra_lookup_kwargs(request, lookup_kwargs)) 189 else: 190 lookup_kwargs.update(extra_lookup_kwargs) 173 191 object_list = mod.get_list(**lookup_kwargs) 174 192 if not allow_empty and not object_list: 175 193 raise Http404 … … 182 200 'previous_day': date - datetime.timedelta(days=1), 183 201 'next_day': (date < datetime.date.today()) and (date + datetime.timedelta(days=1)) or None, 184 202 }) 203 if callable(extra_context): 204 extra_context = extra_context(request, lookup_kwargs) 185 205 for key, value in extra_context.items(): 186 206 if callable(value): 187 207 c[key] = value() … … 233 253 lookup_kwargs['%s__exact' % slug_field] = slug 234 254 else: 235 255 raise AttributeError("Generic detail view must be called with either an object_id or a slug/slugfield") 236 lookup_kwargs.update(extra_lookup_kwargs) 256 if callable(extra_lookup_kwargs): 257 lookup_kwargs.update(extra_lookup_kwargs(request, lookup_kwargs)) 258 else: 259 lookup_kwargs.update(extra_lookup_kwargs) 237 260 try: 238 261 object = mod.get_object(**lookup_kwargs) 239 262 except ObjectDoesNotExist: … … 248 271 c = Context(request, { 249 272 'object': object, 250 273 }) 274 if callable(extra_context): 275 extra_context = extra_context(request, lookup_kwargs) 251 276 for key, value in extra_context.items(): 252 277 if callable(value): 253 278 c[key] = value() -
views/generic/list_detail.py
36 36 number of objects, total 37 37 """ 38 38 mod = models.get_module(app_label, module_name) 39 lookup_kwargs = extra_lookup_kwargs.copy() 39 if callable(extra_lookup_kwargs): 40 lookup_kwargs = extra_lookup_kwargs(request, {}) 41 else: 42 lookup_kwargs = extra_lookup_kwargs.copy() 40 43 if paginate_by: 41 44 paginator = ObjectPaginator(mod, lookup_kwargs, paginate_by) 42 45 page = request.GET.get('page', 0) … … 68 71 }) 69 72 if len(object_list) == 0 and not allow_empty: 70 73 raise Http404 74 if callable(extra_context): 75 extra_context = extra_context(request, lookup_kwargs) 71 76 for key, value in extra_context.items(): 72 77 if callable(value): 73 78 c[key] = value() … … 98 103 lookup_kwargs['%s__exact' % slug_field] = slug 99 104 else: 100 105 raise AttributeError("Generic detail view must be called with either an object_id or a slug/slug_field") 101 lookup_kwargs.update(extra_lookup_kwargs) 106 if callable(extra_lookup_kwargs): 107 lookup_kwargs.update(extra_lookup_kwargs(request, lookup_kwargs)) 108 else: 109 lookup_kwargs.update(extra_lookup_kwargs) 102 110 try: 103 111 object = mod.get_object(**lookup_kwargs) 104 112 except ObjectDoesNotExist: … … 113 121 c = Context(request, { 114 122 'object': object, 115 123 }) 124 if callable(extra_context): 125 extra_context = extra_context(request, lookup_kwargs) 116 126 for key, value in extra_context.items(): 117 127 if callable(value): 118 128 c[key] = value() -
views/generic/create_update.py
58 58 c = Context(request, { 59 59 'form' : form, 60 60 }) 61 if callable(extra_context): 62 extra_context = extra_context(request, {}) 61 63 for key, value in extra_context.items(): 62 64 if callable(value): 63 65 c[key] = value() … … 92 94 lookup_kwargs['%s__exact' % slug_field] = slug 93 95 else: 94 96 raise AttributeError("Generic edit view must be called with either an object_id or a slug/slug_field") 95 lookup_kwargs.update(extra_lookup_kwargs) 97 if callable(extra_lookup_kwargs): 98 lookup_kwargs.update(extra_lookup_kwargs(request, lookup_kwargs)) 99 else: 100 lookup_kwargs.update(extra_lookup_kwargs) 96 101 try: 97 102 object = mod.get_object(**lookup_kwargs) 98 103 except ObjectDoesNotExist: … … 130 135 'form' : form, 131 136 'object' : object, 132 137 }) 138 if callable(extra_context): 139 extra_context = extra_context(request, lookup_kwargs) 133 140 for key, value in extra_context.items(): 134 141 if callable(value): 135 142 c[key] = value() … … 168 175 lookup_kwargs['%s__exact' % slug_field] = slug 169 176 else: 170 177 raise AttributeError("Generic delete view must be called with either an object_id or a slug/slug_field") 171 lookup_kwargs.update(extra_lookup_kwargs) 178 if callable(extra_lookup_kwargs): 179 lookup_kwargs.update(extra_lookup_kwargs(request, lookup_kwargs)) 180 else: 181 lookup_kwargs.update(extra_lookup_kwargs) 172 182 try: 173 183 object = mod.get_object(**lookup_kwargs) 174 184 except ObjectDoesNotExist: … … 186 196 c = Context(request, { 187 197 'object' : object, 188 198 }) 199 if callable(extra_context): 200 extra_context = extra_context(request, lookup_kwargs) 189 201 for key, value in extra_context.items(): 190 202 if callable(value): 191 203 c[key] = value()