Ejemplo n.º 1
0
 def _getter(field):
     if field.data:
         val = get_kb_mapping("json_projects", str(field.data))
         if val:
             data = json_projects_kb_mapper(val)
             return data["fields"][key_name]
     return ""
Ejemplo n.º 2
0
 def _getter(field):
     if field.data:
         val = get_kb_mapping('json_projects', str(field.data))
         if val:
             data = json_projects_kb_mapper(val)
             return data['fields'][key_name]
     return ''
Ejemplo n.º 3
0
 def _getter(field):
     if field.data:
         val = get_kb_mapping('json_projects', str(field.data))
         if val:
             data = json_projects_kb_mapper(val)
             return data['fields'][key_name]
     return ''
Ejemplo n.º 4
0
def grants_validator(form, field):
    if field.data:
        for item in field.data:
            val = get_kb_mapping('json_projects', str(item['id']))
            if val:
                data = json_projects_kb_mapper(val)
                item['acronym'] = data['fields']['acronym']
                item['title'] = data['fields']['title']
                continue
            raise ValidationError("Invalid grant identifier %s" % item['id'])
Ejemplo n.º 5
0
def grants_validator(form, field):
    if field.data:
        for item in field.data:
            val = get_kb_mapping('json_projects', str(item['id']))
            if val:
                data = json_projects_kb_mapper(val)
                item['acronym'] = data['fields']['acronym']
                item['title'] = data['fields']['title']
                continue
            raise ValidationError("Invalid grant identifier %s" % item['id'])
Ejemplo n.º 6
0
def grants_validator(form, field):
    if field.data:
        for item in field.data:
            val = get_kb_mapping("json_projects", str(item["id"]))
            if val:
                data = json_projects_kb_mapper(val)
                item["acronym"] = data["fields"]["acronym"]
                item["title"] = data["fields"]["title"]
                continue
            raise ValidationError("Invalid grant identifier %s" % item["id"])
 def _f1(x):
     info = get_kb_mapping('json_projects', str(x))
     data = json.loads(info['value'])
     return {'id': x, 'title': data.get('title', ''), 'acronym': data.get('acronym', '')}
Ejemplo n.º 8
0
def process_recjson(deposition, recjson):
    """
    Process exported recjson (common for both new and edited records)
    """
    # ================
    # ISO format dates
    # ================
    for k in recjson.keys():
        if isinstance(recjson[k], date):
            recjson[k] = recjson[k].isoformat()

    # =======
    # Authors
    # =======
    if 'authors' in recjson and recjson['authors']:
        recjson['_first_author'] = recjson['authors'][0]
        recjson['_additional_authors'] = recjson['authors'][1:]

    if 'keywords' in recjson and recjson['keywords']:
        recjson['keywords'] = sorted(
            set([item.strip() for item in recjson['keywords'].split(',')]))

    # ===========
    # Communities
    # ===========
    try:
        communities = recjson.get('provisional_communities', [])

        # Extract identifier (i.e. elements are mapped from dict ->
        # string)
        recjson['provisional_communities'] = map(
            lambda x: x['identifier'],
            filter(lambda x: x.get('provisional', False), communities)
        )

        recjson['communities'] = map(
            lambda x: x['identifier'],
            filter(lambda x: not x.get('provisional', False), communities)
        )
    except TypeError:
        # Happens on re-run
        pass

    # ===========
    # Access groups
    # ===========
    try:
        access_groups = recjson.get('access_groups', [])

        # Extract identifier (i.e. elements are mapped from dict ->
        # string)
        recjson['access_groups'] = map(
            lambda x: x['identifier'], access_groups
        )
        recjson['access_groups_title'] = map(
            lambda x: x['title'], access_groups
        )
    except TypeError:
        # Happens on re-run
        pass

    # ==============
    # Related inputs
    # ==============
    for rel_input_type in ['rel_dataset', 'rel_software']:
        try:
            rel_input = recjson.get(rel_input_type, [])

            # Extract identifier (i.e. elements are mapped from dict ->
            # string)
            recjson[rel_input_type] = map(
                lambda x: x['identifier'], rel_input
            )
        except TypeError:
            # Happens on re-run
            pass

    # =============================
    # Related/alternate identifiers
    # =============================
    if recjson.get('related_identifiers', []):
        related_identifiers = recjson.get('related_identifiers', [])

        recjson['related_identifiers'] = filter(
            lambda x: x.get('relation', '') != 'isAlternativeIdentifier',
            related_identifiers
        )

        recjson['alternate_identifiers'] = map(
            lambda x: {'scheme': x['scheme'], 'identifier': x['identifier']},
            filter(
                lambda x: x.get('relation', '') == 'isAlternativeIdentifier',
                related_identifiers
            )
        )

    # =================
    # License
    # =================
    if recjson['access_right'] in ["open", "embargoed"]:
        info = get_kb_mapping(CFG_LICENSE_KB, str(recjson['license']))
        if info:
            info = json.loads(info['value'])
            recjson['license'] = dict(
                identifier=recjson['license'],
                source=CFG_LICENSE_SOURCE,
                license=info['title'],
                url=info['url'],
            )
    elif 'license' in recjson:
        del recjson['license']

    # =================
    # Project info
    # =================
    if recjson.get('project'):
        if 'record_curated_in_project' in recjson:
            if recjson['upload_type'] != 'dataset':
                recjson['record_curated_in_project'] = True
            else:
                curated = _get_bool_value_from_bool_or_str(
                    recjson['record_curated_in_project'])
                recjson['record_curated_in_project'] = curated
        if 'record_public_from_project' in recjson:
            public = _get_bool_value_from_bool_or_str(
                recjson['record_public_from_project'])
            recjson['record_public_from_project'] = public
    else:
        for k in ['record_curated_in_project', 'record_public_from_project']:
            if k in recjson:
                del recjson[k]
    if 'record_selected_for_archive' in recjson:
        archived = _get_bool_value_from_bool_or_str(
            recjson['record_selected_for_archive'])
        recjson['record_selected_for_archive'] = archived

    # Requirements
    # =================
    # if recjson.get('os', []):
    #    info = get_kb_mapping(CFG_REQUIREMENT_KB, str(recjson['os']))
    #    if info:
    #        info = json.loads(info['value'])
    #        recjson['os'] = dict(
    #            identifier=recjson['os'],
    #            os=info['title'],
    #        )
    # elif 'os' in recjson:
    #    del recjson['os']

    # if recjson.get('flavor', []):
    #    info = get_kb_mapping(CFG_REQUIREMENT_KB, str(recjson['flavor']))
    #    if info:
    #        info = json.loads(info['value'])
    #        recjson['flavor'] = dict(
    #            identifier=recjson['flavor'],
    #            flavor=info['title'],
    #        )
    # elif 'flavor' in recjson:
    #    del recjson['flavor']

    # =======================
    # Filter out empty fields
    # =======================
    filter_empty_elements(recjson)

    # ==================================
    # Map dot-keys to their dictionaries
    # ==================================
    for k in recjson.keys():
        if '.' in k:
            mainkey, subkey = k.split('.')
            if mainkey not in recjson:
                recjson[mainkey] = {}
            recjson[mainkey][subkey] = recjson.pop(k)

    return recjson
