Exemplo n.º 1
0
def format_element(bfo):
    """
    Creates a navigation for comments.
    """

    # get variables
    this_recid = bfo.control_field('001')
    try:
        this_content = bfo.fields('520__a')[0]
    except:
        return ""
    try:
        this_author = bfo.fields('100__a')[0]
    except:
        return ""

    this_limit_content = get_contextual_content(this_content,
                                                [],
                                                max_lines=2)[0]
    menu_recids = []
    current_language = bfo.lang

    post_recid = get_parent_post(this_recid)

    menu_recids = get_comments(post_recid, newest_first=True)

    try:
        menu_out = '<h4>%s</h4>' % cfg_messages["in_issue"][current_language]
    except:
        menu_out = '<h4>%s</h4>' % cfg_messages["in_issue"]['en']

    for recid in menu_recids:
        if str(this_recid) == str(recid):
            menu_out += '<div class="active"><div class="litem"><b>%s</b>: %s [...]</div></div>' % (this_author, this_limit_content)
        else:
            temp_rec = BibFormatObject(recid)
            content = temp_rec.fields('520__a')[0]
            limit_content = get_contextual_content(content,
                                                   [],
                                                   max_lines=1)[0]

            try:
                author = temp_rec.fields('100__a')[0]
            except:
                author = 'Anonymous'
            menu_out += '<div class="litem"><a href="%s/record/%s%s"><b>%s</b>: %s [...]</a></div>' % (CFG_SITE_URL,
                                                                                                recid,
                                                                                                (bfo.lang=="fr") and "?ln=fr" or "?ln=en",
                                                                                                author, limit_content)
            

        
    return menu_out
Exemplo n.º 2
0
def get_widget_html(language, max_photos, collections, separator, ln):
    """
    Returns the content of the widget
    """
    latest_photo_ids = perform_request_search(c=collections,
                                              rg=max_photos,
                                              of='id')
    images_urls = []
    for recid in latest_photo_ids[:max_photos]:
        try:
            photo_record = BibFormatObject(recid)
        except:
            # todo: Exception, no photo in this selection
            continue

        if language == "fr":
            try:
                title = photo_record.fields('246_1a', escape=1)[0]
            except KeyError:
                title = ""
        else:
            try:
                title = photo_record.fields('245__a', escape=1)[0]
            except KeyError:
                # todo: exception, picture with no title
                title = ""

        if CFG_CERN_SITE and photo_record.fields('8567_'):
            # Get from 8567_
            dfs_images = photo_record.fields('8567_')
            for image_block in dfs_images:
                if image_block.get("y", '') == "Icon":
                    if image_block.get("u", '').startswith("http://"):
                        images_urls.append((recid, image_block["u"], title))
                        break  # Just one image per record

        else:
            # Get from 8564_
            images = photo_record.fields('8564_')
            for image_block in images:
                if image_block.get("x", '').lower() == "icon":
                    if image_block.get("q", '').startswith("http://"):
                        images_urls.append((recid, image_block["q"], title))
                        break  # Just one image per record

    # Build output
    html_out = separator.join([
        '<a href="%s/record/%i?ln=%s"><img class="phr" width="100" height="67" src="%s"/>%s</a>'
        % (CFG_SITE_URL, recid, ln, photo_url, title)
        for (recid, photo_url, title) in images_urls
    ])

    return html_out
def get_widget_html(language, max_photos, collections, separator, ln):
    """
    Returns the content of the widget
    """
    latest_photo_ids = perform_request_search(c=collections,
                                              rg=max_photos,
                                              of='id')
    images_urls = []
    for recid in latest_photo_ids[:max_photos]:
        try:
            photo_record = BibFormatObject(recid)
        except:
            # todo: Exception, no photo in this selection
            continue

        if language == "fr":
            try:
                title = photo_record.fields('246_1a', escape=1)[0]
            except KeyError:
                title = ""
        else:
            try:
                title = photo_record.fields('245__a', escape=1)[0]
            except KeyError:
                # todo: exception, picture with no title
                title = ""

        if CFG_CERN_SITE and photo_record.fields('8567_'):
            # Get from 8567_
            dfs_images = photo_record.fields('8567_')
            for image_block in dfs_images:
                if image_block.get("y", '') == "Icon":
                    if image_block.get("u", '').startswith("http://"):
                        images_urls.append((recid, image_block["u"], title))
                        break # Just one image per record

        else:
            # Get from 8564_
            images = photo_record.fields('8564_')
            for image_block in images:
                if image_block.get("x", '').lower() == "icon":
                    if image_block.get("q", '').startswith("http://"):
                        images_urls.append((recid, image_block["q"], title))
                        break # Just one image per record

    # Build output
    html_out = separator.join(['<a href="%s/record/%i?ln=%s"><img class="phr" width="100" height="67" src="%s"/>%s</a>' % (CFG_SITE_URL, recid, ln, photo_url, title) for (recid, photo_url, title) in images_urls])

    return html_out
Exemplo n.º 4
0
def format_element(bfo):
    """
    Formats comments header using the post's name and
    the date in which the comment was added
    """

    this_recid = bfo.control_field('001')

    post_recid = get_parent_post(this_recid)
    post_rec = BibFormatObject(post_recid)
    try:
        post_title = post_rec.fields('245__a')[0]
    except:
        post_title = 'Untitled'

    try:
        addition_date = bfo.fields('269__c')[0]
    except:
        addition_date = ""

    out = '<div id="top"><div id="topbanner">&nbsp;</div>'
    out += '<div id="mainmenu"><table width="100%">'
    out += '<tr><td class="left" style = "font-size: 1em;">Go to post: <a href="%s/record/%s?%s">%s</a>' % \
                (CFG_SITE_URL, post_recid, bfo.lang, post_title)

    out += '<td class="right">%s</td>' % addition_date
    out += '</td></tr></table></div></div>'
    out += '<div id="mainphoto"></div>'

    return out
Exemplo n.º 5
0
def _get_report_numbers(record_id):
    from invenio.bibformat_engine import BibFormatObject
    bfo = BibFormatObject(record_id)
    fields = bfo.fields('037__')
    report_numbers = []
    for field in fields:
        if 'a' in field:
            report_numbers.append(field['a'])
    return report_numbers
Exemplo n.º 6
0
def _get_report_numbers(record_id):
        from invenio.bibformat_engine import BibFormatObject
        bfo = BibFormatObject(record_id)
        fields = bfo.fields('037__')
        report_numbers = []
        for field in fields:
            if 'a' in field:
                report_numbers.append(field['a'])
        return report_numbers
Exemplo n.º 7
0
def format_element(bfo):
    """
    Creates a navigation for articles in the same issue and category.
    """
    # get variables
    this_recid = bfo.control_field('001')
    menu_recids = []
    current_language = bfo.lang
    this_title = ""
    try:
        this_title = bfo.fields('245__a')[0]
    except:
        return ""

    blog_recid = get_parent_blog(this_recid)
    blog_rec = BibFormatObject(blog_recid)
    try:
        blog_title = blog_rec.fields('245__a')[0]
    except:
        blog_title = 'Untitled'

    menu_recids = get_posts(blog_recid, newest_first=True)

    try:
        menu_out = '<h4>%s</h4>' % cfg_messages["in_issue"][current_language]
    except: # in english by default
        menu_out = '<h4>%s</h4>' % cfg_messages["in_issue"]['en']

    for recid in menu_recids:
        if str(this_recid) == str(recid):
            menu_out += '<div class="active"><div class="litem">%s</div></div>' % this_title
        else:
            temp_rec = BibFormatObject(recid)
            try:
                title = temp_rec.fields('245__a')[0]
            except:
                title = 'Untitled'
            menu_out += '<div class="litem"><a href="%s/record/%s%s">%s</a></div>' % (CFG_SITE_URL,
                                                                                      recid,
                                                                                      (bfo.lang=="fr") and "?ln=fr" or "",
                                                                                      title)

    return menu_out
Exemplo n.º 8
0
    def schemaorg_type(recid=None, bfo=None):
        if recid:
            from invenio.bibformat_engine import BibFormatObject
            bfo = BibFormatObject(recid)

        if bfo:
            from invenio.openaire_deposit_config import CFG_OPENAIRE_SCHEMAORG_MAP
            collections = bfo.fields('980__')
            for c in collections:
                a = c.get('a', None)
                b = c.get('b', None)
                res = CFG_OPENAIRE_SCHEMAORG_MAP.get(b if b else a, None)
                if res:
                    return res
        return 'http://schema.org/CreativeWork'
Exemplo n.º 9
0
def get_doi_from_record(recid):
    """
    Given a record ID we fetch it from the DB and return
    the first DOI found as specified by the config variable
    CFG_APSHARVEST_RECORD_DOI_TAG.

    @param recid:  record id record containing a DOI
    @type recid: string/int

    @return: first DOI found in record
    @rtype: string
    """
    record = BibFormatObject(int(recid))
    possible_dois = record.fields(CFG_APSHARVEST_RECORD_DOI_TAG[:-1])
    for doi in possible_dois:
        if '2' in doi and doi.get('2', "") == "DOI":
            # Valid DOI present, add it
            try:
                return doi['a']
            except KeyError:
                continue
Exemplo n.º 10
0
def format_element(bfo, limit, separator='; ',
           extension='[...]',
           print_links = "yes",
           print_affiliations='no',
           affiliation_prefix = ' (',
           affiliation_suffix = ')',
           print_affiliation_first='no',
           interactive="no",
           highlight="no",
           affiliations_separator=" ; ",
           name_last_first = "yes",
           collaboration = "yes",
           id_links = "no",
           markup = "html",
           link_extension = "no",
           suffix = ''
           ):
    """
    Prints the list of authors of a record.

    @param limit the maximum number of authors to display
    @param separator the separator between authors.
    @param extension a text printed if more authors than 'limit' exist
    @param print_links if yes, prints the authors as HTML link to their publications
    @param print_affiliations if yes, make each author name followed by its affiliation
    @param affiliation_prefix prefix printed before each affiliation
    @param affiliation_suffix suffix printed after each affiliation
    @param print_affiliation_first if 'yes', affiliation is printed before the author
    @param interactive if yes, enable user to show/hide authors when there are too many (html + javascript)
    @param highlight highlights authors corresponding to search query if set to 'yes'
    @param affiliations_separator separates affiliation groups
    @param name_last_first if yes (default) print last, first  otherwise first last
    @param collaboration if yes (default) uses collaboration name in place of long author list, if available
    @param id_links if yes (default = no) prints link based on INSPIRE IDs if available - only used if print_links = yes
    @param markup html (default) or latex controls small markup differences
    @param link_extension if 'yes' link the extension to the detailed
    record page

    """
    from urllib import quote
    from cgi import escape
    import re
    from invenio.messages import gettext_set_language
    from invenio.config import CFG_BASE_URL, CFG_SITE_RECORD
    from invenio.bibformat_engine import BibFormatObject

    _ = gettext_set_language(bfo.lang)    # load the right message language

    #regex for parsing last and first names and initials
    re_last_first = re.compile('^(?P<last>[^,]+)\s*,\s*(?P<first_names>[^\,]*)(?P<extension>\,?.*)$')
    re_initials = re.compile(r'(?P<initial>\w)(\w+|\.)\s*')
    re_coll = re.compile(r'\s*collaborations?', re.IGNORECASE)

    bibrec_id = bfo.control_field("001")
    authors = []
    lastauthor = ''

    # HepData and only-INSPIRE data records inherit the list of authors from the original paper
    if (bfo.field("520__9") == "HEPDATA") or (bfo.field("520__9") == "INSPIRE"):
        parent_recid = bfo.field("786__w")
        bfo_parent = BibFormatObject(int(parent_recid))

        authors = []
        authors_1 = bfo_parent.fields('100__', repeatable_subfields_p=True)
        authors_2 = bfo_parent.fields('700__', repeatable_subfields_p=True)
    # other datasources should have a list of authors
    else:
        authors = []
        authors_1 = bfo.fields('100__', repeatable_subfields_p=True)
        authors_2 = bfo.fields('700__', repeatable_subfields_p=True)

    authors.extend(authors_1)
    authors.extend(authors_2)

    # If there are no author check for corporate author in 110__a field
    if len(authors) == 0:
        authors = bfo.fields('110__', repeatable_subfields_p=True)
        # For corporate authors we don't want to reverse names order
        name_last_first = 'yes'
        # And we don't want to create links
        print_links = 'no'

    # Keep real num of authors. fix + affiliations_separator.join(author['u']) + \
    nb_authors = len(authors)

    # Limit num of authors, so that we do not process
    # the authors that will not be shown. This can only
    # be done in non-interactive mode, as interactive mode
    # allows to show all of them.
    if limit.isdigit() and nb_authors > int(limit) \
           and interactive != "yes":
        if bfo.field('710g'):   # check for colln note
            authors = authors[:1]
        else:

            authors = authors[:int(limit)]

    # Process authors to add link, affiliation and highlight
    for author in authors:

        if author.has_key('a'):
            author['a'] = author['a'][0]  # There should not be
                                          # repeatable subfields here.
            if highlight == 'yes':
                from invenio import bibformat_utils
                author['a'] = bibformat_utils.highlight(author['a'],
                                                        bfo.search_pattern)

            #check if we need to reverse last, first
            #we don't try to reverse it if it isn't stored with a comma.
            first_last_match = re_last_first.search(author['a'])
            author['display'] = author['a']

            if name_last_first.lower() == "no":
                if first_last_match:
                    author['display'] = first_last_match.group('first_names') + \
                                        ' ' + \
                                        first_last_match.group('last') + \
                                        first_last_match.group('extension')

            #for latex we do initials only  (asn assume first last)
            if markup == 'latex':
                if first_last_match:
                    first = re_initials.sub('\g<initial>.~', \
                                        first_last_match.group('first_names'))
                    author['display'] = first  + \
                                        first_last_match.group('last') + \
                                        first_last_match.group('extension')


            if print_links.lower() == "yes":

                # if there is an ID, search using that.
                id_link = ''
                if id_links == "yes" and author.has_key('i'):
                    author['i'] = author['i'][0]  #possible to have more IDs?
                    id_link = '<a class="authoridlink" href="' + \
                              CFG_BASE_URL + \
                              '/search?' + \
                              'ln='+ bfo.lang + \
                              '&amp;p=100__i' + escape(':' + author['i']) + \
                              '+or+700__i' + escape(':' + author['i']) +\
                              '">'+escape("(ID Search)") + '</a> '


                author['display'] = '<a class="authorlink" href="' + \
                                    CFG_BASE_URL + \
                                    '/author/'+ quote(author['a']) + \
                                    '?recid=' + bibrec_id + \
                                    '&amp;ln='+ bfo.lang + \
                                    '">' + escape(author['display'])+'</a>' + \
                                    id_link

        if print_affiliations == "yes":
            if author.has_key('e'):
                author['e'] = affiliation_prefix + \
                              affiliations_separator.join(author['e']) + \
                              affiliation_suffix



            if author.has_key('u'):
                author['ilink'] = ['<a class="afflink" href="' + \
                                   CFG_BASE_URL + '/search?cc=Institutions&amp;p=institution:'+ \
                                   quote('"' + string + '"') + \
                                   '&amp;ln=' + bfo.lang + \
                                   '">' + \
                                   string.lstrip() + \
                                   '</a>' for string in author['u']]
                author['u'] = affiliation_prefix + \
                              affiliations_separator.join(author['ilink']) + \
                              affiliation_suffix

#
#  Consolidate repeated affiliations
#
    last = ''
    authors.reverse()
    for author in authors:
        if not author.has_key('u'):
            author['u'] = ''
        #print 'this->'+ author['a']+'\n'
        if last == author['u']:
            author['u'] = ''
        else:
            last = author['u']

    authors.reverse()

    # Flatten author instances
    if print_affiliations == 'yes':
#      100__a (100__e)  700__a (100__e) (100__u)
        if print_affiliation_first.lower() != 'yes':
            authors = [author.get('display', '') + author.get('e', '') + author.get('u', '')
                       for author in authors]

        else:
            authors = [author.get('u', '') + author.get('display', '')
                       for author in authors]

    else:
        authors = [author.get('display', '')
                   for author in authors]

    # link the extension to detailed record
    if link_extension == 'yes' and interactive != 'yes':
        extension = '<a class="authorlink" href="' +  \
                    CFG_BASE_URL + '/' + CFG_SITE_RECORD + '/' + str(bfo.recID) + '">' + \
                    extension + '</a>'

    # Detect Collaborations:
    if collaboration == "yes":
        colls = []
        for coll in bfo.fields("710__g"):
            if coll not in colls:
                colls.append(coll)
    else:
        colls = []
    if colls:
        short_coll = False
        colls = [re_coll.sub('', coll) for coll in colls]
        if print_links.lower() == "yes":
            colls = ['<a class="authorlink" href="' +
                     CFG_BASE_URL + '/search' +
                     '?p=collaboration:' + quote("'" + coll + "'") +
                     '&amp;ln=' + bfo.lang +
                     '">'+escape(coll)+'</a>' for coll in colls]

        coll_display = " and ".join(colls)
        if not coll_display.endswith("aboration"):
            coll_display += " Collaboration"
            if len(colls) > 1:
                coll_display += 's'
        if nb_authors > 1:
            if markup == 'latex':
                coll_display =  authors[0] + extension + "  [" + \
                               coll_display + "]"
            elif interactive == "yes":
                coll_display += " ("  + authors[0] + " "
                extension += ")"
            else:  #html
                coll_display += " (" + authors[0] + extension + ")"
        elif nb_authors == 1:
            short_coll = True
            if markup == 'latex':
                coll_display = authors[0] + " [" + coll_display + "]"
            else:  #html
                coll_display += " (" + authors[0] + " for the collaboration)"
        elif nb_authors == 0:
            short_coll = True
            if markup == 'latex':
                coll_display = "[" + coll_display + "]"

    # Start outputting, depending on options and number of authors
    if colls and (interactive != "yes" or short_coll):
        return coll_display

    if limit.isdigit() and nb_authors > int(limit) and interactive != "yes":
        if markup == 'latex':
            lastauthor = authors.pop()
            lastauthor = ' and ' + lastauthor
            limit = int(limit) - 1

        return separator.join(authors[:int(limit)]) + lastauthor + \
               extension

    elif interactive == "yes" and ((colls and not short_coll) or (limit.isdigit() and nb_authors > int(limit))):
        out = '''
        <script>
        function toggle_authors_visibility(){
            var more = document.getElementById('more');
            var link = document.getElementById('link');
            var extension = document.getElementById('extension');
            if (more.style.display=='none'){
                more.style.display = '';
                extension.style.display = 'none';
                link.innerHTML = "%(show_less)s"
            } else {
                more.style.display = 'none';
                extension.style.display = '';
                link.innerHTML = "%(show_more)s"
            }
            link.style.color = "rgb(204,0,0);"
        }

        function set_up(){
            var extension = document.getElementById('extension');
            extension.innerHTML = '%(extension)s';
            toggle_authors_visibility();
        }

        </script>
        ''' % {'show_less': _("Hide"),
               'show_more': _("Show all %i authors") % nb_authors,
               'extension': extension}

#        out += '<a name="show_hide" />'
        if colls:
            show = coll_display
            more = separator + separator.join(authors[1:]) + ')'
        else:
            show = separator.join(authors[:int(limit)])
            more = separator.join(authors[int(limit):len(authors)])

        out += show
        out += ' <span id="more" style="">' + more + '</span>'
        out += ' <span id="extension"></span>'
        out += ' <small><i><a id="link" href="#"' + \
               ' style="color:green;background:white;" onclick="toggle_authors_visibility()" ' + \
               ' style="color:rgb(204,0,0);"></a></i></small>'
        out += '<script>set_up()</script>'
        return out
    elif nb_authors > 0:
        if markup == 'latex' and nb_authors > 1:
            lastauthor = authors.pop()
            lastauthor = ' and ' + lastauthor
        output = separator.join(authors) + lastauthor
        # remove the dot from the end of authors list when the suffix starts with dot
        # (to avoid two consecutive dots)
        if suffix and output and output[-1] == suffix[0] == '.':
            output = output[:-1]
        return output
def format_element(bfo,
                   new_articles_first='yes',
                   subject_to_css_class_kb="WebJournalSubject2CSSClass",
                   display_all_category_articles='no'):
    """
    Creates a navigation for articles in the same issue and category.

    @param new_articles_first: if 'yes', display new articles before other articles
    @param subject_to_css_class_kb: knowledge base that maps 595__a to a CSS class
    @param display_all_category_articles: if yes, display all articles, whatever category is selected
    """
    # get variables
    args = parse_url_string(bfo.user_info['uri'])
    this_recid = bfo.control_field('001')
    this_issue_number = args["issue"]
    category_name = args["category"]
    journal_name = args["journal_name"]
    ln = bfo.lang
    _ = gettext_set_language(ln)

    this_title = ""
    if ln == "fr":
        if bfo.fields('246_1a'):
            this_title = bfo.fields('246_1a')[0]
        elif bfo.fields('245__a'):
            this_title = bfo.fields('245__a')[0]
    else:
        if bfo.fields('245__a'):
            this_title = bfo.fields('245__a')[0]
        elif bfo.fields('246_1a'):
            this_title = bfo.fields('246_1a')[0]

    journal_categories = [category_name]
    if display_all_category_articles.lower() == 'yes':
        # Let's retrieve all categories. Ok, we are not supposed to do
        # that with that element, but if journal editor wants...
        journal_categories = get_journal_categories(journal_name,
                                                    this_issue_number)

    menu_out = ''

    for category in journal_categories:
        ordered_articles = get_journal_articles(
            journal_name,
            this_issue_number,
            category,
            newest_first=new_articles_first.lower() == 'yes')

        new_articles_only = False
        if ordered_articles.keys() and max(ordered_articles.keys()) < 0:
            # If there are only new articles, don't bother marking them as
            # new
            new_articles_only = True

        menu_out += '<div class="subNavigationMenu">'
        order_numbers = ordered_articles.keys()
        order_numbers.sort()
        for order_number in order_numbers:
            for article_id in ordered_articles[order_number]:
                # A record is considered as new if its position is
                # negative and there are some non-new articles
                article_is_new = (order_number < 0 and not new_articles_only)

                if str(article_id) == this_recid:
                    # Mark as active

                    # Get CSS class (if relevant)
                    notes = bfo.fields('595__a')
                    css_classes = [bfo.kb(subject_to_css_class_kb, note, None) \
                                   for note in notes]
                    css_classes = [css_class for css_class in css_classes \
                                   if css_class is not None]

                    if article_is_new:
                        css_classes.append('new')

                    separator = bfo.field('594__a')
                    if separator == "YES":
                        menu_out += '''<hr/>'''

                    menu_out += '''<div class="active">
            <div class="subNavigationMenuItem %s">%s</div></div>''' % \
                    (' '.join(css_classes),
                     this_title)

                else:
                    temp_rec = BibFormatObject(article_id)
                    title = ''
                    if ln == "fr":
                        title = temp_rec.field('246_1a')
                        if title == '':
                            title = temp_rec.field('245__a')
                    else:
                        title = temp_rec.field('245__a')
                        if title == '':
                            title = temp_rec.field('246_1a')

                    # Get CSS class (if relevant)
                    notes = temp_rec.fields('595__a')
                    css_classes = [temp_rec.kb(subject_to_css_class_kb, note, None) \
                                   for note in notes]
                    css_classes = [css_class for css_class in css_classes \
                                   if css_class is not None]

                    if article_is_new:
                        css_classes.append('new')

                    separator = temp_rec.field('594__a')
                    if separator == "YES":
                        menu_out += '''<hr/>'''

                    menu_out += '''<div class="subNavigationMenuItem %s">
                    <a href="%s">%s</a></div>
                    ''' % (' '.join(css_classes),
                           make_journal_url(
                               bfo.user_info['uri'], {
                                   'recid': article_id,
                                   'ln': bfo.lang,
                                   'category': category
                               }), title)

        menu_out += '</div>'

    return menu_out
