diff --git a/README.rst b/README.rst index 20c50d0..4ba8294 100644 --- a/README.rst +++ b/README.rst @@ -94,7 +94,9 @@ Installation 'tz_detect.middleware.TimezoneMiddleware', ) -7. (Optional) Configure the countries in which your app will be most commonly used: +7. (Optional) Configure optional settings + + Set the countries in which your app will be most commonly used: .. code-block:: python @@ -104,6 +106,13 @@ Installation # Defaults to the top Internet using countries. TZ_DETECT_COUNTRIES = ('CN', 'US', 'IN', 'JP', 'BR', 'RU', 'DE', 'FR', 'GB') + Set the session key that will be used to store the detected timezone + + .. code-block:: python + + # Session key to use, defaults to "detected_tz" + TZ_SESSOIN_KEY = "my-session-key" + Please see ``example`` application. This application is used to manually test the functionalities of this package. This also serves as a good example. diff --git a/tz_detect/defaults.py b/tz_detect/defaults.py index 9b1f59c..5dd162d 100644 --- a/tz_detect/defaults.py +++ b/tz_detect/defaults.py @@ -6,3 +6,7 @@ # Defaults to top Internet using countries. TZ_DETECT_COUNTRIES = getattr(settings, "TZ_DETECT_COUNTRIES", ("CN", "US", "IN", "JP", "BR", "RU", "DE", "FR", "GB")) + + +# Session yey to use to store the detected timezone +TZ_SESSION_KEY = getattr(settings, "TZ_SESSION_KEY", "detected_tz") diff --git a/tz_detect/middleware.py b/tz_detect/middleware.py index a46428f..8a0340c 100644 --- a/tz_detect/middleware.py +++ b/tz_detect/middleware.py @@ -2,6 +2,9 @@ from django.utils import timezone from pytz.tzinfo import BaseTzInfo +from .defaults import TZ_SESSION_KEY + + try: from django.utils.deprecation import MiddlewareMixin except ImportError: # Django < 1.10 @@ -12,7 +15,7 @@ class TimezoneMiddleware(MiddlewareMixin): def process_request(self, request): - tz = request.session.get("detected_tz") + tz = request.session.get(TZ_SESSION_KEY) if tz: # ``request.timezone_active`` is used in the template tag # to detect if the timezone has been activated diff --git a/tz_detect/tests.py b/tz_detect/tests.py index 91e6494..65ad8a5 100644 --- a/tz_detect/tests.py +++ b/tz_detect/tests.py @@ -11,6 +11,8 @@ from tz_detect.utils import convert_header_name, offset_to_timezone from tz_detect.views import SetOffsetView +from .defaults import TZ_SESSION_KEY + class ViewTestCase(TestCase): def setUp(self): @@ -26,8 +28,8 @@ def test_xhr_valid_offset(self): response = SetOffsetView.as_view()(request) self.assertEqual(response.status_code, 200) - self.assertIn("detected_tz", request.session) - self.assertIsInstance(request.session["detected_tz"], int) + self.assertIn(TZ_SESSION_KEY, request.session) + self.assertIsInstance(request.session[TZ_SESSION_KEY], int) def test_xhr_valid_timezone(self): timezone_name = "Europe/Amsterdam" @@ -36,8 +38,8 @@ def test_xhr_valid_timezone(self): response = SetOffsetView.as_view()(request) self.assertEqual(response.status_code, 200) - self.assertIn("detected_tz", request.session) - self.assertEqual(request.session["detected_tz"], timezone_name) + self.assertIn(TZ_SESSION_KEY, request.session) + self.assertEqual(request.session[TZ_SESSION_KEY], timezone_name) def test_xhr_bad_method(self): request = self.factory.get("/abc") @@ -64,7 +66,7 @@ def test_middleware_timezone_string(self): timezone_name = "Europe/Amsterdam" request = self.factory.post("/abc", {"timezone": timezone_name}) self.add_session(request) - request.session["detected_tz"] = timezone_name + request.session[TZ_SESSION_KEY] = timezone_name get_response = lambda x: HttpResponse("") TimezoneMiddleware(get_response).process_request(request) diff --git a/tz_detect/views.py b/tz_detect/views.py index 535de10..d8f63cd 100644 --- a/tz_detect/views.py +++ b/tz_detect/views.py @@ -2,13 +2,16 @@ from django.views.generic import View +from .defaults import TZ_SESSION_KEY + + class SetOffsetView(View): http_method_names = ["post"] def post(self, request, *args, **kwargs): timezone = request.POST.get("timezone", None) if timezone: - request.session["detected_tz"] = timezone + request.session[TZ_SESSION_KEY] = timezone else: offset = request.POST.get("offset", None) if not offset: @@ -19,6 +22,6 @@ def post(self, request, *args, **kwargs): except ValueError: return HttpResponse("Invalid 'offset' value provided", status=400) - request.session["detected_tz"] = int(offset) + request.session[TZ_SESSION_KEY] = int(offset) return HttpResponse("OK")