PageRenderTime 108ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/djangoplus/middleware/__init__.py

http://django-plus.googlecode.com/
Python | 73 lines | 62 code | 6 blank | 5 comment | 4 complexity | b2bd4e86c45ffa8be7c803d53f2695ee MD5 | raw file
Possible License(s): LGPL-3.0
  1. import hmac, sha, base64, os
  2. from django.conf import settings
  3. from django.shortcuts import render_to_response
  4. from django.template import RequestContext
  5. from django.http import HttpResponseRedirect, HttpResponse
  6. from django.utils.translation import ugettext as _
  7. from django.template.defaultfilters import slugify
  8. try:
  9. import Image, ImageDraw, ImageFont
  10. except:
  11. from PIL import Image, ImageDraw, ImageFont
  12. from djangoplus import app_settings
  13. class FakeSessionCookieMiddleware(object):
  14. """
  15. Thanks to Dan Fairs
  16. The source post is available at:
  17. http://www.stereoplex.com/two-voices/cookieless-django-sessions-and-authentication-without-cookies
  18. """
  19. def process_request(self, request):
  20. if request.GET.has_key(settings.SESSION_COOKIE_NAME):
  21. request.COOKIES[settings.SESSION_COOKIE_NAME] = request.GET[settings.SESSION_COOKIE_NAME]
  22. elif request.POST.has_key(settings.SESSION_COOKIE_NAME):
  23. request.COOKIES[settings.SESSION_COOKIE_NAME] = request.POST[settings.SESSION_COOKIE_NAME]
  24. class ProtectAntiRobotsMiddleware(object):
  25. def process_request(self, request):
  26. current_domain = app_settings.ROBOT_PROTECTION_DOMAIN
  27. if request.path_info.startswith('/protectantirobots/'):
  28. path = request.GET.get('path', '') or request.POST.get('path', '')
  29. if request.path == '/protectantirobots/img/':
  30. s = base64.b64decode(request.COOKIES['protectantirobots_key_'+path])
  31. size = (100,30)
  32. img = Image.new("RGB", size, "white")
  33. draw = ImageDraw.Draw(img)
  34. font = ImageFont.truetype(os.path.dirname(__file__)+"/FreeSansBold.ttf", 24)
  35. draw.text((2,2), s, fill="red", font=font)
  36. draw.line((0, 0) + img.size, fill=128)
  37. draw.line((0, img.size[1], img.size[0], 0), fill=128)
  38. del draw
  39. ret = HttpResponse(mimetype='image/gif')
  40. img.save(ret, "GIF")
  41. elif 'k' in request.GET:
  42. if request.POST:
  43. n = request.POST['n']
  44. sec = base64.b64decode(request.COOKIES['protectantirobots_key_'+path])
  45. if n == sec:
  46. ret = HttpResponseRedirect(request.COOKIES['protectantirobots_referer_'+path])
  47. ret.set_cookie('protectantirobots_sec_'+str(path), sec, domain=current_domain)
  48. else:
  49. ret = render_to_response(
  50. 'djangoplus/protectantirobots.html',
  51. {'msg': _('Invalid number!'), 'path': path},
  52. context_instance=RequestContext(request),
  53. )
  54. else:
  55. ret = render_to_response(
  56. 'djangoplus/protectantirobots.html',
  57. locals(),
  58. context_instance=RequestContext(request),
  59. )
  60. ret.set_cookie('protectantirobots_key_'+str(path), request.REQUEST['k'], domain=current_domain)
  61. ret.set_cookie('protectantirobots_referer_'+str(path), request.META.get('HTTP_REFERER', ''), domain=current_domain)
  62. return ret