Skip to content

Instantly share code, notes, and snippets.

@johnboxall
Created January 26, 2011 04:13
Show Gist options
  • Select an option

  • Save johnboxall/796216 to your computer and use it in GitHub Desktop.

Select an option

Save johnboxall/796216 to your computer and use it in GitHub Desktop.
from django.core.exceptions import ObjectDoesNotExist
class InfoMiddleware(object):
'''
Record refer and user-agent info about this dude.
'''
def process_request(self, request):
if request.user.is_anonymous():
if 'referer' not in request.session:
request.session['referer'] = request.META.get('HTTP_REFERER')
elif 'user_agent' not in request.session:
user_agent = request.META.get('HTTP_USER_AGENT')
try:
profile = request.user.get_profile()
except ObjectDoesNotExist:
pass
else:
if profile.settings.get('user_agent') != user_agent:
profile.settings['user_agent'] = user_agent
profile.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment