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 support for timezone/language settings #11
Changes from 1 commit
65d5fc1
c69c403
9fcdbf9
4f128d2
2a8c9f8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from django.conf import settings | ||
from django.db import models | ||
|
||
import pytz | ||
|
||
|
||
TIMEZONE_CHOICES = tuple(zip(pytz.all_timezones, pytz.all_timezones)) | ||
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. This constant seems useless to me. Either we should inline this or make it configurable via settings. 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. Oh, I see it being used elsewhere. Let's make it configurable via settings in 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. Also, I don't know what I was smoking to allow this to be a tuple. Use |
||
|
||
|
||
class TimeZoneField(models.CharField): | ||
|
||
__metaclass__ = models.SubfieldBase | ||
|
||
def __init__(self, *args, **kwargs): | ||
defaults = { | ||
"max_length": 100, | ||
"default": settings.TIME_ZONE, | ||
"choices": TIMEZONE_CHOICES | ||
} | ||
defaults.update(kwargs) | ||
return super(TimeZoneField, self).__init__(*args, **defaults) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,20 +6,63 @@ | |
from django.core.urlresolvers import reverse | ||
from django.db import models | ||
from django.db.models import Q | ||
from django.db.models.signals import post_save | ||
from django.dispatch import receiver | ||
from django.template.loader import render_to_string | ||
from django.utils import timezone | ||
from django.utils.translation import gettext_lazy as _ | ||
from django.utils.translation import get_language_from_request, gettext_lazy as _ | ||
|
||
from django.contrib.auth.models import User | ||
from django.contrib.auth.models import User, AnonymousUser | ||
from django.contrib.sites.models import Site | ||
|
||
from account import signals | ||
from account.conf import settings | ||
from account.fields import TimeZoneField | ||
from account.managers import EmailAddressManager, EmailConfirmationManager | ||
from account.signals import signup_code_sent, signup_code_used | ||
from account.utils import random_token | ||
|
||
|
||
class Account(models.Model): | ||
|
||
user = models.OneToOneField(User, related_name="account", verbose_name=_("user")) | ||
|
||
timezone = TimeZoneField(_("timezone")) | ||
language = models.CharField(_("language"), | ||
max_length = 10, | ||
choices = settings.LANGUAGES, | ||
default = settings.LANGUAGE_CODE | ||
) | ||
|
||
@classmethod | ||
def for_request(cls, request): | ||
if request.user.is_authenticated(): | ||
try: | ||
account = Account._default_manager.get(user=request.user) | ||
except Account.DoesNotExist: | ||
account = AnonymousAccount(request) | ||
else: | ||
account = AnonymousAccount(request) | ||
return account | ||
|
||
def __unicode__(self): | ||
return self.user.username | ||
|
||
|
||
class AnonymousAccount(object): | ||
|
||
def __init__(self, request=None): | ||
self.user = AnonymousUser() | ||
self.timezone = settings.TIME_ZONE | ||
if request is not None: | ||
self.language = get_language_from_request(request) | ||
else: | ||
self.language = settings.LANGUAGE_CODE | ||
|
||
def __unicode__(self): | ||
return "AnonymousAccount" | ||
|
||
|
||
class SignupCode(models.Model): | ||
|
||
class AlreadyExists(Exception): | ||
|
@@ -227,3 +270,9 @@ def send(self): | |
self.sent = timezone.now() | ||
self.save() | ||
signals.email_confirmation_sent.send(sender=self.__class__, confirmation=self) | ||
|
||
|
||
@receiver(post_save, sender=User) | ||
def create_account(sender, **kwargs): | ||
if kwargs["created"]: | ||
Account.objects.get_or_create(user=kwargs["instance"]) | ||
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. We talked about not using |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from django.utils import translation | ||
from django.utils.cache import patch_vary_headers | ||
|
||
from account.models import Account | ||
|
||
|
||
class LocaleMiddleware(object): | ||
""" | ||
This is a very simple middleware that parses a request | ||
and decides what translation object to install in the current | ||
thread context depending on the user's account. This allows pages | ||
to be dynamically translated to the language the user desires | ||
(if the language is available, of course). | ||
""" | ||
|
||
def get_language_for_user(self, request): | ||
if request.user.is_authenticated(): | ||
try: | ||
account = Account.objects.get(user=request.user) | ||
return account.language | ||
except Account.DoesNotExist: | ||
pass | ||
return translation.get_language_from_request(request) | ||
|
||
def process_request(self, request): | ||
translation.activate(self.get_language_for_user(request)) | ||
request.LANGUAGE_CODE = translation.get_language() | ||
|
||
def process_response(self, request, response): | ||
patch_vary_headers(response, ("Accept-Language",)) | ||
response["Content-Language"] = translation.get_language() | ||
translation.deactivate() | ||
return response |
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.
This is incorrect. You need:
from account.conf import settings