Statistics
| Branch: | Tag: | Revision:

root / cloudcms / middleware.py @ d240ebcb

History | View | Annotate | Download (1.4 kB)

1 f6e90a2b Kostas Papadimitriou
import urlparse
2 f6e90a2b Kostas Papadimitriou
import urllib
3 f6e90a2b Kostas Papadimitriou
4 f6e90a2b Kostas Papadimitriou
from django.contrib import messages
5 f6e90a2b Kostas Papadimitriou
from django.http import HttpResponseRedirect
6 f6e90a2b Kostas Papadimitriou
7 f6e90a2b Kostas Papadimitriou
class RequestMessagesMiddleware:
8 f6e90a2b Kostas Papadimitriou
    """
9 f6e90a2b Kostas Papadimitriou
    A middleware that checks for specific get parameters in url, appends
10 f6e90a2b Kostas Papadimitriou
    associated messages and redirects to the same url (removing the parameter
11 f6e90a2b Kostas Papadimitriou
    to avoid circular redirects).
12 f6e90a2b Kostas Papadimitriou
    """
13 f6e90a2b Kostas Papadimitriou
14 f6e90a2b Kostas Papadimitriou
    PARAMS_MAP = {
15 f6e90a2b Kostas Papadimitriou
        'logged_in': ((messages.SUCCESS, 'You have successfully logged in'),),
16 f6e90a2b Kostas Papadimitriou
        'logged_out': ((messages.SUCCESS, 'You have successfully logged out'),),
17 f6e90a2b Kostas Papadimitriou
    }
18 f6e90a2b Kostas Papadimitriou
19 f6e90a2b Kostas Papadimitriou
    def process_request(self, request):
20 f6e90a2b Kostas Papadimitriou
        qs = request.META.get('QUERY_STRING', '')
21 f6e90a2b Kostas Papadimitriou
        if not qs:
22 f6e90a2b Kostas Papadimitriou
            return
23 f6e90a2b Kostas Papadimitriou
24 f6e90a2b Kostas Papadimitriou
        qs = urlparse.parse_qs(qs)
25 f6e90a2b Kostas Papadimitriou
26 f6e90a2b Kostas Papadimitriou
        # parse query string, add messages and keep params used to remove
27 f6e90a2b Kostas Papadimitriou
        # them later from the redirect url
28 f6e90a2b Kostas Papadimitriou
        params_to_remove = []
29 f6e90a2b Kostas Papadimitriou
        for param, messages_tuples in self.PARAMS_MAP.iteritems():
30 f6e90a2b Kostas Papadimitriou
            if param in qs:
31 f6e90a2b Kostas Papadimitriou
                params_to_remove.append(param)
32 f6e90a2b Kostas Papadimitriou
                for message in messages_tuples:
33 f6e90a2b Kostas Papadimitriou
                    msg_type = message[0]
34 f6e90a2b Kostas Papadimitriou
                    msg = message[1]
35 f6e90a2b Kostas Papadimitriou
                    messages.add_message(request, msg_type, msg)
36 f6e90a2b Kostas Papadimitriou
37 f6e90a2b Kostas Papadimitriou
        if not params_to_remove:
38 f6e90a2b Kostas Papadimitriou
            return
39 f6e90a2b Kostas Papadimitriou
40 f6e90a2b Kostas Papadimitriou
        for p in params_to_remove:
41 f6e90a2b Kostas Papadimitriou
            del qs[p]
42 f6e90a2b Kostas Papadimitriou
43 f6e90a2b Kostas Papadimitriou
        # create the redirect url
44 f6e90a2b Kostas Papadimitriou
        redirect_qs = urllib.urlencode(qs)
45 f6e90a2b Kostas Papadimitriou
46 f6e90a2b Kostas Papadimitriou
        return HttpResponseRedirect("/?" + redirect_qs)