Пример #1
0
def tags_count():
    '''Return a sorted list of the groups with the most datasets.'''

    tags_translate = [ _('Transport'), _('Justice'), _('Energy Watter'), _('Environment'), _('Finance Economy'), _('Population'), _('Religion'),   _('Education Culture'),   _('Health Wellness'), _('Accommodation'), _('Tourism') ]

   #/api/action/package_search?fq=tags:"economy"
    tags_counter = []

   #Put the details into a dict.
    for num in range(1,11):
        homepage_tag_num = "homepage_tag"+str(num)
        homepage_tag = config.get(homepage_tag_num)
        homepage_tag_translate = _(homepage_tag)
        homepage_tag_translate.encode('utf-8')
        query = "tags:"+'"'+homepage_tag_translate+'"'
        dataset_dict = {
           'fq': query,
        }

        homepage_tag_icon_num = "homepage_tag_icon"+str(num)
        homepage_tag_icon = config.get(homepage_tag_icon_num)

        # Use the json module to dump the dictionary to a string for posting.
        data_string = urllib.quote(json.dumps(dataset_dict))
        package_search_api = config.get('ckan.site_url')+'/api/3/action/package_search'
        tag = urllib2.Request(package_search_api)
        # Make the HTTP request.
        response = urllib2.urlopen(tag, data_string)
        assert response.code == 200
        # Use the json module to load CKAN's response into a dictionary.
        response_dict = json.loads(response.read())
        tag_count = response_dict['result']['count']
        tags_counter.append(dict([('name', homepage_tag_translate), ('icon', homepage_tag_icon), ('count', tag_count)]))

    return tags_counter
Пример #2
0
    def _send_packager_request(self, packager_url, request_params):
        """Send the request to the ckanpackager service

        @param packager_url: The ckanpackager service URL
        @request_params: The parameters to send
        """

        # Send request
        try:
            request = urllib2.Request(packager_url)
            response = urllib2.urlopen(request,
                                       urllib.urlencode(request_params))
        except urllib2.URLError as e:
            print(e)

            raise PackageListControllerError(
                _("Failed to contact the ckanpackager service"))
        if response.code != 200:
            response.close()

            print(response.__dict__)
            raise PackageListControllerError(
                _("Failed to contact the ckanpackager service - or it returned an error"
                  ))

        # Read response and return.
        try:
            data = response.read()
            result = json.loads(data)
        except ValueError:
            raise PackageListControllerError(
                _("Could not parse response from ckanpackager service"))
        finally:
            response.close()
        return result
Пример #3
0
 def spatial_coverage(self, type, name):
     spatial_dict = {}
     data = None
     item = None
     apidatahost = config.get('ckanext.dge.apidata.host', None)
     apidataurl = config.get('ckanext.dge.apidata.url.spatial', None)
     apidatatype = None
     if type:
         if type == 'Provincia':
             apidatatype = 'Province'
         elif type == 'Autonomia':
             apidatatype = 'Autonomous-region'
         elif type == 'Pais':
             apidatatype = 'Country'
         else:
             apidatatype = None
     if apidatahost and apidataurl and apidatatype and name:
         url = u'%s/%s/%s/%s' % (apidatahost, apidataurl, apidatatype, name)
         if url:
             url = urllib.quote(url.encode('utf8'), ':/')
             response = urllib.urlopen(url)
             if response:
                 data = json.loads(response.read())
         if data:
             items = data.get('result', {}).get('items', {})
             if items and items[0]:
                 item = items[0]
     spatial_dict['type'] = type
     spatial_dict['name'] = name
     spatial_dict['label'] = name
     if item:
         spatial_dict['label'] = item.get('label', '')
         spatial_dict['about'] = item.get('_about', '')
         rows = []
         rows.append({'key': 'rdfs:label', 'value': item.get('label', '')})
         if type == 'Provincia':
             rows.append({
                 'key': 'esadm:autonomia',
                 'value': item.get('autonomia', '')
             })
         if type == 'Provincia' or type == 'Autonomia':
             rows.append({
                 'key': 'esadm:pais',
                 'value': item.get('pais', '')
             })
             rows.append({
                 'key': 'owl:sameAs',
                 'value': item.get('sameAs', '')
             })
         complete_type = item.get('type', '')
         if complete_type:
             s_type = complete_type.split('#')
             type = s_type[-1] if s_type else type
         rows.append({'key': 'rdf:type', 'value': 'esadm:%s' % type})
         spatial_dict['rows'] = rows
     c.spatial_dict = spatial_dict
     request.environ['PATH_INFO'] = urllib.quote(
         request.environ['PATH_INFO'])
     return render('static/spatial-coverage.html')
