Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the session key configurable. #71

Merged
merged 1 commit into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
TZ_SESSOIN_KEY = "my-session-key"
TZ_SESSION_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.
Expand Down
4 changes: 4 additions & 0 deletions tz_detect/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Session yey to use to store the detected timezone
# Session key to use to store the detected timezone

TZ_SESSION_KEY = getattr(settings, "TZ_SESSION_KEY", "detected_tz")
5 changes: 4 additions & 1 deletion tz_detect/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
12 changes: 7 additions & 5 deletions tz_detect/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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"
Expand All @@ -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")
Expand All @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions tz_detect/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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")
Loading