Ticket #1164: manipulator_contexts.diff
File manipulator_contexts.diff, 14.2 KB (added by , 19 years ago) |
---|
-
django/contrib/admin/views/stages/add.py
4 4 from django.contrib.admin.views.stages.modify import render_change_form 5 5 from django.core import formfields, template 6 6 from django.core.extensions import DjangoContext as Context 7 from django.core.extensions import ManipulatorContext 7 8 from django.db import models 8 9 from django.utils.httpwrappers import HttpResponse, HttpResponseRedirect 9 10 from django.utils.text import capfirst, get_text_list … … 22 23 23 24 if not request.user.has_perm(app_label + '.' + opts.get_add_permission()): 24 25 raise PermissionDenied 25 manipulator = model.AddManipulator() 26 context = ManipulatorContext(model, request) 27 manipulator = model.AddManipulator(context=context) 26 28 if request.POST: 27 29 new_data = request.POST.copy() 28 30 if opts.has_field_type(models.FileField): -
django/contrib/admin/views/stages/change.py
1 1 from django.contrib.admin.views.main import get_model_and_app 2 2 from django.core import formfields, template 3 3 from django.core.extensions import DjangoContext as Context 4 from django.core.extensions import ManipulatorContext 4 5 from django.contrib.admin.views.stages.modify import render_change_form 5 6 from django.db import models 6 7 from django.utils.text import capfirst, get_text_list … … 36 37 raise PermissionDenied 37 38 if request.POST and request.POST.has_key("_saveasnew"): 38 39 return add_stage(request, path, form_url='../../add/') 40 context = ManipulatorContext(model, request) 39 41 40 42 try: 41 manipulator = model.ChangeManipulator(object_id )43 manipulator = model.ChangeManipulator(object_id, context=context) 42 44 except ObjectDoesNotExist: 43 45 raise Http404 44 46 -
django/db/models/manipulators.py
62 62 63 63 name = property(_get_name) 64 64 65 # TODO: I don't like initializing the manipulator with the context, but it's 66 # easier than passing around the context to a bunch of different methods in 67 # the manipulator 65 68 class AutomaticManipulator(Manipulator, Naming): 66 69 def _prepare(cls, model): 67 70 cls.model = model … … 82 85 setattr(other_cls, name, ManipulatorDescriptor(name, cls)) 83 86 contribute_to_class = classmethod(contribute_to_class) 84 87 85 def __init__(self, original_object=None, follow=None, name_parts=() 88 def __init__(self, original_object=None, follow=None, name_parts=(), context=None): 86 89 Naming.__init__(self, name_parts) 87 90 if name_parts == (): 88 91 self.follow = self.model._meta.get_follow(follow) … … 90 93 self.follow = follow 91 94 self.fields_, self.children = [], {} 92 95 self.original_object = original_object 96 self.context = context 93 97 for f in self.opts.get_data_holders(self.follow): 94 98 fol = self.follow[f.name] 95 fields,manipulators = f.get_fields_and_manipulators(self.opts, self, follow=fol )99 fields,manipulators = f.get_fields_and_manipulators(self.opts, self, follow=fol, context=self.context) 96 100 97 101 if fields != None: 98 102 self.fields_.extend(fields) … … 145 149 # Fields with auto_now_add should keep their original value in the change stage. 146 150 auto_now_add = self.change and getattr(f, 'auto_now_add', False) 147 151 if self.follow.get(f.name, None) and not auto_now_add: 148 param = f.get_manipulator_new_data(expanded_data )152 param = f.get_manipulator_new_data(expanded_data, context=self.context) 149 153 else: 150 154 param = self.get_original_value(f) 151 155 params[f.attname] = param … … 280 284 class ModelAddManipulator(AutomaticManipulator): 281 285 change = False 282 286 add = True 283 def __init__(self, follow=None, name_parts=() ):284 super(ModelAddManipulator, self).__init__(follow=follow, name_parts=name_parts )287 def __init__(self, follow=None, name_parts=(), context=None): 288 super(ModelAddManipulator, self).__init__(follow=follow, name_parts=name_parts, context=context) 285 289 286 290 def get_original_value(self, field): 287 291 return field.get_default() … … 293 297 change = True 294 298 add = False 295 299 296 def __init__(self, obj_key=None, follow=None, name_parts=() ):300 def __init__(self, obj_key=None, follow=None, name_parts=(), context=None): 297 301 assert obj_key is not None, "ChangeManipulator.__init__() must be passed obj_key parameter." 298 302 opts = self.model._meta 299 303 if isinstance(obj_key, self.model): … … 321 325 else: 322 326 raise 323 327 324 super(ModelChangeManipulator, self).__init__(original_object=original_object, follow=follow, name_parts=name_parts )328 super(ModelChangeManipulator, self).__init__(original_object=original_object, follow=follow, name_parts=name_parts, context=context) 325 329 #self.original_object = original_object 326 330 327 331 #if self.opts.get_ordered_objects(): … … 336 340 return "<Automatic ChangeManipulator '%s' for %s:%r >" % (self.name_prefix, self.model.__name__, self.obj_key) 337 341 338 342 class ManipulatorCollection(list, Naming): 339 def __init__(self, model, follow, name_parts=() ):343 def __init__(self, model, follow, name_parts=(), context=None): 340 344 Naming.__init__(self, name_parts) 341 345 self.model = model 342 346 self.follow = follow 347 self.context = context 343 348 self._load() 344 349 345 350 def _get_list(self): … … 349 354 man_class = self.model.ChangeManipulator 350 355 351 356 for i,obj in enumerate(self._get_list()): 352 self.append(man_class(obj, self.follow, self.name_parts + (str(i),)))357 self.append(man_class(obj, self.follow, self.name_parts + (str(i),), context=self.context)) 353 358 354 359 def _save_child(self, manip, parent_key): 355 360 manip.save_from_update() … … 438 443 self.append(None) 439 444 440 445 prefix = '%s%s.' % (self.name_prefix, index ) 441 child_manip = man_class(self.follow, self.name_parts + ( str(index), ))446 child_manip = man_class(self.follow, self.name_parts + (str(index),), context=self.context) 442 447 443 448 self[index] = child_manip 444 449 return child_manip -
django/db/models/fields/__init__.py
218 218 field_objs = self.get_manipulator_field_objs() 219 219 return (field_objs,params) 220 220 221 def get_fields_and_manipulators(self, opts, manipulator, follow 221 def get_fields_and_manipulators(self, opts, manipulator, follow, context=None): 222 222 change = manipulator.change 223 223 rel = manipulator.name_prefix != '' 224 224 name_prefix = manipulator.name_prefix … … 278 278 def get_validator_unique_lookup_type(self): 279 279 return '%s__exact' % f.name 280 280 281 def get_manipulator_new_data(self, new_data, rel=False ):281 def get_manipulator_new_data(self, new_data, rel=False, context=None): 282 282 """ 283 283 Given the full new_data dictionary (from the manipulator), returns this 284 284 field's data. … … 329 329 def bind(self, fieldmapping, original, bound_field_class=BoundField): 330 330 return bound_field_class(self, fieldmapping, original) 331 331 332 def get_context_processors(self): 333 return [] 334 332 335 class AutoField(Field): 333 336 empty_strings_allowed = False 334 337 def __init__(self, *args, **kwargs): … … 343 346 def get_manipulator_field_objs(self): 344 347 return [formfields.HiddenField] 345 348 346 def get_manipulator_new_data(self, new_data, rel=False ):349 def get_manipulator_new_data(self, new_data, rel=False, context=None): 347 350 # Never going to be called 348 351 # Not in main change pages 349 352 # ignored in related context 350 353 if not rel: 351 354 return None 352 return Field.get_manipulator_new_data(self, new_data, rel )355 return Field.get_manipulator_new_data(self, new_data, rel, context) 353 356 354 357 def contribute_to_class(self, cls, name): 355 358 if cls._meta.has_auto_field: … … 440 443 def get_manipulator_field_names(self, name_prefix): 441 444 return [name_prefix + self.name + '_date', name_prefix + self.name + '_time'] 442 445 443 def get_manipulator_new_data(self, new_data, rel=False ):446 def get_manipulator_new_data(self, new_data, rel=False, context=None): 444 447 date_field, time_field = self.get_manipulator_field_names('') 445 448 #if rel: 446 449 # d = new_data.get(date_field, [None])[0] -
django/db/models/related.py
1 1 from django.db.models.manipulators import ManipulatorCollection 2 2 3 3 class RelatedManipulatorCollection(ManipulatorCollection): 4 def __init__(self, related, parent_name_parts , instance, follow):4 def __init__(self, related, parent_name_parts , instance, follow, context=None): 5 5 name_parts = parent_name_parts + (related.var_name, ) 6 6 self.instance = instance 7 7 self.related = related 8 8 super(RelatedManipulatorCollection, self).__init__( 9 related.model, follow,name_parts)9 related.model, follow, name_parts, context) 10 10 11 11 def _save_child(self, manip, parent_key): 12 12 setattr(manip.original_object, self.related.field.attname, parent_key) … … 96 96 def __repr__(self): 97 97 return "<RelatedObject: %s related to %s>" % (self.name, self.field.name) 98 98 99 def get_fields_and_manipulators(self, opts, manipulator, follow 100 return ([], self.get_manipulators(manipulator, follow ) )99 def get_fields_and_manipulators(self, opts, manipulator, follow, context=None): 100 return ([], self.get_manipulators(manipulator, follow, context) ) 101 101 102 def get_manipulators(self, parent_manipulator, follow):102 def get_manipulators(self, parent_manipulator, follow, context=None): 103 103 name_parts = parent_manipulator.name_parts 104 104 obj = parent_manipulator.original_object 105 105 106 return RelatedManipulatorCollection(self, name_parts, obj, follow )106 return RelatedManipulatorCollection(self, name_parts, obj, follow, context) 107 107 108 108 def bind(self, field_mapping, original, bound_related_object_class=BoundRelatedObject): 109 109 return bound_related_object_class(self, field_mapping, original) -
django/core/extensions.py
62 62 for processor in get_standard_processors() + processors: 63 63 self.update(processor(request)) 64 64 65 # TODO: should this really be a subclass of template.Context? It should 66 # (mostly) behave the same, but really has nothing to do with the template 67 # system. Maybe template.Context ought to be moved to just django.core.Context 68 class ManipulatorContext(Context): 69 """ 70 This subclass of template.Context automatically populates itself using 71 the processors defined in each field's get_context_processors method. 72 Additional processors can be specified as a list of callables 73 using the "processors" keyword argument. 74 """ 75 def __init__(self, model, request, dict=None, processors=None): 76 Context.__init__(self, dict) 77 78 standard_processors = [] 79 # Get all the processors from the fields in this model. 80 for f in model._meta.fields: 81 field_context_processors = f.get_context_processors() 82 standard_processors.extend(field_context_processors) 83 standard_processors = tuple(standard_processors) 84 if processors is None: 85 processors = () 86 else: 87 processors = tuple(processors) 88 for processor in standard_processors + processors: 89 self.update(processor(request)) 90 91 92 65 93 # PermWrapper and PermLookupDict proxy the permissions system into objects that 66 94 # the template system can understand. 67 95 -
django/views/generic/create_update.py
3 3 from django.core.template import loader 4 4 from django.core import formfields, meta 5 5 from django.views.auth.login import redirect_to_login 6 from django.core.extensions import DjangoContext 6 from django.core.extensions import DjangoContext, ManipulatorContext 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 … … 23 23 return redirect_to_login(request.path) 24 24 25 25 mod = models.get_module(app_label, module_name) 26 manipulator = mod.AddManipulator(follow=follow) 26 context = ManipulatorContext(mod, request) 27 manipulator = model.AddManipulator(follow=follow, context=context) 27 28 if request.POST: 28 29 # If data was POSTed, we're trying to create a new object 29 30 new_data = request.POST.copy() … … 88 89 return redirect_to_login(request.path) 89 90 90 91 mod = models.get_module(app_label, module_name) 92 context = ManipulatorContext(mod, request) 91 93 92 94 # Look up the object to be edited 93 95 lookup_kwargs = {} … … 103 105 except ObjectDoesNotExist: 104 106 raise Http404("%s.%s does not exist for %s" % (app_label, module_name, lookup_kwargs)) 105 107 106 manipulator = mod.ChangeManipulator(object.id, follow=follow )108 manipulator = mod.ChangeManipulator(object.id, follow=follow, context=context) 107 109 108 110 if request.POST: 109 111 new_data = request.POST.copy()