Пример #4
0
    def request(self, url, data, method):

        response = ''
        request_json = ''
        try:
            if(method == "get"):
                response = urllib2.urlopen(url + '?' + data)
            else:
                response = urllib2.urlopen(url, data)
            request_json = response.read()
        except urllib2.HTTPError, e:
            request_json =  e.fp.read()
Пример #5
0
def tags_count():
    '''Return a sorted list of the groups with the most datasets.'''

    tags_translate = [
        _('Transport'),
        _('Justice'),
        _('Energy Watter'),
        _('Environment'),
        _('Finance Economy'),
        _('Population'),
        _('Religion'),
        _('Education Culture'),
        _('Health Wellness'),
        _('Accommodation'),
        _('Tourism')
    ]

    #/api/action/package_search?fq=tags:"economy"
    tags_counter = []

    #Put the details into a dict.
    for num in range(1, 11):
        homepage_tag_num = "homepage_tag" + str(num)
        homepage_tag = config.get(homepage_tag_num)
        homepage_tag_translate = _(homepage_tag)
        homepage_tag_translate.encode('utf-8')
        query = "tags:" + '"' + homepage_tag_translate + '"'
        dataset_dict = {
            'fq': query,
        }

        homepage_tag_icon_num = "homepage_tag_icon" + str(num)
        homepage_tag_icon = config.get(homepage_tag_icon_num)

        # Use the json module to dump the dictionary to a string for posting.
        data_string = urllib.quote(json.dumps(dataset_dict))
        package_search_api = config.get(
            'ckan.site_url') + '/api/3/action/package_search'
        tag = urllib2.Request(package_search_api)
        # Make the HTTP request.
        response = urllib2.urlopen(tag, data_string)
        assert response.code == 200
        # Use the json module to load CKAN's response into a dictionary.
        response_dict = json.loads(response.read())
        tag_count = response_dict['result']['count']
        tags_counter.append(
            dict([('name', homepage_tag_translate),
                  ('icon', homepage_tag_icon), ('count', tag_count)]))

    return tags_counter
Пример #6
0
    def get_all_packages(self, max_records=99999):
        request = urllib2.Request(
            'http://ckan:5000/api/3/action/package_search?q=*:*&rows=%s' %
            max_records)

        response = urllib2.urlopen(request)

        if response:
            if response.code == 200:
                # Use the json module to load CKAN's response into a dictionary.
                response_dict = json.loads(response.read())
                assert response_dict['success'] is True

                return response_dict
        return None
Пример #7
0
    def request(self, url, data, method):

        response = ''
        request_json = ''
        try:
            if (method == "get"):
                response = urllib2.urlopen(url + '?' + data)
            else:
                response = urllib2.urlopen(url, data)
            request_json = response.read()

            if (json.loads(request_json)['code'] > 0):
                log.info('disques request error %s' % json.loads(request_json))

        except urllib2.HTTPError, e:
            request_json = e.fp.read()
Пример #8
0
 def theme(self, name):
     apidatahost = config.get('ckanext.dge.apidata.host', None)
     apidataurl = config.get('ckanext.dge.apidata.url.sector', None)
     data = None
     theme_dict = {}
     item = None
     if apidatahost and apidataurl and name:
         url = '%s/%s/%s' % (apidatahost, apidataurl, name)
         if url:
             error_loading_data = False
             response = urllib.urlopen(url)
             if response:
                 data = json.loads(response.read())
         if data:
             items = data.get('result', {}).get('items')
             if items and items[0]:
                 item = items[0]
     theme_dict['name'] = name
     theme_dict['type'] = 'sector'
     theme_dict['label'] = name
     if item:
         theme_dict['label'] = item.get('prefLabel', '')
         theme_dict['about'] = item.get('_about', '')
         rows = []
         rows.append({
             'key': 'skos:inScheme',
             'value': item.get('inScheme', '')
         })
         rows.append({
             'key': 'skos :prefLabel',
             'value': item.get('prefLabel', '')
         })
         complete_type = item.get('type', '')
         type = ''
         if complete_type:
             s_type = complete_type.split('#')
             type = s_type[-1] if s_type else 'Concept'
         rows.append({'key': 'rdf:type', 'value': 'skos:%s' % (type)})
         theme_dict['rows'] = rows
     c.theme_dict = theme_dict
     return render('static/theme.html')
