Ticket #1132: current_user_field_patch.2.diff

File current_user_field_patch.2.diff, 8.0 KB (added by jkocherhans <jkocherhans@…>, 18 years ago)

updated with ideas from rjwittams

  • django/contrib/admin/views/main.py

     
    396396    mod, opts = _get_mod_opts(app_label, module_name)
    397397    if not request.user.has_perm(app_label + '.' + opts.get_add_permission()):
    398398        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
    400403    if request.POST:
    401404        new_data = request.POST.copy()
    402405        if opts.has_field_type(meta.FileField):
     
    437440    # Populate the FormWrapper.
    438441    form = formfields.FormWrapper(manipulator, new_data, errors, edit_inline=True)
    439442
    440     c = Context(request, {
     443    extra_context = {
    441444        'title': _('Add %s') % opts.verbose_name,
    442445        'form': form,
    443446        'is_popup': request.REQUEST.has_key('_popup'),
    444447        'show_delete': show_delete,
    445     })
     448    }
     449    c.update(extra_context)
    446450    if object_id_override is not None:
    447451        c['object_id'] = object_id_override
    448452
     
    471475    if request.POST and request.POST.has_key("_saveasnew"):
    472476        return add_stage(request, app_label, module_name, form_url='../add/')
    473477    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)
    475481    except ObjectDoesNotExist:
    476482        raise Http404
    477483
     
    532538            orig_list = func()
    533539            form.order_objects.extend(orig_list)
    534540
    535     c = Context(request, {
     541    extra_context = {
    536542        'title': _('Change %s') % opts.verbose_name,
    537543        'form': form,
    538544        'object_id': object_id,
    539545        'original': manipulator.original_object,
    540546        'is_popup' : request.REQUEST.has_key('_popup')
    541     })
    542 
     547    }
     548    c.update(extra_context)
    543549    return render_change_form(opts,manipulator, app_label, c, change=True)
    544550
    545551def _nest_help(obj, depth, val):
  • django/core/meta/fields.py

     
    197197        "Returns a boolean of whether this field has a default value."
    198198        return self.default != NOT_PROVIDED
    199199
    200     def get_default(self):
     200    def get_default(self, context=None):
    201201        "Returns the default value for this field."
    202202        if self.default != NOT_PROVIDED:
    203203            if hasattr(self.default, '__get_value__'):
     
    293293        field_names = self.get_manipulator_field_names(name_prefix)
    294294        return [man(field_name=field_names[i], **params) for i, man in enumerate(field_objs)]
    295295
    296     def get_manipulator_new_data(self, new_data, rel=False):
     296    def get_manipulator_new_data(self, new_data, rel=False, context=None):
    297297        """
    298298        Given the full new_data dictionary (from the manipulator), returns this
    299299        field's data.
    300300        """
    301301        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]
    303303        else:
    304             val = new_data.get(self.name, self.get_default())
     304            val = new_data.get(self.name, self.get_default(context))
    305305            if not self.empty_strings_allowed and val == '' and self.null:
    306306                val = None
    307307            return val
     
    358358    def get_manipulator_field_objs(self):
    359359        return [formfields.HiddenField]
    360360
    361     def get_manipulator_new_data(self, new_data, rel=False):
     361    def get_manipulator_new_data(self, new_data, rel=False, context=None):
    362362        if not rel:
    363363            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)
    365365
    366366class BooleanField(Field):
    367367    def __init__(self, *args, **kwargs):
     
    438438    def get_manipulator_field_names(self, name_prefix):
    439439        return [name_prefix + self.name + '_date', name_prefix + self.name + '_time']
    440440
    441     def get_manipulator_new_data(self, new_data, rel=False):
     441    def get_manipulator_new_data(self, new_data, rel=False, context=None):
    442442        date_field, time_field = self.get_manipulator_field_names('')
    443443        if rel:
    444444            d = new_data.get(date_field, [None])[0]
     
    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
     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
    744769class ManyToManyField(Field):
    745770    def __init__(self, to, **kwargs):
    746771        kwargs['verbose_name'] = kwargs.get('verbose_name', to._meta.verbose_name_plural)
  • django/core/meta/__init__.py

     
    17091709        setattr(man, k, v)
    17101710    return man
    17111711
    1712 def manipulator_init(opts, add, change, self, obj_key=None, follow=None):
     1712def manipulator_init(opts, add, change, self, obj_key=None, follow=None, context=None):
    17131713    self.follow = opts.get_follow(follow)
     1714    self.context = context
    17141715
    17151716    if change:
    17161717        assert obj_key is not None, "ChangeManipulator.__init__() must be passed obj_key parameter."
     
    17581759        # Fields with auto_now_add should keep their original value in the change stage.
    17591760        auto_now_add = change and getattr(f, 'auto_now_add', False)
    17601761        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)
    17621763        else:
    17631764            if change:
    17641765                param = getattr(self.original_object, f.attname)
     
    18331834                            pass
    18341835
    18351836                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, ''):
    18371838                        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, ''):
    18391840                        all_cores_blank = False
    18401841                    # If this field isn't editable, give it the same value it had
    18411842                    # previously, according to the given ID. If the ID wasn't
     
    18521853                        else:
    18531854                            param = f.get_default()
    18541855                    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)
    18561857                    if param != None:
    18571858                       params[f.attname] = param
    18581859
Back to Top