2024-01-06 12:13:39 +08:00
|
|
|
from django.db import models
|
|
|
|
from django.contrib.auth.models import AbstractUser
|
|
|
|
from django.urls import reverse
|
|
|
|
from django_resized import ResizedImageField
|
2024-05-10 23:15:29 +08:00
|
|
|
from django.utils import timezone
|
|
|
|
from datetime import timedelta
|
2024-01-06 12:13:39 +08:00
|
|
|
|
|
|
|
|
|
|
|
class CustomUser(AbstractUser):
|
|
|
|
# first_name inherited from base user class
|
|
|
|
# last_name inherited from base user class
|
|
|
|
# email inherited from base user class
|
|
|
|
# username inherited from base user class
|
|
|
|
# password inherited from base user class
|
|
|
|
# is_admin inherited from base user class
|
|
|
|
|
2024-01-06 12:35:36 +08:00
|
|
|
avatar = ResizedImageField(
|
|
|
|
null=True, force_format="WEBP", quality=100, upload_to='avatars/')
|
2024-01-06 12:13:39 +08:00
|
|
|
|
2024-05-10 23:15:29 +08:00
|
|
|
# Used for onboarding processes
|
|
|
|
# Set this to False later on once the user makes actions
|
|
|
|
onboarding = models.BooleanField(default=True)
|
|
|
|
|
|
|
|
user_group = models.ForeignKey(
|
|
|
|
'user_groups.UserGroup', on_delete=models.SET_NULL, null=True)
|
2024-01-06 12:13:39 +08:00
|
|
|
|
|
|
|
@property
|
2024-05-10 23:15:29 +08:00
|
|
|
def group_member(self):
|
|
|
|
if self.user_group:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
2024-01-06 12:13:39 +08:00
|
|
|
|
2024-05-10 23:15:29 +08:00
|
|
|
# Can be used to show tooltips for newer users
|
2024-01-06 12:13:39 +08:00
|
|
|
@property
|
2024-05-10 23:15:29 +08:00
|
|
|
def is_new(self):
|
|
|
|
current_date = timezone.now()
|
|
|
|
return self.date_joined + timedelta(days=1) < current_date
|
2024-01-06 12:13:39 +08:00
|
|
|
|
2024-05-10 23:15:29 +08:00
|
|
|
@property
|
|
|
|
def full_name(self):
|
|
|
|
return f"{self.first_name} {self.last_name}"
|
2024-01-06 12:13:39 +08:00
|
|
|
|
2024-05-10 23:15:29 +08:00
|
|
|
@property
|
|
|
|
def group_member(self):
|
|
|
|
if self.user_group:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
2024-01-06 12:13:39 +08:00
|
|
|
|
2024-05-10 23:15:29 +08:00
|
|
|
@property
|
|
|
|
def group_owner(self):
|
|
|
|
if self.user_group and self == self.user_group.owner:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
2024-01-06 12:13:39 +08:00
|
|
|
|
2024-05-10 23:15:29 +08:00
|
|
|
@property
|
|
|
|
def admin_url(self):
|
|
|
|
return reverse('admin:users_customuser_change', args=(self.pk,))
|