-
Notifications
You must be signed in to change notification settings - Fork 95
feat: User timezone #459
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
Open
menezesd
wants to merge
14
commits into
vEnhance:main
Choose a base branch
from
menezesd:user-timezone
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: User timezone #459
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
9650c7d
feat: add user timezone setting with auto-detection
a665f0b
style: apply ruff and djlint formatting
0d7233f
fix: use getattr to satisfy pyright type checking in TimezoneMiddleware
b48586c
refactor: use get_or_create for UserProfile access in TimezoneMiddleware
c1eaea4
refactor: improve timezone feature based on feedback
646c8b9
fix: apply ruff formatting to migration file
2dfc932
fix: avoid timezone middleware writes and improve timezone picker
f99b3e4
chore: merge timezone and disable_hints migrations
cd69130
test: cover timezone middleware and view context
131aa80
chore: format merge migration
e6038f2
refactor: clean up migration files
vEnhance 24f509f
polish: some presentation changes
vEnhance 7938d97
tests: match PREFERRED_TIMEZONES
vEnhance 3716127
Improve timezone picker search and add alias coverage tests
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # Generated by Django 5.2.12 on 2026-03-05 23:37 | ||
|
|
||
| from django.db import migrations, models | ||
|
|
||
| import core.models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ("core", "0064_userprofile_disable_hints"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AddField( | ||
| model_name="userprofile", | ||
| name="timezone", | ||
| field=models.CharField( | ||
| blank=True, | ||
| default="", | ||
| help_text="Your local time zone for displaying timestamps. Leave blank to use server time (America/New_York).", | ||
| max_length=63, | ||
| validators=[core.models.validate_timezone], | ||
| verbose_name="Time zone", | ||
| ), | ||
| ), | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,9 +2,11 @@ | |
|
|
||
| import datetime | ||
| import os | ||
| import zoneinfo | ||
| from typing import Callable | ||
|
|
||
| from django.contrib.auth import get_user_model | ||
| from django.core.exceptions import ValidationError | ||
| from django.db import models | ||
| from django.db.models.manager import BaseManager | ||
| from django.urls import reverse | ||
|
|
@@ -14,6 +16,14 @@ | |
| # Create your models here. | ||
|
|
||
|
|
||
| def validate_timezone(value: str) -> None: | ||
| if value: # blank is allowed | ||
| try: | ||
| zoneinfo.ZoneInfo(value) | ||
| except zoneinfo.ZoneInfoNotFoundError: | ||
| raise ValidationError(f"{value} is not a valid timezone") | ||
|
|
||
|
|
||
| class Semester(models.Model): | ||
| """Represents an academic semester/year/etc, e.g. "Fall 2017" | ||
| or "Year III".""" | ||
|
|
@@ -298,6 +308,14 @@ class UserProfile(models.Model): | |
| help_text="Hide all hints from the problem archive (ARCH).", | ||
| default=False, | ||
| ) | ||
| timezone = models.CharField( | ||
menezesd marked this conversation as resolved.
Show resolved
Hide resolved
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so i don't know if this is possible, but i think this would be better with CHOICES somehow if it could be done. basically i don't think it's good UX for a end-user to have to type the exact case-sensitive string "America/Los_Angeles"... I would be unable to remember that. |
||
| max_length=63, | ||
| blank=True, | ||
| default="", | ||
| verbose_name="Time zone", | ||
| help_text="Your local time zone for displaying timestamps. Leave blank to use server time (America/New_York).", | ||
| validators=[validate_timezone], | ||
| ) | ||
|
|
||
| email_on_announcement = models.BooleanField( | ||
| verbose_name="Receive emails for announcements", | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually, now that i think about it, why is there a
timezone.deactivatehere?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok as far as I can understand,
timezone.activate()sets Django timezone in request-local context, and that context can be reused by later requests on the same worker.timezone.deactivate()clears it so the next request doesn’t accidentally inherit another user’s timezone and instead falls back to settings.TIME_ZONE.An alternative is not to set a global request timezone at all; convert
datetimesonly where needed in templates.