Hi guys,

recently I found out that for much easier admin interface role-based
customization,
some methods that doesn't have access to request and edited object
now, needs them.
However, I don't want to apply threadlocal patch -- why it's not added
to django yet if it is the best way to go?
And I think adding request and object as parameter for all methods and
constant passing of them from one method to another is a waste as
well.

I.e, I might need to customize fields shown for different user roles (groups).

Currently I do it this way:

class MyModelAdmin(ModelAdmin):
    list_display_for_user = ...
    list_display_for_admin = ...

    @property
    def list_display(self):
        if self._request.user.is_superuser and
self._request.REQUEST['role'] == 'admin':
            return list_display_for_admin
        else:
            return list_display_for_user

For this, I currently need to amend ModelAdmin this way:

    def changelist_view(self, request):
        self._request = request
        self._object = None
        self._action = "changelist"
        return super(MyModelAdmin, self).changelist_view(request)

    def change_view(self, request, object_id):
        self._request = request
        self._object = None
        try:
            #XXX: lines are copied from parent, so why not save an
_object into field right there?
            #XXX: I have to copy all code of superclass change_view here
            #XXX: to have 1 sql request instead of 2 requests, that
are the same!
            self._object = model._default_manager.get(pk=object_id)
        except model.DoesNotExist:
            # Don't raise Http404 just yet, because we haven't checked
            # permissions yet. We don't want an unauthenticated user to be able
            # to determine whether a given object exists.
            self._object = None
        self._action = "change"
        return super(MyModelAdmin, self).change_view(request, object_id)

Another example:
    def save_change(self, request, model, form, formsets=None):
        #XXX: Actually, this task can be done with pre_save+post_save hooks.
        #XXX: But imagine you need request/form_data there as well.
        old_status = None
        if self._object: old_status = self._object.status
        new_status = form.cleaned_data['status']
        self._object.change_status(request, old_status, new_status)
        return super(MyModelAdmin, self).save_change(request, model,
form, formsets=None)

Third one:
    def formfield_for_dbfield(self, db_field, **kwargs):
        superclass = super(MyModelAdmin, self)
        field = superclass.formfield_for_dbfield(db_field, **kwargs)
        if db_field.name == 'channel' and isinstance(field.widget,
Select) and self._object:
            editable = [(x.id, unicode(x)) for x in
self._object.channels.filter(creator=self._request.user)]
            field.widget.choices = [('', '--------')]+editable

And so on.

So, how about an idea of adding request, object, and action to
ModelAdmin instances in newforms-admin code rather than in user code
and refactoring newforms-admin code based on this decision?

-- 
Best regards, Yuri V. Baburov, ICQ# 99934676, Skype: yuri.baburov,
MSN: [EMAIL PROTECTED]

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to