Exemplo n.º 12
0
def format_element(bfo):
    """
    Displays the description of how users should cite
    any content of the archive. The citation includes:
    For blogs: "title".
    (record_creation_date). record_url
    Retrieved from the original "original_url"
    For blog posts: author. "title". Blog: "blog_title".
    (record_creation_date). record_url
    Retrieved from the original "original_url"
    For comments: author. Blog post: "post_title".
    (record_creation_date). record_url
    Retrieved from the original "original_url"
    """

    coll = bfo.fields('980__a')[0]
    recid = bfo.control_field('001')

    # let's get the fields we want to show
    if coll in ["BLOGPOST", "COMMENT"]:
        author = bfo.fields('100__a')[0]
        try:
            original_creation_date = bfo.fields('269__c')[0]
        except:
            original_creation_date = ""

    try:
        title = bfo.fields('245__a')[0]
    except:
        title = "Untitled"

    try:
        original_url = bfo.fields('520__u')[0]
    except:
        raise Exception("URL not found")

    # creation date of a record
    record_creation_date = get_creation_date(recid)
    # url in the archive
    record_url = CFG_SITE_URL + "/record/" + recid

    if coll == "BLOGPOST":
        # we will also show the blog's title of 
        # the corresponding blog post
        blog_recid = get_parent_blog(recid)
        blog_bfo = BibFormatObject(blog_recid)
        try:
            blog_title = blog_bfo.fields('245__a')[0]
        except:
            blog_title = 'Untitled'

        description = """<table style="border:1px solid black;"><tr><td>\
        <span><b>%s</b>. '%s'. Blog: '%s'. </br> \
        (%s). <i>'%s'</i> </br> \
        Retrieved from the original <i>'%s'</i><span></td></tr></table>""" \
        % (author, title, blog_title, record_creation_date, record_url, original_url)

    elif coll == "COMMENT":
        # we will also show the post's title of
        # the corresponding comment
        post_recid = get_parent_post(recid)
        post_bfo = BibFormatObject(post_recid)
        try:
            post_title = post_bfo.fields('245__a')[0]
        except:
            post_title = 'Untitled'

        description = """<table style="border:1px solid black;"><tr><td>\
        <span><b>%s. </b>Blog post: '%s'.</br> \
        (%s). <i>'%s'</i> </br> \
        Retrieved from the original <i>'%s'</i><span></td></tr></table>""" \
        % (author, post_title, record_creation_date, record_url, original_url)

    else: # coll == "BLOG"
        description = """<table style="border:1px solid black;"><tr><td>\
        <span>'%s' </br> \
        (%s). <i>'%s'</i> </br> \
        Retrieved from the original <i>'%s'</i><span></td></tr></table>""" \
        % (title, record_creation_date, record_url, original_url)

    out = """
        <script type="text/javascript">
        function displayCitationDescription(){
            var description = document.getElementById('description');
            var citation_link = document.getElementById('citation_link');
            if (description.style.display == 'none'){
                description.style.display = '';
                citation_link.innerHTML = "Hide citation description"
            } else {
                description.style.display = 'none';
                citation_link.innerHTML = "How to cite this"
            }
        }
        </script>
        """

    out += '<span id="description" style="">' + description + '</span>'
    out += '<a class="moreinfo" id="citation_link" \
            href="javascript:void(0)" onclick="displayCitationDescription()""></a>'
    out += '<script type="text/javascript">displayCitationDescription()</script>'

    return out
Exemplo n.º 13
0
def VoyagerHoldings(recid):

    from invenio.bibformat_engine import BibFormatObject
    from invenio.bibformat_elements import bfe_ILO_links
    from invenio.messages import gettext_set_language
    # prepare variables

    from flask import g
    bfo = BibFormatObject(recid)
    _ = gettext_set_language(g.ln)  # load the right message language
    holdings = {}
    out_table = []
    out_html = ''
    row = ''
    callno = ''
    issues_info = ''
    HQ = False
    conv = bfo.field('970__a')
    item_type = bfo.field("996__a")
    holdings = bfo.fields("964")
    kept = bfo.field("866_0a")
    kept_note = bfo.field("866_0z")
    voyager_sysno = bfo.field("970__a")
    voyager_sysno = re.sub('^LABORDOC-', '', voyager_sysno)
    links = bfe_ILO_links.format_element(bfo,
                                         style='',
                                         prefix_en='',
                                         suffix='',
                                         separator='<br />',
                                         show_icons='no',
                                         focus_on_main_file='yes')
    #request_url = '''http://golf.ilo.org/cgi-bin/Pwebrecon.cgi?bbid=%s&BIB=%s&PAGE=REQUESTBIB" onclick="newWin(this.href); return false;''' % (voyager_sysno, voyager_sysno)
    request_url = '''http://ringo.ilo.org:7008/vwebv/patronRequests?&sk=en_ILO&bibId=%s" onclick="newWin(this.href); return false;''' % (
        voyager_sysno)

    # Start of the html, request text and link plus holdings table headings

    out_html = """  
         <script language="JavaScript" src="http://www.ilo.org/webcommon/s-includes/popups.js" type="text/javascript"></script>
         <div class="span8">
             <div>
             ILO staff may request this item by clicking on the blue Request button. Enter your client number and
             last name and click Log in. If you do not have a Client number, contact the Loans Service
             at tel. +(4122)799 8705; Email:
             <a href="mailto:[email protected]">[email protected]</a><br /><br />
             If you are not ILO staff and would like to see this item, you may wish to contact your library to
             ask for an inter-library loan or visit an ILO Library near you.
             </div>"""

    out_html += """<div class="pull-left tableHoldings">
                 <table>
                 <thead>
                 <tr>
                 <th>%s</th>
                 <th>%s</th>
                 </tr>
                 </thead>
                 <tbody>
             """ % ("Location", "Call Number")

    # iterate through holdings and make rows for the table

    for out in holdings:
        location = str(out.get("m", " "))
        callno = str(out.get("e", " "))
        if callno.find('NYP') >= 1:
            callno = callno.replace('NYP', 'not yet published')
        callno = ' ' + callno + ' '
        if callno.find('NYP') >= 1:
            callno = callno.replace('NYP', 'not yet published')
        row = "<tr><td>" + location + "</td><td>" + callno + "</td></tr>"
        if row.find('Main collection') >= 1 and HQ == False:
            row = row.replace('Library - Main collection', 'HQ Library')
            if kept != 0 or kept_note != 0:
                issues_info = 'HQ Library' + kept + '&nbsp;' + kept_note
                row = row.replace('HQ Library', issues_info)
            out_table.append(row)
            HQ = True
        elif row.find('Electronic') >= 1:
            links = bfe_ILO_links.format_element(bfo,
                                                 style='',
                                                 prefix_en='',
                                                 suffix='',
                                                 separator='<br /',
                                                 show_icons='no',
                                                 focus_on_main_file='yes')
            links = '</td><td>' + links + '</td></tr>'
            row = re.sub('</td><td>.*</td></tr>', links, row)
            out_table.append(row)
        elif row.find('Main collection') >= 1 and HQ:
            pass
        else:
            out_table.append(row)

    if conv.startswith('ILOCONV'):
        row = "<tr><td>" + 'Electronic documents' + "</td><td>" + links + "</td></tr>"
        out_table.append(row)

    out_table.sort()
    out_html += ''.join(out_table) + '</tbody></table></div>'
    out_html += """<div class="span2 requestButton"> <a href="%s">
                   <h4><i class="icon-book"> </i>  %s </h4></a> </div></div>""" % (
        request_url, _("Request item"))

    return out_html
def format_element(bfo, latest_issue_only='yes', newest_articles_only='yes',
           link_category_headers='yes', display_categories='', hide_when_only_new_records="no"):
    """
    Display the index to the newest articles (of the latest issue, or of the displayed issue)

    @param latest_issue_only: if 'yes', always display articles of the latest issue, even if viewing a past issue
    @param newest_articles_only: only display new articles, not those that also appeared in previous issues
    @param link_category_headers: if yes, category headers link to index page of that category
    @param display_categories: comma-separated list of categories to display. If none, display all
    @param hide_when_only_new_records: if 'yes' display new articles only if old articles exist in this issue
    """
    args = parse_url_string(bfo.user_info['uri'])
    journal_name = args["journal_name"]
    ln = args["ln"]
    _ = gettext_set_language(ln)

    if latest_issue_only.lower() == 'yes':
        issue_number = get_current_issue(bfo.lang, journal_name)
    else:
        issue_number = args["issue"]

    # Try to get HTML from cache
    if args['verbose'] == 0:
        cached_html = _get_whatsNew_from_cache(journal_name, issue_number, ln)
        if cached_html:
            return cached_html

    # No cache? Build from scratch
    # 1. Get the articles
    journal_categories = get_journal_categories(journal_name,
                                                issue_number)
    if display_categories:
        display_categories = display_categories.lower().split(',')
        journal_categories = [category for category in journal_categories \
                              if category.lower() in display_categories]
    whats_new_articles = {}
    for category in journal_categories:
        whats_new_articles[category] = get_journal_articles(journal_name,
                                                            issue_number,
                                                            category,
                                                            newest_only=newest_articles_only.lower() == 'yes')

    # Do we want to display new articles only if they have been added
    # to an issue that contains non-new records?
    if hide_when_only_new_records.lower() == "yes":
        # First gather all articles in this issue
        all_whats_new_articles = {}
        for category in journal_categories:
            all_whats_new_articles[category] = get_journal_articles(journal_name,
                                                                    issue_number,
                                                                    category,
                                                                    newest_first=True,
                                                                    newest_only=False)
        # Then check if we have some articles at position > -1
        has_old_articles = False
        for articles in all_whats_new_articles.values():
            if len([order for order in articles.keys() if order > -1]) > 0:
                has_old_articles = True
                break
        if not has_old_articles:
            # We don't have old articles? Thend don't consider any
            for category in journal_categories:
                whats_new_articles[category] = {}

    # 2. Build the HTML
    html_out = u''
    html_out += _get_breaking_news(ln, journal_name)
    for category in journal_categories:
        articles_in_category = whats_new_articles[category]
        html_articles_in_category = u""
        # Generate the list of articles in this category
        order_numbers = articles_in_category.keys()
        order_numbers.sort()
        for order in order_numbers:
            articles = articles_in_category[order]
            for recid in articles:
                link = make_journal_url(bfo.user_info['uri'], {'journal_name': journal_name,
                                                               'issue_number': issue_number.split('/')[0],
                                                               'issue_year': issue_number.split('/')[1],
                                                               'category': category,
                                                               'recid': recid,
                                                               'ln': bfo.lang})
                temp_rec = BibFormatObject(recid)
                if ln == 'fr':
                    try:
                        title = temp_rec.fields('246_1a')[0]
                    except:
                        continue
                else:
                    try:
                        title = temp_rec.field('245__a')
                    except:
                        continue
                try:
                    html_articles_in_category += u'<li><a href="%s">%s</a></li>' % \
                                                 (link, title.decode('utf-8'))
                except:
                    pass

        if html_articles_in_category:
            # Good, we found some new articles for this category.
            # Then insert the genereated results into a larger list
            # with category as "parent".
            html_out += '<li>'
            if link_category_headers.lower() == 'yes':
                html_out += '<a href="'
                html_out += make_journal_url(bfo.user_info['uri'],
                                             {'journal_name': journal_name,
                                              'issue_number': issue_number.split('/')[0],
                                              'issue_year': issue_number.split('/')[1],
                                              'category': category,
                                              'recid': '',
                                              'ln': bfo.lang})
                html_out += '" class="whatsNewCategory">%s</a>' % category
            else:
                html_out += '<span class="whatsNewCategory">%s</span>' % category

            html_out += '<ul class="whatsNewItem">'
            html_out += html_articles_in_category
            html_out += '</ul></li>'

    if not html_out:
        html_out = '<i>' + _('There are no new articles for the moment') + '</i>'
    else:
        html_out = '<ul class="whatsNew">' + html_out + '</ul>'

    if args['verbose'] == 0:
        cache_whatsNew(html_out.encode('utf-8'), journal_name, issue_number, ln)

    return html_out.encode('utf-8')
