示例#1
0
def ogdch_prepare_pkg_dict_for_api(pkg_dict):
    if not _is_dataset_package_type(pkg_dict):
        return pkg_dict

    pkg_dict = package_map_ckan_default_fields(pkg_dict)

    # groups
    if pkg_dict['groups'] is not None:
        for group in pkg_dict['groups']:
            """
            TODO: somehow the title is messed up here,
            but the display_name is okay
            """
            group['title'] = group['display_name']
            for field in group:
                group[field] = ogdch_loc_utils.parse_json(group[field])

    # load organization from API to get all fields defined in schema
    # by default, CKAN loads organizations only from the database
    if pkg_dict['owner_org'] is not None:
        pkg_dict['organization'] = logic.get_action('organization_show')(
            {}, {
                'id': pkg_dict['owner_org'],
                'include_users': False,
                'include_followers': False,
            })

    if ogdch_request_utils.request_is_api_request():
        _transform_package_dates(pkg_dict)
        _transform_publisher(pkg_dict)
    return pkg_dict
    def before_view(self, org_dict):
        """
        Transform org_dict before view.

        This method is not called before requests to CKAN's default API (e.g.
        organization_show). It is called in the course of our
        ogdch_package_show API, and in that case, the data is not localized.
        """
        org_dict = ogdch_localize_utils.parse_json_attributes(
            ckan_dict=org_dict)
        org_dict['display_name'] = org_dict['title']

        if ogdch_request_utils.request_is_api_request():
            return org_dict

        request_lang = ogdch_request_utils.get_request_language()
        org_dict = ogdch_localize_utils.localize_ckan_sub_dict(
            ckan_dict=org_dict, lang_code=request_lang)
        return org_dict
    def before_show(self, res_dict):
        """
        Transform res_dict before view.

        This method is not called before requests to CKAN's default API (e.g.
        resource_show). It is called in the course of our ogdch_package_show
        API, and in that case, the data is not localized.
        """
        res_dict = ogdch_localize_utils.parse_json_attributes(
            ckan_dict=res_dict)  # noqa
        res_dict['display_name'] = res_dict['title']
        res_dict = ogdch_format_utils.prepare_resource_format(
            resource=res_dict, format_mapping=self.format_mapping)

        if ogdch_request_utils.request_is_api_request():
            return res_dict

        request_lang = ogdch_request_utils.get_request_language()
        res_dict = ogdch_localize_utils.localize_ckan_sub_dict(
            ckan_dict=res_dict, lang_code=request_lang)
        return res_dict
    def before_view(self, pkg_dict):
        """
        Transform pkg_dict before view.

        This method is not called before requests to CKAN's default API (e.g.
        package_show). It is called in the course of our ogdch_package_show
        API, and in that case, the data is not localized.
        """
        pkg_dict = ogdch_localize_utils.parse_json_attributes(
            ckan_dict=pkg_dict)
        pkg_dict = ogdch_plugin_utils.package_map_ckan_default_fields(pkg_dict)
        pkg_dict['resources'] = [
            ogdch_format_utils.prepare_resource_format(
                resource=resource, format_mapping=self.format_mapping)
            for resource in pkg_dict.get('resources')
        ]

        if ogdch_request_utils.request_is_api_request():
            return pkg_dict

        request_lang = ogdch_request_utils.get_request_language()

        pkg_dict = ogdch_localize_utils.localize_ckan_sub_dict(
            pkg_dict, request_lang)  # noqa
        pkg_dict['resources'] = [
            ogdch_localize_utils.localize_ckan_sub_dict(ckan_dict=resource,
                                                        lang_code=request_lang)
            for resource in pkg_dict.get('resources')
        ]
        pkg_dict['groups'] = [
            ogdch_localize_utils.localize_ckan_sub_dict(ckan_dict=grp,
                                                        lang_code=request_lang)
            for grp in pkg_dict.get('groups')
        ]
        if pkg_dict.get("organization"):
            pkg_dict[
                'organization'] = ogdch_localize_utils.localize_ckan_sub_dict(  # noqa
                    ckan_dict=pkg_dict['organization'],
                    lang_code=request_lang)
        return pkg_dict