예제 #1
0
def get_metadata_comparison(workspace, mapname):
    uuid = get_map_uuid(workspace, mapname)
    csw = common_util.create_csw()
    if uuid is None or csw is None:
        return {}
    muuid = get_metadata_uuid(uuid)
    element = common_util.get_record_element_by_id(csw, muuid)
    if element is None:
        return {}

    # current_app.logger.info(f"xml\n{ET.tostring(el)}")

    props = common_util.parse_md_properties(element, [
        'abstract',
        'extent',
        'graphic_url',
        'identifier',
        'map_endpoint',
        'map_file_endpoint',
        'operates_on',
        'organisation_name',
        'publication_date',
        'reference_system',
        'revision_date',
        'title',
    ], METADATA_PROPERTIES)
    # current_app.logger.info(f"props:\n{json.dumps(props, indent=2)}")
    # current_app.logger.info(f"csw.request={csw.request}")
    url = csw.request.replace(settings.CSW_URL, settings.CSW_PROXY_URL)
    return {f"{url}": props}
예제 #2
0
def get_map_info(workspace, mapname):
    uuid = get_map_uuid(workspace, mapname)
    try:
        csw = common_util.create_csw()
        if uuid is None or csw is None:
            return {}
        muuid = get_metadata_uuid(uuid)
        csw.getrecordbyid(id=[muuid], esn='brief')
    except (HTTPError, ConnectionError):
        current_app.logger.info(traceback.format_exc())
        return {}
    if muuid in csw.records:
        return {
            'metadata': {
                'identifier':
                muuid,
                'csw_url':
                settings.CSW_PROXY_URL,
                'record_url':
                settings.CSW_RECORD_URL.format(identifier=muuid),
                'comparison_url':
                url_for('rest_workspace_map_metadata_comparison.get',
                        workspace=workspace,
                        mapname=mapname),
            }
        }
    return {}
예제 #3
0
def get_template_path_and_values(username,
                                 mapname,
                                 http_method=None,
                                 actor_name=None):
    assert http_method in [
        common.REQUEST_METHOD_POST, common.REQUEST_METHOD_PATCH
    ]
    uuid_file_path = get_publication_uuid_file(MAP_TYPE, username, mapname)
    publ_datetime = datetime.fromtimestamp(os.path.getmtime(uuid_file_path))
    revision_date = datetime.now()
    map_json = get_map_json(username, mapname)
    operates_on = map_json_to_operates_on(map_json, editor=actor_name)
    publ_info = get_publication_info(
        username,
        MAP_TYPE,
        mapname,
        context={
            'keys': ['title', 'bounding_box', 'description'],
        })
    bbox_3857 = publ_info.get('bounding_box')
    if bbox_util.is_empty(bbox_3857):
        bbox_3857 = settings.LAYMAN_DEFAULT_OUTPUT_BBOX
    extent = bbox_util.transform(tuple(bbox_3857),
                                 epsg_from=3857,
                                 epsg_to=4326)
    title = publ_info['title']
    abstract = publ_info.get('description')
    md_language = next(
        iter(
            common_language.get_languages_iso639_2(' '.join([
                title or '',
                abstract or '',
            ]))), None)

    prop_values = _get_property_values(
        username=username,
        mapname=mapname,
        uuid=get_map_uuid(username, mapname),
        title=title,
        abstract=abstract or None,
        publication_date=publ_datetime.strftime('%Y-%m-%d'),
        revision_date=revision_date.strftime('%Y-%m-%d'),
        md_date_stamp=date.today().strftime('%Y-%m-%d'),
        identifier=url_for('rest_workspace_map.get',
                           workspace=username,
                           mapname=mapname),
        identifier_label=mapname,
        extent=extent,
        epsg_codes=map_json_to_epsg_codes(map_json),
        md_organisation_name=None,
        organisation_name=None,
        operates_on=operates_on,
        md_language=md_language,
    )
    if http_method == common.REQUEST_METHOD_POST:
        prop_values.pop('revision_date', None)
    template_path = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                                 'record-template.xml')
    return template_path, prop_values
예제 #4
0
def delete_map(workspace, mapname):
    uuid = get_map_uuid(workspace, mapname)
    muuid = get_metadata_uuid(uuid)
    if muuid is None:
        return
    try:
        common_util.csw_delete(muuid)
    except (HTTPError, ConnectionError) as exc:
        current_app.logger.info(traceback.format_exc())
        raise LaymanError(38) from exc
예제 #5
0
def patch_map(workspace,
              mapname,
              metadata_properties_to_refresh=None,
              actor_name=None,
              create_if_not_exists=True,
              timeout=5):
    # current_app.logger.info(f"patch_map metadata_properties_to_refresh={metadata_properties_to_refresh}")
    metadata_properties_to_refresh = metadata_properties_to_refresh or []
    if len(metadata_properties_to_refresh) == 0:
        return {}
    uuid = get_map_uuid(workspace, mapname)
    csw = common_util.create_csw()
    if uuid is None or csw is None:
        return None
    muuid = get_metadata_uuid(uuid)
    element = common_util.get_record_element_by_id(csw, muuid)
    if element is None:
        if create_if_not_exists:
            return csw_insert(workspace, mapname, actor_name=actor_name)
        return None
    # current_app.logger.info(f"Current element=\n{ET.tostring(element, encoding='unicode', pretty_print=True)}")

    _, prop_values = get_template_path_and_values(
        workspace,
        mapname,
        http_method=common.REQUEST_METHOD_PATCH,
        actor_name=actor_name)
    prop_values = {
        k: v
        for k, v in prop_values.items()
        if k in metadata_properties_to_refresh + ['md_date_stamp']
    }
    # current_app.logger.info(f"update_map prop_values={prop_values}")
    basic_template_path = os.path.join(
        os.path.dirname(os.path.realpath(__file__)), './record-template.xml')
    element = common_util.fill_xml_template_obj(
        element,
        prop_values,
        METADATA_PROPERTIES,
        basic_template_path=basic_template_path)
    record = ET.tostring(element, encoding='unicode', pretty_print=True)
    # current_app.logger.info(f"update_map record=\n{record}")
    try:
        common_util.csw_update({
            'muuid': muuid,
            'record': record,
        },
                               timeout=timeout)
    except (HTTPError, ConnectionError) as exc:
        current_app.logger.info(traceback.format_exc())
        raise LaymanError(38) from exc
    return muuid
예제 #6
0
def delete_map(workspace, mapname):
    uuid = get_map_uuid(workspace, mapname)
    muuid = get_metadata_uuid(uuid)
    if muuid is None:
        return
    micka_requests.csw_delete(muuid)
예제 #7
0
def get_template_path_and_values(workspace,
                                 mapname,
                                 http_method=None,
                                 actor_name=None):
    assert http_method in [
        common.REQUEST_METHOD_POST, common.REQUEST_METHOD_PATCH
    ]
    uuid_file_path = get_publication_uuid_file(MAP_TYPE, workspace, mapname)
    publ_datetime = datetime.fromtimestamp(os.path.getmtime(uuid_file_path))
    revision_date = datetime.now()
    map_json = get_map_json(workspace, mapname)
    operates_on = map_json_to_operates_on(map_json, editor=actor_name)
    publ_info = get_publication_info(
        workspace,
        MAP_TYPE,
        mapname,
        context={
            'keys':
            ['title', 'native_bounding_box', 'description', 'native_crs'],
        })
    native_bbox = publ_info.get('native_bounding_box')
    crs = publ_info.get('native_crs')
    if bbox_util.is_empty(native_bbox):
        native_bbox = crs_def.CRSDefinitions[crs].default_bbox
    extent = bbox_util.transform(native_bbox,
                                 crs_from=publ_info.get('native_crs'),
                                 crs_to=crs_def.EPSG_4326)
    title = publ_info['title']
    abstract = publ_info.get('description')
    md_language = next(
        iter(
            common_language.get_languages_iso639_2(' '.join([
                title or '',
                abstract or '',
            ]))), None)

    prop_values = _get_property_values(
        workspace=workspace,
        mapname=mapname,
        uuid=get_map_uuid(workspace, mapname),
        title=title,
        abstract=abstract or None,
        publication_date=publ_datetime.strftime('%Y-%m-%d'),
        revision_date=revision_date.strftime('%Y-%m-%d'),
        md_date_stamp=date.today().strftime('%Y-%m-%d'),
        identifier=url_for('rest_workspace_map.get',
                           workspace=workspace,
                           mapname=mapname),
        identifier_label=mapname,
        extent=extent,
        crs_list=[crs],
        md_organisation_name=None,
        organisation_name=None,
        operates_on=operates_on,
        md_language=md_language,
    )
    if http_method == common.REQUEST_METHOD_POST:
        prop_values.pop('revision_date', None)
    template_path = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                                 'record-template.xml')
    return template_path, prop_values