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+unsubscr...@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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.