Example #1
0
def check_record(record, overwrite=True):
    """ Calculates wikipedia link based on viaf id"""

    maxi = 0
    for k in record.iterkeys():
        if record[k][-1][-1] > maxi:
            maxi = record[k][-1][-1]


    if not overwrite and get_wiki_link_from_record(record):
        record.warn("Author already had a link to wikipedia")
    else:
        control_nos = []
        if record.get('035',None):
            control_nos = [t[1] for d in record.get('035',()) if d and d[0] for t in d[0] if t and t[1]]
        for control_no in control_nos:
            if (control_no.find("|(VIAF)") != -1):
                viaf_id = control_no.split("|(VIAF)")[1]
                link = get_wikipedia_link(viaf_id)
                if link:
                    linkfield = ([(CFG_VIAF_LINK_NAME_LABEL_SUBFIELD,CFG_VIAF_WIKIPEDIA_NAME_VALUE_SUBFIELD),(CFG_VIAF_WIKIPEDIA_LINK_SUBFIELD,link)] , '', '', ' ', maxi)
                    if get_wiki_link_from_record(record):
                        for field in record[CFG_VIAF_WIKIPEDIA_LINK_BFO_FIELD]:
                            for subfield in field:
                                if type(subfield) is list and subfield[0] == CFG_VIAF_LINK_NAME_LABEL_SUBFIELD and subfield[1] == CFG_VIAF_WIKIPEDIA_NAME_VALUE_SUBFIELD:
                                    for sub in field:
                                        if type(sub) is list and sub[0] == CFG_VIAF_WIKIPEDIA_LINK_SUBFIELD:
                                            sub[1] = link

                    else:
                        record_add_field(record,CFG_VIAF_WIKIPEDIA_LINK_BFO_FIELD, \
                                subfields=[(CFG_VIAF_LINK_NAME_LABEL_SUBFIELD,CFG_VIAF_WIKIPEDIA_NAME_VALUE_SUBFIELD),(CFG_VIAF_WIKIPEDIA_LINK_SUBFIELD,link)])
                    record.set_amended("Added wiki link to author")
Example #2
0
def format_element(bfo):
    """ Prints the control number of an author authority record in HTML.
    By default prints brief version.

    @param brief: whether the 'brief' rather than the 'detailed' format
    @type brief: 'yes' or 'no'
    """

    from invenio.messages import gettext_set_language
    _ = gettext_set_language(bfo.lang)  # load the right message language

    control_nos = [d['a'] for d in bfo.fields('035__') if d['a'] is not None]
    control_nos = filter(None, control_nos)  # fastest way to remove empty ""s

    style = "style='width:auto;height:20px;margin-right:10px'"
    links_formatted = []
    for control_no in control_nos:
        from urllib import quote
        image_pattern = "<a href='%(external_article)s'><img class='author_usefull_link' src='/img/%(image)s'/>%(text)s</a>"

        if (control_no.find("|(VIAF)") != -1):
            viaf_id = control_no.split("|(VIAF)")[1]
            link_to_wikipedia = get_wiki_link_from_record(bfo)
            if not link_to_wikipedia:
                link_to_wikipedia = get_wikipedia_link(viaf_id)
            ## Wikipedia link with wiki icon
            if link_to_wikipedia:
                image_element = image_pattern % {
                    "text": "Wikipedia link",
                    "image": "wikipedia.png",
                    "external_article": link_to_wikipedia
                }
                links_formatted.append(image_element)
            ## VIAF link
            image_element = image_pattern \
                    % { "text" : "VIAF cluster link","image": "viaf.png", "external_article": str("http://viaf.org/viaf/"+viaf_id) }
            links_formatted.append(image_element)
            ## Library of congress link
        if (control_no.find("|(DLC)") != -1):
            dlc_id = control_no.split("|(DLC)")[1].replace(" ", "")
            link_to_lccn = "http://lccn.loc.gov/" + dlc_id
            image_element = image_pattern % {
                "text": "Library of Congress link",
                "image": "library_of_congress.png",
                "external_article": link_to_lccn
            }
            links_formatted.append(image_element)

    if links_formatted:
        title = "<strong>" + _("Useful links") + "</strong>"
        if links_formatted:
            content = "<ul><li>" + "</li><li> ".join(
                links_formatted) + "</li></ul>"
        else:
            content = "<strong style='color:red'>Missing !</strong>"

        return "<p>" + title + ": " + content + "</p>"
    else:
        return None
