Coverage for portality/forms/fields.py: 94%

32 statements  

« prev     ^ index     » next       coverage.py v6.4.2, created at 2022-07-22 15:59 +0100

1from wtforms import Field, SelectField, FormField, RadioField 

2from wtforms import widgets 

3 

4 

5class DOAJSelectField(SelectField): 

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

7 if 'default' not in kwargs: 

8 kwargs['default'] = '' 

9 super(DOAJSelectField, self).__init__(*args, **kwargs) 

10 

11 

12class TagListField(Field): 

13 widget = widgets.TextInput() 

14 

15 def _value(self): 

16 if self.data: 

17 return ', '.join(self.data) 

18 else: 

19 return '' 

20 

21 def get_list(self): 

22 return self.data 

23 

24 def process_formdata(self, valuelist): 

25 if valuelist: 

26 self.data = [] 

27 for v in valuelist: 

28 self.data += [c for c in [x.strip() for x in v.split(",") if x]] 

29 else: 

30 self.data = [] 

31 

32 

33class NestedFormField(FormField): 

34 def validate(self, form, extra_validators=tuple()): 

35 self.form.meta.parent_form = form 

36 return super().validate(form, extra_validators) 

37 

38 

39class UnconstrainedRadioField(RadioField): 

40 def pre_validate(self, form): 

41 return 

42 

43 def process_data(self, value): 

44 if value: 

45 super(UnconstrainedRadioField, self).process_data(value) 

46 else: 

47 self.data = None