Ejemplo n.º 9
0
def process_recjson(deposition, recjson):
    """
    Process exported recjson (common for both new and edited records)
    """
    # ================
    # ISO format dates
    # ================
    for k in recjson.keys():
        if isinstance(recjson[k], date):
            recjson[k] = recjson[k].isoformat()

    # =======
    # Authors
    # =======
    if 'authors' in recjson and recjson['authors']:
        recjson['_first_author'] = recjson['authors'][0]
        recjson['_additional_authors'] = recjson['authors'][1:]

    # ===========
    # Communities
    # ===========
    try:
        communities = recjson.get('provisional_communities', [])

        # Extract identifier (i.e. elements are mapped from dict ->
        # string)
        recjson['provisional_communities'] = map(
            lambda x: x['identifier'],
            filter(lambda x: x.get('provisional', False), communities)
        )

        recjson['communities'] = map(
            lambda x: x['identifier'],
            filter(lambda x: not x.get('provisional', False), communities)
        )
    except TypeError:
        # Happens on re-run
        pass

    # =============================
    # Related/alternate identifiers
    # =============================
    if recjson.get('related_identifiers', []):
        related_identifiers = recjson.get('related_identifiers', [])

        recjson['related_identifiers'] = filter(
            lambda x: x.get('relation', '') != 'isAlternativeIdentifier',
            related_identifiers
        )

        recjson['alternate_identifiers'] = map(
            lambda x: {'scheme': x['scheme'], 'identifier': x['identifier']},
            filter(
                lambda x: x.get('relation', '') == 'isAlternativeIdentifier',
                related_identifiers
            )
        )

    # =================
    # License
    # =================
    if recjson['access_right'] in ["open", "embargoed"]:
        info = get_kb_mapping(CFG_LICENSE_KB, str(recjson['license']))
        if info:
            info = json.loads(info['value'])
            recjson['license'] = dict(
                identifier=recjson['license'],
                source=CFG_LICENSE_SOURCE,
                license=info['title'],
                url=info['url'],
            )
    elif 'license' in recjson:
        del recjson['license']

    # =======================
    # Journal
    # =======================
    # Set year or delete fields if no title is provided
    if recjson.get('journal.title', None):
        recjson['journal.year'] = recjson['publication_date'][:4]

    # =======================
    # References
    # =======================
    if recjson.get('references', []):
        recjson['references'] = map(
            lambda x: dict(raw_reference=x),
            recjson['references']
        )

    # =======================
    # Book/chaper/report
    # =======================
    if 'imprint.publisher' in recjson and 'imprint.place' in recjson:
        recjson['imprint.year'] = recjson['publication_date'][:4]

    if 'part_of.title' in recjson:
        mapping = [
            ('part_of.publisher', 'imprint.publisher'),
            ('part_of.place', 'imprint.place'),
            ('part_of.year', 'imprint.year'),
            ('part_of.isbn', 'isbn'),
        ]
        for new, old in mapping:
            if old in recjson:
                try:
                    recjson[new] = recjson[old]
                    del recjson[old]
                except KeyError:
                    pass

    # =================
    # Grants
    # =================
    # Remap incoming dictionary
    recjson['grants'] = map(
        lambda x: dict(
            title="%s - %s (%s)" % (x['acronym'], x['title'], x['id']),
            identifier=x['id']
        ),
        recjson.get('grants', [])
    )

    # =======================
    # Filter out empty fields
    # =======================
    filter_empty_elements(recjson)

    # ==================================
    # Map dot-keys to their dictionaries
    # ==================================
    for k in recjson.keys():
        if '.' in k:
            mainkey, subkey = k.split('.')
            if mainkey not in recjson:
                recjson[mainkey] = {}
            recjson[mainkey][subkey] = recjson.pop(k)

    return recjson
