PageRenderTime 2ms CodeModel.GetById 17ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/djangoplus/middleware/__init__.py

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