Django

Code

Ticket #1132: current_user_field_patch.3.diff

File current_user_field_patch.3.diff, 9.2 kB (added by jkocherhans <jkocherhans@mac.com>, 3 years ago)

patch for magic-removal

  • django/contrib/admin/views/stages/add.py

    old new  
    2222 
    2323    if not request.user.has_perm(app_label + '.' + opts.get_add_permission()): 
    2424        raise PermissionDenied 
    25     manipulator = model.AddManipulator() 
     25    c = Context(request) 
     26    manipulator = model.AddManipulator(context=c) 
    2627    if request.POST: 
    2728        new_data = request.POST.copy() 
    2829        if opts.has_field_type(models.FileField): 
     
    7879    # Populate the FormWrapper. 
    7980    form = formfields.FormWrapper(manipulator, new_data, errors) 
    8081 
    81     c = Context(request,
     82    extra_context =
    8283        'title': _('Add %s') % opts.verbose_name, 
    8384        'form': form, 
    8485        'is_popup': request.REQUEST.has_key('_popup'), 
    8586        'show_delete': show_delete, 
    8687        'path': path , 
    87     }) 
     88    } 
     89    c.update(extra_context) 
    8890 
    8991    if object_id_override is not None: 
    9092        c['object_id'] = object_id_override 
  • django/contrib/admin/views/stages/change.py

    old new  
    3838        return add_stage(request, path, form_url='../../add/') 
    3939     
    4040    try: 
    41         manipulator = model.ChangeManipulator(object_id) 
     41        c = Context(request) 
     42        manipulator = model.ChangeManipulator(object_id, context=c) 
    4243    except ObjectDoesNotExist: 
    4344        raise Http404 
    4445 
     
    9697    form.original = manipulator.original_object 
    9798    form.order_objects = [] 
    9899 
    99     c = Context(request,
     100    extra_context =
    100101        'title': _('Change %s') % opts.verbose_name, 
    101102        'form': form, 
    102103        'object_id': object_id, 
    103104        'original': manipulator.original_object, 
    104105        'is_popup': request.REQUEST.has_key('_popup'), 
    105106        'path': path , 
    106     }) 
     107    } 
     108    c.update(extra_context) 
    107109    return render_change_form(model, manipulator, app_label, c, change=True) 
  • django/db/models/manipulators.py

    old new  
    8282        setattr(other_cls, name, ManipulatorDescriptor(name, cls)) 
    8383    contribute_to_class = classmethod(contribute_to_class) 
    8484 
    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 
    8687        Naming.__init__(self, name_parts) 
    8788        if name_parts == (): 
    8889            self.follow = self.model._meta.get_follow(follow) 
     
    142143            if over: 
    143144                param = over 
    144145            else: 
    145                 # Fields with auto_now_add should keep their original value in the change stage. 
    146                 auto_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
    149150                else: 
    150151                    param = self.get_original_value(f) 
    151152            params[f.attname] = param 
     
    280281class ModelAddManipulator(AutomaticManipulator): 
    281282    change = False 
    282283    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
    285286 
    286287    def get_original_value(self, field): 
    287         return field.get_default(
     288        return field.get_default(context=self.context
    288289     
    289290    def __repr__(self): 
    290291        return "<Automatic AddManipulator '%s' for %s>" % (self.name_prefix, self.model.__name__, ) 
     
    293294    change = True 
    294295    add = False 
    295296 
    296     def __init__(self, obj_key=None, follow=None, name_parts=()): 
     297    def __init__(self, obj_key=None, follow=None, name_parts=(), context=None): 
    297298        assert obj_key is not None, "ChangeManipulator.__init__() must be passed obj_key parameter." 
    298299        opts = self.model._meta 
    299300        if isinstance(obj_key, self.model): 
     
    321322                else: 
    322323                    raise 
    323324         
    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
    325326        #self.original_object = original_object 
    326327 
    327328        #if self.opts.get_ordered_objects(): 
  • django/db/models/fields/__init__.py

    old new  
    185185        "Returns a boolean of whether this field has a default value." 
    186186        return self.default != NOT_PROVIDED 
    187187 
    188     def get_default(self): 
     188    def get_default(self, context=None): 
    189189        "Returns the default value for this field." 
    190190        if self.default != NOT_PROVIDED: 
    191191            if hasattr(self.default, '__get_value__'): 
     
    278278    def get_validator_unique_lookup_type(self): 
    279279        return '%s__exact' % f.name 
    280280 
    281     def get_manipulator_new_data(self, new_data, rel=False): 
     281    def get_manipulator_new_data(self, new_data, rel=False, context=None): 
    282282        """ 
    283283        Given the full new_data dictionary (from the manipulator), returns this 
    284284        field's data. 
     
    286286        #if rel: 
    287287        #    return new_data.get(self.name, [self.get_default()])[0] 
    288288        #else: 
    289         val = new_data.get(self.name, self.get_default()) 
     289        val = new_data.get(self.name, self.get_default(context=context)) 
    290290        if not self.empty_strings_allowed and val == '' and self.null: 
    291291            val = None 
    292292        return val 
     
    343343    def get_manipulator_field_objs(self): 
    344344        return [formfields.HiddenField] 
    345345 
    346     def get_manipulator_new_data(self, new_data, rel=False): 
     346    def get_manipulator_new_data(self, new_data, rel=False, context=None): 
    347347        # Never going to be called 
    348348        # Not in main change pages 
    349349        # ignored in related context 
    350350        if not rel: 
    351351            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
    353353 
    354354    def contribute_to_class(self, cls, name): 
    355355        if cls._meta.has_auto_field: 
     
    377377    empty_strings_allowed = False 
    378378    def __init__(self, verbose_name=None, name=None, auto_now=False, auto_now_add=False, **kwargs): 
    379379        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 
    380382        #HACKs : auto_now_add/auto_now should be done as a default or a pre_save. 
    381383        if auto_now or auto_now_add: 
    382384            kwargs['editable'] = False 
     
    440442    def get_manipulator_field_names(self, name_prefix): 
    441443        return [name_prefix + self.name + '_date', name_prefix + self.name + '_time'] 
    442444 
    443     def get_manipulator_new_data(self, new_data, rel=False): 
     445    def get_manipulator_new_data(self, new_data, rel=False, context=None): 
    444446        date_field, time_field = self.get_manipulator_field_names('') 
    445447        #if rel: 
    446448        #    d = new_data.get(date_field, [None])[0] 
  • django/db/models/fields/related.py

    old new  
    382382        self.edit_inline = False 
    383383        self.raw_id_admin = raw_id_admin 
    384384        assert not (self.raw_id_admin and self.filter_interface), "ManyToMany relationships may not use both raw_id_admin and filter_interface" 
     385 
     386class 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