justpalma

models.py

Sep 25th, 2020
1,106
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from django.db import models
  2. from django.contrib.auth.models import AbstractUser
  3. from django.core.exceptions import ObjectDoesNotExist, ValidationError
  4. from django.db import models
  5. from django.utils.translation import gettext_lazy as _
  6.  
  7. from Progetto_web import settings
  8.  
  9. # type of user
  10. # manager -> can insert sport facilities
  11. # player -> can book sport facilities
  12.  
  13.  
  14. # Create your models here.
  15.  
  16.  
  17. class AccountUser(AbstractUser):
  18.     # email = models.EmailField(_('email address'), unique=True)
  19.     AbstractUser._meta.get_field('email')._unique = True
  20.  
  21.     terms_of_service_acceptance = models.BooleanField(default=False)
  22.     terms_of_service_acceptance_datetime = models.DateTimeField(auto_now_add=True)
  23.  
  24.     is_club = models.BooleanField(default=False)
  25.  
  26.     REQUIRED_FIELDS = ['username']
  27.     USERNAME_FIELD = 'email'
  28.  
  29.     class Meta:
  30.         verbose_name = _('Account')
  31.         verbose_name_plural = _('Accounts')
  32.  
  33.     @property
  34.     def has_profile(self):
  35.         try:
  36.             assert self.profile
  37.             return True
  38.         except ObjectDoesNotExist:
  39.             return False
  40.  
  41.     def clean(self):
  42.         if not self.terms_of_service_acceptance:
  43.             raise ValidationError(_("You must accept the terms of service"))
  44.  
  45.  
  46. class Club(models.Model):
  47.  
  48.     first_name = models.CharField(max_length=50)
  49.     middle_name = models.CharField(max_length=50, null=True, blank=True)
  50.     last_name = models.CharField(max_length=50)
  51.  
  52.     club_name = models.CharField(max_length=50)
  53.     city = models.CharField(max_length=50)
  54.  
  55.     user = models.OneToOneField(
  56.         settings.AUTH_USER_MODEL,
  57.         on_delete=models.CASCADE,
  58.         related_name='club'
  59.     )
  60.  
  61.     class Meta:
  62.         verbose_name_plural = 'Clubs'
  63.         ordering = ['-club_name', 'city']
  64.  
  65.  
  66.     def save(self, *args, **kwargs):
  67.         super().save(*args, **kwargs)
  68.  
  69.  
  70. class Player(models.Model):
  71.     first_name = models.CharField(max_length=50)
  72.     middle_name = models.CharField(max_length=50, null=True, blank=True)
  73.     last_name = models.CharField(max_length=50)
  74.  
  75.     user = models.OneToOneField(
  76.         settings.AUTH_USER_MODEL,
  77.         on_delete=models.CASCADE,
  78.         related_name='Players'
  79.     )
  80.  
  81.     class Meta:
  82.         verbose_name_plural = 'People'
  83.         ordering = ['-last_name', 'first_name']
  84.  
  85.     def save(self, *args, **kwargs):
  86.         super().save(*args, **kwargs)
  87.  
  88.  
  89.  
RAW Paste Data