Пример #9
0
    def form(self):
        """
        Return a report form
        :return: html
        """

        data = {}
        errors = {}
        error_summary = {}
        # Submit the data
        if 'save' in request.params:
            data, errors, error_summary = self._submit(self.context)
        else:
            # get the referrer header
            # cut it to get the the source id
            # use the ckan api to get the autohr and maintainer details of the data and assign it to the global parameters
            try:

                uri = request.headers.get('Referer')
                global report_referrer
                report_referrer = uri
                dataid = ""
                if uri:
                    if "resource" in uri:
                        dataidIndex = uri.index("resource")
                        dataid = uri[dataidIndex + 9:]
                if "?" in dataid:
                    dataid = dataid.split("?")[0]
                data['id'] = dataid

                # /api/3/action/package_show?id=c388e16d-72bf-4cbf-b9b0-1b6ef9caf8cf
                import urllib2, urllib, json
                # Put the details of the dataset we're going to create into a dict.
                dataset_dict = {
                    'id': dataid,
                }
                # Use the json module to dump the dictionary to a string for posting.
                data_string = urllib.quote(json.dumps(dataset_dict))
                # We'll use the package_create function to create a new dataset.
                resource_show_api = config.get(
                    'ckan.site_url') + '/api/3/action/resource_show'
                resource = urllib2.Request(resource_show_api)
                # Creating a dataset requires an authorization header.
                # Replace *** with your API key, from your user account on the CKAN site
                # that you're creating the dataset on.
                resource.add_header('Authorization',
                                    '8c644165-16ef-4f45-b081-c17236b486fe')
                # Make the HTTP request.
                response = urllib2.urlopen(resource, data_string)
                assert response.code == 200
                # Use the json module to load CKAN's response into a dictionary.
                response_dict = json.loads(response.read())

                global report_resource_name
                global author_email_address
                global report_organization_name
                global report_dataset_name

                report_resource_name = response_dict['result']['name']

                dataset_dict = {
                    'id': response_dict['result']['package_id'],
                }
                data_string = urllib.quote(json.dumps(dataset_dict))
                package_show_api = config.get(
                    'ckan.site_url') + '/api/3/action/package_show'
                resource = urllib2.Request(package_show_api)
                resource.add_header('Authorization',
                                    '8c644165-16ef-4f45-b081-c17236b486fe')
                response = urllib2.urlopen(resource, data_string)
                response_dict = json.loads(response.read())

                author_email_address = response_dict['result']['author_email']
                report_organization_name = response_dict['result'][
                    'organization']['title']
                report_dataset_name = response_dict['result']['title']

            except AttributeError:
                data['id'] = None
            try:  # Try and use logged in user values for default values
                data['name'] = base.c.userobj.fullname or base.c.userobj.name
                data['email'] = base.c.userobj.email
            except AttributeError:
                data['name'] = data['email'] = None
        if data.get('success', False):
            return p.toolkit.render('report/success.html')
        else:
            vars = {
                'data': data,
                'errors': errors,
                'error_summary': error_summary
            }
            return p.toolkit.render('report/form.html', extra_vars=vars)
Пример #10
0
    def form(self):
        """
        Return a report form
        :return: html
        """

        data = {}
        errors = {}
        error_summary = {}
        # Submit the data
        if 'save' in request.params:
            data, errors, error_summary = self._submit(self.context)
        else:
            # get the referrer header
            # cut it to get the the source id
            # use the ckan api to get the autohr and maintainer details of the data and assign it to the global parameters
            try:
                uri = request.headers.get('Referer')
                #global report_referrer
                global dataid
                global datasetid
                if uri:
                    if "resource" in uri:
                        dataidIndex = uri.index("resource")
                        dataSetIdIndex = uri.index("dataset")
                        datasetid = uri[dataSetIdIndex + 8:dataidIndex - 1]
                        dataid = uri[dataidIndex + 9:]
                if "?" in dataid:
                    dataid = dataid.split("?")[0]

                data['id'] = dataid
                data['report_dataset_id'] = datasetid

                import urllib2, urllib, json
                # Put the details of the dataset we're going to create into a dict.
                dataset_dict = {
                    'id': dataid,
                }
                # Use the json module to dump the dictionary to a string for posting.
                data_string = urllib.quote(json.dumps(dataset_dict))
                # We'll use the package_create function to create a new dataset.
                resource_show_api = config.get(
                    'ckan.site_url') + '/api/3/action/resource_show'
                resource = urllib2.Request(resource_show_api)
                # Make the HTTP request.
                response = urllib2.urlopen(resource, data_string)
                assert response.code == 200
                # Use the json module to load CKAN's response into a dictionary.
                response_dict = json.loads(response.read())
                report_resource_name = response_dict['result']['name']
                data['report_resource_name'] = report_resource_name
                dataset_dict = {
                    'id': response_dict['result']['package_id'],
                }
                data_string = urllib.quote(json.dumps(dataset_dict))
                package_show_api = config.get(
                    'ckan.site_url') + '/api/3/action/package_show'
                resource = urllib2.Request(package_show_api)
                response = urllib2.urlopen(resource, data_string)
                response_dict = json.loads(response.read())

                author_email_address = response_dict['result']['author_email']
                data['author_email_address'] = author_email_address
                report_organization_name = response_dict['result'][
                    'organization']['title']
                data['report_organization_name'] = report_organization_name
                report_dataset_name = response_dict['result']['title']
                data['report_dataset_name'] = report_dataset_name

            except Exception as ex:
                log.info("form() Exception: " + ex.message)
                data['id'] = None
            try:  # Try and use logged in user values for default values
                data['name'] = base.c.userobj.fullname or base.c.userobj.name
                data['email'] = base.c.userobj.email
            except AttributeError:
                data['name'] = data['email'] = None
        if data.get('success', False):
            return p.toolkit.render('report/success.html')
        else:
            vars = {
                'data': data,
                'errors': errors,
                'error_summary': error_summary
            }
            return p.toolkit.render('report/form.html', extra_vars=vars)
