Coverage for ckanext/udc/graph/ckan_field.py: 100%

13 statements  

« prev     ^ index     » next       coverage.py v7.7.1, created at 2026-01-19 23:48 +0000

1from copy import deepcopy 

2 

3# Field name mapping: normalized name -> actual data_dict field name 

4ckanFieldMapping = { 

5 "title": "title_translated", 

6 "description": "description_translated", 

7 "tags": "tags_translated", 

8 

9 "source": "url" 

10} 

11ckanFieldKeys = [ 

12 "name", "title", "notes", "tags", "id", "pkg_name", "author", "author_email", 

13 "url", "version" 

14] 

15 

16 

17def prepare_data_dict(data_dict: dict) -> dict: 

18 """ 

19 Prepare data_dict for template compilation by applying field name mappings. 

20 This replaces the need for CKANField wrapper. 

21  

22 Returns a new dict with normalized field names that can be used directly 

23 in the mapping templates. 

24 """ 

25 result = deepcopy(data_dict) 

26 

27 # Apply field mappings 

28 for normalized_name, actual_field in ckanFieldMapping.items(): 

29 if actual_field in data_dict and data_dict[actual_field] is not None: 

30 result[normalized_name] = data_dict[actual_field] 

31 

32 # Special handling for 'id': prefer 'pkg_name' on update, 'id' on create 

33 if 'pkg_name' in data_dict and data_dict['pkg_name']: 

34 result['id'] = data_dict['pkg_name'] 

35 elif 'id' in data_dict: 

36 result['id'] = data_dict['id'] 

37 

38 return result