Ejemplo n.º 10
0
def process_recjson(deposition, recjson):
    """
    Process exported recjson (common for both new and edited records)
    """
    # ================
    # ISO format dates
    # ================
    for k in recjson.keys():
        if isinstance(recjson[k], date):
            recjson[k] = recjson[k].isoformat()

    # =======
    # Authors
    # =======
    if 'authors' in recjson and recjson['authors']:
        recjson['first_author'] = recjson['authors'][0]
        recjson['additional_authors'] = recjson['authors'][1:]
        del recjson['authors']

    # ===========
    # Communities
    # ===========
    # FIXME: Properly deal with provisional/non-provisional
    try:
        # Extract identifier (i.e. elements are mapped from dict ->
        # string)
        recjson['provisional_communities'] = list(set(map(
            lambda x: x['identifier'],
            recjson.get('provisional_communities', [])
        )))

        recjson['communities'] = list(set(map(
            lambda x: x['identifier'],
            recjson.get('communities', [])
        )))
    except TypeError:
        # Happens on re-run
        pass

    # =================
    # License
    # =================
    if recjson['access_right'] in ["open", "embargoed"]:
        info = get_kb_mapping(CFG_LICENSE_KB, str(recjson['license']))
        if info:
            info = json.loads(info['value'])
            recjson['license'] = dict(
                identifier=recjson['license'],
                source=CFG_LICENSE_SOURCE,
                license=info['title'],
                url=info['url'],
            )
    elif 'license' in recjson:
        del recjson['license']

    # =======================
    # Journal
    # =======================
    # Set year or delete fields if no title is provided
    if recjson.get('journal.title', None):
        recjson['journal.year'] = recjson['publication_date'][:4]

    # =======================
    # Book/chaper/report
    # =======================
    if 'imprint.publisher' in recjson and 'imprint.place' in recjson:
        recjson['imprint.year'] = recjson['publication_date'][:4]

    if 'part_of.title' in recjson:
        mapping = [
            ('part_of.publisher', 'imprint.publisher'),
            ('part_of.place', 'imprint.place'),
            ('part_of.year', 'imprint.year'),
            ('part_of.isbn', 'isbn'),
        ]
        for new, old in mapping:
            if old in recjson:
                try:
                    recjson[new] = recjson[old]
                    del recjson[old]
                except KeyError:
                    pass

    # =================
    # Grants
    # =================
    # Remap incoming dictionary
    recjson['grants'] = map(
        lambda x: dict(
            title="%s - %s (%s)" % (x['acronym'], x['title'], x['id']),
            identifier=x['id']
        ),
        recjson.get('grants', [])
    )

    # =======================
    # Filter out empty fields
    # =======================
    filter_empty_elements(recjson)

    # ==================================
    # Map dot-keys to their dictionaries
    # ==================================
    for k in recjson.keys():
        if '.' in k:
            mainkey, subkey = k.split('.')
            if mainkey not in recjson:
                recjson[mainkey] = {}
            recjson[mainkey][subkey] = recjson.pop(k)

    return recjson
 def _f1(x):
     info = get_kb_mapping('json_projects', str(x))
     data = json.loads(info['value'])
     return {'id': x, 'title': data.get('title', ''), 'acronym': data.get('acronym', '')}
Ejemplo n.º 12
0
def format_element(bfo, req_id=''):
    info = get_kb_mapping('requirements', req_id)
    if info:
        info = json.loads(info['value'])
        return info['title']
Ejemplo n.º 13
0
def format_element(bfo, req_id=""):
    info = get_kb_mapping("requirements", req_id)
    if info:
        info = json.loads(info["value"])
        return info["title"]