Coverage for apis_core/generic/forms/widgets.py: 0%

26 statements  

« prev     ^ index     » next       coverage.py v7.6.4, created at 2024-11-22 07:51 +0000

1import json 

2 

3from django.forms.widgets import Input 

4 

5 

6class NewlineSeparatedListWidget(Input): 

7 input_type = "text" 

8 template_name = "widgets/multiline.html" 

9 

10 class Media: 

11 js = ["js/multiline.js"] 

12 

13 def value_from_datadict(self, data, files, name): 

14 values = [item for item in data.getlist(name) if item] 

15 return "\n".join(values) 

16 

17 def format_value(self, value): 

18 if value == "" or value is None: 

19 return None 

20 return value.split("\n") 

21 

22 

23class JSONListWidget(Input): 

24 input_type = "text" 

25 template_name = "widgets/multiline.html" 

26 

27 class Media: 

28 js = ["js/multiline.js"] 

29 

30 def value_from_datadict(self, data, files, name): 

31 values = [item for item in data.getlist(name) if item] 

32 return values 

33 

34 def format_value(self, value): 

35 if value == "" or value is None: 

36 return None 

37 return json.loads(value)