]> git.openstreetmap.org Git - osqa.git/blob - forum/middleware/request_utils.py
OSQA-386
[osqa.git] / forum / middleware / request_utils.py
1 from forum.settings import MAINTAINANCE_MODE, APP_LOGO, APP_TITLE\r
2 \r
3 from forum.http_responses import HttpResponseServiceUnavailable\r
4 \r
5 class RequestUtils(object):\r
6     def __init__(self):\r
7         self.request = None\r
8 \r
9     def set_sort_method(self, sort):\r
10         self.request.session['questions_sort_method'] = sort\r
11 \r
12     def sort_method(self, default):\r
13         sort = self.request.REQUEST.get('sort', None)\r
14         if sort is None:\r
15             return self.request.session.get('questions_sort_method', default)\r
16         else:\r
17             self.set_sort_method(sort)\r
18             return sort\r
19 \r
20     def page_size(self, default):\r
21         pagesize = self.request.REQUEST.get('pagesize', None)\r
22         if pagesize is None:\r
23             return int(self.request.session.get('questions_pagesize', default))\r
24         else:\r
25             self.request.session['questions_pagesize'] = pagesize\r
26             return int(pagesize)\r
27 \r
28     def process_request(self, request):\r
29         if MAINTAINANCE_MODE.value is not None and isinstance(MAINTAINANCE_MODE.value.get('allow_ips', None), list):\r
30             ip = request.META['REMOTE_ADDR']\r
31 \r
32             if not ip in MAINTAINANCE_MODE.value['allow_ips']:\r
33                 return HttpResponseServiceUnavailable(MAINTAINANCE_MODE.value.get('message', ''))\r
34 \r
35         if request.session.get('redirect_POST_data', None):\r
36             request.POST = request.session.pop('redirect_POST_data')\r
37             request.META['REQUEST_METHOD'] = "POST"\r
38 \r
39         self.request = request\r
40         request.utils = self\r
41         return None