# Path: account/signals.py

# 
# Path: account/conf.py
# def load_path_attr(path):
#     def configure_hookset(self, value):
# class AccountAppConf(AppConf):
#     OPEN_SIGNUP = True
#     LOGIN_URL = "account_login"
#     LOGOUT_URL = "account_logout"
#     SIGNUP_REDIRECT_URL = "/"
#     LOGIN_REDIRECT_URL = "/"
#     LOGOUT_REDIRECT_URL = "/"
#     PASSWORD_CHANGE_REDIRECT_URL = "account_password"
#     PASSWORD_RESET_REDIRECT_URL = "account_login"
#     PASSWORD_RESET_TOKEN_URL = "account_password_reset_token"
#     PASSWORD_EXPIRY = 0
#     PASSWORD_USE_HISTORY = False
#     PASSWORD_STRIP = True
#     REMEMBER_ME_EXPIRY = 60 * 60 * 24 * 365 * 10
#     USER_DISPLAY = lambda user: user.username  # noqa
#     CREATE_ON_SAVE = True
#     EMAIL_UNIQUE = True
#     EMAIL_CONFIRMATION_REQUIRED = False
#     EMAIL_CONFIRMATION_EMAIL = True
#     EMAIL_CONFIRMATION_EXPIRE_DAYS = 3
#     EMAIL_CONFIRMATION_AUTO_LOGIN = False
#     EMAIL_CONFIRMATION_ANONYMOUS_REDIRECT_URL = "account_login"
#     EMAIL_CONFIRMATION_AUTHENTICATED_REDIRECT_URL = None
#     EMAIL_CONFIRMATION_URL = "account_confirm_email"
#     SETTINGS_REDIRECT_URL = "account_settings"
#     NOTIFY_ON_PASSWORD_CHANGE = True
#     DELETION_EXPUNGE_HOURS = 48
#     DEFAULT_HTTP_PROTOCOL = "https"
#     HOOKSET = "account.hooks.AccountDefaultHookSet"
#     TIMEZONES = TIMEZONES
#     LANGUAGES = LANGUAGES
# 
# Path: account/models.py
# class Account(models.Model):
# 
#     user = models.OneToOneField(settings.AUTH_USER_MODEL, related_name="account", verbose_name=_("user"), on_delete=models.CASCADE)
#     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 user.is_authenticated:
#             account = user.account
#             if account:
#                 return account
#         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"))
#         tz = settings.TIME_ZONE if not self.timezone else self.timezone
#         return now.astimezone(pytz.timezone(tz))
# 
#     def localtime(self, value):
#         """
#         Given a datetime object as value convert it to the timezone of
#         the account.
#         """
#         tz = 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(tz))
# 
# Path: account/utils.py
# def check_password_expired(user):
#     """
#     Return True if password is expired and system is using
#     password expiration, False otherwise.
#     """
#     if not settings.ACCOUNT_PASSWORD_USE_HISTORY:
#         return False
# 
#     if hasattr(user, "password_expiry"):
#         # user-specific value
#         expiry = user.password_expiry.expiry
#     else:
#         # use global value
#         expiry = settings.ACCOUNT_PASSWORD_EXPIRY
# 
#     if expiry == 0:  # zero indicates no expiration
#         return False
# 
#     try:
#         # get latest password info
#         latest = user.password_history.latest("timestamp")
#     except PasswordHistory.DoesNotExist:
#         return False
# 
#     now = datetime.datetime.now(tz=pytz.UTC)
#     expiration = latest.timestamp + datetime.timedelta(seconds=expiry)
# 
#     if expiration < now:
#         return True
#     else:
#         return False


from urllib.parse import urlparse, urlunparse
from django.contrib import messages
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.http import HttpResponseRedirect, QueryDict
from django.urls import resolve, reverse
from django.utils import timezone, translation
from django.utils.cache import patch_vary_headers
from django.utils.deprecation import MiddlewareMixin as BaseMiddleware
from django.utils.translation import gettext_lazy as _
from account import signals
from account.conf import settings
from account.models import Account
from account.utils import check_password_expired




