Skip to content

Commit

Permalink
Update before dataset index
Browse files Browse the repository at this point in the history
  • Loading branch information
blagojabozinovski committed Oct 30, 2024
1 parent 330022d commit d3871fb
Showing 1 changed file with 45 additions and 2 deletions.
47 changes: 45 additions & 2 deletions ckanext/alisea/plugin.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import ckan.plugins as plugins
import ckan.plugins.toolkit as toolkit
import json
from ckan.lib.plugins import DefaultTranslation
from collections import OrderedDict
from ckanext.alisea import helpers as h
import json
import ast
import logging

log = logging.getLogger(__name__)


class AliseaPlugin(plugins.SingletonPlugin, DefaultTranslation):
Expand Down Expand Up @@ -109,9 +113,48 @@ def _before_index_dump_dicts(self, data_dict):
data_dict[key] = json.dumps(value)
return data_dict

def convert_stringified_lists(self, data_dict):
"""
Converts stringified lists in the data dictionary to actual lists.
Args:
data_dict (dict): The data dictionary to be processed.
Returns:
dict: The processed data dictionary with actual lists.
This function iterates over the items in the data dictionary and converts
any stringified lists (strings that start with '[' and end with ']') into
actual lists. Keys that start with 'extras_', 'res_', or are 'validated_data_dict'
are excluded from this conversion.
"""
# Excluded items
excluded_keys = [
key for key in data_dict
if key.startswith('extras_') or key.startswith('res_') or key == 'validated_data_dict'
]

# Filter data dictionary
filter_data_dict = {
key: value for key, value in data_dict.items()
if key not in excluded_keys
}

for key, value in filter_data_dict.items():
if isinstance(value, str) and value.startswith('[') and value.endswith(']'):
try:
data_dict[key] = ast.literal_eval(value)
except (ValueError, SyntaxError) as e:
log.error("Error converting stringified list for key '%s': %s", key, e)

return data_dict


def before_dataset_index(self, data_dict):

data_dict = self._before_index_dump_dicts(data_dict)
data_dict = self.convert_stringified_lists(data_dict)
data_dict['agroecology_category'] = json.loads(json.dumps(data_dict.get('agroecology_category', '[]')))
data_dict['agroecology_keyword'] = json.loads(json.dumps(data_dict.get('agroecology_keyword', '[]')))
data_dict = self._before_index_dump_dicts(data_dict)

return data_dict

0 comments on commit d3871fb

Please sign in to comment.