def perform_request_contact(req, ln, journal_name, verbose=0): """ Display contact information """ try: contact_page_template = get_journal_template('contact', journal_name, ln) except InvenioWebJournalTemplateNotFoundError as e: register_exception(req=req) return e.user_box(req) user_info = collect_user_info(req) temp_marc = '''<record> <controlfield tag="001">0</controlfield> </record>''' bfo = BibFormatObject(0, ln=ln, xml_record=temp_marc, user_info=user_info) bfo.req = req html = format_with_format_template(contact_page_template, bfo) return html
def perform_request_index(req, journal_name, issue_number, ln, category, editor=False, verbose=0): """ Central logic function for index pages. Brings together format templates and MARC rules from the config, with the requested index page, given by the url parameters. From config: - page template for index pages -> formatting - MARC rule list -> Category Navigation - MARC tag used for issue numbers -> search (later in the format elements) Uses BibFormatObject and format_with_format_template to produce the required HTML. """ current_issue = get_current_issue(ln, journal_name) if not get_release_datetime(issue_number, journal_name): # Unreleased issue. Display latest released issue? unreleased_issues_mode = get_unreleased_issue_hiding_mode(journal_name) if not editor and \ (unreleased_issues_mode == 'all' or \ (unreleased_issues_mode == 'future' and \ issue_is_later_than(issue_number, current_issue))): redirect_to_url(req, "%s/journal/%s/%s/%s?ln=%s" % \ (CFG_SITE_URL, journal_name, current_issue.split('/')[1], current_issue.split('/')[0], ln)) try: index_page_template = get_journal_template('index', journal_name, ln) except InvenioWebJournalTemplateNotFoundError as e: register_exception(req=req) return e.user_box(req) temp_marc = '''<record> <controlfield tag="001">0</controlfield> </record>''' # create a record and get HTML back from bibformat user_info = collect_user_info(req) bfo = BibFormatObject(0, ln=ln, xml_record=temp_marc, user_info=user_info) bfo.req = req verbosity = 0 if editor: # Increase verbosity only for editors/admins verbosity = verbose html = format_with_format_template(index_page_template, bfo, verbose=verbosity) return html
def _eval_bibformat(ctx, recID, template_code): """ Bridge between BibFormat and XSL stylesheets. Can be used in that way in XSL stylesheet (provided ``xmlns:fn="http://cdsweb.cern.ch/bibformat/fn"`` has been declared):: <xsl:value-of select="fn:eval_bibformat(marc:controlfield[@tag='001'], '<BFE_SERVER_INFO var="recurl">')"/> if recID is string, value is converted to int if recID is Node, first child node (text node) is taken as value template_code is evaluated as a format template piece of code. '<' and '"' need to be escaped with '<' and '"' :param ctx: context as passed by lxml :param recID: record ID :param template_code: the code calling a BFE_ as it would be used in format template :return: the evaluated call to a format template (usually a call to a format element) :rtype: str """ from invenio.modules.formatter.engine import format_with_format_template, \ BibFormatObject try: if isinstance(recID, str): recID_int = int(recID) elif isinstance(recID, (int, long)): recID_int = recID elif isinstance(recID, list): recID = recID[0] if isinstance(recID, str): recID_int = int(recID) else: recID_int = int(recID.text) else: recID_int = int(recID.text) bfo = BibFormatObject(recID_int) out = format_with_format_template(None, bfo, verbose=0, format_template_code=template_code) return out[0] except Exception: current_app.logger.exception( "Error during formatting function evaluation.") return ''
def _eval_bibformat(ctx, recID, template_code): """ Bridge between BibFormat and XSL stylesheets. Can be used in that way in XSL stylesheet (provided ``xmlns:fn="http://cdsweb.cern.ch/bibformat/fn"`` has been declared):: <xsl:value-of select="fn:eval_bibformat(marc:controlfield[@tag='001'], '<BFE_SERVER_INFO var="recurl">')"/> if recID is string, value is converted to int if recID is Node, first child node (text node) is taken as value template_code is evaluated as a format template piece of code. '<' and '"' need to be escaped with '<' and '"' :param ctx: context as passed by lxml :param recID: record ID :param template_code: the code calling a BFE_ as it would be used in format template :return: the evaluated call to a format template (usually a call to a format element) :rtype: str """ from invenio.modules.formatter.engine import format_with_format_template, \ BibFormatObject try: if isinstance(recID, str): recID_int = int(recID) elif isinstance(recID, (int, long)): recID_int = recID elif isinstance(recID, list): recID = recID[0] if isinstance(recID, str): recID_int = int(recID) else: recID_int = int(recID.text) else: recID_int = int(recID.text) bfo = BibFormatObject(recID_int) out = format_with_format_template(None, bfo, verbose=0, format_template_code=template_code) return out[0] except Exception: current_app.logger.exception( "Error during formatting function evaluation." ) return ''
def perform_request_search(req, journal_name, ln, archive_issue, archive_select, archive_date, archive_search, verbose=0): """ Logic for the search / archive page. """ try: search_page_template = get_journal_template('search', journal_name, ln) except InvenioWebJournalTemplateNotFoundError as e: register_exception(req=req) return e.user_box(req) if archive_select == "False" and archive_search == "False": temp_marc = '''<record> <controlfield tag="001">0</controlfield> </record>''' user_info = collect_user_info(req) bfo = BibFormatObject(0, ln=ln, xml_record=temp_marc, user_info=user_info) bfo.req = req html = format_with_format_template(search_page_template, bfo, verbose=verbose) return html elif archive_select == "Go": redirect_to_url(req, "%s/journal/%s/%s/%s?ln=%s" % (CFG_SITE_URL, journal_name, archive_issue.split('/')[1], archive_issue.split('/')[0], ln)) elif archive_search == "Go": try: archive_issue_time = datetime.datetime(*time.strptime(archive_date, "%d/%m/%Y")[0:5]) archive_issue = datetime_to_issue(archive_issue_time, journal_name) if not archive_issue: archive_issue = get_current_issue(ln, journal_name) except ValueError: archive_issue = get_current_issue(ln, journal_name) redirect_to_url(req, "%s/journal/%s/%s/%s?ln=%s" % (CFG_SITE_URL, journal_name, archive_issue.split('/')[1], archive_issue.split('/')[0], ln))
def eval_bibformat_lxml(ctx, recID, template_code): """ libxslt extension function: Bridge between BibFormat and XSL stylesheets. Returns the evaluation of the given piece of format template Can be used in that way in XSL stylesheet (provided xmlns:fn="http://cdsweb.cern.ch/bibformat/fn" has been declared): <xsl:value-of select="fn:eval_bibformat(marc:controlfield[@tag='001'],'<BFE_SERVER_INFO var="recurl">')" /> if recID is string, value is converted to int if recID is Node, first child node (text node) is taken as value template_code is evaluated as a format template piece of code. '<' and '"' need to be escaped with '<' and '"' @param ctx: context as passed by lxml @param recID: record ID @param template_code: the code calling a BFE_ as it would be use in format template @return: the evalued call to a format template (usually a call to a format element) @rtype: string """ #' from invenio.modules.formatter.engine import \ format_with_format_template, \ BibFormatObject try: if isinstance(recID, str): recID_int = int(recID) elif isinstance(recID, (int, long)): recID_int = recID elif isinstance(recID, list): recID = recID[0] if isinstance(recID, str): recID_int = int(recID) else: recID_int = int(recID.text) else: recID_int = int(recID.text) bfo = BibFormatObject(recID_int) return format_with_format_template(None, bfo, verbose=0, format_template_code=template_code) except Exception as err: sys.stderr.write("Error during formatting function evaluation: " + \ str(err) + \ '\n') return ''
def perform_request_popup(req, ln, journal_name, record): """ Display the popup window """ try: popup_page_template = get_journal_template('popup', journal_name, ln) except InvenioWebJournalTemplateNotFoundError as e: register_exception(req=req) return e.user_box(req) user_info = collect_user_info(req) bfo = BibFormatObject(record, ln=ln, user_info=user_info) bfo.req = req html = format_with_format_template(popup_page_template, bfo) return html
def perform_request_article(req, journal_name, issue_number, ln, category, recid, editor=False, verbose=0): """ Central logic function for article pages. Loads the format template for article display and displays the requested article using BibFormat. 'Editor' mode generates edit links on the article view page and disables caching. """ current_issue = get_current_issue(ln, journal_name) if not get_release_datetime(issue_number, journal_name): # Unreleased issue. Display latest released issue? unreleased_issues_mode = get_unreleased_issue_hiding_mode(journal_name) if not editor and \ (unreleased_issues_mode == 'all' or \ (unreleased_issues_mode == 'future' and \ issue_is_later_than(issue_number, current_issue))): redirect_to_url(req, "%s/journal/%s/%s/%s?ln=%s" % \ (CFG_SITE_URL, journal_name, current_issue.split('/')[1], current_issue.split('/')[0], ln)) try: index_page_template = get_journal_template('detailed', journal_name, ln) except InvenioWebJournalTemplateNotFoundError as e: register_exception(req=req) return e.user_box(req) user_info = collect_user_info(req) bfo = BibFormatObject(recid, ln=ln, user_info=user_info) bfo.req = req # if it is cached, return it cached_html = get_article_page_from_cache(journal_name, category, recid, issue_number, ln, bfo) if cached_html and not editor: return cached_html # Check that this recid is indeed an article is_article = False articles = get_journal_articles(journal_name, issue_number, category) for order, recids in iteritems(articles): if recid in recids: is_article = True break if not is_article: redirect_to_url(req, "%s/journal/%s/%s/%s?ln=%s" % \ (CFG_SITE_URL, journal_name, issue_number.split('/')[1], issue_number.split('/')[0], ln)) # create a record and get HTML back from bibformat verbosity = 0 if editor: # Increase verbosity only for editors/admins verbosity = verbose html_out = format_with_format_template(index_page_template, bfo, verbose=verbosity) # cache if not in editor mode, and if database is not down if not editor and not CFG_ACCESS_CONTROL_LEVEL_SITE == 2: cache_article_page(html_out, journal_name, category, recid, issue_number, ln) return html_out
def format_template_show_preview_or_save(req, bft, ln=CFG_SITE_LANG, code=None, ln_for_preview=CFG_SITE_LANG, pattern_for_preview="", content_type_for_preview='text/html', save_action=None, navtrail=""): """ Print the preview of a record with a format template. To be included inside Format template editor. If the save_action has a value, then the code should also be saved at the same time @param req: the request object @param code: the code of a template to use for formatting @param ln: language @param ln_for_preview: the language for the preview (for bfo) @param pattern_for_preview: the search pattern to be used for the preview (for bfo) @param content_type_for_preview: the content-type to use to serve the preview page @param save_action: has a value if the code has to be saved @param bft: the filename of the template to save @param navtrail: navigation trail @return: a web page """ ln = wash_language(ln) _ = gettext_set_language(ln) (auth_code, auth_msg) = check_user(req, 'cfgbibformat') if not auth_code: user_info = collect_user_info(req) uid = user_info['uid'] bft = wash_url_argument(bft, 'str') if save_action is not None and code is not None: #save bibformatadminlib.update_format_template_code(bft, code=code) bibformat_engine.clear_caches() if code is None: code = bibformat_engine.get_format_template(bft)['code'] ln_for_preview = wash_language(ln_for_preview) pattern_for_preview = wash_url_argument(pattern_for_preview, 'str') if pattern_for_preview == "": try: recID = search_pattern(p='-collection:DELETED').pop() except KeyError: return page(title="No Document Found", body="", uid=uid, language=ln_for_preview, navtrail = "", lastupdated=__lastupdated__, req=req, navmenuid='search') pattern_for_preview = "recid:%s" % recID else: try: recID = search_pattern(p=pattern_for_preview + \ ' -collection:DELETED').pop() except KeyError: return page(title="No Record Found for %s" % pattern_for_preview, body="", uid=uid, language=ln_for_preview, navtrail = "", lastupdated=__lastupdated__, req=req) units = create_basic_search_units(None, pattern_for_preview, None) keywords = [unit[1] for unit in units if unit[0] != '-'] bfo = bibformat_engine.BibFormatObject(recID = recID, ln = ln_for_preview, search_pattern = keywords, xml_record = None, user_info = user_info) body = bibformat_engine.format_with_format_template(bft, bfo, verbose=7, format_template_code=code) if content_type_for_preview == 'text/html': #Standard page display with CDS headers, etc. return page(title="", body=body, uid=uid, language=ln_for_preview, navtrail = navtrail, lastupdated=__lastupdated__, req=req, navmenuid='search') else: #Output with chosen content-type. req.content_type = content_type_for_preview req.send_http_header() req.write(body) else: return page_not_authorized(req=req, text=auth_msg)