1import os
2import uuid
3
4from django.conf import settings
5from django.db import models
6from django.utils.translation import get_language
7from django.utils.translation import gettext_lazy as _
8
9
10def upload_avatar_to(instance, filename):
11    filename, ext = os.path.splitext(filename)
12    return os.path.join(
13        'avatar_images',
14        'avatar_{uuid}_{filename}{ext}'.format(
15            uuid=uuid.uuid4(), filename=filename, ext=ext)
16    )
17
18
19class UserProfile(models.Model):
20    user = models.OneToOneField(
21        settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='wagtail_userprofile'
22    )
23
24    submitted_notifications = models.BooleanField(
25        verbose_name=_('submitted notifications'),
26        default=True,
27        help_text=_("Receive notification when a page is submitted for moderation")
28    )
29
30    approved_notifications = models.BooleanField(
31        verbose_name=_('approved notifications'),
32        default=True,
33        help_text=_("Receive notification when your page edit is approved")
34    )
35
36    rejected_notifications = models.BooleanField(
37        verbose_name=_('rejected notifications'),
38        default=True,
39        help_text=_("Receive notification when your page edit is rejected")
40    )
41
42    updated_comments_notifications = models.BooleanField(
43        verbose_name=_('updated comments notifications'),
44        default=True,
45        help_text=_("Receive notification when comments have been created, resolved, or deleted on a page that you have subscribed to receive comment notifications on")
46    )
47
48    preferred_language = models.CharField(
49        verbose_name=_('preferred language'),
50        max_length=10,
51        help_text=_("Select language for the admin"),
52        default=''
53    )
54
55    current_time_zone = models.CharField(
56        verbose_name=_('current time zone'),
57        max_length=40,
58        help_text=_("Select your current time zone"),
59        default=''
60    )
61
62    avatar = models.ImageField(
63        verbose_name=_('profile picture'),
64        upload_to=upload_avatar_to,
65        blank=True,
66    )
67
68    @classmethod
69    def get_for_user(cls, user):
70        return cls.objects.get_or_create(user=user)[0]
71
72    def get_preferred_language(self):
73        return self.preferred_language or get_language()
74
75    def get_current_time_zone(self):
76        return self.current_time_zone or settings.TIME_ZONE
77
78    def __str__(self):
79        return self.user.get_username()
80
81    class Meta:
82        verbose_name = _('user profile')
83        verbose_name_plural = _('user profiles')
84