class LocaleMiddleware(BaseMiddleware):
    """
    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

# Path: account/signals.py

# 
# Path: account/conf.py
# def load_path_attr(path):
#     def configure_hookset(self, value):
# class AccountAppConf(AppConf):
#     OPEN_SIGNUP = True
#     LOGIN_URL = "account_login"
#     LOGOUT_URL = "account_logout"
#     SIGNUP_REDIRECT_URL = "/"
#     LOGIN_REDIRECT_URL = "/"
#     LOGOUT_REDIRECT_URL = "/"
#     PASSWORD_CHANGE_REDIRECT_URL = "account_password"
#     PASSWORD_RESET_REDIRECT_URL = "account_login"
#     PASSWORD_RESET_TOKEN_URL = "account_password_reset_token"
#     PASSWORD_EXPIRY = 0
#     PASSWORD_USE_HISTORY = False
#     PASSWORD_STRIP = True
#     REMEMBER_ME_EXPIRY = 60 * 60 * 24 * 365 * 10
#     USER_DISPLAY = lambda user: user.username  # noqa
#     CREATE_ON_SAVE = True
#     EMAIL_UNIQUE = True
#     EMAIL_CONFIRMATION_REQUIRED = False
#     EMAIL_CONFIRMATION_EMAIL = True
#     EMAIL_CONFIRMATION_EXPIRE_DAYS = 3
#     EMAIL_CONFIRMATION_AUTO_LOGIN = False
#     EMAIL_CONFIRMATION_ANONYMOUS_REDIRECT_URL = "account_login"
#     EMAIL_CONFIRMATION_AUTHENTICATED_REDIRECT_URL = None
#     EMAIL_CONFIRMATION_URL = "account_confirm_email"
#     SETTINGS_REDIRECT_URL = "account_settings"
#     NOTIFY_ON_PASSWORD_CHANGE = True
#     DELETION_EXPUNGE_HOURS = 48
#     DEFAULT_HTTP_PROTOCOL = "https"
#     HOOKSET = "account.hooks.AccountDefaultHookSet"
#     TIMEZONES = TIMEZONES
#     LANGUAGES = LANGUAGES
# 
# Path: account/models.py
# class Account(models.Model):
# 
#     user = models.OneToOneField(settings.AUTH_USER_MODEL, related_name="account", verbose_name=_("user"), on_delete=models.CASCADE)
#     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 user.is_authenticated:
#             account = user.account
#             if account:
#                 return account
#         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"))
#         tz = settings.TIME_ZONE if not self.timezone else self.timezone
#         return now.astimezone(pytz.timezone(tz))
# 
#     def localtime(self, value):
#         """
#         Given a datetime object as value convert it to the timezone of
#         the account.
#         """
#         tz = 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(tz))
# 
# Path: account/utils.py
# def check_password_expired(user):
#     """
#     Return True if password is expired and system is using
#     password expiration, False otherwise.
#     """
#     if not settings.ACCOUNT_PASSWORD_USE_HISTORY:
#         return False
# 
#     if hasattr(user, "password_expiry"):
#         # user-specific value
#         expiry = user.password_expiry.expiry
#     else:
#         # use global value
#         expiry = settings.ACCOUNT_PASSWORD_EXPIRY
# 
#     if expiry == 0:  # zero indicates no expiration
#         return False
# 
#     try:
#         # get latest password info
#         latest = user.password_history.latest("timestamp")
#     except PasswordHistory.DoesNotExist:
#         return False
# 
#     now = datetime.datetime.now(tz=pytz.UTC)
#     expiration = latest.timestamp + datetime.timedelta(seconds=expiry)
# 
#     if expiration < now:
#         return True
#     else:
#         return False


# Path: account/middleware.py
from urllib.parse import urlparse, urlunparse
from django.contrib import messages
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.http import HttpResponseRedirect, QueryDict
from django.urls import resolve, reverse
from django.utils import timezone, translation
from django.utils.cache import patch_vary_headers
from django.utils.deprecation import MiddlewareMixin as BaseMiddleware
from django.utils.translation import gettext_lazy as _
from account import signals
from account.conf import settings
from account.models import Account
from account.utils import check_password_expired





class LocaleMiddleware(BaseMiddleware):
    """
    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: