Advertisement
lamorfini

Untitled

May 19th, 2024
698
0
351 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.21 KB | None | 0 0
  1. from django.contrib.auth.models import AbstractUser, PermissionsMixin
  2. from django.db import models
  3. from django.utils import timezone
  4. from django.utils.translation import gettext_lazy as _
  5. from core.choices import Display_name, Sex
  6. from stdimage import StdImageField
  7. from core.utils import PathAndRename, resize_and_autorotate
  8. from django.conf import settings
  9. from core.choices import Status
  10. from django.urls import reverse
  11.  
  12. class User(AbstractUser, PermissionsMixin):
  13.     email = models.EmailField(_('Email address'), unique=True)
  14.     display_name = models.CharField(max_length=4, choices=Display_name.choices, default=Display_name.USERNAME, blank=True, null=True)
  15.     date_of_birth = models.DateField(_('Date of birth'), blank=True, null=True)
  16.     sex = models.CharField(max_length=4, choices=Sex.choices, default=Sex.NONE, blank=True, null=True)
  17.     telephone = models.CharField(_('Telephone'), max_length=255, blank=True, null=True)
  18.     mobile_phone = models.CharField(_('Telephone Mobile'), max_length=255,  blank=True, null=True)
  19.     address = models.CharField(_('Address'), max_length=255, blank=True, null=True)
  20.     secondary_address = models.CharField(_('Secondary Address'), max_length=255, blank=True, null=True)
  21.     city = models.CharField(_('City'), max_length=255, blank=True, null=True)
  22.     province = models.CharField(_('Province'), max_length=255, blank=True, null=True)
  23.     state = models.CharField(_('State'), max_length=255, null=True, blank=True)
  24.     postal_code = models.CharField(_('Postal code'), max_length=50, blank=True, null=True)
  25.     note = models.TextField(_('Note'), blank=True, null=True)
  26.     date_joined = models.DateTimeField(default=timezone.now)
  27.    
  28.     USERNAME_FIELD  = 'username'
  29.     EMAIL_FIELD     = 'email'
  30.     REQUIRED_FIELDS = ['first_name', 'last_name', 'email']
  31.  
  32.  
  33.     def __str__(self):
  34.         return f'{self.username}'
  35.  
  36.     def save(self, *args, **kwargs):
  37.         super(User, self).save(*args, **kwargs)
  38.  
  39.     class Meta:
  40.         ordering = ('-date_joined',)
  41.  
  42.  
  43.  
  44. class Photo(models.Model):
  45.     user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
  46.     photo_profile =StdImageField(upload_to=PathAndRename("user_photo/"), delete_orphans=True, render_variations=resize_and_autorotate,  blank=True ,
  47.                             variations={'thumbnail': (160, 120, True),}
  48.                             )
  49.     photo_views = models.BooleanField(default=False)
  50.     likes = models.BigIntegerField(default=0)
  51.     views = models.BigIntegerField(default=0)
  52.     status = models.CharField(max_length=50, choices=Status.choices, default=Status.DRAFT)
  53.     created = models.DateTimeField(_('created'), auto_now_add=True)
  54.     modified = models.DateTimeField(_('modified'), auto_now=True)
  55.  
  56.     def save(self, *args, **kwargs):
  57.         super().save(*args, **kwargs)
  58.  
  59.     def __str__(self):
  60.         return self.photo_profile
  61.  
  62.     def get_absolute_url(self):
  63.         """
  64.        <a href="{{ model.get_absolute_url }}" class="model__title">{{ model.title }}</a>
  65.        """
  66.         return reverse('detail', kwargs={'user': self.user})
  67.  
  68.     class Meta:
  69.         verbose_name = _("Photo")
  70.         verbose_name_plural = _("All Photo user")
  71.         ordering = ['-created']
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement