Ticket #1132: current_user_field_patch.diff

File current_user_field_patch.diff, 3.5 KB (added by jkocherhans <jkocherhans@…>, 19 years ago)
  • django/contrib/admin/views/main.py

     
    405405        manipulator.do_html2python(new_data)
    406406
    407407        if not errors and not request.POST.has_key("_preview"):
    408             new_object = manipulator.save(new_data)
     408            new_object = manipulator.save(new_data, current_user=request.user.id)
    409409            log_add_message(request.user, opts,manipulator,new_object)
    410410            msg = _('The %(name)s "%(obj)s" was added successfully.') % {'name':opts.verbose_name, 'obj':new_object}
    411411            pk_value = getattr(new_object,opts.pk.attname)
     
    484484
    485485        manipulator.do_html2python(new_data)
    486486        if not errors and not request.POST.has_key("_preview"):
    487             new_object = manipulator.save(new_data)
     487            new_object = manipulator.save(new_data, current_user=request.user.id)
    488488            log_change_message(request.user,opts,manipulator,new_object)
    489489            msg = _('The %(name)s "%(obj)s" was changed successfully.') % {'name': opts.verbose_name, 'obj':new_object}
    490490            pk_value = getattr(new_object,opts.pk.attname)
  • django/core/meta/fields.py

     
    741741                    return {self.attname: choice_list[1][0]}
    742742        return Field.flatten_data(self, follow, obj)
    743743
     744class 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
    744757class ManyToManyField(Field):
    745758    def __init__(self, to, **kwargs):
    746759        kwargs['verbose_name'] = kwargs.get('verbose_name', to._meta.verbose_name_plural)
  • django/core/meta/__init__.py

     
    17501750    if change and opts.get_ordered_objects():
    17511751        self.fields.append(formfields.CommaSeparatedIntegerField(field_name="order_"))
    17521752
    1753 def manipulator_save(opts, klass, add, change, self, new_data):
     1753def manipulator_save(opts, klass, add, change, self, new_data, current_user=None):
    17541754    # TODO: big cleanup when core fields go -> use recursive manipulators.
    17551755    from django.utils.datastructures import DotExpandedDict
    17561756    params = {}
     
    17591759        auto_now_add = change and getattr(f, 'auto_now_add', False)
    17601760        if self.follow.get(f.name, None) and not auto_now_add:
    17611761            param = f.get_manipulator_new_data(new_data)
     1762        # CurrentUserFields need to be set to the current user
     1763        elif isinstance(f, CurrentUserField):
     1764            # Only update this field if f.update_on_edit is True or we are in add_stage.
     1765            if f.update_on_edit or add:
     1766                param = current_user
     1767            else:
     1768                param = getattr(self.original_object, f.attname)
    17621769        else:
    17631770            if change:
    17641771                param = getattr(self.original_object, f.attname)
Back to Top