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

Add TimeZoneMixin #143

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Upcoming

* Add `TimeZoneMixin` for custom `User` models.

## v14.0.0

* Clarify error message when your old and new passwords match, you will need to update translations.
Expand Down
10 changes: 10 additions & 0 deletions manage.py
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)
8 changes: 8 additions & 0 deletions user_management/models/mixins.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.conf import settings
from django.contrib.auth.models import BaseUserManager
from django.contrib.auth.tokens import default_token_generator
from django.contrib.sites.models import Site
Expand Down Expand Up @@ -55,6 +56,13 @@ class Meta:
abstract = True


class TimeZoneMixin(models.Model):
timezone = models.CharField(max_length=255, default=settings.TIME_ZONE)
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you think it would be safer to define another setting? It looks like TIME_ZONE could be set to None see https://docs.djangoproject.com/en/1.8/ref/settings/#time-zone


class Meta:
abstract = True


class EmailUserMixin(models.Model):
email = models.EmailField(
verbose_name=_('Email address'),
Expand Down
19 changes: 14 additions & 5 deletions user_management/models/tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,23 @@
EmailVerifyUserMixin,
IsStaffUserMixin,
NameUserMethodsMixin,
TimeZoneMixin,
VerifyEmailMixin,
)


class User(AvatarMixin, VerifyEmailMixin, PermissionsMixin, AbstractBaseUser):
pass
class User(
AvatarMixin, TimeZoneMixin, VerifyEmailMixin, PermissionsMixin,
AbstractBaseUser):
"""A User model using all the custom mixins."""


class BasicUser(BasicUserFieldsMixin, AbstractBaseUser):
pass
"""A User model using just the BasicUserFieldsMixin."""


class VerifyEmailUser(VerifyEmailMixin, AbstractBaseUser):
pass
"""A User model using just the VerifyEmailMixin."""


class CustomVerifyEmailUser(VerifyEmailMixin, AbstractBaseUser):
Expand All @@ -35,6 +38,12 @@ class CustomVerifyEmailUser(VerifyEmailMixin, AbstractBaseUser):
class CustomBasicUserFieldsMixin(
NameUserMethodsMixin, EmailUserMixin, DateJoinedUserMixin,
IsStaffUserMixin):
"""
A replacement for BasicUserFieldsMixin with a custom name field.

Uses NameUserMethodsMixin instead of NameUserMixin.
"""

name = models.TextField()

USERNAME_FIELD = 'email'
Expand All @@ -46,4 +55,4 @@ class Meta:
class CustomNameUser(
AvatarMixin, EmailVerifyUserMixin, CustomBasicUserFieldsMixin,
AbstractBaseUser):
pass
"""A User model using the CustomBasicUserFieldsMixin."""
1 change: 1 addition & 0 deletions user_management/models/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def test_fields(self):
'last_login',
'password',
'avatar',
'timezone',

# Incoming
'groups', # Django permission groups
Expand Down
35 changes: 35 additions & 0 deletions user_management/tests/settings.py
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 user_management/tests/testmigrations/tests/0002_user_timezone.py
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),
Copy link
Contributor

Choose a reason for hiding this comment

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

do you need to update the test migration?

),
]