<commit_before>from django.db import models
from django.conf import settings

try:
    from django.contrib.auth import get_user_model
    User = get_user_model()
except ImportError:
    from django.contrib.auth.models import User


def get_default_timezone():
    """
    Gets the default timezone in settings or GMT
    """
    return getattr(settings, 'TIME_ZONE', 'GMT')


class TimezoneStore(models.Model):
    """
    Database storage for a users timezone
    """
    user = models.OneToOneField(User, related_name = 'timezone')

    # current max length of a tz is 30, going with 48 to be safe
    timezone = models.CharField(max_length = 48, default = get_default_timezone)


    def __unicode__(self):
        return self.timezone


def get_timezone_for_user(self):    
    """
    Magic method that provides attribute like access to getting a users
    timezone. Make sure to select_related on 'timezone'

    Example:
        my_user_object.tz
    """
    if getattr(self, '_timezone', None):
        return self._timezone

    try:
        tz = self.timezone
    except TimezoneStore.DoesNotExist:
        tz, created = TimezoneStore.objects.get_or_create(user = self)

    return tz.timezone

User.add_to_class('tz', property(get_timezone_for_user))<commit_msg>The timezone will be cached for every user.<commit_after>from django.db import models
from django.conf import settings

try:
    from django.contrib.auth import get_user_model
    User = get_user_model()
except ImportError:
    from django.contrib.auth.models import User


def get_default_timezone():
    """
    Gets the default timezone in settings or GMT
    """
    return getattr(settings, 'TIME_ZONE', 'GMT')


class TimezoneStore(models.Model):
    """
    Database storage for a users timezone
    """
    user = models.OneToOneField(User, related_name = 'timezone')

    # current max length of a tz is 30, going with 48 to be safe
    timezone = models.CharField(max_length = 48, default = get_default_timezone)


    def __unicode__(self):
        return self.timezone


def get_timezone_for_user(self):    
    """
    Magic method that provides attribute like access to getting a users
    timezone. Make sure to select_related on 'timezone'

    Example:
        my_user_object.tz
    """
    if getattr(self, '_timezone', None):
        return self._timezone

    try:
        tz = self.timezone
    except TimezoneStore.DoesNotExist:
        tz, created = TimezoneStore.objects.get_or_create(user = self)

    # cache the timezone object for this user
    self._timezone = tz.timezone

    return tz.timezone

User.add_to_class('tz', property(get_timezone_for_user))
{'fraction_non_alphanumeric': 0.08170180722891567, 'fraction_numerical': 0.004518072289156626, 'mean_word_length': 3.264847512038523, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6670519', 'n_tokens_mistral': 827, 'n_tokens_neox': 784, 'n_words': 237}