models.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import os
  2. import uuid
  3. from django.conf import settings
  4. from django.db import models
  5. from django.utils.translation import get_language
  6. from django.utils.translation import gettext_lazy as _
  7. def upload_avatar_to(instance, filename):
  8. filename, ext = os.path.splitext(filename)
  9. return os.path.join(
  10. "avatar_images",
  11. "avatar_{uuid}_{filename}{ext}".format(
  12. uuid=uuid.uuid4(), filename=filename, ext=ext
  13. ),
  14. )
  15. class UserProfile(models.Model):
  16. user = models.OneToOneField(
  17. settings.AUTH_USER_MODEL,
  18. on_delete=models.CASCADE,
  19. related_name="wagtail_userprofile",
  20. )
  21. submitted_notifications = models.BooleanField(
  22. verbose_name=_("submitted notifications"),
  23. default=True,
  24. help_text=_("Receive notification when a page is submitted for moderation"),
  25. )
  26. approved_notifications = models.BooleanField(
  27. verbose_name=_("approved notifications"),
  28. default=True,
  29. help_text=_("Receive notification when your page edit is approved"),
  30. )
  31. rejected_notifications = models.BooleanField(
  32. verbose_name=_("rejected notifications"),
  33. default=True,
  34. help_text=_("Receive notification when your page edit is rejected"),
  35. )
  36. updated_comments_notifications = models.BooleanField(
  37. verbose_name=_("updated comments notifications"),
  38. default=True,
  39. help_text=_(
  40. "Receive notification when comments have been created, resolved, or deleted on a page that you have subscribed to receive comment notifications on"
  41. ),
  42. )
  43. preferred_language = models.CharField(
  44. verbose_name=_("preferred language"),
  45. max_length=10,
  46. help_text=_("Select language for the admin"),
  47. default="",
  48. )
  49. current_time_zone = models.CharField(
  50. verbose_name=_("current time zone"),
  51. max_length=40,
  52. help_text=_("Select your current time zone"),
  53. default="",
  54. )
  55. avatar = models.ImageField(
  56. verbose_name=_("profile picture"),
  57. upload_to=upload_avatar_to,
  58. blank=True,
  59. )
  60. dismissibles = models.JSONField(default=dict, blank=True)
  61. class AdminColorThemes(models.TextChoices):
  62. SYSTEM = "system", _("System default")
  63. LIGHT = "light", _("Light")
  64. DARK = "dark", _("Dark")
  65. theme = models.CharField(
  66. verbose_name=_("admin theme"),
  67. choices=AdminColorThemes.choices,
  68. default=AdminColorThemes.SYSTEM,
  69. max_length=40,
  70. )
  71. class AdminContrastThemes(models.TextChoices):
  72. SYSTEM = "system", _("System default")
  73. MORE_CONTRAST = "more_contrast", _("More contrast")
  74. contrast = models.CharField(
  75. verbose_name=_("contrast"),
  76. choices=AdminContrastThemes.choices,
  77. default=AdminContrastThemes.SYSTEM,
  78. max_length=40,
  79. )
  80. class AdminDensityThemes(models.TextChoices):
  81. DEFAULT = "default", _("Default")
  82. SNUG = "snug", _("Snug")
  83. density = models.CharField(
  84. # Translators: "Density" is the term used to describe the amount of space between elements in the user interface
  85. verbose_name=_("density"),
  86. choices=AdminDensityThemes.choices,
  87. default=AdminDensityThemes.DEFAULT,
  88. max_length=40,
  89. )
  90. @classmethod
  91. def get_for_user(cls, user):
  92. return cls.objects.get_or_create(user=user)[0]
  93. def get_preferred_language(self):
  94. return self.preferred_language or get_language()
  95. def get_current_time_zone(self):
  96. return self.current_time_zone or settings.TIME_ZONE
  97. def __str__(self):
  98. return self.user.get_username()
  99. class Meta:
  100. verbose_name = _("user profile")
  101. verbose_name_plural = _("user profiles")