class Account(models.Model): user = models.OneToOneField(settings.AUTH_USER_MODEL, related_name="account", verbose_name=_("user")) timezone = TimeZoneField(_("timezone")) language = models.CharField( _("language"), max_length=10, choices=settings.ACCOUNT_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 @classmethod def create(cls, request=None, **kwargs): create_email = kwargs.pop("create_email", True) confirm_email = kwargs.pop("confirm_email", None) account = cls(**kwargs) if "language" not in kwargs: if request is None: account.language = settings.LANGUAGE_CODE else: account.language = translation.get_language_from_request(request, check_path=True) account.save() if create_email and account.user.email: kwargs = {"primary": True} if confirm_email is not None: kwargs["confirm"] = confirm_email EmailAddress.objects.add_email(account.user, account.user.email, **kwargs) return account def __str__(self): return str(self.user) def now(self): """ Returns a timezone aware datetime localized to the account's timezone. """ now = datetime.datetime.utcnow().replace(tzinfo=pytz.timezone("UTC")) timezone = settings.TIME_ZONE if not self.timezone else self.timezone return now.astimezone(pytz.timezone(timezone)) def localtime(self, value): """ Given a datetime object as value convert it to the timezone of the account. """ timezone = settings.TIME_ZONE if not self.timezone else self.timezone if value.tzinfo is None: value = pytz.timezone(settings.TIME_ZONE).localize(value) return value.astimezone(pytz.timezone(timezone))
class AbstractAccount(AbstractUser): timezone = TimeZoneField(_("timezone")) language = models.CharField(_("language"), max_length=10, choices=settings.ACCOUNT_LANGUAGES, default=settings.LANGUAGE_CODE) class Meta: verbose_name = _("account") verbose_name_plural = _("accounts") abstract = True @classmethod def for_request(cls, request): return request.user @classmethod def create(cls, request=None, **kwargs): create_email = kwargs.pop("create_email", True) confirm_email = kwargs.pop("confirm_email", None) account = cls(**kwargs) if "language" not in kwargs: if request is None: account.language = settings.LANGUAGE_CODE else: account.language = translation.get_language_from_request(request, check_path=True) account.save() if create_email and account.user.email: kwargs = {"primary": True} if confirm_email is not None: kwargs["confirm"] = confirm_email EmailAddress.objects.add_email(account.user, account.user.email, **kwargs) return account def now(self): """ Returns a timezone aware datetime localized to the account's timezone. """ now = datetime.datetime.utcnow().replace(tzinfo=pytz.timezone("UTC")) timezone = settings.TIME_ZONE if not self.timezone else self.timezone return now.astimezone(pytz.timezone(timezone)) def localtime(self, value): """ Given a datetime object as value convert it to the timezone of the account. """ timezone = settings.TIME_ZONE if not self.timezone else self.timezone if value.tzinfo is None: value = pytz.timezone(settings.TIME_ZONE).localize(value) return value.astimezone(pytz.timezone(timezone))
class Account(models.Model): user = models.OneToOneField(settings.AUTH_USER_MODEL, related_name="account", verbose_name=_("user"), on_delete=models.CASCADE) name = models.CharField(max_length=75, blank=True) apellidos = models.CharField(max_length=125, blank=True) avatar = models.ImageField(upload_to=avatar_upload, blank=True) bio = models.TextField(blank=True) affiliation = models.CharField(max_length=100, blank=True) location = models.CharField(max_length=100, blank=True) website = models.CharField(max_length=250, blank=True) twitter_username = models.CharField("Twitter Username", max_length=100, blank=True) created_at = models.DateTimeField(default=timezone.now) modified_at = models.DateTimeField(default=timezone.now) # if 'mod_address' in settings.INSTALLED_APPS: # domicilio = models.ForeignKey('mod_addresses.Domicilio', on_delete=models.PROTECT, null=True, blank=True) timezone = TimeZoneField(_("timezone")) language = models.CharField( _("language"), max_length=10, choices=settings.ACCOUNT_LANGUAGES, default=DEFAULT_LANGUAGE, ) @classmethod def for_request(cls, request): user = getattr(request, "user", None) if user and is_authenticated(user): try: return Account._default_manager.get(user=user) except Account.DoesNotExist: pass return AnonymousAccount(request) @classmethod def create(cls, request=None, **kwargs): create_email = kwargs.pop("create_email", True) confirm_email = kwargs.pop("confirm_email", None) account = cls(**kwargs) if "language" not in kwargs: if request is None: account.language = DEFAULT_LANGUAGE else: account.language = translation.get_language_from_request( request, check_path=True) account.save() if create_email and account.user.email: kwargs = {"primary": True} if confirm_email is not None: kwargs["confirm"] = confirm_email EmailAddress.objects.add_email(account.user, account.user.email, **kwargs) return account def __str__(self): return str(self.user) def now(self): """ Returns a timezone aware datetime localized to the account's timezone. """ now = datetime.datetime.utcnow().replace(tzinfo=pytz.timezone("UTC")) timezone = settings.TIME_ZONE if not self.timezone else self.timezone return now.astimezone(pytz.timezone(timezone)) def localtime(self, value): """ Given a datetime object as value convert it to the timezone of the account. """ timezone = settings.TIME_ZONE if not self.timezone else self.timezone if value.tzinfo is None: value = pytz.timezone(settings.TIME_ZONE).localize(value) return value.astimezone(pytz.timezone(timezone)) def save(self, *args, **kwargs): self.modified_at = timezone.now() return super(Account, self).save(*args, **kwargs) @property def display_name(self): if self.name: return self.name else: return self.user.username
class Account(models.Model): STATE_CHOICES = state_choices user = models.OneToOneField(settings.AUTH_USER_MODEL, related_name="account", verbose_name=_("user"), on_delete=models.CASCADE) timezone = TimeZoneField( _("timezone"), default="America/New_York" ) language = models.CharField( _("language"), max_length=10, choices=settings.ACCOUNT_LANGUAGES, default=settings.LANGUAGE_CODE ) initial_password = models.CharField(max_length=40, null=True, blank=True) spouse_name = models.CharField(_('spouse name'), max_length=30, null=True, blank=True) street_address = models.CharField(verbose_name=_('address'), max_length=100, help_text=_('example: 55 Linda Lane'), blank=True) city = models.CharField(verbose_name=_('city'), max_length=100, help_text=_('example: Allentown'), blank=True) state = models.CharField(verbose_name=_('state'), max_length=40, choices=STATE_CHOICES, default='PA') zip_code = models.CharField(verbose_name=_('zip code'), max_length=5) main_phone = PhoneNumberField(verbose_name=_('main phone'), max_length=15, blank=True) alt_phone = PhoneNumberField(verbose_name=_('alternate phone (not required)'), max_length=15, null=True, blank=True) @classmethod def for_request(cls, request): user = getattr(request, "user", None) if user and is_authenticated(user): try: return Account._default_manager.get(user=user) except Account.DoesNotExist: pass return AnonymousAccount(request) @classmethod def create(cls, request=None, **kwargs): create_email = kwargs.pop("create_email", True) confirm_email = kwargs.pop("confirm_email", None) account = cls(**kwargs) if "language" not in kwargs: if request is None: account.language = settings.LANGUAGE_CODE else: account.language = translation.get_language_from_request(request, check_path=True) account.save() if create_email and account.user.email: kwargs = {"primary": True} if confirm_email is not None: kwargs["confirm"] = confirm_email EmailAddress.objects.add_email(account.user, account.user.email, **kwargs) return account def __str__(self): return str(self.user.full_family_name()) def now(self): """ Returns a timezone aware datetime localized to the account's timezone. """ now = datetime.datetime.utcnow().replace(tzinfo=pytz.timezone("UTC")) timezone = settings.TIME_ZONE if not self.timezone else self.timezone return now.astimezone(pytz.timezone(timezone)) def localtime(self, value): """ Given a datetime object as value convert it to the timezone of the account. """ timezone = settings.TIME_ZONE if not self.timezone else self.timezone if value.tzinfo is None: value = pytz.timezone(settings.TIME_ZONE).localize(value) return value.astimezone(pytz.timezone(timezone)) def get_address(self): return "{}\n{}, {} {}".format( self.street_address, self.city, self.state, self.zip_code )