| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# |
| 3 |
# Copyright (C) 2016-2023 by the Free Software Foundation, Inc. |
| 4 |
# |
| 5 |
# This file is part of Django-Mailman. |
| 6 |
# |
| 7 |
# Django-Mailman is free software: you can redistribute it and/or modify it |
| 8 |
# under the terms of the GNU General Public License as published by the Free |
| 9 |
# Software Foundation, either version 3 of the License, or (at your option) |
| 10 |
# any later version. |
| 11 |
# |
| 12 |
# Django-Mailman is distributed in the hope that it will be useful, but WITHOUT |
| 13 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 |
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 15 |
# more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License along with |
| 18 |
# Django-Mailman. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
# |
| 20 |
# Author: Aurelien Bompard <[email protected]> |
| 21 |
# |
| 22 |
|
| 23 |
|
| 24 |
# https://docs.djangoproject.com/en/dev/topics/i18n/timezones/ |
| 25 |
|
| 26 |
from django.core.exceptions import ObjectDoesNotExist |
| 27 |
from django.utils import timezone |
| 28 |
|
| 29 |
|
| 30 |
class TimezoneMiddleware(object): |
| 31 |
|
| 32 |
def __init__(self, get_response): |
| 33 |
self.get_response = get_response |
| 34 |
|
| 35 |
def __call__(self, request): |
| 36 |
response = self.get_response(request) |
| 37 |
if not request.user.is_authenticated: |
| 38 |
return response |
| 39 |
try: |
| 40 |
profile = request.user.mailman_profile |
| 41 |
except ObjectDoesNotExist: |
| 42 |
return response |
| 43 |
if profile.timezone: |
| 44 |
timezone.activate(profile.timezone) |
| 45 |
return response |
| 46 |
|
| 47 |
|
| 48 |
class PaginationMiddleware(object): |
| 49 |
""" |
| 50 |
Inserts a variable representing the current page onto the request object if |
| 51 |
it exists in either **GET** or **POST** portions of the request. |
| 52 |
""" |
| 53 |
def __init__(self, get_response): |
| 54 |
self.get_response = get_response |
| 55 |
|
| 56 |
def __call__(self, request): |
| 57 |
try: |
| 58 |
request.page = int(request.REQUEST['page']) |
| 59 |
except (KeyError, ValueError, TypeError): |
| 60 |
request.page = 1 |
| 61 |
return self.get_response(request) |