from django.conf import settings from django.db import models class Note(models.Model): class Kind(models.TextChoices): TASK = 'task', 'Tâche' INFO = 'info', 'Information' MEMO = 'memo', 'Mémo' SUGGESTION = 'sugg', 'Suggestion' POLL = 'poll', 'Sondage' title = models.CharField( max_length=160, ) content = models.TextField( blank=True, max_length=5000, ) kind = models.CharField( max_length=4, choices=Kind.choices, default=Kind.MEMO, ) author = models.ForeignKey( settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, null=True, related_name='authored_notes', ) recipients = models.ManyToManyField( settings.AUTH_USER_MODEL, blank=True, related_name='addressed_notes', ) is_public = models.BooleanField( default=True, ) completed_at = models.DateTimeField( null=True, blank=True, ) created_at = models.DateTimeField( auto_now_add=True, ) updated_at = models.DateTimeField( auto_now=True, ) class Meta: ordering = [ '-created_at', '-pk', ] constraints = [ models.CheckConstraint( condition=( models.Q(kind='task') | models.Q( completed_at__isnull=True, ) ), name='only_tasks_can_be_completed', ), ] @property def is_done(self): return self.completed_at is not None def __str__(self): return self.title class Poll(models.Model): class VoteMode(models.TextChoices): SINGLE = 'single', 'Un seul choix' MULTIPLE = 'multiple', 'Plusieurs choix libres' EXACT = 'exact', 'Nombre précis de choix' note = models.OneToOneField(Note, on_delete=models.CASCADE, related_name='poll') vote_mode = models.CharField(max_length=8, choices=VoteMode.choices, default=VoteMode.SINGLE) exact_choices = models.PositiveSmallIntegerField(default=1) is_anonymous = models.BooleanField(default=True) closes_at = models.DateTimeField(null=True, blank=True) closed_at = models.DateTimeField(null=True, blank=True) locked_at = models.DateTimeField(null=True, blank=True) class PollOption(models.Model): poll = models.ForeignKey(Poll, on_delete=models.CASCADE, related_name='options') label = models.CharField(max_length=240) position = models.PositiveSmallIntegerField() class Meta: ordering = ['position', 'pk'] constraints = [models.UniqueConstraint(fields=['poll', 'position'], name='poll_option_position_unique')] class PollVote(models.Model): poll = models.ForeignKey(Poll, on_delete=models.CASCADE, related_name='votes') user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, on_delete=models.SET_NULL, related_name='poll_votes') options = models.ManyToManyField(PollOption, related_name='votes') created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) class Meta: constraints = [models.UniqueConstraint(fields=['poll', 'user'], name='poll_user_vote_unique')]