Skip to content

Commit

Permalink
Split data themes for indexing; Fix documentation as links
Browse files Browse the repository at this point in the history
  • Loading branch information
mpolidori committed Jul 18, 2024
1 parent 866e8be commit 724a825
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 15 deletions.
32 changes: 19 additions & 13 deletions ckanext/portalopendatadk/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,9 @@ def package_create(context, data_dict):
if not data_dict.get(field):
errors[field] = [_('Missing value')]

documentation = data_dict.get("documentation")
data_dict['url'] = documentation

log.debug(
'package_create validate_errs=%r user=%s package=%s data=%r',
errors,
Expand Down Expand Up @@ -274,13 +277,13 @@ def package_create(context, data_dict):

# We have to do this after save so the package id is available.
# If this fails, we have to rollback the transaction.
try:
data_dict['url'] = data_dict.get('documentation')
upload = uploader.get_resource_uploader(data_dict)
upload.upload(pkg.id, uploader.get_max_resource_size())
except Exception as e:
model.Session.rollback()
raise ValidationError({'upload': [_('Documentation upload failed: {}').format(e)]})
if documentation and 'http' not in documentation:
try:
upload = uploader.get_resource_uploader(data_dict)
upload.upload(pkg.id, uploader.get_max_resource_size())
except Exception as e:
model.Session.rollback()
raise ValidationError({'upload': [_('Documentation upload failed: {}').format(e)]})

# Needed to let extensions know the package and resources ids
model.Session.flush()
Expand Down Expand Up @@ -406,12 +409,15 @@ def package_update(context, data_dict):
if not data_dict.get(field):
errors[field] = [_('Missing value')]

try:
data_dict['url'] = data_dict.get('documentation')
upload = uploader.get_resource_uploader(data_dict)
upload.upload(data_dict['id'], uploader.get_max_resource_size())
except Exception as e:
errors['upload'] = [_('Upload failed: %s') % str(e)]
documentation = data_dict.get('documentation')
data_dict["url"] = documentation

if documentation and 'http' not in documentation:
try:
upload = uploader.get_resource_uploader(data_dict)
upload.upload(data_dict['id'], uploader.get_max_resource_size())
except Exception as e:
errors['upload'] = [_('Upload failed: %s') % str(e)]

log.debug(
'package_update validate_errs=%r user=%s package=%s data=%r',
Expand Down
15 changes: 14 additions & 1 deletion ckanext/portalopendatadk/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,17 @@ def get_dcat_license_options(selected=None):
return option
return None

return dcat_license_options
return dcat_license_options


def fix_data_themes(data_themes_raw):
data_themes = []

if data_themes_raw:
if isinstance(data_themes_raw, list):
data_themes_raw = data_themes_raw[0]

if isinstance(data_themes_raw, (str, unicode)):
data_themes = data_themes_raw.strip("{}").split(",")

return data_themes
25 changes: 25 additions & 0 deletions ckanext/portalopendatadk/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class PortalOpenDataDKPlugin(
plugins.implements(plugins.IRoutes, inherit=True)
plugins.implements(plugins.IAuthFunctions, inherit=True)
plugins.implements(plugins.IValidators, inherit=True)
plugins.implements(plugins.IPackageController, inherit=True)

def before_map(self, map):
# Pass requests to ODDKUserController to verify admin status
Expand Down Expand Up @@ -173,6 +174,7 @@ def package_types(self):

def dataset_facets(self, facets_dict, package_type):
facets_dict['update_frequency'] = plugins.toolkit._('Update frequency')
facets_dict['data_themes'] = plugins.toolkit._('Categories')
return facets_dict

def organization_facets(self, facets_dict, organization_type, package_type):
Expand All @@ -183,6 +185,29 @@ def group_facets(self, facets_dict, group_type, package_type):
facets_dict['update_frequency'] = plugins.toolkit._('Update frequency')
return facets_dict

# IPackageController

def after_show(self, context, pkg_dict):
data_themes = pkg_dict.get('data_themes')
data_themes = oddk_helpers.fix_data_themes(data_themes)

if data_themes:
pkg_dict['data_themes'] = data_themes

return pkg_dict

def before_search(self, search_params):
return search_params

def before_index(self, pkg_dict):
data_themes = pkg_dict.get('extras_data_themes')
data_themes = oddk_helpers.fix_data_themes(data_themes)

if data_themes:
pkg_dict['data_themes'] = data_themes

return pkg_dict

# IAuthFunctions

def get_auth_functions(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
<script>
document.addEventListener('DOMContentLoaded', () => {
const selectElement = document.getElementById('field-{{ field.field_name }}');
const options = selectElement.options;

selectElement.addEventListener('mousedown', (event) => {
event.preventDefault();
Expand Down

0 comments on commit 724a825

Please sign in to comment.