Ticket #1132: current_user_field_patch.2.diff
File current_user_field_patch.2.diff, 8.0 KB (added by , 19 years ago) |
---|
-
django/contrib/admin/views/main.py
396 396 mod, opts = _get_mod_opts(app_label, module_name) 397 397 if not request.user.has_perm(app_label + '.' + opts.get_add_permission()): 398 398 raise PermissionDenied 399 manipulator = mod.AddManipulator() 399 # Create the context here so the manipulator can access it. 400 c = Context(request, {}) 401 manipulator = mod.AddManipulator(context=c) 402 400 403 if request.POST: 401 404 new_data = request.POST.copy() 402 405 if opts.has_field_type(meta.FileField): … … 437 440 # Populate the FormWrapper. 438 441 form = formfields.FormWrapper(manipulator, new_data, errors, edit_inline=True) 439 442 440 c = Context(request,{443 extra_context = { 441 444 'title': _('Add %s') % opts.verbose_name, 442 445 'form': form, 443 446 'is_popup': request.REQUEST.has_key('_popup'), 444 447 'show_delete': show_delete, 445 }) 448 } 449 c.update(extra_context) 446 450 if object_id_override is not None: 447 451 c['object_id'] = object_id_override 448 452 … … 471 475 if request.POST and request.POST.has_key("_saveasnew"): 472 476 return add_stage(request, app_label, module_name, form_url='../add/') 473 477 try: 474 manipulator = mod.ChangeManipulator(object_id) 478 # Create the context here so the manipulator can access it. 479 c = Context(request) 480 manipulator = mod.ChangeManipulator(object_id, context=c) 475 481 except ObjectDoesNotExist: 476 482 raise Http404 477 483 … … 532 538 orig_list = func() 533 539 form.order_objects.extend(orig_list) 534 540 535 c = Context(request,{541 extra_context = { 536 542 'title': _('Change %s') % opts.verbose_name, 537 543 'form': form, 538 544 'object_id': object_id, 539 545 'original': manipulator.original_object, 540 546 'is_popup' : request.REQUEST.has_key('_popup') 541 } )542 547 } 548 c.update(extra_context) 543 549 return render_change_form(opts,manipulator, app_label, c, change=True) 544 550 545 551 def _nest_help(obj, depth, val): -
django/core/meta/fields.py
197 197 "Returns a boolean of whether this field has a default value." 198 198 return self.default != NOT_PROVIDED 199 199 200 def get_default(self ):200 def get_default(self, context=None): 201 201 "Returns the default value for this field." 202 202 if self.default != NOT_PROVIDED: 203 203 if hasattr(self.default, '__get_value__'): … … 293 293 field_names = self.get_manipulator_field_names(name_prefix) 294 294 return [man(field_name=field_names[i], **params) for i, man in enumerate(field_objs)] 295 295 296 def get_manipulator_new_data(self, new_data, rel=False ):296 def get_manipulator_new_data(self, new_data, rel=False, context=None): 297 297 """ 298 298 Given the full new_data dictionary (from the manipulator), returns this 299 299 field's data. 300 300 """ 301 301 if rel: 302 return new_data.get(self.name, [self.get_default( )])[0]302 return new_data.get(self.name, [self.get_default(context)])[0] 303 303 else: 304 val = new_data.get(self.name, self.get_default( ))304 val = new_data.get(self.name, self.get_default(context)) 305 305 if not self.empty_strings_allowed and val == '' and self.null: 306 306 val = None 307 307 return val … … 358 358 def get_manipulator_field_objs(self): 359 359 return [formfields.HiddenField] 360 360 361 def get_manipulator_new_data(self, new_data, rel=False ):361 def get_manipulator_new_data(self, new_data, rel=False, context=None): 362 362 if not rel: 363 363 return None 364 return Field.get_manipulator_new_data(self, new_data, rel )364 return Field.get_manipulator_new_data(self, new_data, rel, context) 365 365 366 366 class BooleanField(Field): 367 367 def __init__(self, *args, **kwargs): … … 438 438 def get_manipulator_field_names(self, name_prefix): 439 439 return [name_prefix + self.name + '_date', name_prefix + self.name + '_time'] 440 440 441 def get_manipulator_new_data(self, new_data, rel=False ):441 def get_manipulator_new_data(self, new_data, rel=False, context=None): 442 442 date_field, time_field = self.get_manipulator_field_names('') 443 443 if rel: 444 444 d = new_data.get(date_field, [None])[0] … … 741 741 return {self.attname: choice_list[1][0]} 742 742 return Field.flatten_data(self, follow, obj) 743 743 744 class CurrentUserField(ForeignKey): 745 def __init__(self, to=None, update_on_edit=True, **kwargs): 746 self.update_on_edit = update_on_edit 747 if to is None: 748 from django.models.auth.users import User 749 to = User 750 # TODO: should these be overridable? I can't think of a reason why 751 # it would be necessary to do so. 752 kwargs['blank'] = True 753 kwargs['null'] = True 754 kwargs['editable'] = False 755 ForeignKey.__init__(self, to, **kwargs) 756 757 def get_default(self, context=None): 758 if context is not None: 759 return context['user'].id 760 else: 761 return None 762 763 def get_follow(self, override=None): 764 if override != None: 765 return override 766 else: 767 return True 768 744 769 class ManyToManyField(Field): 745 770 def __init__(self, to, **kwargs): 746 771 kwargs['verbose_name'] = kwargs.get('verbose_name', to._meta.verbose_name_plural) -
django/core/meta/__init__.py
1709 1709 setattr(man, k, v) 1710 1710 return man 1711 1711 1712 def manipulator_init(opts, add, change, self, obj_key=None, follow=None ):1712 def manipulator_init(opts, add, change, self, obj_key=None, follow=None, context=None): 1713 1713 self.follow = opts.get_follow(follow) 1714 self.context = context 1714 1715 1715 1716 if change: 1716 1717 assert obj_key is not None, "ChangeManipulator.__init__() must be passed obj_key parameter." … … 1758 1759 # Fields with auto_now_add should keep their original value in the change stage. 1759 1760 auto_now_add = change and getattr(f, 'auto_now_add', False) 1760 1761 if self.follow.get(f.name, None) and not auto_now_add: 1761 param = f.get_manipulator_new_data(new_data )1762 param = f.get_manipulator_new_data(new_data, context=self.context) 1762 1763 else: 1763 1764 if change: 1764 1765 param = getattr(self.original_object, f.attname) … … 1833 1834 pass 1834 1835 1835 1836 for f in related.opts.fields: 1836 if f.core and not isinstance(f, FileField) and f.get_manipulator_new_data(rel_new_data, rel=True ) in (None, ''):1837 if f.core and not isinstance(f, FileField) and f.get_manipulator_new_data(rel_new_data, rel=True, context=self.context) in (None, ''): 1837 1838 all_cores_given = False 1838 elif f.core and not isinstance(f, FileField) and f.get_manipulator_new_data(rel_new_data, rel=True ) not in (None, ''):1839 elif f.core and not isinstance(f, FileField) and f.get_manipulator_new_data(rel_new_data, rel=True, context=self.context) not in (None, ''): 1839 1840 all_cores_blank = False 1840 1841 # If this field isn't editable, give it the same value it had 1841 1842 # previously, according to the given ID. If the ID wasn't … … 1852 1853 else: 1853 1854 param = f.get_default() 1854 1855 else: 1855 param = f.get_manipulator_new_data(rel_new_data, rel=True )1856 param = f.get_manipulator_new_data(rel_new_data, rel=True, context=context) 1856 1857 if param != None: 1857 1858 params[f.attname] = param 1858 1859