|
1 | | -from django.contrib.auth.models import AbstractUser |
| 1 | +from django.conf import settings |
| 2 | +from django.contrib.auth.base_user import BaseUserManager |
| 3 | +from django.contrib.auth.models import PermissionsMixin, AbstractUser |
| 4 | +from django.core.validators import RegexValidator |
2 | 5 | from django.db import models |
| 6 | +from django.utils import timezone |
| 7 | +from django.utils.translation import gettext_lazy as _ |
| 8 | +from simple_history.models import HistoricalRecords |
3 | 9 |
|
4 | 10 | from .utils import profile_upload_to_unique |
5 | 11 |
|
6 | 12 |
|
7 | | -class User(AbstractUser): |
8 | | - email = models.EmailField(unique=True) |
| 13 | +class Profile(models.Model): |
| 14 | + """ |
| 15 | + Model representing a user's profile. |
| 16 | + Connects to income, expenses, and allows for personal data storage. |
| 17 | + """ |
| 18 | + user = models.OneToOneField( |
| 19 | + settings.AUTH_USER_MODEL, |
| 20 | + on_delete=models.CASCADE, |
| 21 | + related_name="profile" |
| 22 | + ) |
| 23 | + balance = models.DecimalField( |
| 24 | + max_digits=10, |
| 25 | + decimal_places=2, |
| 26 | + default=0.0, |
| 27 | + help_text="User's current balance." |
| 28 | + ) |
| 29 | + date_created = models.DateTimeField(auto_now_add=True) |
| 30 | + profile_pic = models.ImageField( |
| 31 | + upload_to=profile_upload_to_unique, |
| 32 | + blank=True, |
| 33 | + null=True, |
| 34 | + help_text="User's profile picture." |
| 35 | + ) |
9 | 36 |
|
| 37 | + # Add historical records field to track changes |
| 38 | + history = HistoricalRecords() |
10 | 39 |
|
11 | | -class Profile(models.Model): |
12 | | - user = models.OneToOneField(User, on_delete=models.CASCADE) |
13 | | - bio = models.TextField(blank=True) |
14 | | - location = models.CharField(max_length=100, blank=True) |
15 | | - birth_date = models.DateField(null=True, blank=True) |
16 | | - picture = models.ImageField(upload_to=profile_upload_to_unique, null=True, blank=True) |
| 40 | + def __str__(self): |
| 41 | + """String representation of the profile object, displaying the associated user's username.""" |
| 42 | + return f'Profile of {self.user}' |
17 | 43 |
|
18 | | - class Meta: |
19 | | - verbose_name = "Profile" |
20 | | - verbose_name_plural = "Profiles" |
21 | | - indexes = [ |
22 | | - models.Index(fields=['user']), |
23 | | - ] |
24 | | - ordering = ['user'] |
| 44 | + |
| 45 | +# Custom User Manager |
| 46 | +class UserManager(BaseUserManager): |
| 47 | + def create_user(self, email, password=None, **extra_fields): |
| 48 | + if not email: |
| 49 | + raise ValueError(_('The Email field must be set')) |
| 50 | + email = self.normalize_email(email) |
| 51 | + user = self.model(email=email, **extra_fields) |
| 52 | + user.set_password(password) |
| 53 | + user.save(using=self._db) |
| 54 | + return user |
| 55 | + |
| 56 | + def create_superuser(self, email, password=None, **extra_fields): |
| 57 | + extra_fields.setdefault('is_staff', True) |
| 58 | + extra_fields.setdefault('is_superuser', True) |
| 59 | + extra_fields.setdefault('is_active', True) |
| 60 | + |
| 61 | + if extra_fields.get('is_staff') is not True: |
| 62 | + raise ValueError(_('Superuser must have is_staff=True.')) |
| 63 | + if extra_fields.get('is_superuser') is not True: |
| 64 | + raise ValueError(_('Superuser must have is_superuser=True.')) |
| 65 | + |
| 66 | + return self.create_user(email, password, **extra_fields) |
| 67 | + |
| 68 | + |
| 69 | +# Custom User Model |
| 70 | +class UserAccount(AbstractUser, PermissionsMixin): |
| 71 | + email = models.EmailField(_('email address'), unique=True) |
| 72 | + username = models.CharField(_('username'), max_length=30, unique=True, blank=False, |
| 73 | + help_text="User's unique username", |
| 74 | + validators=[ |
| 75 | + RegexValidator( |
| 76 | + regex=r'^[\w-]+$', |
| 77 | + message=_( |
| 78 | + "Username can only contain letters, numbers, underscores, or hyphens.") |
| 79 | + ) |
| 80 | + ] |
| 81 | + ) |
| 82 | + phone_number = models.CharField(_('phone number'), max_length=15, unique=True, null=True, blank=True, |
| 83 | + validators=[ |
| 84 | + RegexValidator( |
| 85 | + regex=r'^\+?1?\d{9,15}$', |
| 86 | + message=_( |
| 87 | + "Phone number must be entered in the format: '+999999999'. Up to 15 digits allowed." |
| 88 | + ), |
| 89 | + ) |
| 90 | + ], ) |
| 91 | + first_name = models.CharField(_('first name'), max_length=30, blank=True) |
| 92 | + last_name = models.CharField(_('last name'), max_length=30, blank=True) |
| 93 | + profile_image = models.ImageField(_('profile image'), upload_to='profile_images/', null=True, blank=True) |
| 94 | + bio = models.TextField(_('bio'), max_length=500, blank=True) |
| 95 | + last_login_ip = models.GenericIPAddressField(_('last login IP'), null=True, blank=True) |
| 96 | + last_login = models.DateTimeField(_('last login'), auto_now=True) |
| 97 | + date_joined = models.DateTimeField(_('date joined'), default=timezone.now) |
| 98 | + |
| 99 | + is_active = models.BooleanField(_('active'), default=True) |
| 100 | + is_staff = models.BooleanField(_('staff status'), default=False) |
| 101 | + is_manager = models.BooleanField(_('manager status'), default=False) |
| 102 | + is_admin = models.BooleanField(_('admin status'), default=False) |
| 103 | + |
| 104 | + objects = UserManager() |
| 105 | + |
| 106 | + USERNAME_FIELD = 'email' |
| 107 | + REQUIRED_FIELDS = ['username', 'phone_number', 'first_name', 'last_name'] |
| 108 | + |
| 109 | + @property |
| 110 | + def name(self): |
| 111 | + return f'{self.first_name} {self.last_name}' |
25 | 112 |
|
26 | 113 | def __str__(self): |
27 | | - return f'{self.user.username} Profile' |
| 114 | + return self.username or self.email |
| 115 | + |
| 116 | + class Meta: |
| 117 | + verbose_name = _('user') |
| 118 | + verbose_name_plural = _('users') |
| 119 | + ordering = ['-pk'] |
0 commit comments