On Tue, Oct 11, 2011 at 8:11 PM, Steven Cummings <estebistec@gmail.com> wrote:
The wrapper that you typically see is the new function that is returned. This usually invokes the original, given function in addition to some extra ("decorating") logic around that call. Based on that question it sounds like you might want to check out some introductory material on decorators [1][2].Django view decorators simply need to be able to decorate a view function or class, which means they need to support decorating something that takes a request and optional args and returns a response. That means your wrapper function can specify this much of the signature, and that's how you get ahold of the request to work with. So, based on your pseudocode:def intercept_foos(view_func):def wrapper(request, *args, **kwargs):if request.user.foo():return redirect('/foo')return view_func(request, *args, **kwargs)return wrapperThen just apply it as you did in your urls.py example.Hope this helps.[1] http://stackoverflow.com/questions/739654/understanding-python-decorators - A good starting point[2] http://micheles.googlecode.com/hg/decorator/documentation.html - A handy module and discussion of preserving function signatures--Steven
On Tue, Oct 11, 2011 at 4:59 PM, Kurtis Mullins <kurtis.mullins@gmail.com> wrote:Thanks Fred! I tried to look through your code and understand what's going on -- but I'm still at a loss. I'm guessing I need to look at the Django source to see what should be returned when you hit a URL and what is passed. The wrapper is confusing me and I've seen that in Django code as well. I'm going to go out on a limb and say it's probably not as easy as I hoped, haha.
On Tue, Oct 11, 2011 at 4:42 PM, Sells, Fred <fred.sells@adventistcare.org> wrote:I'm no expert but this is what I built to log all user actions -- warts
and all
def decorate(func):
##################print 'Decorating %s...' % func.__name__
def wrapped( *args, **kwargs):
request = args[0]
if len(args)>1: command=str(args[1])
else: command = ''
ipaddr = request.META['REMOTE_ADDR']
user = request.session.get('user', None)
if user: userid = user.userid
else: userid = '?'
qs = extract_querystring_from_request(request)
parameters = Parameters(**qs)
resid = parameters.pop('resid', '')
assessmentid = parameters.pop('id', 0)
facility = parameters.pop('facility','')
modified = func.__name__ in ("setvalues", "setraw", "create",
'editcaas')
### print func.__name__, 'zzzzzzzzzzzzzzzzzzzzz', modified, qs
modified = modified or (func.__name__=='command' and
command!='print')
fieldnames = parameters.values().keys()
for signature in SIGNATURE_FIELDS:
if signature in fieldnames: command='signit'
if (not facility) and resid:
facility = resid[:2]
option = str(parameters)[:110]
action = '%s:%s' % (func.__name__, command)
### print "\n\n\n\ncalled wrapped function with ", (action,
option, str(parameters))
record = models.HipaaLog.objects.create(Version=K.VERSION,
userid=userid,
Action=action,
Options=option,
#StartTime =
datetime.datetime.now(),
AssessmentId=assessmentid, ResidentId=resid,
Modified = modified,
IpAddress = ipaddr,
Facility=facility)
#print record.Modified, record.Action, 'record'
results = func( *args, **kwargs)
#record.StopTime = datetime.datetime.now()
#record.save()
return results
############################print 'done'
return wrapped
--------------------------------
@decorate
def command(request, *args, **kwargs):
...
-----Original Message-----
From: django-users@googlegroups.com
[mailto:django-users@googlegroups.com] On Behalf Of Kurtis
Sent: Tuesday, October 11, 2011 12:03 PM
To: Django users
Subject: Sample Custom Decorator
Hey Guys,
Would anyone be willing to show me an example of a very simple and
dumb decorator for views? I've been trying to read the existing
decorators and play with a couple of snippets but I'm having a lot of
trouble with two aspects -- grabbing the User Instance and Redirecting
somewhere besides Login (without making it look like a hack).
Here's some ugly pseudo-code for what I'm trying to accomplish...
# Custom Decorator
def my_decorator(function = None)
# Grab Data
request = how_do_i_get_this?
user = request.user
# Perform Logic, Redirect or Continue Normally
if user.foo():
redirect to '/foo' URL
else
display requested view?
# Decorated TemplateView
url(r'^$', my_decorator(TemplateView.as_view(template_name =
'bar.html'))),
Thanks!
--
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.
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en.
--
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.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
--
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.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
--
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.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
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.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
No comments:
Post a Comment