Coverage for website/events/forms.py: 97.30%

27 statements  

« prev     ^ index     » next       coverage.py v7.6.7, created at 2025-08-14 10:31 +0000

1from django import forms 

2 

3from documents.forms import DocumentFileInput 

4from events.models.documents import EventDocument 

5 

6from .models import RegistrationInformationField 

7 

8 

9class FieldsForm(forms.Form): 

10 """Form that outputs the correct widgets for the information fields.""" 

11 

12 def __init__(self, *args, **kwargs): 

13 self.information_fields = kwargs.pop("fields") 

14 super().__init__(*args, **kwargs) 

15 

16 for key, field in self.information_fields.items(): 

17 field_type = field["type"] 

18 

19 if field_type == RegistrationInformationField.BOOLEAN_FIELD: 

20 self.fields[key] = forms.BooleanField(required=False) 

21 elif field_type == RegistrationInformationField.INTEGER_FIELD: 

22 self.fields[key] = forms.IntegerField(required=field["required"]) 

23 elif field_type == RegistrationInformationField.TEXT_FIELD: 23 ↛ 28line 23 didn't jump to line 28 because the condition on line 23 was always true

24 self.fields[key] = forms.CharField( 

25 required=field["required"], max_length=5000 

26 ) 

27 

28 self.fields[key].label = field["label"] 

29 self.fields[key].help_text = field["description"] 

30 self.fields[key].initial = field["value"] 

31 

32 def field_values(self): 

33 for key in self.information_fields: 

34 yield key, self.cleaned_data[key] 

35 

36 

37class EventDocumentForm(forms.ModelForm): 

38 """Form that overrides the widgets for the files.""" 

39 

40 class Meta: 

41 model = EventDocument 

42 fields = ( 

43 "name", 

44 "file", 

45 "members_only", 

46 "owner", 

47 ) 

48 widgets = { 

49 "file": DocumentFileInput, 

50 }