Thanks Russ :)
Sent from my Windows Phone
-----Original Message-----
From: Russell Keith-Magee
Sent: 1/25/2013 4:08 PM
To: django-users@googlegroups.com
Subject: Re: Please explain this django admin code
On Fri, Jan 25, 2013 at 10:36 PM, Tundebabzy <tundebabzy@gmail.com> wrote:
I'll be really grateful if someone can take the time to explain these
lines of code. Its from contrib.admin.options
def wrap(view):
def wrapper(*args, **kwargs):
return self.admin_site.admin_view(view)(*args,
**kwargs) # This is the brain twisting line
return update_wrapper(wrapper, view)
The syntax foo.bar()(*osama **obama) is just blowing fuses in my brain
and then from the code above, I can't even find where admin_view is
defined.
Admin is really composed of two types of object -- an Admin Site
object, and a collection of ModelAdmin objects. The AdminSite object
governs the access to the admin site; a ModelAdmin object exists for
each model registered with the admin. As a result of that
relationship, every ModelAdmin object also knows the site that it is a
part of.
So - if you're in contrib.admin.options, you're looking at the code
for the ModelAdmin object. self.admin_site refers to the site that the
ModelAdmin instance belongs to; the admin_view method belongs to that
site object. It's essentially a decorator providing the permissions
check for the current user.
A decorator is just a function that takes a function as an argument,
and returns a function as a result. Since it returns a function, the
return value can itself be invoked.
So:
self.admin_site
is the admin site.
self.admin_site.admin_view
is a decorator method.
self.admin_site.admin_view(my_view)
is the application of the decorator applied to a specific view
self.admin_site.admin_view(my_view)(*args, **kwargs)
is the invocation of the decoration result using the given args and kwargs.
Yours,
Russ Magee %-)
--
You received this message because you are subscribed to the Google
Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscribe@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment