Skip to content
Snippets Groups Projects
Select Git revision
  • 6a73f36d3a86db1a4d7a230fc03a3c82ea71abb4
  • main default protected
  • release
  • OZG-7856_schadcode_scanner
  • ci-pipeline
  • OZG-7526-signatur-nicht-uebernommen
  • OZG-6223-zip-download-bug
  • OZG-7367-tooltip-extension
  • OZG-7023-OZG-6956-E2E-externe-Stellen
  • OZG-6238-npm-durch-pnpm-ersetzen
  • release-admin
  • release-info
  • OZG-6700-admin-feature-toggle
  • E2E-Updates
  • OZG-7047-tooltips
  • OZG-6957-e2e-fachstellen-oe-daten
  • OZG-7006-ZuarbeitAnfragen
  • temp_OZG-7027
  • unit-tests-hotfix
  • OZG-6731-POC-keycloakResourceService-with-multiple-stateResources
  • e2e-add-zufi-version
  • 2.26.0
  • 2.25.0
  • 2.24.2
  • 2.24.1
  • 2.24.0
  • 2.23.0
  • 2.22.0
  • 2.21.0
  • 2.20.0
  • 2.21.0-SNAPSHOT
  • 2.19.0
  • 2.18.0
  • 2.17.1
  • 1.3.0
  • release-admin-1.3.0
  • release-info-1.3.0
  • 2.17.0
  • 2.16.0
  • 2.15.0
  • release-admin-1.1.0
41 results

RootController.java

Blame
  • helpers.py 4.12 KiB
    import logging
    import traceback
    import ast
    import ckan.plugins.toolkit as toolkit
    import ckan.logic as logic
    import ckan.model as model
    import json
    from ckan.common import c
    import datetime
    from dateutil import parser
    from ckan.common import config
    import urllib
    import hashlib
    
    get_action = logic.get_action
    log = logging.getLogger(__name__)
    
    
    def odsh_openness_score_dataset_html(dataset):
        score = 0
        #dataset = json.loads(dataset)
        resources = dataset.get('resources')
        if resources is None:
            return 0
        for resource in resources:
            r_qa = resource.get('qa')
            if r_qa:
                try:
                    qa = None
                    # r_qa might be a string of a dictionary when 'dataset' is send from solr
                    if isinstance(r_qa, basestring):
                        qa = ast.literal_eval(r_qa)
                    else:
                        qa = r_qa
                    resource_score = qa.get('openness_score')
                    if resource_score > score:
                        score = resource_score
                except AttributeError, e:
                    log.error('Error while calculating openness score %s: %s\nException: %s',
                              e.__class__.__name__,  unicode(e), traceback.format_exc())
        return score
    
    
    def odsh_get_resource_details(resource_id):
        resource_details = toolkit.get_action('resource_show')(
            data_dict={'id': resource_id})
        return resource_details
    
    
    def odsh_get_resource_views(pkg_dict, resource):
    
        context = {'model': model, 'session': model.Session,
                   'user': c.user, 'for_view': True,
                   'auth_user_obj': c.userobj}
        return get_action('resource_view_list')(
            context, {'id': resource['id']})
    
    
    def odsh_get_bounding_box(pkg_dict):
        try:
            extras = pkg_dict.get('extras')
            spatial = None
            for f in extras:
                if 'key' in f and f['key'] == 'spatial':
                    spatial = f['value']
                    break
    
            if spatial is not None:
                d = json.loads(spatial)
                if 'coordinates' in d:
                    coords = d['coordinates']
                    return compute_bounding_box(coords)
        except Exception, e:
            log.error('Error while bounding box %s: %s\nException: %s',
                      e.__class__.__name__,  unicode(e), traceback.format_exc())
        return None
    
    
    def compute_bounding_box(coords):
        if len(coords) == 0:
            return None
    
        coords = [c for sublist in coords for c in sublist]
        if type(coords[0][0]) == list:
            # multipolygon
            coords = [c for sublist in coords for c in sublist]
    
        minx = min(coords, key=lambda t: t[0])[0]
        maxx = max(coords, key=lambda t: t[0])[0]
        miny = min(coords, key=lambda t: t[1])[1]
        maxy = max(coords, key=lambda t: t[1])[1]
    
        return [maxx, minx, maxy, miny]
    
    
    def odsh_get_spatial_text(pkg_dict):
        extras = pkg_dict.get('extras')
        spatial = None
        if extras is None:
            return None
        for f in extras:
            if 'key' in f and f['key'] == 'spatial_text':
                spatial = f['value']
                return spatial
        return None
    
    
    def odsh_render_datetime(datetime_, date_format='{0.day:02d}.{0.month:02d}.{0.year:4d}'):
        dt = parser.parse(datetime_)
        return date_format.format(dt)
    
    
    def odsh_upload_known_formats():
        value = config.get('ckanext.odsh.upload_formats', [])
        value = toolkit.aslist(value)
        return value
    
    
    def odsh_encodeurl(url):
        return urllib.quote(url, safe='')
    
    
    def odsh_create_checksum(in_string):
        hashstring = hashlib.md5(in_string.encode('utf-8')).hexdigest()
        return int(hashstring, base=16)
    
    
    def odsh_extract_error(key, errors):
        if not errors or not ('extras' in errors):
            return None
        ext = errors['extras']
        for item in ext:
            if 'key' in item:
                for error in item['key']:
                    if error.startswith(key):
                        return error.replace(key+':', '')
    
    
    def odsh_extract_value_from_extras(extras, key):
        if not extras:
            return None
        for item in extras:
            if 'key' in item and item['key'].lower() == key.lower():
                if 'value' in item:
                    return item['value']
                return None