Пример #11
0
    def form(self):
        """
        Return a report form
        :return: html
        """

        data = {}
        errors = {}
        error_summary = {}
        # Submit the data
        if 'save' in request.params:
            data, errors, error_summary = self._submit(self.context)
        else:
            # get the referrer header
            # cut it to get the the source id
            # use the ckan api to get the autohr and maintainer details of the data and assign it to the global parameters
            try:
                uri = request.headers.get('Referer')
                global report_referrer
                log.info("report form() uri: " + uri)
                global dataid
                global datasetid
                if uri:
                    if "resource" in uri:
                        dataidIndex = uri.index("resource")
                        dataSetIdIndex = uri.index("dataset")
                        datasetid = uri[dataSetIdIndex+8:dataidIndex-1]
                        dataid = uri[dataidIndex+9:]
                if "?" in dataid:
                    dataid = dataid.split("?")[0]
                data['id'] = dataid
                import urllib2, urllib, json
                # Put the details of the dataset we're going to create into a dict.
                dataset_dict = {
                    'id': dataid,
                }
                # Use the json module to dump the dictionary to a string for posting.
                data_string = urllib.quote(json.dumps(dataset_dict))
                # We'll use the package_create function to create a new dataset.
                resource_show_api = config.get('ckan.site_url')+'/api/3/action/resource_show'
                resource = urllib2.Request(resource_show_api)
                # Creating a dataset requires an authorization header.
                # Replace *** with your API key, from your user account on the CKAN site
                # that you're creating the dataset on.
                # Make the HTTP request.
                response = urllib2.urlopen(resource, data_string)
                assert response.code == 200
                # Use the json module to load CKAN's response into a dictionary.
                response_dict = json.loads(response.read())

                global report_resource_name
                global author_email_address
                global report_organization_name
                global report_dataset_name

                report_resource_name = response_dict['result']['name']

                dataset_dict = {
                    'id': response_dict['result']['package_id'],
                }
                data_string = urllib.quote(json.dumps(dataset_dict))
                package_show_api = config.get('ckan.site_url')+'/api/3/action/package_show'
                resource = urllib2.Request(package_show_api)
                response = urllib2.urlopen(resource, data_string)
                response_dict = json.loads(response.read())

                author_email_address = response_dict['result']['author_email']
                report_organization_name = response_dict['result']['organization']['title']
                report_dataset_name = response_dict['result']['title']
                report_referrer = config.get('ckan.site_url') + '/dataset/' + datasetid + '/resource/' + dataid
                log.info("report form() report_referrer: " + report_referrer)

            except Exception as ex:
                log.info("form() Exception: " + ex.message)
                data['id'] = None
            try:   # Try and use logged in user values for default values
                data['name'] = base.c.userobj.fullname or base.c.userobj.name
                data['email'] = base.c.userobj.email
            except AttributeError:
                data['name'] = data['email'] = None
        if data.get('success', False):
            return p.toolkit.render('report/success.html')
        else:
            vars = {'data': data, 'errors': errors, 'error_summary': error_summary}
            return p.toolkit.render('report/form.html', extra_vars=vars)