Example #3
0
def format_element(bfo):
    """ Prints the control number of an author authority record in HTML.
    By default prints brief version.

    @param brief: whether the 'brief' rather than the 'detailed' format
    @type brief: 'yes' or 'no'
    """

    from invenio.messages import gettext_set_language
    _ = gettext_set_language(bfo.lang)    # load the right message language

    control_nos = [d['a'] for d in bfo.fields('035__') if d['a'] is not None]
    control_nos = filter(None, control_nos) # fastest way to remove empty ""s

    style = "style='width:auto;height:20px;margin-right:10px'"
    links_formatted = []
    for control_no in control_nos:
        from urllib import quote
        image_pattern = "<a href='%(external_article)s'><img class='author_usefull_link' src='/img/%(image)s'/>%(text)s</a>"

        if (control_no.find("|(VIAF)") != -1):
            viaf_id = control_no.split("|(VIAF)")[1]
            link_to_wikipedia = get_wiki_link_from_record(bfo)
            if not link_to_wikipedia:
                link_to_wikipedia = get_wikipedia_link(viaf_id)
            ## Wikipedia link with wiki icon
            if link_to_wikipedia:
                image_element = image_pattern % { "text": "Wikipedia link", "image": "wikipedia.png", "external_article": link_to_wikipedia}
                links_formatted.append(image_element)
            ## VIAF link
            image_element = image_pattern \
                    % { "text" : "VIAF cluster link","image": "viaf.png", "external_article": str("http://viaf.org/viaf/"+viaf_id) }
            links_formatted.append(image_element)
            ## Library of congress link
        if (control_no.find("|(DLC)") != -1):
            dlc_id = control_no.split("|(DLC)")[1].replace(" ","")
            link_to_lccn = "http://lccn.loc.gov/"+ dlc_id
            image_element = image_pattern % { "text": "Library of Congress link", "image": "library_of_congress.png", "external_article" : link_to_lccn }
            links_formatted.append(image_element)





    if links_formatted:
        title = "<strong>" + _("Useful links") + "</strong>"
        if links_formatted:
            content = "<ul><li>" + "</li><li> ".join(links_formatted) + "</li></ul>"
        else:
            content = "<strong style='color:red'>Missing !</strong>"

        return "<p>" + title + ": " + content + "</p>"
    else:
        return None
Example #4
0
def check_record(record, overwrite=True):
    """ Calculates wikipedia link based on viaf id"""

    maxi = 0
    for k in record.iterkeys():
        if record[k][-1][-1] > maxi:
            maxi = record[k][-1][-1]

    if not overwrite and get_wiki_link_from_record(record):
        record.warn("Author already had a link to wikipedia")
    else:
        control_nos = []
        if record.get('035', None):
            control_nos = [
                t[1] for d in record.get('035', ()) if d and d[0] for t in d[0]
                if t and t[1]
            ]
        for control_no in control_nos:
            if (control_no.find("|(VIAF)") != -1):
                viaf_id = control_no.split("|(VIAF)")[1]
                link = get_wikipedia_link(viaf_id)
                if link:
                    linkfield = ([(CFG_VIAF_LINK_NAME_LABEL_SUBFIELD,
                                   CFG_VIAF_WIKIPEDIA_NAME_VALUE_SUBFIELD),
                                  (CFG_VIAF_WIKIPEDIA_LINK_SUBFIELD, link)],
                                 '', '', ' ', maxi)
                    if get_wiki_link_from_record(record):
                        for field in record[CFG_VIAF_WIKIPEDIA_LINK_BFO_FIELD]:
                            for subfield in field:
                                if type(subfield) is list and subfield[
                                        0] == CFG_VIAF_LINK_NAME_LABEL_SUBFIELD and subfield[
                                            1] == CFG_VIAF_WIKIPEDIA_NAME_VALUE_SUBFIELD:
                                    for sub in field:
                                        if type(sub) is list and sub[
                                                0] == CFG_VIAF_WIKIPEDIA_LINK_SUBFIELD:
                                            sub[1] = link

                    else:
                        record_add_field(record,CFG_VIAF_WIKIPEDIA_LINK_BFO_FIELD, \
                                subfields=[(CFG_VIAF_LINK_NAME_LABEL_SUBFIELD,CFG_VIAF_WIKIPEDIA_NAME_VALUE_SUBFIELD),(CFG_VIAF_WIKIPEDIA_LINK_SUBFIELD,link)])
                    record.set_amended("Added wiki link to author")