from django.contrib.auth.models import User, Group from django.db import models from django.dispatch import receiver from django.db.models.signals import post_save, post_delete from django.utils.translation import ugettext_lazy as _ from cof.petits_cours_models import choices_length # --- # User management # --- OCCUPATION_CHOICES = ( ('exterieur', _("Extérieur")), ('1A', _("1A")), ('2A', _("2A")), ('3A', _("3A")), ('4A', _("4A")), ('archicube', _("Archicube")), ('doctorant', _("Doctorant")), ('CST', _("CST")), ) class Profile(models.Model): user = models.OneToOneField( User, on_delete=models.CASCADE, related_name="profile" ) login_clipper = models.CharField("Login clipper", max_length=8, blank=True) phone = models.CharField("Téléphone", max_length=20, blank=True) occupation = models.CharField(_("Occupation"), default="1A", choices=OCCUPATION_CHOICES, max_length=choices_length( OCCUPATION_CHOICES)) departement = models.CharField(_("Département"), max_length=50, blank=True) comments = models.TextField( "Commentaires visibles par l'utilisateur", blank=True) class Meta: verbose_name = "Profil" verbose_name_plural = "Profils" def __str__(self): return self.user.username @receiver(post_save, sender=User) def create_user_profile(sender, instance, created, **kwargs): if created: Profile.objects.get_or_create(user=instance) @receiver(post_delete, sender=Profile) def post_delete_user(sender, instance, *args, **kwargs): instance.user.delete() # --- # Clubs # --- class Club(models.Model): ANNUAL = "ANN" SEMESTER = "SEM" COURSE = "COU" COTISATION_FREQUENCY_CHOICES = [ (ANNUAL, _("Annuel")), (SEMESTER, _("Semestriel")), (COURSE, _("Au cours")) ] associations = models.ManyToManyField(Group, related_name="clubs") name = models.CharField(_("Nom"), max_length=200, unique=True) description = models.TextField("Description", blank=True) members = models.ManyToManyField( User, through="ClubUser", related_name="in_clubs", blank=True ) price = models.DecimalField( _("Cotisation (€)"), decimal_places=2, max_digits=5, blank=True, default=0 ) cotisation_frequency = models.CharField( _("Fréquence de la cotisation"), default=ANNUAL, choices=COTISATION_FREQUENCY_CHOICES, max_length=3, blank=True ) def __str__(self): template = ( self.price and "{name} ({price}€ / {cotisation_frequency})" or "{name}" ) return template.format(**vars(self)) class ClubUser(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE) club = models.ForeignKey(Club, on_delete=models.CASCADE) is_respo = models.BooleanField(_("Est responsable du club")) has_paid = models.BooleanField(_("A payé sa cotisation")) # --- # Events # --- class Event(models.Model): associations = models.ManyToManyField(Group, related_name="events") title = models.CharField("Titre", max_length=200) location = models.CharField("Lieu", max_length=200) start_date = models.DateTimeField("Date de début", blank=True, null=True) end_date = models.DateTimeField("Date de fin", blank=True, null=True) description = models.TextField("Description", blank=True) image = models.ImageField( "Image", blank=True, null=True, upload_to="imgs/events/" ) registration_open = models.BooleanField( "Inscriptions ouvertes", default=True ) old = models.BooleanField("Archiver (événement fini)", default=False) class Meta: verbose_name = "Événement" def __str__(self): return self.title class EventCommentField(models.Model): TEXT = "text" CHAR = "char" FIELD_TYPE = [ (TEXT, _("Texte long")), (CHAR, _("Texte court")), ] event = models.ForeignKey( Event, on_delete=models.CASCADE, related_name="commentfields" ) name = models.CharField("Champ", max_length=200) fieldtype = models.CharField( "Type", max_length=10, choices=FIELD_TYPE, default=TEXT ) default = models.TextField("Valeur par défaut", blank=True) class Meta: verbose_name = "Champ" def __str__(self): return self.name class EventCommentValue(models.Model): commentfield = models.ForeignKey( EventCommentField, on_delete=models.CASCADE, related_name="values" ) registration = models.ForeignKey( "EventRegistration", on_delete=models.CASCADE, related_name="comments" ) content = models.TextField("Contenu", blank=True, null=True) def __str__(self): return "Commentaire de {}".format(self.commentfield) class EventOption(models.Model): event = models.ForeignKey( Event, on_delete=models.CASCADE, related_name="options" ) name = models.CharField("Option", max_length=200) multi_choices = models.BooleanField("Choix multiples", default=False) class Meta: verbose_name = "Option des événements" verbose_name_plural = "Options des événements" def __str__(self): return self.name class EventOptionChoice(models.Model): event_option = models.ForeignKey( EventOption, on_delete=models.CASCADE, related_name="choices" ) value = models.CharField("Valeur", max_length=200) class Meta: verbose_name = "Choix" verbose_name_plural = "Choix" def __str__(self): return self.value class EventRegistration(models.Model): user = models.ForeignKey( User, on_delete=models.CASCADE ) event = models.ForeignKey( Event, on_delete=models.CASCADE ) options = models.ManyToManyField(EventOptionChoice) filledcomments = models.ManyToManyField(EventCommentField, through=EventCommentValue) paid = models.BooleanField("A payé", default=False) class Meta: verbose_name = "Inscription" unique_together = ("user", "event") def __str__(self): return "Inscription de {} à {}".format(self.user, self.event.title)