Coverage for website/events/models/registration_information_field.py: 97.30%
62 statements
« prev ^ index » next coverage.py v7.6.7, created at 2025-08-14 10:31 +0000
« prev ^ index » next coverage.py v7.6.7, created at 2025-08-14 10:31 +0000
1from django.db import models
2from django.utils.translation import gettext_lazy as _
4from . import Event, EventRegistration
7class RegistrationInformationField(models.Model):
8 """Describes a field description to ask for when registering."""
10 BOOLEAN_FIELD = "boolean"
11 INTEGER_FIELD = "integer"
12 TEXT_FIELD = "text"
14 FIELD_TYPES = (
15 (BOOLEAN_FIELD, _("Checkbox")),
16 (TEXT_FIELD, _("Text")),
17 (INTEGER_FIELD, _("Integer")),
18 )
20 event = models.ForeignKey(Event, models.CASCADE)
22 type = models.CharField(
23 _("field type"),
24 choices=FIELD_TYPES,
25 max_length=10,
26 )
28 name = models.CharField(
29 _("field name"),
30 max_length=100,
31 )
33 description = models.TextField(
34 _("description"),
35 null=True,
36 blank=True,
37 )
39 required = models.BooleanField(
40 _("required"),
41 )
43 def get_value_for(self, registration):
44 if self.type == self.TEXT_FIELD:
45 value_set = self.textregistrationinformation_set
46 elif self.type == self.BOOLEAN_FIELD:
47 value_set = self.booleanregistrationinformation_set
48 elif self.type == self.INTEGER_FIELD: 48 ↛ 51line 48 didn't jump to line 51 because the condition on line 48 was always true
49 value_set = self.integerregistrationinformation_set
51 try:
52 return value_set.get(registration=registration).value
53 except (
54 TextRegistrationInformation.DoesNotExist,
55 BooleanRegistrationInformation.DoesNotExist,
56 IntegerRegistrationInformation.DoesNotExist,
57 ):
58 return None
60 def set_value_for(self, registration, value):
61 if self.type == self.TEXT_FIELD:
62 value_set = self.textregistrationinformation_set
63 elif self.type == self.BOOLEAN_FIELD:
64 value_set = self.booleanregistrationinformation_set
65 elif self.type == self.INTEGER_FIELD: 65 ↛ 68line 65 didn't jump to line 68 because the condition on line 65 was always true
66 value_set = self.integerregistrationinformation_set
68 try:
69 field_value = value_set.get(registration=registration)
70 except BooleanRegistrationInformation.DoesNotExist:
71 field_value = BooleanRegistrationInformation()
72 except TextRegistrationInformation.DoesNotExist:
73 field_value = TextRegistrationInformation()
74 except IntegerRegistrationInformation.DoesNotExist:
75 field_value = IntegerRegistrationInformation()
77 field_value.registration = registration
78 field_value.field = self
79 field_value.value = value
80 field_value.full_clean()
81 field_value.save()
83 def __str__(self):
84 return f"{self.name} ({dict(self.FIELD_TYPES)[self.type]})"
86 class Meta:
87 order_with_respect_to = "event"
90class AbstractRegistrationInformation(models.Model):
91 """Abstract to contain common things for registration information."""
93 registration = models.ForeignKey(EventRegistration, models.CASCADE)
94 field = models.ForeignKey(RegistrationInformationField, models.CASCADE)
95 changed = models.DateTimeField(_("last changed"), auto_now=True)
97 def __str__(self):
98 return f"{self.registration} - {self.field}: {self.value}"
100 class Meta:
101 abstract = True
104class BooleanRegistrationInformation(AbstractRegistrationInformation):
105 """Checkbox information filled in by members when registering."""
107 value = models.BooleanField()
110class TextRegistrationInformation(AbstractRegistrationInformation):
111 """Checkbox information filled in by members when registering."""
113 value = models.TextField(blank=True, null=True, max_length=5000)
116class IntegerRegistrationInformation(AbstractRegistrationInformation):
117 """Checkbox information filled in by members when registering."""
119 value = models.IntegerField()