Exemplo n.º 15
0
def format_element(bfo, new_articles_first='yes',
           subject_to_css_class_kb="WebJournalSubject2CSSClass",
           display_all_category_articles='no', display_category_title='no'):
    """
    List all articles one after the other, on the same page.

    Similar to bfe_webjournal_articles_overview, but displays full articles.

    Note that you cannot use both bfe_webjournal_articles_overview and
    bfe_webjournal_articles: you have to choose one of them, as they
    use the same cache location (It would also not make sense to use
    both...).

    @param new_articles_first: if 'yes', display new articles before other articles
    @param subject_to_css_class_kb: knowledge base that maps 595__a to a CSS class
    @param display_all_category_articles: if yes, display all articles, whatever category is selected
    @param display_category_title: if yes, display category title (useful if display_all_category_articles is enabled)

    @see: bfe_webjournal_articles_overview.py
    """
    args = parse_url_string(bfo.user_info['uri'])
    journal_name = args["journal_name"]
    this_issue_number = args["issue"]
    category_name = args["category"]
    verbose = args["verbose"]
    ln = bfo.lang
    _ = gettext_set_language(ln)

    # Try to get the page from cache. Only if issue is older or equal
    # to latest release.
    latest_released_issue = get_current_issue(ln, journal_name)
    if verbose == 0 and not issue_is_later_than(this_issue_number,
                                                latest_released_issue):
        cached_html = get_index_page_from_cache(journal_name, category_name,
                                                this_issue_number, ln)
        if cached_html:
            return cached_html

    # Shall we display current category, or all?
    categories = [category_name]
    if display_all_category_articles.lower() == 'yes':
        categories = get_journal_categories(journal_name,
                                            this_issue_number)

    out = ''
    for category_name in categories:
        if display_category_title.lower() == 'yes':
            out += '<h2>' + _(category_name) + '</h2>'

        out += '<table border="0" cellpadding="0" cellspacing="0">'
        # Get the id list
        ordered_articles = get_journal_articles(journal_name,
                                                this_issue_number,
                                                category_name,
                                                newest_first=new_articles_first.lower() == 'yes')
        new_articles_only = False
        if ordered_articles.keys() and max(ordered_articles.keys()) < 0:
            # If there are only new articles, don't bother marking them as
            # new
            new_articles_only = True

        order_numbers = ordered_articles.keys()
        order_numbers.sort()

        for order_number in order_numbers:
            for article_id in ordered_articles[order_number]:
                # A record is considered as new if its position is
                # negative and there are some non-new articles
                article_is_new = (order_number < 0 and not new_articles_only)

                temp_rec = BibFormatObject(article_id)
                title = ''
                if ln == "fr":
                    title = temp_rec.field('246_1a')
                    if title == '':
                        title = temp_rec.field('245__a')
                else:
                    title = temp_rec.field('245__a')
                    if title == '':
                        title = temp_rec.field('246_1a')

                # Get CSS class (if relevant)
                notes = temp_rec.fields('595__a')
                css_classes = [temp_rec.kb(subject_to_css_class_kb, note, None) \
                               for note in notes]
                css_classes = [css_class for css_class in css_classes \
                               if css_class is not None]

                if article_is_new:
                    css_classes.append('new')

                # Finally create the output. Two different outputs
                # depending on if we have text to display or not
                text = []
                if ln == "fr":
                    text = temp_rec.fields('590__b')
                    if not text or \
                           (len(text) == 1 and \
                            (text[0].strip() in ['', '<br />', '<!--HTML--><br />'])):
                        text = temp_rec.fields('520__b')
                else:
                    text = temp_rec.fields('520__b')
                    if not text or \
                           (len(text) == 1 and \
                            (text[0].strip() in ['', '<br />', '<!--HTML--><br />'])):
                        text = temp_rec.fields('590__b')
                text = '<br/>'.join(text)


                out += '''
                            <tr><td class="article">
                               <h%(header_tag_size)s class="%(css_classes)s articleTitle" style="clear:both;">
                                   %(title)s
                               </h%(header_tag_size)s>
                               <div class="articleBody">
                                   <div class="articleText">
                                      %(text)s
                                   </div>
                               </div>
                           </td></tr>
                        ''' % {'title': title,
                               'text': text,
                               'header_tag_size': (display_category_title.lower() == 'yes') and '3' or '2',
                               'css_classes': ' '.join(css_classes)}
        out += '</table>'

    if verbose == 0 and not CFG_ACCESS_CONTROL_LEVEL_SITE == 2 :
        cache_index_page(out, journal_name, category_name,
                         this_issue_number, ln)

    return out
def format_marcxml_file(marcxml, is_file=False):
    '''
        Parse the given marcxml file to retreive the metadata needed by the
        forward of the document to ArXiv.org
        @param marcxml: marxml file that contains metadata from Invenio
        @return: (dictionnary) couple of key value needed for the push
    '''

    #init the return tuple
    marcxml_values = { 'id'            : '',
                       'title'         : '',
                       'summary'       : '',
                       'contributors'  : [],
                       'journal_refs'  : [],
                       'report_nos'    : [],
                       'comment'       : '',
                       'doi'           : '' }

    # check if the marcxml is not empty
    if marcxml == '':
        marcxml_values['error'] = "MARCXML string is empty !"
        return marcxml_values

    #get the tag id and code from tag table
    main_report_number = CFG_MARC_REPORT_NUMBER
    add_report_number = CFG_MARC_ADDITIONAL_REPORT_NUMBER
    main_title = CFG_MARC_TITLE
    main_summary = CFG_MARC_ABSTRACT
    main_author = CFG_MARC_AUTHOR_NAME
    main_author_affiliation = CFG_MARC_AUTHOR_AFFILIATION
    add_author = CFG_MARC_CONTRIBUTOR_NAME
    add_author_affiliation = CFG_MARC_CONTRIBUTOR_AFFILIATION
    main_comment = CFG_MARC_COMMENT
    doi = CFG_MARC_DOI
    journal_ref_code = CFG_MARC_JOURNAL_REF_CODE
    journal_ref_title = CFG_MARC_JOURNAL_REF_TITLE
    journal_ref_page = CFG_MARC_JOURNAL_REF_PAGE
    journal_ref_year = CFG_MARC_JOURNAL_REF_YEAR

    #init tmp values
    contributor = {'name' : '', 'email' : '', 'affiliation' : []}

    try:
        bfo = BibFormatObject(recID=None, xml_record=marcxml)
    except:
        marcxml_values['error'] = "Unable to open marcxml file !"
        return marcxml_values

    marcxml_values = { 'id'           : bfo.field(main_report_number),
                       'title'        : bfo.field(main_title),
                       'summary'      : bfo.field(main_summary),
                       'report_nos'   : bfo.fields(add_report_number),
                       'contributors' : [],
                       'journal_refs' : [],
                       'comment'      : bfo.field(main_comment),
                       'doi'          : bfo.field(doi)}

    authors = bfo.fields(main_author[:-1], repeatable_subfields_p=True)
    for author in authors:
        name = author.get(main_author[-1], [''])[0]
        affiliation = author.get(main_author_affiliation[-1], [])
        author = {'name': name, 'email': '', 'affiliation': affiliation}
        marcxml_values['contributors'].append(author)

    authors = bfo.fields(add_author[:-1], repeatable_subfields_p=True)
    for author in authors:
        name = author.get(add_author[-1], [''])[0]
        affiliation = author.get(add_author_affiliation[-1], [])
        author = {'name': name, 'email': '', 'affiliation': affiliation}
        marcxml_values['contributors'].append(author)

    journals = bfo.fields(journal_ref_title[:-1])
    for journal in journals:
        journal_title = journal.get(journal_ref_title[-1], '')
        journal_page = journal.get(journal_ref_page[-1], '')
        journal_code = journal.get(journal_ref_code[-1], '')
        journal_year = journal.get(journal_ref_year[-1], '')
        journal = "%s: %s (%s) pp. %s" % (journal_title, journal_code, journal_year, journal_page)
        marcxml_values['journal_refs'].append(journal)

    return marcxml_values
Exemplo n.º 17
0
def format_element(bfo,
                   latest_issue_only='yes',
                   newest_articles_only='yes',
                   link_category_headers='yes',
                   display_categories='',
                   hide_when_only_new_records="no"):
    """
    Display the index to the newest articles (of the latest issue, or of the displayed issue)

    @param latest_issue_only: if 'yes', always display articles of the latest issue, even if viewing a past issue
    @param newest_articles_only: only display new articles, not those that also appeared in previous issues
    @param link_category_headers: if yes, category headers link to index page of that category
    @param display_categories: comma-separated list of categories to display. If none, display all
    @param hide_when_only_new_records: if 'yes' display new articles only if old articles exist in this issue
    """
    args = parse_url_string(bfo.user_info['uri'])
    journal_name = args["journal_name"]
    ln = args["ln"]
    _ = gettext_set_language(ln)

    if latest_issue_only.lower() == 'yes':
        issue_number = get_current_issue(bfo.lang, journal_name)
    else:
        issue_number = args["issue"]

    # Try to get HTML from cache
    if args['verbose'] == 0:
        cached_html = _get_whatsNew_from_cache(journal_name, issue_number, ln)
        if cached_html:
            return cached_html

    # No cache? Build from scratch
    # 1. Get the articles
    journal_categories = get_journal_categories(journal_name, issue_number)
    if display_categories:
        display_categories = display_categories.lower().split(',')
        journal_categories = [category for category in journal_categories \
                              if category.lower() in display_categories]
    whats_new_articles = {}
    for category in journal_categories:
        whats_new_articles[category] = get_journal_articles(
            journal_name,
            issue_number,
            category,
            newest_only=newest_articles_only.lower() == 'yes')

    # Do we want to display new articles only if they have been added
    # to an issue that contains non-new records?
    if hide_when_only_new_records.lower() == "yes":
        # First gather all articles in this issue
        all_whats_new_articles = {}
        for category in journal_categories:
            all_whats_new_articles[category] = get_journal_articles(
                journal_name,
                issue_number,
                category,
                newest_first=True,
                newest_only=False)
        # Then check if we have some articles at position > -1
        has_old_articles = False
        for articles in all_whats_new_articles.values():
            if len([order for order in articles.keys() if order > -1]) > 0:
                has_old_articles = True
                break
        if not has_old_articles:
            # We don't have old articles? Thend don't consider any
            for category in journal_categories:
                whats_new_articles[category] = {}

    # 2. Build the HTML
    html_out = _get_breaking_news(ln, journal_name)
    for category in journal_categories:
        articles_in_category = whats_new_articles[category]
        html_articles_in_category = ""
        # Generate the list of articles in this category
        order_numbers = articles_in_category.keys()
        order_numbers.sort()
        for order in order_numbers:
            articles = articles_in_category[order]
            for recid in articles:
                link = make_journal_url(
                    bfo.user_info['uri'], {
                        'journal_name': journal_name,
                        'issue_number': issue_number.split('/')[0],
                        'issue_year': issue_number.split('/')[1],
                        'category': category,
                        'recid': recid,
                        'ln': bfo.lang
                    })
                temp_rec = BibFormatObject(recid)
                if ln == 'fr':
                    try:
                        title = temp_rec.fields('246_1a')[0]
                    except:
                        continue
                else:
                    try:
                        title = temp_rec.field('245__a')
                    except:
                        continue
                try:
                    html_articles_in_category += '<li><a href="%s">%s</a></li>' % \
                                                 (link, title)
                except:
                    pass

        if html_articles_in_category:
            # Good, we found some new articles for this category.
            # Then insert the genereated results into a larger list
            # with category as "parent".
            html_out += '<li>'
            if link_category_headers.lower() == 'yes':
                html_out += '<a href="'
                html_out += make_journal_url(
                    bfo.user_info['uri'], {
                        'journal_name': journal_name,
                        'issue_number': issue_number.split('/')[0],
                        'issue_year': issue_number.split('/')[1],
                        'category': category,
                        'recid': '',
                        'ln': bfo.lang
                    })
                html_out += '" class="whatsNewCategory">%s</a>' % category
            else:
                html_out += '<span class="whatsNewCategory">%s</span>' % category

            html_out += '<ul class="whatsNewItem">'
            html_out += html_articles_in_category
            html_out += '</ul></li>'

    if not html_out:
        html_out = '<i>' + _(
            'There are no new articles for the moment') + '</i>'
    else:
        html_out = '<ul class="whatsNew">' + html_out + '</ul>'

    if args['verbose'] == 0:
        cache_whatsNew(html_out, journal_name, issue_number, ln)

    return html_out
def format_element(bfo, number_of_featured_articles="1",
           number_of_articles_with_image="3", new_articles_first='yes',
           image_px_width="300", small_image_px_width="200",
           subject_to_css_class_kb="WebJournalSubject2CSSClass",
           link_image_to_article='yes', image_alignment='left'):
    """
    Creates an overview of all the articles of a certain category in one
    specific issue.

    Note the following:
    <ul>
    <li>The element consider only the latest issue: when viewing
    archives of your journal, readers will see the newest articles of
    the latest issue, not the ones of the issue they are looking
    at</li>

    <li>This is not an index of the articles of the latest issue: it
    display only <b>new</b> articles, that is articles that have never
    appeared in a previous issue</li>

    <li>This element produces a table-based layout, in order to have a
    more or less readable HTML alert when sent some Email clients
    (Outlook 2007)</li>

    <li>When producing the HTML output of images, this element tries to
    insert the width and height attributes to the img tag: this is
    necessary in order to produce nice HTML alerts. This dimension
    therefore overrides any dimension defined in the CSS. The Python
    Image Library (PIL) should be installed for this element to
    recognize the size of images.</li>
    </ul>

    @param number_of_featured_articles: the max number of records with emphasized title
    @param number_of_articles_with_image: the max number of records for which their image is displayed
    @param new_articles_first: if 'yes', display new articles before other articles
    @param image_px_width: (integer) width of first image featured on this page
    @param small_image_px_width: (integer) width of small images featured on this page
    @param subject_to_css_class_kb: knowledge base that maps 595__a to a CSS class
    @param link_image_to_article: if 'yes', link image (if any) to article
    @param image_alignment: 'left', 'center' or 'right'. To help rendering in Outlook.
    """
    args = parse_url_string(bfo.user_info['uri'])
    journal_name = args["journal_name"]
    this_issue_number = args["issue"]
    category_name = args["category"]
    verbose = args["verbose"]
    ln = bfo.lang
    _ = gettext_set_language(ln)

    if image_px_width.isdigit():
        image_px_width = int(image_px_width)
    else:
        image_px_width = None
    if small_image_px_width.isdigit():
        small_image_px_width = int(small_image_px_width)
    else:
        small_image_px_width = None

    # We want to put emphasis on the n first articles (which are not
    # new)
    if number_of_featured_articles.isdigit():
        number_of_featured_articles = int(number_of_featured_articles)
    else:
        number_of_featured_articles = 0

    # Only n first articles will display images
    if number_of_articles_with_image.isdigit():
        number_of_articles_with_image = int(number_of_articles_with_image)
    else:
        number_of_articles_with_image = 0

    # Help image alignement without CSS, to have better rendering in Outlook
    img_align = ''
    if image_alignment:
        img_align = 'align="%s"' % image_alignment

    # Try to get the page from cache. Only if issue is older or equal
    # to latest release.
    latest_released_issue = get_current_issue(ln, journal_name)
    if verbose == 0 and not issue_is_later_than(this_issue_number,
                                                latest_released_issue):
        cached_html = get_index_page_from_cache(journal_name, category_name,
                                                this_issue_number, ln)
        if cached_html:
            return cached_html

    out = '<table border="0" cellpadding="0" cellspacing="0">'
    # Get the id list
    ordered_articles = get_journal_articles(journal_name,
                                            this_issue_number,
                                            category_name,
                                            newest_first=new_articles_first.lower() == 'yes')
    new_articles_only = False
    if ordered_articles.keys() and max(ordered_articles.keys()) < 0:
        # If there are only new articles, don't bother marking them as
        # new
        new_articles_only = True

    order_numbers = ordered_articles.keys()
    order_numbers.sort()
    img_css_class = "featuredImageScale"

    for order_number in order_numbers:
        for article_id in ordered_articles[order_number]:
            # A record is considered as new if its position is
            # negative and there are some non-new articles
            article_is_new = (order_number < 0 and not new_articles_only)

            temp_rec = BibFormatObject(article_id)
            title = ''
            if ln == "fr":
                title = temp_rec.field('246_1a')
                if title == '':
                    title = temp_rec.field('245__a')
            else:
                title = temp_rec.field('245__a')
                if title == '':
                    title = temp_rec.field('246_1a')

            # Get CSS class (if relevant)
            notes = temp_rec.fields('595__a')
            css_classes = [temp_rec.kb(subject_to_css_class_kb, note, None) \
                           for note in notes]
            css_classes = [css_class for css_class in css_classes \
                           if css_class is not None]

            if article_is_new:
                css_classes.append('new')

            # Maybe we want to force image to appear?
            display_image_on_index = False
            if 'display_image_on_index' in notes:
                display_image_on_index = True

            # Build generic link to this article
            article_link = make_journal_url(bfo.user_info['uri'], {'recid':str(article_id),
                                                                   'ln': bfo.lang})

            # Build the "more" link
            more_link = '''<a class="readMore" title="link to the article" href="%s"> &gt;&gt; </a>
                        ''' % (article_link)

            # If we should display an image along with the text,
            # prepare it here
            img = ''
            if (number_of_articles_with_image > 0 and \
                   not article_is_new) or display_image_on_index:
                img = _get_feature_image(temp_rec, ln)
                if img != "":
                    # Now we will try to identify image size in order
                    # to resize it in the HTML for a nicer rendering
                    # of the HTML alert in email clients (Outlook wants
                    # both height and width)
                    img_width = None
                    img_height = None
                    small_img_width = None
                    small_img_height = None
                    width_and_height = ''
                    if PIL_imported:
                        try:
                            local_img = os.path.join(CFG_TMPDIR,
                                                     'webjournal_' + \
                                                     ''.join([char for char in img \
                                                              if char.isalnum()]))
                            if len(local_img) > 255:
                                # Shorten to 255 chars
                                local_img = local_img[0:100] + '_' + local_img[156:]
                            if not os.path.exists(local_img):
                                # Too bad, must download entire image for PIL
                                (local_img, headers) = urllib.urlretrieve(img, local_img)
                            img_file = Image.open(local_img)
                        except IOError, e:
                            pass
                        else:
                            orig_img_width = img_file.size[0]
                            orig_img_height = img_file.size[1]
                            # Then scale according to user-defined width
                            ## First image
                            ratio = float(orig_img_width) / image_px_width
                            img_width = image_px_width
                            img_height = int(orig_img_height / ratio)
                            ## Other smaller images
                            ratio = float(orig_img_width) / small_image_px_width
                            small_img_width = small_image_px_width
                            small_img_height = int(orig_img_height / ratio)

                    # Note that we cannot reuse the nice phl, ph and
                    # phr classes to put a frame around the image:
                    # this is not supported in Outlook 2007 when HTML
                    # alert is sent.
                    if not img_css_class == "featuredImageScale":
                        # Not first image: display smaller
                        img_width = small_img_width
                        img_height = small_img_height

                    if img_width and img_height:
                        width_and_height = 'width="%i" height="%i"' % \
                                           (img_width, img_height)
                    img = '<img alt="" class="%s" src="%s" %s %s/>' % \
                          (img_css_class, img, img_align, width_and_height)
                    number_of_articles_with_image -= 1

                    # Next images will be displayed smaller
                    img_css_class = "featuredImageScaleSmall"

            # Determine size of the title
            header_tag_size = '3'
            if number_of_featured_articles > 0 and \
                   not article_is_new:
                # n first articles are especially featured
                header_tag_size = '2'
                number_of_featured_articles -= 1

            # Finally create the output. Two different outputs
            # depending on if we have text to display or not
            text = ''
            if not article_is_new:
                text = _get_feature_text(temp_rec, ln)
            # Link image to article if wanted
            if link_image_to_article.lower() == 'yes':
                img = create_html_link(urlbase=article_link,
                                       link_label=img,
                                       urlargd={})
            if text != '':
                out += '''
                        <tr><td class="article">
                           <h%(header_tag_size)s class="%(css_classes)s articleTitle" style="clear:both;">
                               <a title="link to the article" href="%(article_link)s">%(title)s</a>
                           </h%(header_tag_size)s>
                           <div class="articleBody">
                               %(img)s
                               %(text)s
                               %(more_link)s
                           </div>
                       </td></tr>
                    ''' % {'article_link': article_link,
                           'title': title,
                           'img': img,
                           'text': text,
                           'more_link': more_link,
                           'css_classes': ' '.join(css_classes),
                           'header_tag_size': header_tag_size}
            else:
                out += '''
                       <tr><td class="article">
                           <h%(header_tag_size)s class="%(css_classes)s articleTitle" style="clear:both;">
                               <a title="link to the article" href="%(article_link)s">%(title)s</a>&nbsp;&nbsp;
                               %(more_link)s
                           </h%(header_tag_size)s>
                           %(img)s
                       </td></tr>
                       ''' % {'article_link': article_link,
                              'title': title,
                              'more_link': more_link,
                              'img': img,
                              'css_classes': ' '.join(css_classes),
                              'header_tag_size': header_tag_size}
Exemplo n.º 19
0
def format_element(bfo,
                   limit,
                   separator='; ',
                   extension='[...]',
                   print_links="yes",
                   print_affiliations='no',
                   affiliation_prefix=' (',
                   affiliation_suffix=')',
                   print_affiliation_first='no',
                   interactive="no",
                   highlight="no",
                   affiliations_separator=" ; ",
                   name_last_first="yes",
                   collaboration="yes",
                   id_links="no",
                   markup="html",
                   link_extension="no",
                   suffix=''):
    """
    Prints the list of authors of a record.

    @param limit the maximum number of authors to display
    @param separator the separator between authors.
    @param extension a text printed if more authors than 'limit' exist
    @param print_links if yes, prints the authors as HTML link to their publications
    @param print_affiliations if yes, make each author name followed by its affiliation
    @param affiliation_prefix prefix printed before each affiliation
    @param affiliation_suffix suffix printed after each affiliation
    @param print_affiliation_first if 'yes', affiliation is printed before the author
    @param interactive if yes, enable user to show/hide authors when there are too many (html + javascript)
    @param highlight highlights authors corresponding to search query if set to 'yes'
    @param affiliations_separator separates affiliation groups
    @param name_last_first if yes (default) print last, first  otherwise first last
    @param collaboration if yes (default) uses collaboration name in place of long author list, if available
    @param id_links if yes (default = no) prints link based on INSPIRE IDs if available - only used if print_links = yes
    @param markup html (default) or latex controls small markup differences
    @param link_extension if 'yes' link the extension to the detailed
    record page

    """
    from urllib import quote
    from cgi import escape
    import re
    from invenio.messages import gettext_set_language
    from invenio.config import CFG_BASE_URL, CFG_SITE_RECORD
    from invenio.bibformat_engine import BibFormatObject

    _ = gettext_set_language(bfo.lang)  # load the right message language

    #regex for parsing last and first names and initials
    re_last_first = re.compile(
        '^(?P<last>[^,]+)\s*,\s*(?P<first_names>[^\,]*)(?P<extension>\,?.*)$')
    re_initials = re.compile(r'(?P<initial>\w)(\w+|\.)\s*')
    re_coll = re.compile(r'\s*collaborations?', re.IGNORECASE)

    bibrec_id = bfo.control_field("001")
    authors = []
    lastauthor = ''

    # HepData and only-INSPIRE data records inherit the list of authors from the original paper
    if (bfo.field("520__9") == "HEPDATA") or (bfo.field("520__9")
                                              == "INSPIRE"):
        parent_recid = bfo.field("786__w")
        bfo_parent = BibFormatObject(int(parent_recid))

        authors = []
        authors_1 = bfo_parent.fields('100__', repeatable_subfields_p=True)
        authors_2 = bfo_parent.fields('700__', repeatable_subfields_p=True)
    # other datasources should have a list of authors
    else:
        authors = []
        authors_1 = bfo.fields('100__', repeatable_subfields_p=True)
        authors_2 = bfo.fields('700__', repeatable_subfields_p=True)

    authors.extend(authors_1)
    authors.extend(authors_2)

    # If there are no author check for corporate author in 110__a field
    if len(authors) == 0:
        authors = bfo.fields('110__', repeatable_subfields_p=True)
        # For corporate authors we don't want to reverse names order
        name_last_first = 'yes'
        # And we don't want to create links
        print_links = 'no'

    # Keep real num of authors. fix + affiliations_separator.join(author['u']) + \
    nb_authors = len(authors)

    # Limit num of authors, so that we do not process
    # the authors that will not be shown. This can only
    # be done in non-interactive mode, as interactive mode
    # allows to show all of them.
    if limit.isdigit() and nb_authors > int(limit) \
           and interactive != "yes":
        if bfo.field('710g'):  # check for colln note
            authors = authors[:1]
        else:

            authors = authors[:int(limit)]

    # Process authors to add link, affiliation and highlight
    for author in authors:

        if author.has_key('a'):
            author['a'] = author['a'][0]  # There should not be
            # repeatable subfields here.
            if highlight == 'yes':
                from invenio import bibformat_utils
                author['a'] = bibformat_utils.highlight(
                    author['a'], bfo.search_pattern)

            #check if we need to reverse last, first
            #we don't try to reverse it if it isn't stored with a comma.
            first_last_match = re_last_first.search(author['a'])
            author['display'] = author['a']

            if name_last_first.lower() == "no":
                if first_last_match:
                    author['display'] = first_last_match.group('first_names') + \
                                        ' ' + \
                                        first_last_match.group('last') + \
                                        first_last_match.group('extension')

            #for latex we do initials only  (asn assume first last)
            if markup == 'latex':
                if first_last_match:
                    first = re_initials.sub('\g<initial>.~', \
                                        first_last_match.group('first_names'))
                    author['display'] = first  + \
                                        first_last_match.group('last') + \
                                        first_last_match.group('extension')

            if print_links.lower() == "yes":

                # if there is an ID, search using that.
                id_link = ''
                if id_links == "yes" and author.has_key('i'):
                    author['i'] = author['i'][0]  #possible to have more IDs?
                    id_link = '<a class="authoridlink" href="' + \
                              CFG_BASE_URL + \
                              '/search?' + \
                              'ln='+ bfo.lang + \
                              '&amp;p=100__i' + escape(':' + author['i']) + \
                              '+or+700__i' + escape(':' + author['i']) +\
                              '">'+escape("(ID Search)") + '</a> '


                author['display'] = '<a class="authorlink" href="' + \
                                    CFG_BASE_URL + \
                                    '/author/'+ quote(author['a']) + \
                                    '?recid=' + bibrec_id + \
                                    '&amp;ln='+ bfo.lang + \
                                    '">' + escape(author['display'])+'</a>' + \
                                    id_link

        if print_affiliations == "yes":
            if author.has_key('e'):
                author['e'] = affiliation_prefix + \
                              affiliations_separator.join(author['e']) + \
                              affiliation_suffix

            if author.has_key('u'):
                author['ilink'] = ['<a class="afflink" href="' + \
                                   CFG_BASE_URL + '/search?cc=Institutions&amp;p=institution:'+ \
                                   quote('"' + string + '"') + \
                                   '&amp;ln=' + bfo.lang + \
                                   '">' + \
                                   string.lstrip() + \
                                   '</a>' for string in author['u']]
                author['u'] = affiliation_prefix + \
                              affiliations_separator.join(author['ilink']) + \
                              affiliation_suffix

#
#  Consolidate repeated affiliations
#
    last = ''
    authors.reverse()
    for author in authors:
        if not author.has_key('u'):
            author['u'] = ''
        #print 'this->'+ author['a']+'\n'
        if last == author['u']:
            author['u'] = ''
        else:
            last = author['u']

    authors.reverse()

    # Flatten author instances
    if print_affiliations == 'yes':
        ##      100__a (100__e)  700__a (100__e) (100__u)
        if print_affiliation_first.lower() != 'yes':
            authors = [
                author.get('display', '') + author.get('e', '') +
                author.get('u', '') for author in authors
            ]

        else:
            authors = [
                author.get('u', '') + author.get('display', '')
                for author in authors
            ]

    else:
        authors = [author.get('display', '') for author in authors]

    # link the extension to detailed record
    if link_extension == 'yes' and interactive != 'yes':
        extension = '<a class="authorlink" href="' +  \
                    CFG_BASE_URL + '/' + CFG_SITE_RECORD + '/' + str(bfo.recID) + '">' + \
                    extension + '</a>'

    # Detect Collaborations:
    if collaboration == "yes":
        colls = []
        for coll in bfo.fields("710__g"):
            if coll not in colls:
                colls.append(coll)
    else:
        colls = []
    if colls:
        short_coll = False
        colls = [re_coll.sub('', coll) for coll in colls]
        if print_links.lower() == "yes":
            colls = [
                '<a class="authorlink" href="' + CFG_BASE_URL + '/search' +
                '?p=collaboration:' + quote("'" + coll + "'") + '&amp;ln=' +
                bfo.lang + '">' + escape(coll) + '</a>' for coll in colls
            ]

        coll_display = " and ".join(colls)
        if not coll_display.endswith("aboration"):
            coll_display += " Collaboration"
            if len(colls) > 1:
                coll_display += 's'
        if nb_authors > 1:
            if markup == 'latex':
                coll_display =  authors[0] + extension + "  [" + \
                               coll_display + "]"
            elif interactive == "yes":
                coll_display += " (" + authors[0] + " "
                extension += ")"
            else:  #html
                coll_display += " (" + authors[0] + extension + ")"
        elif nb_authors == 1:
            short_coll = True
            if markup == 'latex':
                coll_display = authors[0] + " [" + coll_display + "]"
            else:  #html
                coll_display += " (" + authors[0] + " for the collaboration)"
        elif nb_authors == 0:
            short_coll = True
            if markup == 'latex':
                coll_display = "[" + coll_display + "]"

    # Start outputting, depending on options and number of authors
    if colls and (interactive != "yes" or short_coll):
        return coll_display

    if limit.isdigit() and nb_authors > int(limit) and interactive != "yes":
        if markup == 'latex':
            lastauthor = authors.pop()
            lastauthor = ' and ' + lastauthor
            limit = int(limit) - 1

        return separator.join(authors[:int(limit)]) + lastauthor + \
               extension

    elif interactive == "yes" and (
        (colls and not short_coll) or
        (limit.isdigit() and nb_authors > int(limit))):
        out = '''
        <script>
        function toggle_authors_visibility(){
            var more = document.getElementById('more');
            var link = document.getElementById('link');
            var extension = document.getElementById('extension');
            if (more.style.display=='none'){
                more.style.display = '';
                extension.style.display = 'none';
                link.innerHTML = "%(show_less)s"
            } else {
                more.style.display = 'none';
                extension.style.display = '';
                link.innerHTML = "%(show_more)s"
            }
            link.style.color = "rgb(204,0,0);"
        }

        function set_up(){
            var extension = document.getElementById('extension');
            extension.innerHTML = '%(extension)s';
            toggle_authors_visibility();
        }

        </script>
        ''' % {
            'show_less': _("Hide"),
            'show_more': _("Show all %i authors") % nb_authors,
            'extension': extension
        }

        #        out += '<a name="show_hide" />'
        if colls:
            show = coll_display
            more = separator + separator.join(authors[1:]) + ')'
        else:
            show = separator.join(authors[:int(limit)])
            more = separator.join(authors[int(limit):len(authors)])

        out += show
        out += ' <span id="more" style="">' + more + '</span>'
        out += ' <span id="extension"></span>'
        out += ' <small><i><a id="link" href="#"' + \
               ' style="color:green;background:white;" onclick="toggle_authors_visibility()" ' + \
               ' style="color:rgb(204,0,0);"></a></i></small>'
        out += '<script>set_up()</script>'
        return out
    elif nb_authors > 0:
        if markup == 'latex' and nb_authors > 1:
            lastauthor = authors.pop()
            lastauthor = ' and ' + lastauthor
        output = separator.join(authors) + lastauthor
        # remove the dot from the end of authors list when the suffix starts with dot
        # (to avoid two consecutive dots)
        if suffix and output and output[-1] == suffix[0] == '.':
            output = output[:-1]
        return output
def format(bfo, new_articles_first='yes',
           subject_to_css_class_kb="WebJournalSubject2CSSClass",
           display_all_category_articles='no'):
    """
    Creates a navigation for articles in the same issue and category.

    @param new_articles_first: if 'yes', display new articles before other articles
    @param subject_to_css_class_kb: knowledge base that maps 595__a to a CSS class
    @param display_all_category_articles: if yes, display all articles, whatever category is selected
    """
    # get variables
    args = parse_url_string(bfo.user_info['uri'])
    this_recid = bfo.control_field('001')
    this_issue_number = args["issue"]
    category_name = args["category"]
    journal_name = args["journal_name"]
    ln = args["ln"]
    _ = gettext_set_language(ln)

    this_title = ""
    if ln == "fr":
        if bfo.fields('246_1a'):
            this_title = bfo.fields('246_1a')[0]
        elif bfo.fields('245__a'):
            this_title = bfo.fields('245__a')[0]
    else:
        if bfo.fields('245__a'):
            this_title = bfo.fields('245__a')[0]
        elif bfo.fields('246_1a'):
            this_title = bfo.fields('246_1a')[0]

    journal_categories = [category_name]
    if display_all_category_articles.lower() == 'yes':
        # Let's retrieve all categories. Ok, we are not supposed to do
        # that with that element, but if journal editor wants...
        journal_categories = get_journal_categories(journal_name,
                                                    this_issue_number)

    menu_out = ''

    for category in journal_categories:
        ordered_articles = get_journal_articles(journal_name,
                                                this_issue_number,
                                                category,
                                                newest_first=new_articles_first.lower() == 'yes')

        new_articles_only = False
        if ordered_articles.keys() and max(ordered_articles.keys()) < 0:
            # If there are only new articles, don't bother marking them as
            # new
            new_articles_only = True

        menu_out += '<div class="subNavigationMenu">'
        order_numbers = ordered_articles.keys()
        order_numbers.sort()
        for order_number in order_numbers:
            for article_id in ordered_articles[order_number]:
                # A record is considered as new if its position is
                # negative and there are some non-new articles
                article_is_new = (order_number < 0 and not new_articles_only)

                if str(article_id) == this_recid:
                    # Mark as active

                    # Get CSS class (if relevant)
                    notes = bfo.fields('595__a')
                    css_classes = [bfo.kb(subject_to_css_class_kb, note, None) \
                                   for note in notes]
                    css_classes = [css_class for css_class in css_classes \
                                   if css_class is not None]

                    if article_is_new:
                        css_classes.append('new')

                    menu_out += '''<div class="active">
            <div class="subNavigationMenuItem %s">%s</div></div>''' % \
                    (' '.join(css_classes),
                     this_title)
                else:
                    temp_rec = BibFormatObject(article_id)
                    title = ''
                    if ln == "fr":
                        title = temp_rec.field('246_1a')
                        if title == '':
                            title = temp_rec.field('245__a')
                    else:
                        title = temp_rec.field('245__a')
                        if title == '':
                            title = temp_rec.field('246_1a')

                    # Get CSS class (if relevant)
                    notes = temp_rec.fields('595__a')
                    css_classes = [temp_rec.kb(subject_to_css_class_kb, note, None) \
                                   for note in notes]
                    css_classes = [css_class for css_class in css_classes \
                                   if css_class is not None]

                    if article_is_new:
                        css_classes.append('new')

                    menu_out += '''<div class="subNavigationMenuItem %s">
                    <a href="%s">%s</a></div>
                    ''' % (' '.join(css_classes),
                           make_journal_url(bfo.user_info['uri'],
                                            {'recid': article_id,
                                             'ln': bfo.lang,
                                             'category': category}),
                           title)
        menu_out += '</div>'

    return menu_out
def format_marcxml_file(marcxml, is_file=False):
    '''
        Parse the given marcxml file to retreive the metadata needed by the
        forward of the document to ArXiv.org
        @param marcxml: marxml file that contains metadata from Invenio
        @return: (dictionnary) couple of key value needed for the push
    '''

    #init the return tuple
    marcxml_values = {
        'id': '',
        'title': '',
        'summary': '',
        'contributors': [],
        'journal_refs': [],
        'report_nos': [],
        'comment': '',
        'doi': ''
    }

    # check if the marcxml is not empty
    if marcxml == '':
        marcxml_values['error'] = "MARCXML string is empty !"
        return marcxml_values

    #get the tag id and code from tag table
    main_report_number = CFG_MARC_REPORT_NUMBER
    add_report_number = CFG_MARC_ADDITIONAL_REPORT_NUMBER
    main_title = CFG_MARC_TITLE
    main_summary = CFG_MARC_ABSTRACT
    main_author = CFG_MARC_AUTHOR_NAME
    main_author_affiliation = CFG_MARC_AUTHOR_AFFILIATION
    add_author = CFG_MARC_CONTRIBUTOR_NAME
    add_author_affiliation = CFG_MARC_CONTRIBUTOR_AFFILIATION
    main_comment = CFG_MARC_COMMENT
    doi = CFG_MARC_DOI
    journal_ref_code = CFG_MARC_JOURNAL_REF_CODE
    journal_ref_title = CFG_MARC_JOURNAL_REF_TITLE
    journal_ref_page = CFG_MARC_JOURNAL_REF_PAGE
    journal_ref_year = CFG_MARC_JOURNAL_REF_YEAR

    #init tmp values
    contributor = {'name': '', 'email': '', 'affiliation': []}

    try:
        bfo = BibFormatObject(recID=None, xml_record=marcxml)
    except:
        marcxml_values['error'] = "Unable to open marcxml file !"
        return marcxml_values

    marcxml_values = {
        'id': bfo.field(main_report_number),
        'title': bfo.field(main_title),
        'summary': bfo.field(main_summary),
        'report_nos': bfo.fields(add_report_number),
        'contributors': [],
        'journal_refs': [],
        'comment': bfo.field(main_comment),
        'doi': bfo.field(doi)
    }

    authors = bfo.fields(main_author[:-1], repeatable_subfields_p=True)
    for author in authors:
        name = author.get(main_author[-1], [''])[0]
        affiliation = author.get(main_author_affiliation[-1], [])
        author = {'name': name, 'email': '', 'affiliation': affiliation}
        marcxml_values['contributors'].append(author)

    authors = bfo.fields(add_author[:-1], repeatable_subfields_p=True)
    for author in authors:
        name = author.get(add_author[-1], [''])[0]
        affiliation = author.get(add_author_affiliation[-1], [])
        author = {'name': name, 'email': '', 'affiliation': affiliation}
        marcxml_values['contributors'].append(author)

    journals = bfo.fields(journal_ref_title[:-1])
    for journal in journals:
        journal_title = journal.get(journal_ref_title[-1], '')
        journal_page = journal.get(journal_ref_page[-1], '')
        journal_code = journal.get(journal_ref_code[-1], '')
        journal_year = journal.get(journal_ref_year[-1], '')
        journal = "%s: %s (%s) pp. %s" % (journal_title, journal_code,
                                          journal_year, journal_page)
        marcxml_values['journal_refs'].append(journal)

    return marcxml_values
Exemplo n.º 22
0
def format_element(recid):
    """
    Prints the list of authors of a record.

    @param limit: the maximum number of authors to display
    @param separator: the separator between authors.
    @param extension: a text printed if more authors than 'limit' exist
    @param print_links: if yes, prints the authors as HTML link to their publications
    @param print_affiliations: if yes, make each author name followed by its affiliation
    @param affiliation_prefix: prefix printed before each affiliation
    @param affiliation_suffix: suffix printed after each affiliation
    @param interactive: if yes, enable user to show/hide authors when there are too many (html + javascript)
    @param highlight: highlights authors corresponding to search query if set to 'yes'
    """
    from urllib import quote
    from cgi import escape
    from invenio.config import CFG_SITE_URL
    from invenio.messages import gettext_set_language
    from invenio.bibformat_engine import BibFormatObject
    import re

    bfo = BibFormatObject(recid)
    limit = ''
    separator = ' ; '
    extension = '[...]'
    print_links = "yes"
    print_affiliations = 'no'
    affiliation_prefix = ' ('
    affiliation_suffix = ')'
    interactive = "no"
    highlight = "no"

    _ = gettext_set_language(bfo.lang)  # load the right message language

    authors = []
    authors_1 = bfo.fields('1001_')
    authors_2 = bfo.fields('7001_')
    authors_3 = bfo.fields('1101_')
    authors_4 = bfo.fields('1102_')

    # VS hack to take away links from corporate authors

    if authors_3:
        if print_links.lower() == "yes":
            for item in authors_3:
                if item.has_key('a'):
                    itema = str(item.get("a", " "))
                    itema = 'CORP' + itema
                    item.update({'a': itema})
    if authors_4:
        if print_links.lower() == "yes":
            for item in authors_4:
                if item.has_key('a'):
                    itema = str(item.get("a", " "))
                    itema = 'CORP' + itema
                    item.update({'a': itema})

    authors.extend(authors_1)
    authors.extend(authors_2)
    authors.extend(authors_3)
    authors.extend(authors_4)

    nb_authors = len(authors)

    #print '3:'
    #print authors

    # Process authors to add link, highlight and format affiliation
    for author in authors:

        if author.has_key('a'):
            if highlight == 'yes':
                from invenio import bibformat_utils
                author['a'] = bibformat_utils.highlight(
                    author['a'], bfo.search_pattern)

            # VS hack to take away links from corporate authors
            if print_links.lower() == "yes":
                if author['a'].startswith('CORP'):
                    author['a'] = re.sub('^CORP', '', author['a'])
                else:
                    author['a'] = '<a href="' + CFG_SITE_URL + \
                                  '/search?f=author&amp;p='+ quote(author['a']) + \
                                  '&amp;ln='+ bfo.lang + \
                                  '">'+escape(author['a'])+'</a>'

        if author.has_key('u'):
            if print_affiliations == "yes":
                author['u'] = affiliation_prefix + author['u'] + \
                              affiliation_suffix

    # Flatten author instances
    if print_affiliations == 'yes':
        authors = [
            author.get('a', '') + author.get('u', '') for author in authors
        ]
    else:
        authors = [author.get('a', '') for author in authors]

    if limit.isdigit() and nb_authors > int(limit) and interactive != "yes":
        return separator.join(authors[:int(limit)]) + extension

    elif limit.isdigit() and nb_authors > int(limit) and interactive == "yes":
        out = '''
        <script type="text/javascript">
        function toggle_authors_visibility(){
            var more = document.getElementById('more');
            var link = document.getElementById('link');
            var extension = document.getElementById('extension');
            if (more.style.display=='none'){
                more.style.display = '';
                extension.style.display = 'none';
                link.innerHTML = "%(show_less)s"
            } else {
                more.style.display = 'none';
                extension.style.display = '';
                link.innerHTML = "%(show_more)s"
            }
            link.style.color = "rgb(204,0,0);"
        }

        function set_up(){
            var extension = document.getElementById('extension');
            extension.innerHTML = "%(extension)s";
            toggle_authors_visibility();
        }

        </script>
        ''' % {
            'show_less': _("Hide"),
            'show_more': _("Show all %i authors") % nb_authors,
            'extension': extension
        }

        out += '<a name="show_hide" />'
        #        out += '<tr><td class="formatRecordLabel">Author(s)</td><td style="padding-left:5px;"><b>'
        out += separator.join(authors[:int(limit)])
        out += '<span id="more" style="">' + separator + \
               separator.join(authors[int(limit):]) + '</span>'
        out += ' <span id="extension"></span>'
        out += ' <small><i><a id="link" href="#" onclick="toggle_authors_visibility()" style="color:rgb(204,0,0);"></a></i></small>'
        out += '<script type="text/javascript">set_up()</script>'
        #        out += '</b></td></tr>'
        return out
    elif nb_authors > 0:
        return separator.join(authors)
