Coverage for polls/models.py: 98%
41 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-03-29 14:03 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-03-29 14:03 -0600
1from django.db import models
2from app.models import RandomSlugModel, SlugModel, TimeStampedModel
3from app.choices import ModelChoices, ChoiceItem
4from products.models import ProductPurchase
7class Poll(RandomSlugModel, TimeStampedModel):
8 """Model for Organization Poll"""
10 organization = models.ForeignKey("organizations.Organization", on_delete=models.CASCADE)
11 name = models.CharField(max_length=32)
13 class StatusChoices(ModelChoices):
14 building = ChoiceItem("BU", "building")
15 ready = ChoiceItem("RE", "ready")
16 published = ChoiceItem("PU", "published")
17 finished = ChoiceItem("FI", "finished")
19 status = models.CharField(max_length=2, choices=StatusChoices.choices, default=StatusChoices.building)
21 class Meta:
22 ordering = ["-created_at"]
25class Question(RandomSlugModel):
26 """Model for poll's question"""
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)
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")
38 type = models.CharField(max_length=3, choices=TypeChoices.choices)
40 class Meta:
41 ordering = ["order"]
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)
49class Option(RandomSlugModel):
50 """Answer Option"""
52 question = models.ForeignKey("polls.Question", on_delete=models.CASCADE, related_name="options")
53 text = models.CharField(max_length=256)
55 class Meta:
56 ordering = ["question", "text"]
59class Answer(RandomSlugModel, TimeStampedModel):
60 """Model for poll's answers"""
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")
65 text = models.CharField(max_length=256)
66 selected_options = models.ManyToManyField("polls.Option", blank=True)