-
Notifications
You must be signed in to change notification settings - Fork 24
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
Add TimeZoneMixin #143
Open
LilyFoote
wants to merge
6
commits into
master
Choose a base branch
from
timezone-field
base: master
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
Add TimeZoneMixin #143
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f763edc
Add manage.py to allow creating test migrations
LilyFoote 971c2c8
Add TimeZoneMixin with timezone field
LilyFoote 41e8371
Add TimeZoneMixin to changelog
LilyFoote a5ec7ed
Merge branch 'master' into timezone-field
LilyFoote 959678c
Add timezone_choices helper function
LilyFoote 82855e8
Add choices to TimeZoneMixin.timezone field
LilyFoote 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 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 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,10 @@ | ||
#!/usr/bin/env python | ||
import os | ||
import sys | ||
|
||
if __name__ == "__main__": | ||
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "user_management.tests.settings") | ||
|
||
from django.core.management import execute_from_command_line | ||
|
||
execute_from_command_line(sys.argv) |
This file contains 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 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 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 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 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,53 @@ | ||
from unittest import TestCase | ||
|
||
from ..utils import timezone_choices | ||
|
||
|
||
class TestTimeZoneChoices(TestCase): | ||
timezones = [ | ||
'Arctic/Longyearbyen', | ||
'Asia/Qyzylorda', | ||
'America/Argentina/Ushuaia', | ||
'America/Iqaluit', | ||
'GMT', | ||
'Indian/Christmas', | ||
'UTC', | ||
] | ||
|
||
def setUp(self): | ||
self.choices = timezone_choices(self.timezones) | ||
|
||
def test_exclude_GMT_UTC(self): | ||
self.assertNotIn('GMT', self.choices) | ||
self.assertNotIn('UTC', self.choices) | ||
|
||
def test_choice_groups(self): | ||
expected_choices = ( | ||
( | ||
'Arctic', | ||
( | ||
('Arctic/Longyearbyen', 'Longyearbyen'), | ||
), | ||
), | ||
( | ||
'Asia', | ||
( | ||
('Asia/Qyzylorda', 'Qyzylorda'), | ||
), | ||
), | ||
( | ||
'America', | ||
( | ||
('America/Argentina/Ushuaia', 'Argentina/Ushuaia'), | ||
('America/Iqaluit', 'Iqaluit'), | ||
), | ||
), | ||
( | ||
'Indian', | ||
( | ||
('Indian/Christmas', 'Christmas'), | ||
), | ||
), | ||
) | ||
|
||
self.assertEqual(self.choices, expected_choices) |
This file contains 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,20 @@ | ||
from collections import defaultdict, OrderedDict | ||
|
||
|
||
class DefaultOrderedDict(OrderedDict, defaultdict): | ||
def __init__(self, default_factory=None, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self.default_factory = default_factory | ||
|
||
|
||
def timezone_choices(timezones, exclude=('GMT', 'UTC')): | ||
choices = DefaultOrderedDict(list) | ||
|
||
for timezone in timezones: | ||
if timezone in exclude: | ||
continue | ||
|
||
continent, _sep, town = timezone.partition('/') | ||
choices[continent].append((timezone, town)) | ||
|
||
return tuple((c, tuple(t)) for c, t in choices.items()) | ||
This file contains 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,35 @@ | ||
SECRET_KEY = 'not-for-production' | ||
DEBUG = True | ||
|
||
ALLOWED_HOSTS = [] | ||
|
||
INSTALLED_APPS = ( | ||
# Put contenttypes before auth to work around test issue. | ||
# See: https://code.djangoproject.com/ticket/10827#comment:12 | ||
'django.contrib.sites', | ||
'django.contrib.contenttypes', | ||
'django.contrib.auth', | ||
'django.contrib.sessions', | ||
'django.contrib.admin', | ||
|
||
'rest_framework.authtoken', | ||
|
||
# Added for templates | ||
'user_management.api', | ||
'user_management.models.tests', | ||
) | ||
|
||
AUTH_USER_MODEL = 'tests.User' | ||
|
||
MIDDLEWARE_CLASSES = () | ||
|
||
LANGUAGE_CODE = 'en-us' | ||
TIME_ZONE = 'UTC' | ||
USE_I18N = True | ||
USE_L10N = True | ||
USE_TZ = True | ||
|
||
MIGRATION_MODULES = { | ||
'api': 'user_management.tests.testmigrations.api', | ||
'tests': 'user_management.tests.testmigrations.tests', | ||
} |
19 changes: 19 additions & 0 deletions
19
user_management/tests/testmigrations/tests/0002_user_timezone.py
This file contains 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,19 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import unicode_literals | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('tests', '0001_initial'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='user', | ||
name='timezone', | ||
field=models.CharField(default='UTC', max_length=255), | ||
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. do you need to update the test migration? |
||
), | ||
] |
21 changes: 21 additions & 0 deletions
21
user_management/tests/testmigrations/tests/0003_user_timezone_choices.py
This file contains 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,21 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import unicode_literals | ||
|
||
import pytz | ||
from django.db import migrations, models | ||
from user_management.models.utils import timezone_choices | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('tests', '0002_user_timezone'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='user', | ||
name='timezone', | ||
field=models.CharField(choices=timezone_choices(pytz.common_timezones), max_length=255), | ||
), | ||
] |
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.
cool, I wasn't aware django could deal with grouped choices