Ticket #1085: generic.diff
File generic.diff, 22.4 KB (added by , 19 years ago) |
---|
-
django/core/xheaders.py
4 4 5 5 This module contains utility functions for retrieving and doing interesting 6 6 things with these special "X-Headers" (so called because the HTTP spec demands 7 that custom headers are pref xed with "X-").7 that custom headers are prefixed with "X-"). 8 8 9 9 Next time you're at slashdot.org, watch out for X-Fry and X-Bender. :) 10 10 """ 11 11 12 def populate_xheaders(request, response, package, python_module_name, object_id):12 def populate_xheaders(request, response, object_type, object_id): 13 13 """ 14 14 Adds the "X-Object-Type" and "X-Object-Id" headers to the given 15 HttpResponse according to the given package, python_module_name and15 HttpResponse according to the given object_type and 16 16 object_id -- but only if the given HttpRequest object has an IP address 17 17 within the INTERNAL_IPS setting. 18 18 """ 19 19 from django.conf.settings import INTERNAL_IPS 20 20 if request.META.get('REMOTE_ADDR') in INTERNAL_IPS: 21 response['X-Object-Type'] = "%s.%s" % (package, python_module_name)21 response['X-Object-Type'] = str(object_type) 22 22 response['X-Object-Id'] = str(object_id) -
django/views/generic/date_based.py
2 2 from django.core.exceptions import Http404, ObjectDoesNotExist 3 3 from django.core.extensions import DjangoContext 4 4 from django.core.xheaders import populate_xheaders 5 from django.models import get_module6 5 from django.utils.httpwrappers import HttpResponse 6 from string import lower 7 7 import datetime, time 8 8 9 def archive_index(request, app_label, mod ule_name, date_field, num_latest=15,9 def archive_index(request, app_label, model, date_field, num_latest=15, 10 10 template_name=None, template_loader=loader, extra_lookup_kwargs={}, 11 11 extra_context={}, allow_empty=False, context_processors=None): 12 12 """ 13 13 Generic top-level archive of date-based objects. 14 14 15 Templates: ``<app_label>/<mod ule_name>_archive``15 Templates: ``<app_label>/<model_name>_archive`` 16 16 Context: 17 17 date_list 18 18 List of years 19 19 latest 20 20 Latest N (defaults to 15) objects by date 21 21 """ 22 mod = get_module(app_label, module_name)23 22 lookup_kwargs = {'%s__lte' % date_field: datetime.datetime.now()} 24 23 lookup_kwargs.update(extra_lookup_kwargs) 25 date_list = getattr(mod , "get_%s_list" % date_field)('year', **lookup_kwargs)[::-1]24 date_list = getattr(model.objects, "get_%s_list" % date_field)('year', **lookup_kwargs)[::-1] 26 25 if not date_list and not allow_empty: 27 raise Http404("No %s .%s available" % (app_label, module_name))26 raise Http404("No %s available" % model.__name__) 28 27 29 28 if date_list and num_latest: 30 29 lookup_kwargs.update({ 31 30 'limit': num_latest, 32 31 'order_by': ('-' + date_field,), 33 32 }) 34 latest = mod .get_list(**lookup_kwargs)33 latest = model.objects.get_list(**lookup_kwargs) 35 34 else: 36 35 latest = None 37 36 38 37 if not template_name: 39 template_name = "%s/%s_archive" % (app_label, module_name)38 template_name = "%s/%s_archive" % (app_label, lower(model.__name__)) 40 39 t = template_loader.get_template(template_name) 41 40 c = DjangoContext(request, { 42 41 'date_list' : date_list, … … 49 48 c[key] = value 50 49 return HttpResponse(t.render(c)) 51 50 52 def archive_year(request, year, app_label, mod ule_name, date_field,51 def archive_year(request, year, app_label, model, date_field, 53 52 template_name=None, template_loader=loader, extra_lookup_kwargs={}, 54 53 extra_context={}, context_processors=None): 55 54 """ 56 55 Generic yearly archive view. 57 56 58 Templates: ``<app_label>/<mod ule_name>_archive_year``57 Templates: ``<app_label>/<model_name>_archive_year`` 59 58 Context: 60 59 date_list 61 60 List of months in this year with objects 62 61 year 63 62 This year 64 63 """ 65 mod = get_module(app_label, module_name)66 64 now = datetime.datetime.now() 67 65 lookup_kwargs = {'%s__year' % date_field: year} 68 66 # Only bother to check current date if the year isn't in the past. 69 67 if int(year) >= now.year: 70 68 lookup_kwargs['%s__lte' % date_field] = now 71 69 lookup_kwargs.update(extra_lookup_kwargs) 72 date_list = getattr(mod , "get_%s_list" % date_field)('month', **lookup_kwargs)70 date_list = getattr(model.objects, "get_%s_list" % date_field)('month', **lookup_kwargs) 73 71 if not date_list: 74 72 raise Http404 75 73 if not template_name: 76 template_name = "%s/%s_archive_year" % (app_label, module_name)74 template_name = "%s/%s_archive_year" % (app_label, lower(model.__name__)) 77 75 t = template_loader.get_template(template_name) 78 76 c = DjangoContext(request, { 79 77 'date_list': date_list, … … 86 84 c[key] = value 87 85 return HttpResponse(t.render(c)) 88 86 89 def archive_month(request, year, month, app_label, mod ule_name, date_field,87 def archive_month(request, year, month, app_label, model, date_field, 90 88 month_format='%b', template_name=None, template_loader=loader, 91 89 extra_lookup_kwargs={}, extra_context={}, context_processors=None): 92 90 """ 93 91 Generic monthly archive view. 94 92 95 Templates: ``<app_label>/<mod ule_name>_archive_month``93 Templates: ``<app_label>/<model_name>_archive_month`` 96 94 Context: 97 95 month: 98 96 this month … … 104 102 except ValueError: 105 103 raise Http404 106 104 107 mod = get_module(app_label, module_name)108 105 now = datetime.datetime.now() 109 106 # Calculate first and last day of month, for use in a date-range lookup. 110 107 first_day = date.replace(day=1) … … 117 114 if last_day >= now.date(): 118 115 lookup_kwargs['%s__lte' % date_field] = now 119 116 lookup_kwargs.update(extra_lookup_kwargs) 120 object_list = mod .get_list(**lookup_kwargs)117 object_list = model.objects.get_list(**lookup_kwargs) 121 118 if not object_list: 122 119 raise Http404 123 120 if not template_name: 124 template_name = "%s/%s_archive_month" % (app_label, module_name)121 template_name = "%s/%s_archive_month" % (app_label, lower(model.__name__)) 125 122 t = template_loader.get_template(template_name) 126 123 c = DjangoContext(request, { 127 124 'object_list': object_list, … … 134 131 c[key] = value 135 132 return HttpResponse(t.render(c)) 136 133 137 def archive_day(request, year, month, day, app_label, mod ule_name, date_field,134 def archive_day(request, year, month, day, app_label, model, date_field, 138 135 month_format='%b', day_format='%d', template_name=None, 139 136 template_loader=loader, extra_lookup_kwargs={}, extra_context={}, 140 137 allow_empty=False, context_processors=None): 141 138 """ 142 139 Generic daily archive view. 143 140 144 Templates: ``<app_label>/<mod ule_name>_archive_day``141 Templates: ``<app_label>/<model_name>_archive_day`` 145 142 Context: 146 143 object_list: 147 144 list of objects published that day … … 157 154 except ValueError: 158 155 raise Http404 159 156 160 mod = get_module(app_label, module_name)161 157 now = datetime.datetime.now() 162 158 lookup_kwargs = { 163 159 '%s__range' % date_field: (datetime.datetime.combine(date, datetime.time.min), datetime.datetime.combine(date, datetime.time.max)), … … 166 162 if date >= now.date(): 167 163 lookup_kwargs['%s__lte' % date_field] = now 168 164 lookup_kwargs.update(extra_lookup_kwargs) 169 object_list = mod .get_list(**lookup_kwargs)165 object_list = model.objects.get_list(**lookup_kwargs) 170 166 if not allow_empty and not object_list: 171 167 raise Http404 172 168 if not template_name: 173 template_name = "%s/%s_archive_day" % (app_label, module_name)169 template_name = "%s/%s_archive_day" % (app_label, lower(model.__name__)) 174 170 t = template_loader.get_template(template_name) 175 171 c = DjangoContext(request, { 176 172 'object_list': object_list, … … 197 193 }) 198 194 return archive_day(request, **kwargs) 199 195 200 def object_detail(request, year, month, day, app_label, mod ule_name, date_field,196 def object_detail(request, year, month, day, app_label, model, date_field, 201 197 month_format='%b', day_format='%d', object_id=None, slug=None, 202 198 slug_field=None, template_name=None, template_name_field=None, 203 199 template_loader=loader, extra_lookup_kwargs={}, extra_context={}, … … 205 201 """ 206 202 Generic detail view from year/month/day/slug or year/month/day/id structure. 207 203 208 Templates: ``<app_label>/<mod ule_name>_detail``204 Templates: ``<app_label>/<model_name>_detail`` 209 205 Context: 210 206 object: 211 207 the object to be detailed … … 215 211 except ValueError: 216 212 raise Http404 217 213 218 mod = get_module(app_label, module_name)219 214 now = datetime.datetime.now() 220 215 lookup_kwargs = { 221 216 '%s__range' % date_field: (datetime.datetime.combine(date, datetime.time.min), datetime.datetime.combine(date, datetime.time.max)), … … 224 219 if date >= now.date(): 225 220 lookup_kwargs['%s__lte' % date_field] = now 226 221 if object_id: 227 lookup_kwargs['%s__exact' % mod .Klass._meta.pk.name] = object_id222 lookup_kwargs['%s__exact' % model.Klass._meta.pk.name] = object_id 228 223 elif slug and slug_field: 229 224 lookup_kwargs['%s__exact' % slug_field] = slug 230 225 else: 231 226 raise AttributeError("Generic detail view must be called with either an object_id or a slug/slugfield") 232 227 lookup_kwargs.update(extra_lookup_kwargs) 233 228 try: 234 object = mod .get_object(**lookup_kwargs)229 object = model.objects.get_object(**lookup_kwargs) 235 230 except ObjectDoesNotExist: 236 raise Http404("%s .%s does not exist for %s" % (app_label, module_name, lookup_kwargs))231 raise Http404("%s does not exist for %s" % (model.__name__, lookup_kwargs)) 237 232 if not template_name: 238 template_name = "%s/%s_detail" % (app_label, module_name)233 template_name = "%s/%s_detail" % (app_label, lower(model.__name__)) 239 234 if template_name_field: 240 235 template_name_list = [getattr(object, template_name_field), template_name] 241 236 t = template_loader.select_template(template_name_list) … … 250 245 else: 251 246 c[key] = value 252 247 response = HttpResponse(t.render(c)) 253 populate_xheaders(request, response, app_label, module_name, getattr(object, object._meta.pk.name))248 populate_xheaders(request, response, model.__name__, getattr(object, object._meta.pk.name)) 254 249 return response -
django/views/generic/list_detail.py
5 5 from django.core.extensions import DjangoContext 6 6 from django.core.paginator import ObjectPaginator, InvalidPage 7 7 from django.core.exceptions import Http404, ObjectDoesNotExist 8 from string import lower 8 9 9 def object_list(request, app_label, mod ule_name, paginate_by=None, allow_empty=False,10 def object_list(request, app_label, model, paginate_by=None, allow_empty=False, 10 11 template_name=None, template_loader=loader, extra_lookup_kwargs={}, 11 12 extra_context={}, context_processors=None): 12 13 """ 13 14 Generic list of objects. 14 15 15 Templates: ``<app_label>/<mod ule_name>_list``16 Templates: ``<app_label>/<model_name>_list`` 16 17 Context: 17 18 object_list 18 19 list of objects … … 35 36 hits 36 37 number of objects, total 37 38 """ 38 mod = models.get_module(app_label, module_name)39 39 lookup_kwargs = extra_lookup_kwargs.copy() 40 40 if paginate_by: 41 paginator = ObjectPaginator(mod , lookup_kwargs, paginate_by)41 paginator = ObjectPaginator(model, lookup_kwargs, paginate_by) 42 42 page = request.GET.get('page', 0) 43 43 try: 44 44 object_list = paginator.get_page(page) … … 61 61 'hits' : paginator.hits, 62 62 }, context_processors) 63 63 else: 64 object_list = mod .get_list(**lookup_kwargs)64 object_list = model.objects.get_list(**lookup_kwargs) 65 65 c = DjangoContext(request, { 66 66 'object_list': object_list, 67 67 'is_paginated': False … … 74 74 else: 75 75 c[key] = value 76 76 if not template_name: 77 template_name = "%s/%s_list" % (app_label, module_name)77 template_name = "%s/%s_list" % (app_label, lower(model.__name__)) 78 78 t = template_loader.get_template(template_name) 79 79 return HttpResponse(t.render(c)) 80 80 81 def object_detail(request, app_label, mod ule_name, object_id=None, slug=None,81 def object_detail(request, app_label, model, object_id=None, slug=None, 82 82 slug_field=None, template_name=None, template_name_field=None, 83 83 template_loader=loader, extra_lookup_kwargs={}, extra_context={}, 84 84 context_processors=None): 85 85 """ 86 86 Generic list of objects. 87 87 88 Templates: ``<app_label>/<mod ule_name>_detail``88 Templates: ``<app_label>/<model_name>_detail`` 89 89 Context: 90 90 object 91 91 the object 92 92 """ 93 mod = models.get_module(app_label, module_name)94 93 lookup_kwargs = {} 95 94 if object_id: 96 95 lookup_kwargs['pk'] = object_id … … 100 99 raise AttributeError("Generic detail view must be called with either an object_id or a slug/slug_field") 101 100 lookup_kwargs.update(extra_lookup_kwargs) 102 101 try: 103 object = mod .get_object(**lookup_kwargs)102 object = model.objects.get_object(**lookup_kwargs) 104 103 except ObjectDoesNotExist: 105 raise Http404("%s .%s does not exist for %s" % (app_label, module_name, lookup_kwargs))104 raise Http404("%s does not exist for %s" % (model.__name__, lookup_kwargs)) 106 105 if not template_name: 107 template_name = "%s/%s_detail" % (app_label, module_name)106 template_name = "%s/%s_detail" % (app_label, lower(model.__name__)) 108 107 if template_name_field: 109 108 template_name_list = [getattr(object, template_name_field), template_name] 110 109 t = template_loader.select_template(template_name_list) … … 119 118 else: 120 119 c[key] = value 121 120 response = HttpResponse(t.render(c)) 122 populate_xheaders(request, response, app_label, module_name, getattr(object, object._meta.pk.name))121 populate_xheaders(request, response, model.__name__, getattr(object, object._meta.pk.name)) 123 122 return response -
django/views/generic/create_update.py
7 7 from django.core.paginator import ObjectPaginator, InvalidPage 8 8 from django.utils.httpwrappers import HttpResponse, HttpResponseRedirect 9 9 from django.core.exceptions import Http404, ObjectDoesNotExist, ImproperlyConfigured 10 from string import lower 10 11 11 def create_object(request, app_label, mod ule_name, template_name=None,12 def create_object(request, app_label, model, template_name=None, 12 13 template_loader=loader, extra_context={}, post_save_redirect=None, 13 14 login_required=False, follow=None, context_processors=None): 14 15 """ 15 16 Generic object-creation function. 16 17 17 Templates: ``<app_label>/<mod ule_name>_form``18 Templates: ``<app_label>/<model_name>_form`` 18 19 Context: 19 20 form 20 21 the form wrapper for the object … … 22 23 if login_required and request.user.is_anonymous(): 23 24 return redirect_to_login(request.path) 24 25 25 mod = models.get_module(app_label, module_name)26 26 manipulator = mod.AddManipulator(follow=follow) 27 27 if request.POST: 28 28 # If data was POSTed, we're trying to create a new object 29 29 new_data = request.POST.copy() 30 30 31 if mod .Klass._meta.has_field_type(meta.FileField):31 if model.Klass._meta.has_field_type(meta.FileField): 32 32 new_data.update(request.FILES) 33 33 34 34 # Check for errors … … 40 40 new_object = manipulator.save(new_data) 41 41 42 42 if not request.user.is_anonymous(): 43 request.user.add_message("The %s was created sucessfully." % mod .Klass._meta.verbose_name)43 request.user.add_message("The %s was created sucessfully." % model.Klass._meta.verbose_name) 44 44 45 45 # Redirect to the new object: first by trying post_save_redirect, 46 46 # then by obj.get_absolute_url; fail if neither works. … … 58 58 # Create the FormWrapper, template, context, response 59 59 form = formfields.FormWrapper(manipulator, new_data, errors) 60 60 if not template_name: 61 template_name = "%s/%s_form" % (app_label, module_name)61 template_name = "%s/%s_form" % (app_label, lower(model.__name__)) 62 62 t = template_loader.get_template(template_name) 63 63 c = DjangoContext(request, { 64 64 'form': form, … … 70 70 c[key] = value 71 71 return HttpResponse(t.render(c)) 72 72 73 def update_object(request, app_label, mod ule_name, object_id=None, slug=None,73 def update_object(request, app_label, model, object_id=None, slug=None, 74 74 slug_field=None, template_name=None, template_loader=loader, 75 75 extra_lookup_kwargs={}, extra_context={}, post_save_redirect=None, 76 76 login_required=False, follow=None, context_processors=None): 77 77 """ 78 78 Generic object-update function. 79 79 80 Templates: ``<app_label>/<mod ule_name>_form``80 Templates: ``<app_label>/<model>_form`` 81 81 Context: 82 82 form 83 83 the form wrapper for the object … … 87 87 if login_required and request.user.is_anonymous(): 88 88 return redirect_to_login(request.path) 89 89 90 mod = models.get_module(app_label, module_name)91 90 92 91 # Look up the object to be edited 93 92 lookup_kwargs = {} 94 93 if object_id: 95 lookup_kwargs['%s__exact' % mod .Klass._meta.pk.name] = object_id94 lookup_kwargs['%s__exact' % model.Klass._meta.pk.name] = object_id 96 95 elif slug and slug_field: 97 96 lookup_kwargs['%s__exact' % slug_field] = slug 98 97 else: 99 98 raise AttributeError("Generic edit view must be called with either an object_id or a slug/slug_field") 100 99 lookup_kwargs.update(extra_lookup_kwargs) 101 100 try: 102 object = mod .get_object(**lookup_kwargs)101 object = model.objects.get_object(**lookup_kwargs) 103 102 except ObjectDoesNotExist: 104 raise Http404("%s .%s does not exist for %s" % (app_label, module_name, lookup_kwargs))103 raise Http404("%s does not exist for %s" % (model.__name__, lookup_kwargs)) 105 104 106 manipulator = mod .ChangeManipulator(object.id, follow=follow)105 manipulator = model.ChangeManipulator(object.id, follow=follow) 107 106 108 107 if request.POST: 109 108 new_data = request.POST.copy() … … 113 112 manipulator.save(new_data) 114 113 115 114 if not request.user.is_anonymous(): 116 request.user.add_message("The %s was updated sucessfully." % mod .Klass._meta.verbose_name)115 request.user.add_message("The %s was updated sucessfully." % model.Klass._meta.verbose_name) 117 116 118 117 # Do a post-after-redirect so that reload works, etc. 119 118 if post_save_redirect: … … 129 128 130 129 form = formfields.FormWrapper(manipulator, new_data, errors) 131 130 if not template_name: 132 template_name = "%s/%s_form" % (app_label, module_name)131 template_name = "%s/%s_form" % (app_label, lower(model.__name__)) 133 132 t = template_loader.get_template(template_name) 134 133 c = DjangoContext(request, { 135 134 'form': form, … … 141 140 else: 142 141 c[key] = value 143 142 response = HttpResponse(t.render(c)) 144 populate_xheaders(request, response, app_label, module_name, getattr(object, object._meta.pk.name))143 populate_xheaders(request, response, model.__name__, getattr(object, object._meta.pk.name)) 145 144 return response 146 145 147 def delete_object(request, app_label, mod ule_name, post_delete_redirect,146 def delete_object(request, app_label, model, post_delete_redirect, 148 147 object_id=None, slug=None, slug_field=None, template_name=None, 149 148 template_loader=loader, extra_lookup_kwargs={}, extra_context={}, 150 149 login_required=False, context_processors=None): … … 155 154 fetched using GET; for safty, deletion will only be performed if this 156 155 view is POSTed. 157 156 158 Templates: ``<app_label>/<mod ule_name>_confirm_delete``157 Templates: ``<app_label>/<model_name>_confirm_delete`` 159 158 Context: 160 159 object 161 160 the original object being deleted … … 163 162 if login_required and request.user.is_anonymous(): 164 163 return redirect_to_login(request.path) 165 164 166 mod = models.get_module(app_label, module_name)167 165 168 166 # Look up the object to be edited 169 167 lookup_kwargs = {} 170 168 if object_id: 171 lookup_kwargs['%s__exact' % mod .Klass._meta.pk.name] = object_id169 lookup_kwargs['%s__exact' % model.Klass._meta.pk.name] = object_id 172 170 elif slug and slug_field: 173 171 lookup_kwargs['%s__exact' % slug_field] = slug 174 172 else: 175 173 raise AttributeError("Generic delete view must be called with either an object_id or a slug/slug_field") 176 174 lookup_kwargs.update(extra_lookup_kwargs) 177 175 try: 178 object = mod .get_object(**lookup_kwargs)176 object = model.objects.get_object(**lookup_kwargs) 179 177 except ObjectDoesNotExist: 180 raise Http404("%s .%s does not exist for %s" % (app_label, module_name, lookup_kwargs))178 raise Http404("%s does not exist for %s" % (model.__name__, lookup_kwargs)) 181 179 182 180 if request.META['REQUEST_METHOD'] == 'POST': 183 181 object.delete() 184 182 if not request.user.is_anonymous(): 185 request.user.add_message("The %s was deleted." % mod .Klass._meta.verbose_name)183 request.user.add_message("The %s was deleted." % model.Klass._meta.verbose_name) 186 184 return HttpResponseRedirect(post_delete_redirect) 187 185 else: 188 186 if not template_name: 189 template_name = "%s/%s_confirm_delete" % (app_label, module_name)187 template_name = "%s/%s_confirm_delete" % (app_label, lower(model.__name__)) 190 188 t = template_loader.get_template(template_name) 191 189 c = DjangoContext(request, { 192 190 'object': object, … … 197 195 else: 198 196 c[key] = value 199 197 response = HttpResponse(t.render(c)) 200 populate_xheaders(request, response, app_label, module_name, getattr(object, object._meta.pk.name))198 populate_xheaders(request, response, model.__name__, getattr(object, object._meta.pk.name)) 201 199 return response