Skip to content

Commit

Permalink
feat: Add timezone support
Browse files Browse the repository at this point in the history
  • Loading branch information
jon-nfc committed Nov 11, 2024
1 parent 3bad391 commit 616f660
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 0 deletions.
38 changes: 38 additions & 0 deletions app/app/middleware/timezone.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import zoneinfo

from django.utils import timezone


class TimezoneMiddleware:
def __init__(self, get_response):
self.get_response = get_response

def __call__(self, request):

def _activate_tz(tz):
timezone.activate(zoneinfo.ZoneInfo(tz))

# tzname = request.session.get("django_timezone", None)

# if tzname:

# _activate_tz(tzname)

# else:

user = request.user

if hasattr(user, 'user_settings'):

tzname = user.user_settings.all()[0].timezone

# set the cookie
# request.session['django_timezone'] = tzname

_activate_tz(tzname)

else:

timezone.deactivate()

return self.get_response(request)
1 change: 1 addition & 0 deletions app/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'core.middleware.get_request.RequestMiddleware',
'app.middleware.timezone.TimezoneMiddleware',
]


Expand Down
1 change: 1 addition & 0 deletions app/settings/forms/user_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Meta:

fields = [
'default_organization',
'timezone',
]

model = UserSettings
Expand Down

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions app/settings/models/user_settings.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
import zoneinfo

from django.contrib.auth.models import User
from django.conf import settings
from django.db import models
from django.db.models.signals import post_save
from django.dispatch import receiver

from access.fields import *
from access.models import Organization

sorted_timezones = sorted(zoneinfo.available_timezones())

TIMEZONES = tuple(zip(
sorted_timezones,
sorted_timezones
))



class UserSettingsCommonFields(models.Model):

Expand Down Expand Up @@ -46,6 +57,7 @@ class Meta:
blank= False,
help_text = 'User this Setting belongs to',
on_delete=models.CASCADE,
related_name='user_settings',
verbose_name = 'User'
)

Expand All @@ -60,6 +72,14 @@ class Meta:
verbose_name = 'Default Organization'
)

timezone = models.CharField(
default='UTC',
choices=TIMEZONES,
help_text = 'What Timezone do you wish to have times displayed in',
max_length=32,
verbose_name = 'Your Timezone',
)


def get_organization(self):

Expand Down
5 changes: 5 additions & 0 deletions app/settings/templates/settings/user_settings.html.j2
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
<span>{{ settings.default_organization }}</span>
</div>

<div class="detail-view-field">
<label>{{ form.timezone.label }}</label>
<span>{{ settings.timezone }}</span>
</div>

</div>
<div>
<input type="button" value="Edit"
Expand Down

0 comments on commit 616f660

Please sign in to comment.