Skip to content
models.py 8.96 KiB
Newer Older
from __future__ import annotations


__copyright__ = "Copyright (C) 2014 Andreas Kloeckner"

__license__ = """
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""


Andreas Klöckner's avatar
Andreas Klöckner committed
from django.contrib.auth.models import (
    AbstractBaseUser,
    PermissionsMixin,
    UserManager,
Andreas Klöckner's avatar
Andreas Klöckner committed
)
from django.contrib.auth.validators import ASCIIUsernameValidator
from django.db import models
from django.utils import timezone
from django.utils.translation import gettext, gettext_lazy as _, pgettext_lazy
Andreas Klöckner's avatar
Andreas Klöckner committed
from course.constants import USER_STATUS_CHOICES

class User(AbstractBaseUser, PermissionsMixin):
    username = models.CharField(
        max_length=200,
        unique=True,
        help_text=_(
            "Required. Letters, digits and @/./+/-/_ only."),
Dong Zhuang's avatar
Dong Zhuang committed
        validators=[ASCIIUsernameValidator()],
        error_messages={
            "unique": _("A user with that username already exists."),
    first_name = models.CharField(_("first name"), max_length=100, blank=True)
    last_name = models.CharField(_("last name"), max_length=100, blank=True)
    email = models.EmailField(_("email address"), blank=True,
            max_length=200)
Andreas Klöckner's avatar
Andreas Klöckner committed
    name_verified = models.BooleanField(
Andreas Klöckner's avatar
Andreas Klöckner committed
        default=False,
        help_text=_(
            "Indicates that this user's name has been verified "
            "as being associated with the individual able to sign "
            "in to this account."
Andreas Klöckner's avatar
Andreas Klöckner committed
        ),
    )
    is_active = models.BooleanField(
        pgettext_lazy("User status", "active"),
        default=True,
        help_text=_(
            "Designates whether this user should be treated as active. "
            "Unselect this instead of deleting accounts."
    date_joined = models.DateTimeField(_("date joined"), default=timezone.now)

    objects = UserManager()

Andreas Klöckner's avatar
Andreas Klöckner committed
    is_staff = models.BooleanField(
Andreas Klöckner's avatar
Andreas Klöckner committed
        default=False,
        help_text=_("Designates whether the user can log into this admin site."),
Andreas Klöckner's avatar
Andreas Klöckner committed
    )

    institutional_id = models.CharField(max_length=100,
            verbose_name=_("Institutional ID"),
            blank=True, null=True, unique=True, db_index=True)
Andreas Klöckner's avatar
Andreas Klöckner committed
    institutional_id_verified = models.BooleanField(
        _("Institutional ID verified"),
Andreas Klöckner's avatar
Andreas Klöckner committed
        default=False,
        help_text=_(
            "Indicates that this user's institutional ID has been verified "
            "as being associated with the individual able to log "
            "in to this account."
Andreas Klöckner's avatar
Andreas Klöckner committed
        ),
    )
    status = models.CharField(max_length=50,
            choices=USER_STATUS_CHOICES,
            verbose_name=_("User status"),
Andreas Klöckner's avatar
Andreas Klöckner committed
            null=True)
    sign_in_key = models.CharField(max_length=50,
            help_text=_("The sign in token sent out in email."),
            null=True, unique=True, db_index=True, blank=True,
            # Translators: the sign in token of the user.
            verbose_name=_("Sign in key"))
Andreas Klöckner's avatar
Andreas Klöckner committed
    key_time = models.DateTimeField(default=None,
            null=True, blank=True,
            help_text=_("The time stamp of the sign in token."),
            # Translators: the time when the token is sent out.
            verbose_name=_("Key time"))
Andreas Klöckner's avatar
Andreas Klöckner committed

    editor_mode = models.CharField(max_length=20,
            help_text=_("Which key bindings you prefer when editing "
                        "larger amounts of text or code. "
                        "(If you do not understand what this means, "
                        "leave it as 'Default'.)"),
            choices=(
                ("default", _("Default")),
                ("emacs", "Emacs"),
                ("vim", "Vim"),
                ),
            default="default",
            # Translators: the text editor used by participants
            verbose_name=_("Editor mode"))

    USERNAME_FIELD = "username"
    REQUIRED_FIELDS = ["email"]
        verbose_name = _("user")
        verbose_name_plural = _("users")
    def get_full_name(
                self, allow_blank=True, force_verbose_blank=False
            ) -> str | None:
Andreas Klöckner's avatar
Andreas Klöckner committed
        if (not allow_blank
                and (not self.first_name or not self.last_name)):
        def verbose_blank(s: str) -> str:
            if force_verbose_blank:
                if not s:
                    return gettext("(blank)")
        def default_fullname(first_name: str, last_name: str) -> str:
Andreas Klöckner's avatar
Andreas Klöckner committed
            Returns the first_name plus the last_name, with a space in
            return f"{verbose_blank(first_name)} {verbose_blank(last_name)}"
        from accounts.utils import relate_user_method_settings
        format_method = relate_user_method_settings.custom_full_name_method
        if format_method is None:
            format_method = default_fullname
            full_name = format_method(
                verbose_blank(self.first_name), verbose_blank(self.last_name))
Andreas Klöckner's avatar
Andreas Klöckner committed
        except Exception:
            full_name = default_fullname(
                verbose_blank(self.first_name), verbose_blank(self.last_name))
        return full_name.strip()
    def get_masked_profile(self) -> str:
        """
        Returns the masked user profile.
        """

        def default_mask_method(user):
            return "{}{}".format(_("User"), str(user.pk))
        from accounts.utils import relate_user_method_settings
        mask_method = relate_user_method_settings.custom_profile_mask_method
        if mask_method is None:
            mask_method = default_mask_method

        # Intentionally don't fallback if it failed -- let user see the exception.
        result = mask_method(self)
        if not result:
            raise RuntimeError("get_masked_profile should not return None.")
        else:
            result = str(result).strip()
        if not result:
            raise RuntimeError("get_masked_profile should not return "
                               "an empty string.")
        return result
    def get_short_name(self) -> str:
        """Returns the short name for the user."""
        return self.first_name

    def get_email_appellation(self) -> str:
        """Return the appellation of the receiver in email."""
        from accounts.utils import relate_user_method_settings
        priority_list = (
            relate_user_method_settings.email_appellation_priority_list)

        for attr in priority_list:
            if attr == "full_name":
                appellation = self.get_full_name(allow_blank=False)
            else:
                appellation = getattr(self, attr)

        return gettext("user")
    def clean(self) -> None:

        # email can be None in Django admin when create new user
        if self.email is not None:
            self.email = self.email.strip()

        if self.email:
            qset = self.__class__.objects.filter(email__iexact=self.email)
            if self.pk is not None:
                # In case editing an existing user object
                qset = qset.exclude(pk=self.pk)
            if qset.exists():
                from django.core.exceptions import ValidationError
                raise ValidationError(
                    {"email": _("That email address is already in use.")})
    def save(self, *args, **kwargs):
        update_fields = kwargs.get("update_fields")

        # This is for backward compatibility.
        # Because user instances are frequently updated when auth_login,
Dong Zhuang's avatar
Dong Zhuang committed
        # reset_password. Without this, no user will be able to login.
        if ((update_fields is not None and "email" in update_fields)
                or self.pk is None):
            self.clean()

        if self.institutional_id is not None:
            self.institutional_id = self.institutional_id.strip()

        # works around https://code.djangoproject.com/ticket/4136#comment:33
        self.institutional_id = self.institutional_id or None
# }}}

# vim: foldmethod=marker