Coverage for polls/models.py: 98%

41 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2023-07-17 14:22 -0600

1from django.db import models 

2from app.models import RandomSlugModel, SlugModel, TimeStampedModel 

3from app.choices import ModelChoices, ChoiceItem 

4from products.models import ProductPurchase 

5 

6 

7class Poll(RandomSlugModel, TimeStampedModel): 

8 """Model for Organization Poll""" 

9 

10 organization = models.ForeignKey("organizations.Organization", on_delete=models.CASCADE) 

11 name = models.CharField(max_length=32) 

12 

13 class StatusChoices(ModelChoices): 

14 building = ChoiceItem("BU", "building") 

15 ready = ChoiceItem("RE", "ready") 

16 published = ChoiceItem("PU", "published") 

17 finished = ChoiceItem("FI", "finished") 

18 

19 status = models.CharField(max_length=2, choices=StatusChoices.choices, default=StatusChoices.building) 

20 

21 class Meta: 

22 ordering = ["-created_at"] 

23 

24 

25class Question(RandomSlugModel): 

26 """Model for poll's question""" 

27 

28 order = models.IntegerField(null=True, blank=True) 

29 poll = models.ForeignKey("polls.Poll", on_delete=models.CASCADE, related_name="questions") 

30 text = models.CharField(max_length=256) 

31 

32 class TypeChoices(ModelChoices): 

33 short_text = ChoiceItem("STX", "short text") 

34 long_text = ChoiceItem("LTX", "long text") 

35 single_choice = ChoiceItem("SCH", "single choice") 

36 multiple_choice = ChoiceItem("MCH", "multiple choice") 

37 

38 type = models.CharField(max_length=3, choices=TypeChoices.choices) 

39 

40 class Meta: 

41 ordering = ["order"] 

42 

43 def save(self, *args, **kwargs): 

44 if not self.order: 44 ↛ 46line 44 didn't jump to line 46, because the condition on line 44 was never false

45 self.order = self.poll.questions.count() + 1 

46 super(Question, self).save(*args, **kwargs) 

47 

48 

49class Option(RandomSlugModel): 

50 """Answer Option""" 

51 

52 question = models.ForeignKey("polls.Question", on_delete=models.CASCADE, related_name="options") 

53 text = models.CharField(max_length=256) 

54 

55 class Meta: 

56 ordering = ["question", "text"] 

57 

58 

59class Answer(RandomSlugModel, TimeStampedModel): 

60 """Model for poll's answers""" 

61 

62 question = models.ForeignKey("polls.Question", on_delete=models.PROTECT, related_name="answers") 

63 membership = models.ForeignKey("memberships.Membership", on_delete=models.PROTECT, related_name="answers") 

64 

65 text = models.CharField(max_length=256) 

66 selected_options = models.ManyToManyField("polls.Option", blank=True)