Exemplo n.º 23
0
def format_element(bfo, new_articles_first='yes',
           subject_to_css_class_kb="WebJournalSubject2CSSClass",
           display_all_category_articles='no', display_category_title='no'):
    """
    List all articles one after the other, on the same page.

    Similar to bfe_webjournal_articles_overview, but displays full articles.

    Note that you cannot use both bfe_webjournal_articles_overview and
    bfe_webjournal_articles: you have to choose one of them, as they
    use the same cache location (It would also not make sense to use
    both...).

    @param new_articles_first: if 'yes', display new articles before other articles
    @param subject_to_css_class_kb: knowledge base that maps 595__a to a CSS class
    @param display_all_category_articles: if yes, display all articles, whatever category is selected
    @param display_category_title: if yes, display category title (useful if display_all_category_articles is enabled)

    @see: bfe_webjournal_articles_overview.py
    """
    args = parse_url_string(bfo.user_info['uri'])
    journal_name = args["journal_name"]
    this_issue_number = args["issue"]
    category_name = args["category"]
    verbose = args["verbose"]
    ln = args["ln"]
    _ = gettext_set_language(ln)

    # Try to get the page from cache. Only if issue is older or equal
    # to latest release.
    latest_released_issue = get_current_issue(ln, journal_name)
    if verbose == 0 and not issue_is_later_than(this_issue_number,
                                                latest_released_issue):
        cached_html = get_index_page_from_cache(journal_name, category_name,
                                                this_issue_number, ln)
        if cached_html:
            return cached_html

    # Shall we display current category, or all?
    categories = [category_name]
    if display_all_category_articles.lower() == 'yes':
        categories = get_journal_categories(journal_name,
                                            this_issue_number)

    out = ''
    for category_name in categories:
        if display_category_title.lower() == 'yes':
            out += '<h2>' + _(category_name) + '</h2>'

        out += '<table border="0" cellpadding="0" cellspacing="0">'
        # Get the id list
        ordered_articles = get_journal_articles(journal_name,
                                                this_issue_number,
                                                category_name,
                                                newest_first=new_articles_first.lower() == 'yes')
        new_articles_only = False
        if ordered_articles.keys() and max(ordered_articles.keys()) < 0:
            # If there are only new articles, don't bother marking them as
            # new
            new_articles_only = True

        order_numbers = ordered_articles.keys()
        order_numbers.sort()

        for order_number in order_numbers:
            for article_id in ordered_articles[order_number]:
                # A record is considered as new if its position is
                # negative and there are some non-new articles
                article_is_new = (order_number < 0 and not new_articles_only)

                temp_rec = BibFormatObject(article_id)
                title = ''
                if ln == "fr":
                    title = temp_rec.field('246_1a')
                    if title == '':
                        title = temp_rec.field('245__a')
                else:
                    title = temp_rec.field('245__a')
                    if title == '':
                        title = temp_rec.field('246_1a')

                # Get CSS class (if relevant)
                notes = temp_rec.fields('595__a')
                css_classes = [temp_rec.kb(subject_to_css_class_kb, note, None) \
                               for note in notes]
                css_classes = [css_class for css_class in css_classes \
                               if css_class is not None]

                if article_is_new:
                    css_classes.append('new')

                # Finally create the output. Two different outputs
                # depending on if we have text to display or not
                text = []
                if ln == "fr":
                    text = temp_rec.fields('590__b')
                    if not text or \
                           (len(text) == 1 and \
                            (text[0].strip() in ['', '<br />', '<!--HTML--><br />'])):
                        text = temp_rec.fields('520__b')
                else:
                    text = temp_rec.fields('520__b')
                    if not text or \
                           (len(text) == 1 and \
                            (text[0].strip() in ['', '<br />', '<!--HTML--><br />'])):
                        text = temp_rec.fields('590__b')
                text = '<br/>'.join(text)


                out += '''
                            <tr><td class="article">
                               <h%(header_tag_size)s class="%(css_classes)s articleTitle" style="clear:both;">
                                   %(title)s
                               </h%(header_tag_size)s>
                               <div class="articleBody">
                                   <div class="articleText">
                                      %(text)s
                                   </div>
                               </div>
                           </td></tr>
                        ''' % {'title': title,
                               'text': text,
                               'header_tag_size': (display_category_title.lower() == 'yes') and '3' or '2',
                               'css_classes': ' '.join(css_classes)}
        out += '</table>'

    if verbose == 0 and not CFG_ACCESS_CONTROL_LEVEL_SITE == 2 :
        cache_index_page(out, journal_name, category_name,
                         this_issue_number, ln)

    return out
def format_element(
    bfo,
    limit,
    min_photos="",
    print_links="yes",
    style="",
    separator="",
    focus_on_click='no',
    open_pdf="no",
    use_cover_photos_only="no"
):
    """
    Prints html photo thumbnails.
    @param limit the max number of thumbnails to display
    @param print_links if 'yes', each image is linked to its detailed record
    @param min_photos the minimum number of photos which must be available to print the thumbnails
    @param style the css style applied to the image (Eg: 'max-width:40px')
    @param separator printed between each image
    @param focus_on_click if 'yes', add #tirage to printed link so that detailed format can focus on clicked image
    @param user_cover_photos_only if 'yes', only look for album photos that are indicated as "Cover" photos
    """

    out = ""

    album = bfo.field('999__a') == "ALBUM"

    if album:
        if use_cover_photos_only == "yes":
            # Get all the photos in this album
            photos_in_album = bfo.fields('774')
            # Only keep the photos that are indicated as "Cover" photos
            cover_photos_in_album = filter(
                lambda photo: "Cover" in photo.get("n", ""),
                photos_in_album
            )
            # If photo has been indicated as "Cover" photo, keep all of them
            if not cover_photos_in_album:
                cover_photos_in_album = photos_in_album
            # Get the record_id of the first "Cover" photo found
            try:
                record_id = cover_photos_in_album[0].get("r", "")
            except IndexError:
                return ''
        else:
            record_id = bfo.field('774__r')
        record = BibFormatObject(record_id)
        resources_1 = record.fields("8567_")
        resources_2 = record.fields("8564_")
        bibarchive = BibRecDocs(record_id)

    else:
        resources_1 = bfo.fields("8567_")
        resources_2 = bfo.fields("8564_")
        bibarchive = BibRecDocs(bfo.recID)

    has_bibdoc_files = bool(len(bibarchive.list_bibdocs()))

    # We order these resources by tirage, subfield 8
    def cmp_tirage(x, y):
        "Compare tirage"
        x_tirage = x.get('8', '1000')
        y_tirage = y.get('8', '1000')
        try:
            x_tirage = int(x_tirage)
            y_tirage = int(y_tirage)
        except:
            pass
        return cmp(x_tirage, y_tirage)
    resources_1.sort(cmp_tirage)

    if limit.isdigit() and int(limit) >= 0:
        max_photos = int(limit)
    else:
        max_photos = len(resources_1) + len(resources_2)

    if min_photos.isdigit() and int(min_photos) >= 0:
        min_photos = int(min_photos)
    else:
        min_photos = 0

    if style:
        style = 'style="' + style +'"'

    num_displayed_photo = 0

    # 8567 resources
    for resource in resources_1:
        if num_displayed_photo < max_photos and \
               resource.get("y", "").lower() == "icon":
            num_displayed_photo += 1
            if print_links.lower() == 'yes':
                out += '<a href="'+weburl+'/record/'+bfo.control_field("001")
                if focus_on_click.lower() == 'yes' and resource.get("8", "") != '':
                    out += '#' + resource.get("8", "")
                out += '">'
            photo_url = resource.get("u", "")
            photo_url = photo_url.replace('http://mediaarchive.cern.ch', 'https://mediastream.cern.ch')
            out += '<img '+style+' src="' + url_safe_escape(photo_url) + \
                   '" alt="" border="0"/>'
            if print_links.lower() == 'yes':
                out += '</a>'
            out += separator

    if out == '':

        if album:

            if use_cover_photos_only == "yes":
                # Get the record_ids of the "Cover" photos in the album using
                # the previously calculated `cover_photos_in_album` list.
                photo_ids = filter(
                    None,
                    map(
                        lambda photo: photo.get("r"),
                        cover_photos_in_album
                    )
                )
            else:
                photo_ids = bfo.fields('774__r')

            for photo_id in photo_ids:
                bibarchive = BibRecDocs(photo_id)
                # get the first bibdoc
                bibdoc_pictures = [bibdoc for bibdoc in bibarchive.list_bibdocs()]
                for doc in bibdoc_pictures:
                    # in this case focus on does not work - different sorting
                    if num_displayed_photo >= max_photos:
                        break
                    icon = doc.get_icon()
                    if not icon:
                        continue
                    num_displayed_photo += 1
                    link_tag = False
                    if print_links.lower() == 'yes':
                        out += '<a href="' + weburl + '/record/' + bfo.control_field("001")
                        #if focus_on_click.lower() == 'yes':
                        #    out += '#%s' %(i+1)
                        out += '">'
                        link_tag = True
                    elif open_pdf.lower() == 'yes':
                        try:
                            resource_pdf_path = resource.get_file('pdf').get_url()
                        except:
                            resource_pdf_path = ''
                        if resource_pdf_path:
                            out += '<a href="%s">' % resource_pdf_path
                            link_tag = True

                    out += '<img '+ style + ' src="%s" alt="" border="0"/>' % url_safe_escape(icon.get_url())
                    if link_tag:
                        out += '</a>'
                    out += separator

        else:
            bibdoc_pictures = [(bibdoc, bibarchive.get_docname(bibdoc.get_id())) for bibdoc in bibarchive.list_bibdocs()]
            bibdoc_pictures = sorted(bibdoc_pictures, key=itemgetter(1), cmp=alphanum)
            for i, (resource, dummy) in enumerate(bibdoc_pictures):
                # in this case focus on does not work - different sorting
                if num_displayed_photo >= max_photos:
                    break
                icon = resource.get_icon()
                if not icon:
                    continue
                if icon.hidden_p():
                    continue
                num_displayed_photo += 1
                link_tag = False
                if print_links.lower() == 'yes':
                    out += '<a href="' + weburl + '/record/' + bfo.control_field("001")
                    #if focus_on_click.lower() == 'yes':
                    #    out += '#%s' %(i+1)
                    out += '">'
                    link_tag = True
                elif open_pdf.lower() == 'yes':
                    try:
                        resource_pdf_path = resource.get_file('pdf').get_url()
                    except:
                        resource_pdf_path = ''
                    if resource_pdf_path:
                        out += '<a href="%s">' % resource_pdf_path
                        link_tag = True

                out += '<img '+ style + ' src="%s" alt="" border="0"/>' % url_safe_escape(icon.get_url())
                if link_tag:
                    out += '</a>'
                out += separator

    # 8564 resources
    if out == '':
        for resource in resources_2:
            if num_displayed_photo < max_photos and \
                   resource.get("x", "").lower() == "icon" and resource.get("u", "") != "" and \
                   (not resource.get("u", '').split("/")[2] in ['documents.cern.ch', 'doc.cern.ch', 'preprints.cern.ch'] or \
                    (len(resources_1) == 0 and not has_bibdoc_files)) and open_pdf.lower() == 'no':
                num_displayed_photo += 1
                if print_links.lower() == 'yes':
                    out += '<a href="'+weburl+'/record/'+bfo.control_field("001")
                    if focus_on_click.lower() == 'yes' and resource.get("8", "") != '':
                        out += '#' + resource.get("8", "")
                    out += '">'
                out += '<img '+style+' src="' + url_safe_escape(resource.get("u", "")) + \
                       '" alt="" border="0"/>'
                if print_links.lower() == 'yes':
                    out += '</a>'
                out += separator


    # No icon in metadata. Try to read on dfs disk
    # If icon does already exist but is not not in metdata,
    # place it in 'icon_exists_links'. Else put it in
    # 'icon_missing_links', which will be used if really no
    # icon exist for that record (there are chances that this
    # icon will be created later)
    #
    # If icon exists but not in metadata, try to update record metadata
    icon_exists_links = []
    icon_missing_links = []
    if out == '':
        masters_paths = [link['d'] for link in bfo.fields('8567_') \
                         if link.get('x', '') == 'Absolute master path' and \
                            link.get('d', '') != '']
        for master_path in masters_paths:
            try:
                path_components = master_path.split('\\')[-3:] # take 3 last components
                path_components[-1] = path_components[-1][:-4] # remove .jpg
                filename = path_components[-1] + '-Icon.jpg'
                path_components.append(filename)
                link = 'http://mediaarchive.cern.ch/MediaArchive/Photo/Public/' + \
                       '/'.join(path_components)
                # check if file exists
                if file_exists(link):
                    icon_exists_links.append('<a href="' + weburl + '/record/' + \
                                             bfo.control_field("001") + '">' + \
                                             '<img '+style+' src="' + link + '" alt="" border="0"/></a>')

                    # Also try to update the record metadata using the info.xml file
                    info_xml_url = 'http://mediaarchive.cern.ch/MediaArchive/Photo/Public/' + \
                                   '/'.join(path_components[:-1]) + \
                                   '/' + 'info.xml'
                    _perform_request_add_slave_url(info_xml_url)
                else:
                    icon_missing_links.append('<a href="' + weburl + '/record/' + \
                                              bfo.control_field("001") + '">' + \
                                              '<img '+style+' src="' + link + '" alt="" border="0"/></a>')
            except Exception, e:
                continue

        # First add icons that we know exist for sure
        for icon in icon_exists_links:
            if num_displayed_photo < max_photos:
                num_displayed_photo += 1
                out += icon
                out += separator

        # Last attempt: add icon even if not exists
        for icon in icon_missing_links:
            if num_displayed_photo < max_photos:
                num_displayed_photo += 1
                out += icon
                out += separator