Ticket #1132: current_user_field_patch.3.diff
File current_user_field_patch.3.diff, 9.2 KB (added by , 19 years ago) |
---|
-
django/contrib/admin/views/stages/add.py
22 22 23 23 if not request.user.has_perm(app_label + '.' + opts.get_add_permission()): 24 24 raise PermissionDenied 25 manipulator = model.AddManipulator() 25 c = Context(request) 26 manipulator = model.AddManipulator(context=c) 26 27 if request.POST: 27 28 new_data = request.POST.copy() 28 29 if opts.has_field_type(models.FileField): … … 78 79 # Populate the FormWrapper. 79 80 form = formfields.FormWrapper(manipulator, new_data, errors) 80 81 81 c = Context(request,{82 extra_context = { 82 83 'title': _('Add %s') % opts.verbose_name, 83 84 'form': form, 84 85 'is_popup': request.REQUEST.has_key('_popup'), 85 86 'show_delete': show_delete, 86 87 'path': path , 87 }) 88 } 89 c.update(extra_context) 88 90 89 91 if object_id_override is not None: 90 92 c['object_id'] = object_id_override -
django/contrib/admin/views/stages/change.py
38 38 return add_stage(request, path, form_url='../../add/') 39 39 40 40 try: 41 manipulator = model.ChangeManipulator(object_id) 41 c = Context(request) 42 manipulator = model.ChangeManipulator(object_id, context=c) 42 43 except ObjectDoesNotExist: 43 44 raise Http404 44 45 … … 96 97 form.original = manipulator.original_object 97 98 form.order_objects = [] 98 99 99 c = Context(request,{100 extra_context = { 100 101 'title': _('Change %s') % opts.verbose_name, 101 102 'form': form, 102 103 'object_id': object_id, 103 104 'original': manipulator.original_object, 104 105 'is_popup': request.REQUEST.has_key('_popup'), 105 106 'path': path , 106 }) 107 } 108 c.update(extra_context) 107 109 return render_change_form(model, manipulator, app_label, c, change=True) -
django/db/models/manipulators.py
82 82 setattr(other_cls, name, ManipulatorDescriptor(name, cls)) 83 83 contribute_to_class = classmethod(contribute_to_class) 84 84 85 def __init__(self, original_object=None, follow=None, name_parts=() ): 85 def __init__(self, original_object=None, follow=None, name_parts=(), context=None): 86 self.context = context 86 87 Naming.__init__(self, name_parts) 87 88 if name_parts == (): 88 89 self.follow = self.model._meta.get_follow(follow) … … 142 143 if over: 143 144 param = over 144 145 else: 145 # Fields with a uto_now_addshould keep their original value in the change stage.146 a uto_now_add = self.change and getattr(f, 'auto_now_add', False)147 if self.follow.get(f.name, None) and not auto_now_add:148 param = f.get_manipulator_new_data(expanded_data )146 # Fields with allow_updates=False should keep their original value in the change stage. 147 allow_updates = self.add or getattr(f, 'allow_updates', True) 148 if self.follow.get(f.name, None) and allow_updates: 149 param = f.get_manipulator_new_data(expanded_data, context=self.context) 149 150 else: 150 151 param = self.get_original_value(f) 151 152 params[f.attname] = param … … 280 281 class ModelAddManipulator(AutomaticManipulator): 281 282 change = False 282 283 add = True 283 def __init__(self, follow=None, name_parts=() ):284 super(ModelAddManipulator, self).__init__(follow=follow, name_parts=name_parts )284 def __init__(self, follow=None, name_parts=(), context=None): 285 super(ModelAddManipulator, self).__init__(follow=follow, name_parts=name_parts, context=context) 285 286 286 287 def get_original_value(self, field): 287 return field.get_default( )288 return field.get_default(context=self.context) 288 289 289 290 def __repr__(self): 290 291 return "<Automatic AddManipulator '%s' for %s>" % (self.name_prefix, self.model.__name__, ) … … 293 294 change = True 294 295 add = False 295 296 296 def __init__(self, obj_key=None, follow=None, name_parts=() ):297 def __init__(self, obj_key=None, follow=None, name_parts=(), context=None): 297 298 assert obj_key is not None, "ChangeManipulator.__init__() must be passed obj_key parameter." 298 299 opts = self.model._meta 299 300 if isinstance(obj_key, self.model): … … 321 322 else: 322 323 raise 323 324 324 super(ModelChangeManipulator, self).__init__(original_object=original_object, follow=follow, name_parts=name_parts )325 super(ModelChangeManipulator, self).__init__(original_object=original_object, follow=follow, name_parts=name_parts, context=context) 325 326 #self.original_object = original_object 326 327 327 328 #if self.opts.get_ordered_objects(): -
django/db/models/fields/__init__.py
185 185 "Returns a boolean of whether this field has a default value." 186 186 return self.default != NOT_PROVIDED 187 187 188 def get_default(self ):188 def get_default(self, context=None): 189 189 "Returns the default value for this field." 190 190 if self.default != NOT_PROVIDED: 191 191 if hasattr(self.default, '__get_value__'): … … 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. … … 286 286 #if rel: 287 287 # return new_data.get(self.name, [self.get_default()])[0] 288 288 #else: 289 val = new_data.get(self.name, self.get_default( ))289 val = new_data.get(self.name, self.get_default(context=context)) 290 290 if not self.empty_strings_allowed and val == '' and self.null: 291 291 val = None 292 292 return val … … 343 343 def get_manipulator_field_objs(self): 344 344 return [formfields.HiddenField] 345 345 346 def get_manipulator_new_data(self, new_data, rel=False ):346 def get_manipulator_new_data(self, new_data, rel=False, context=None): 347 347 # Never going to be called 348 348 # Not in main change pages 349 349 # ignored in related context 350 350 if not rel: 351 351 return None 352 return Field.get_manipulator_new_data(self, new_data, rel )352 return Field.get_manipulator_new_data(self, new_data, rel, context) 353 353 354 354 def contribute_to_class(self, cls, name): 355 355 if cls._meta.has_auto_field: … … 377 377 empty_strings_allowed = False 378 378 def __init__(self, verbose_name=None, name=None, auto_now=False, auto_now_add=False, **kwargs): 379 379 self.auto_now, self.auto_now_add = auto_now, auto_now_add 380 # If auto_now_add is True, we don't want to allow updates to this field. 381 self.allow_updates = not auto_now_add 380 382 #HACKs : auto_now_add/auto_now should be done as a default or a pre_save. 381 383 if auto_now or auto_now_add: 382 384 kwargs['editable'] = False … … 440 442 def get_manipulator_field_names(self, name_prefix): 441 443 return [name_prefix + self.name + '_date', name_prefix + self.name + '_time'] 442 444 443 def get_manipulator_new_data(self, new_data, rel=False ):445 def get_manipulator_new_data(self, new_data, rel=False, context=None): 444 446 date_field, time_field = self.get_manipulator_field_names('') 445 447 #if rel: 446 448 # d = new_data.get(date_field, [None])[0] -
django/db/models/fields/related.py
382 382 self.edit_inline = False 383 383 self.raw_id_admin = raw_id_admin 384 384 assert not (self.raw_id_admin and self.filter_interface), "ManyToMany relationships may not use both raw_id_admin and filter_interface" 385 386 class CurrentUserField(ForeignKey): 387 def __init__(self, to=None, allow_updates=True, **kwargs): 388 self.allow_updates = allow_updates 389 if to is None: 390 from django.models.auth import User 391 to = User 392 # TODO: should these be overridable? I can't think of a reason why 393 # it would be necessary to do so. 394 kwargs['blank'] = True 395 kwargs['null'] = True 396 kwargs['editable'] = False 397 ForeignKey.__init__(self, to, **kwargs) 398 399 def get_default(self, context=None): 400 if context is not None: 401 return context['user'].id 402 else: 403 return None 404 405 def get_follow(self, override=None): 406 if override != None: 407 return override 408 else: 409 return True