コード例 #1
0
ファイル: rems_command.py プロジェクト: kata-csc/ckanext-rems
    def _add_ckan_licenses(self, rems_url, owner_email):
        if rems_url is None:
            rems_url = config.get('rems.rest_base_url') + "addLicense"

        license_register = ckan.model.license.LicenseRegister()
        known_licenses = license_register.values()

        licenses = []

        for ckan_license in known_licenses:
            # TODO: support multiple localized versions of licenses?
            lic = license.License(id=ckan_license.id, value_type='link')
            content = license.LicenseLocalizedContent('en', ckan_license.title, ckan_license.url)
            lic.add_localization(content)
            licenses.append(lic)

        metadata = rems_client.generate_license_metadata(licenses, owner_email)
        json_metadata = json.dumps(metadata)

        print "Sending license metadata to {u} ...".format(u=rems_url)
        #print json_metadata
        rems_client.post_metadata(rems_url, json_metadata)
コード例 #2
0
ファイル: plugin.py プロジェクト: kata-csc/ckanext-rems
    def _post_metadata(self, pkg):
        '''Push created or updated metadata to REMS.

        :param pkg: package to be committed
        :type pkg: ckan.model.Package object
        :raises rems_client.RemsException: if the primary data PID cannot be retrieved or if connection to REMS fails
        '''

        if pkg.extras.get('availability') == 'access_application_rems' and not pkg.private:

            log.debug("Posting updated package metadata to Reetta service")

            rems_id = pkg.extras.get('external_id') if pkg.extras.get('availability') == 'access_application_rems' and pkg.extras.get('external_id') else pkg.id

            if not rems_id:
                raise rems_client.RemsException("Failed to retrieve the ID to send to Reetta service")

            log.debug("Rems ID: {p}".format(p=rems_id))

            # fetch the JSON title string and convert it to format required by REMS
            json_title = json.loads(pkg.title)
            title_list = []
            for k, v in json_title.iteritems():
                # try to convert the 3-letter ISO639-2 T code to two-letter format
                try:
                    l = convert.convert_language_code(k)
                except (ValueError, AttributeError), e:
                    log.warn(str(e))
                    l = k

                title_list.append({'value': v, 'lang': l})

            license_reference = pkg.license_id

            # Use this code if multiple distributors are needed to be placed to rems post request
            # owner_emails = []
            # i = 0
            # while pkg.extras.get('contact_{}_email'.format(str(i))):
            #     owner_emails.append(pkg.extras.get('contact_{}_email'.format(str(i))))
            #     i += 1
            # If the above are uncommented, remove the below line
            owner_emails = [pkg.extras.get('contact_0_email')]
            if not owner_emails:
                raise rems_client.RemsException("Failed to retrieve contact email")

            data_url = pkg.extras.get('access_application_download_URL')

            metadata = rems_client.generate_package_metadata(
                title_list, rems_id, owner_emails, license_reference, data_url)
            metadata_json = json.dumps(metadata)
            # TODO: add 'addCatalogItem' to rabbitMQ queue for asynchronous performance
            request_url = config.get('rems.rest_base_url') + 'addCatalogItem'
            rems_client.post_metadata(request_url, metadata_json, post_format="application/json")

            #return post_success  # Cut from here? So that harvesters don't get flash messages?

            # Note: To be able to update like here, the key must already exist in extras.
            # The validators in ckanext-kata ensure this.
            pkg.extras['access_application_URL'] = rems_client.get_access_application_url(rems_id)


    # def get_data_download_url(self, pkg):
    #     data_url = None
    #     # FIXME: Somehow pick the actual data resource. By resource_type(?):
    #     # 'resources': [{},{...u'resource_type': 'not_known_yet',...},..,{}]
    #     for resource in pkg.resources:
    #         if resource.resource_type == u'documentation':  # FIXME (see above)
    #             data_url = resource.url
    #             break
    #     return data_url
コード例 #3
0
ファイル: plugin.py プロジェクト: kata-csc/ckanext-rems
    def _post_metadata(self, pkg):
        """Push created or updated metadata to REMS.

        :param pkg: package to be committed
        :type pkg: ckan.model.Package object
        :raises rems_client.RemsException: if the primary data PID cannot be retrieved or if connection to REMS fails
        """

        if (
            pkg.extras.get("availability") == u"access_application"
            and pkg.extras["access_application_new_form"] == "True"
        ) and not pkg.private:
            log.debug("Posting updated package metadata to REMS")

            primary_pid = self._get_primary_data_pid(pkg)

            if not primary_pid:
                raise rems_client.RemsException("Failed to retrieve primary data PID")

            log.debug("Primary PID: {p}".format(p=primary_pid))

            # fetch the JSON title string and convert it to format required by REMS
            json_title = json.loads(pkg.title)
            title_list = []
            for k, v in json_title.iteritems():
                # try to convert the 3-letter ISO639-2 T code to two-letter format
                try:
                    l = convert.convert_language_code(k)
                except (ValueError, AttributeError), e:
                    log.warn(str(e))
                    l = k

                title_list.append({"value": v, "lang": l})

            license_reference = pkg.license_id

            # Use this code if multiple distributors are needed to be placed to rems post request
            # owner_emails = []
            # i = 0
            # while pkg.extras.get('contact_{}_email'.format(str(i))):
            #     owner_emails.append(pkg.extras.get('contact_{}_email'.format(str(i))))
            #     i += 1
            # If the above are uncommented, remove the below line
            owner_emails = [pkg.extras.get("contact_0_email")]
            if not owner_emails:
                raise rems_client.RemsException("Failed to retrieve contact email")

            data_url = pkg.extras.get("access_application_download_URL")

            metadata = rems_client.generate_package_metadata(
                title_list, primary_pid, owner_emails, license_reference, data_url
            )
            metadata_json = json.dumps(metadata)
            # TODO: add 'addCatalogItem' to rabbitMQ queue for asynchronous performance
            request_url = config.get("rems.rest_base_url") + "addCatalogItem"
            rems_client.post_metadata(request_url, metadata_json, post_format="application/json")

            # return post_success  # Cut from here? So that harvesters don't get flash messages?

            # Note: To be able to update like here, the key must already exist in extras.
            # The validators in ckanext-kata ensure this.
            pkg.extras["access_application_URL"] = rems_client.get_access_application_url(primary_pid)