Beispiel #1
0
def get_as_text(record_id=0, xml_record=None, ln=CFG_SITE_LANG):
    """Return the record in a textual format"""
    _ = gettext_set_language(ln)
    out = ""
    if record_id != 0:
        rec_in_hb = format_record(record_id, of="hb")
    elif xml_record:
        rec_in_hb = format_record(0, of="hb", xml_record=xml_record)
    rec_in_hb = rec_in_hb.replace('\n', ' ')
    htparser = RecordHTMLParser()
    try:
        htparser.feed(rec_in_hb)
        htparser.close()
        out = htparser.result
    except:
        out = remove_html_markup(rec_in_hb)

    # Remove trailing whitespace and linefeeds
    out = out.strip('\n').strip()
    # Merge consecutive whitespaces. Must be done here, once all HTML
    # tags have been removed
    out = whitespaces_pattern.sub(' ', out)
    # Now consider non-breakable spaces
    out = out.replace(' ', ' ')
    out = re.sub(r"[\-:]?\s*%s\s*[\-:]?" % _("Detailed record"), "", out)
    out = re.sub(r"[\-:]?\s*%s\s*[\-:]?" % _("Similar records"), "", out)
    out = re.sub(r"[\-:]?\s*%s\s*[\-:]?" % _("Cited by"), "", out)
    return out.strip()
Beispiel #2
0
    def test_validate_xml_against_xsd(self):
        """
        Validate generated DataCite XML for all public records
        """
        from invenio.modules.search.models import Collection
        from invenio.modules.formatter import format_record
        from invenio.modules.records.api import get_record

        etree.clear_error_log()

        for recid in Collection.query.filter_by(name='zenodo').first().reclist:
            try:
                xml = None
                record = get_record(recid)
                for identifier in record.get('related_identifiers', []):
                    if identifier['scheme'] != identifier['scheme'].lower():
                        raise Exception("Record %s has problem with upper-case scheme %s" % (recid, identifier['scheme']))
                if record.get('doi', None):
                    # v2.2
                    xml = StringIO(format_record(recid, 'dcite'))
                    xml_doc = etree.parse(xml)
                    self.schema.assertValid(xml_doc)
                    # v3.0
                    xml = StringIO(format_record(recid, 'dcite3'))
                    xml_doc = etree.parse(xml)
                    self.schema3.assertValid(xml_doc)
            except Exception:
                if xml:
                    print(xml.getvalue())
                raise
Beispiel #3
0
def _get_hepnames_data_fallback(bibauthorid_data, person_id):
    '''
    Returns  hepnames data
    @param bibauthorid_data: dict with 'is_baid':bool, 'cid':canonicalID, 'pid':personid
    '''
    cid = str(person_id)
    hepdict = {}
    if bibauthorid_data['cid']:
        cid = bibauthorid_data['cid']
    hepRecord = perform_request_search(rg=0, cc='HepNames', p=cid)[:CFG_WEBAUTHORPROFILE_MAX_HEP_CHOICES]

    hepdict['cid'] = cid
    hepdict['pid'] = person_id

    if not hepRecord or len(hepRecord) > 1:
        #present choice dialog with alternatives?
        names_dict = get_person_names_dicts(person_id)
        dbnames = names_dict[0]['db_names_dict'].keys()
        query = ' or '.join(['"%s"' % str(n) for n in dbnames])
        additional_records = perform_request_search(rg=0, cc='HepNames', p=query)[:CFG_WEBAUTHORPROFILE_MAX_HEP_CHOICES]
        hepRecord += additional_records
        hepdict['HaveHep'] = False
        hepdict['HaveChoices'] = bool(hepRecord)
        #limits possible choiches!
        hepdict['HepChoices'] = [(format_record(x, 'hb'), x) for x in hepRecord ]
        hepdict['heprecord'] = hepRecord
        hepdict['bd'] = bibauthorid_data
    else:
        #show the heprecord we just found.
        hepdict['HaveHep'] = True
        hepdict['HaveChoices'] = False
        hepdict['heprecord'] = format_record(hepRecord[0], 'hd')
        hepdict['bd'] = bibauthorid_data
    return hepdict
Beispiel #4
0
def get_as_text(record_id=0, xml_record=None, ln=CFG_SITE_LANG):
    """Return the record in a textual format"""
    _ = gettext_set_language(ln)
    out = ""
    if record_id != 0:
        rec_in_hb = format_record(record_id, of="hb")
    elif xml_record:
        rec_in_hb = format_record(0, of="hb", xml_record=xml_record)
    rec_in_hb = rec_in_hb.replace('\n', ' ')
    htparser = RecordHTMLParser()
    try:
        htparser.feed(rec_in_hb)
        htparser.close()
        out = htparser.result
    except:
        out = remove_html_markup(rec_in_hb)

    # Remove trailing whitespace and linefeeds
    out = out.strip('\n').strip()
    # Merge consecutive whitespaces. Must be done here, once all HTML
    # tags have been removed
    out = whitespaces_pattern.sub(' ', out)
    # Now consider non-breakable spaces
    out = out.replace(' ', ' ')
    out = re.sub(r"[\-:]?\s*%s\s*[\-:]?" % _("Detailed record"), "", out)
    out = re.sub(r"[\-:]?\s*%s\s*[\-:]?" % _("Similar records"), "", out)
    out = re.sub(r"[\-:]?\s*%s\s*[\-:]?" % _("Cited by"), "", out)
    return out.strip()
    def test_validate_xml_against_xsd(self):
        """
        Validate generated DataCite XML for all public records
        """
        from invenio.modules.search.models import Collection
        from invenio.modules.formatter import format_record
        from invenio.modules.records.api import get_record

        etree.clear_error_log()

        for recid in Collection.query.filter_by(name='zenodo').first().reclist:
            try:
                xml = None
                record = get_record(recid)
                for identifier in record.get('related_identifiers', []):
                    if identifier['scheme'] != identifier['scheme'].lower():
                        raise Exception("Record %s has problem with upper-case scheme %s" % (recid, identifier['scheme']))
                if record.get('doi', None):
                    # v2.2
                    xml = StringIO(format_record(recid, 'dcite'))
                    xml_doc = etree.parse(xml)
                    self.schema.assertValid(xml_doc)
                    # v3.0
                    xml = StringIO(format_record(recid, 'dcite3'))
                    xml_doc = etree.parse(xml)
                    self.schema3.assertValid(xml_doc)
            except Exception:
                if xml:
                    print(xml.getvalue())
                raise
Beispiel #6
0
def iterate_over_new(list, fmt):
    """
    Iterate over list of IDs

    @param list: the list of record IDs to format
    @param fmt: the output format to use
    @return: tuple (total number of records, time taken to format, time taken to insert)
    """
    global total_rec

    formatted_records = ''      # (string-)List of formatted record of an iteration
    tbibformat  = 0     # time taken up by external call
    tbibupload  = 0     # time taken up by external call
    start_date = task_get_task_param('task_starting_time') # Time at which the record was formatted

    tot = len(list)
    count = 0
    for recID in list:
        t1 = os.times()[4]
        start_date = time.strftime('%Y-%m-%d %H:%M:%S')
        format_record(recID, fmt, on_the_fly=True)
        formatted_record = zlib.compress(format_record(recID, fmt, on_the_fly=True))
        run_sql('REPLACE LOW_PRIORITY INTO bibfmt (id_bibrec, format, last_updated, value) VALUES (%s, %s, %s, %s)',
                (recID, fmt, start_date, formatted_record))
        t2 = os.times()[4]
        tbibformat += (t2 - t1)
        count += 1
        if (count % 100) == 0:
            write_message("   ... formatted %s records out of %s" % (count, tot))
            task_update_progress('Formatted %s out of %s' % (count, tot))
            task_sleep_now_if_required(can_stop_too=True)
    if (tot % 100) != 0:
        write_message("   ... formatted %s records out of %s" % (count, tot))
    return (tot, tbibformat, tbibupload)
Beispiel #7
0
def openaire_register_doi(recid):
    """
    Register a DOI for new publication

    If it fails, it will retry every 10 minutes for 1 hour.
    """
    doi_val = get_fieldvalues(recid, "0247_a")[0]
    logger.debug("Found DOI %s in record %s" % (doi_val, recid))

    pid = PersistentIdentifier.get("doi", doi_val)
    if not pid:
        logger.debug("DOI not locally managed.")
        return
    else:
        logger.debug("DOI locally managed.")

    if not pid.has_object("rec", recid):
        raise Exception(
            "DOI %s is not assigned to record %s." % (doi_val, recid))

    if pid.is_new() or pid.is_reserved():
        logger.info("Registering DOI %s for record %s" % (doi_val, recid))

        url = "%s/record/%s" % (CFG_DATACITE_SITE_URL, recid)
        doc = format_record(recid, DEPOSIT_DATACITE_OF)

        if not pid.register(url=url, doc=doc):
            m = "Failed to register DOI %s" % doi_val
            logger.error(m + "\n%s\n%s" % (url, doc))
            if not openaire_register_doi.request.is_eager:
                raise openaire_register_doi.retry(exc=Exception(m))
        else:
            logger.info("Successfully registered DOI %s." % doi_val)
    def formatter(bwo, **kwargs):
        """Return formatted data of object."""
        from invenio.modules.formatter import format_record

        of = kwargs.get("of", "hp")

        extra_data = bwo.get_extra_data()
        xml = extra_data.get("marcxml")

        id_user = bwo.id_user
        user_email = acc_get_user_email(id_user)
        ticket_id = extra_data.get("ticket_id")
        ticket_url = "https://rt.inspirehep.net/Ticket/Display.html?id={}".format(
            ticket_id
            )

        if of == "xm":
            return xml
        else:
            record_preview = format_record(
                    recID=None,
                    of=of,
                    xml_record=xml
                    )
            return render_template("authors/workflows/authorupdate.html",
                                   record_preview=record_preview,
                                   user_email=user_email,
                                   ticket_url=ticket_url,
                                   comments=extra_data.get("comments"))
Beispiel #9
0
    def get(self, record_id):
        from invenio.legacy.search_engine import record_exists, \
            check_user_can_view_record

        # Get output format
        output_format = self.get_output_format()

        # Check record's existence
        record_status = record_exists(record_id)
        if record_status == 0:
            raise RecordNotFoundError(
                message="Record {} does not exist.".format(record_id),
            )
        elif record_status == -1:
            raise RecordDeletedError(
                message="Record {} was deleted.".format(record_id),
            )

        # Check record's access
        (auth_code, auth_mesg) = check_user_can_view_record(
            current_user,
            record_id
        )
        if auth_code == 1:
            raise RecordForbiddenViewError(
                message="Access to record {} is forbidden.".format(record_id),
            )

        # Return record with requested output format.
        result = format_record(recID=record_id, of=output_format)
        return (result, 200)
Beispiel #10
0
    def parse_and_extract_records(self, of='hb'):
        """Parse the buffer and return a list of the recids and a
        dictionary with key:value pairs like the following
        recid:formated record with the selected output format"""

        # the patterns :
        # separate the records from one another
        record_pat = re.compile(r'(<record.*?>.*?</record>)', re.DOTALL + re.MULTILINE + re.IGNORECASE)
        # extract the recid
        recid_pat = re.compile(r'<controlfield tag="001">([0-9]+?)</controlfield>', re.DOTALL + re.MULTILINE + re.IGNORECASE)

        if not of:
            of='hb'

        try:
            results = record_pat.finditer(self.buffer)
            records = {}
            recids = []
            for result in results:
                xml_record = result.group(1)
                recid = recid_pat.search(xml_record).group(1)
                recids.append(recid)
                if of != 'xm':
                    records[recid] = format_record(None, of, xml_record=xml_record)
                elif of == 'xm':
                    records[recid] = xml_record
            return (recids, records)
        except AttributeError:
            # in case there were no results found an Attribute error is raised
            return ([], {})
Beispiel #11
0
def openaire_register_doi(recid):
    """
    Register a DOI for new publication

    If it fails, it will retry every 10 minutes for 1 hour.
    """
    doi_val = get_fieldvalues(recid, "0247_a")[0]
    logger.debug("Found DOI %s in record %s" % (doi_val, recid))

    pid = PersistentIdentifier.get("doi", doi_val)
    if not pid:
        logger.debug("DOI not locally managed.")
        return
    else:
        logger.debug("DOI locally managed.")

    if not pid.has_object("rec", recid):
        raise Exception(
            "DOI %s is not assigned to record %s." % (doi_val, recid))

    if pid.is_new() or pid.is_reserved():
        logger.info("Registering DOI %s for record %s" % (doi_val, recid))

        url = "%s/record/%s" % (CFG_DATACITE_SITE_URL, recid)
        doc = format_record(recid, DEPOSIT_DATACITE_OF)

        if not pid.register(url=url, doc=doc):
            m = "Failed to register DOI %s" % doi_val
            logger.error(m + "\n%s\n%s" % (url, doc))
            if not openaire_register_doi.request.is_eager:
                raise openaire_register_doi.retry(exc=Exception(m))
        else:
            logger.info("Successfully registered DOI %s." % doi_val)
Beispiel #12
0
    def tmpl_get_latest_linkbacks(self, latest_linkbacks, ln):
        """
        Display approved latest added linkbacks to display
        @param latest_linkbacks: a list of lists of linkbacks
        """
        result = ''

        for i in range(len(latest_linkbacks)):
            day_group = latest_linkbacks[i]

            date = day_group[0][6]
            date_day_month = convert_datetext_to_dategui(str(date))[:6]

            result += self.tmpl_heading(date_day_month)
            for j in range(len(day_group)):
                current_linkback = day_group[j]
                link_type = current_linkback[4]
                url = str(current_linkback[1])
                recordid = current_linkback[2]
                result += '<font class="rankscoreinfo"><a>(%s)&nbsp;</a></font>' % link_type
                result += '<small>'
                result += '<a href="%s">%s</a> links to ' % (cgi.escape(url), cgi.escape(get_url_title(url)))
                result += format_record(recID=recordid, of='hs', ln=ln)
                result += '</small>'
                result += '<br>'
            result += '<br>'
        return result
Beispiel #13
0
    def parse_and_extract_records(self, of='hb'):
        """Parse the buffer and return a list of the recids and a
        dictionary with key:value pairs like the following
        recid:formated record with the selected output format"""

        # the patterns :
        # separate the records from one another
        record_pat = re.compile(r'(<record.*?>.*?</record>)', re.DOTALL + re.MULTILINE + re.IGNORECASE)
        # extract the recid
        recid_pat = re.compile(r'<controlfield tag="001">([0-9]+?)</controlfield>', re.DOTALL + re.MULTILINE + re.IGNORECASE)

        if not of:
            of='hb'

        try:
            results = record_pat.finditer(self.buffer)
            records = {}
            recids = []
            for result in results:
                xml_record = result.group(1)
                recid = recid_pat.search(xml_record).group(1)
                recids.append(recid)
                if of != 'xm':
                    records[recid] = format_record(None, of, xml_record=xml_record)
                elif of == 'xm':
                    records[recid] = xml_record
            return (recids, records)
        except AttributeError:
            # in case there were no results found an Attribute error is raised
            return ([], {})
Beispiel #14
0
def print_record(recID,
                 format='hb',
                 ot='',
                 ln=CFG_SITE_LANG,
                 decompress=zlib.decompress,
                 search_pattern=None,
                 user_info=None,
                 verbose=0,
                 sf='',
                 so='d',
                 sp='',
                 rm='',
                 brief_links=True):
    """
    Print record 'recID' formatted according to 'format'.

    'sf' is sort field and 'rm' is ranking method that are passed here
    only for proper linking purposes: e.g. when a certain ranking
    method or a certain sort field was selected, keep it selected in
    any dynamic search links that may be printed.
    """
    from invenio.modules.formatter import format_record
    return format_record(
        recID,
        of=format,
        ln=ln,
        verbose=verbose,
        search_pattern=search_pattern) if record_exists(recID) != 0 else ""
Beispiel #15
0
    def formatter(bwo, **kwargs):
        """Nicely format the record."""
        from pprint import pformat
        from invenio_records.api import Record

        data = bwo.get_data()
        if not data:
            return ''

        formatter = kwargs.get("formatter", None)
        of = kwargs.get("of", None)
        if formatter:
            # A separate formatter is supplied
            return formatter(data)

        if isinstance(data, collections.Mapping):
            # Dicts are cool on its own, but maybe its SmartJson (record)
            try:
                data = Record(data.dumps()).legacy_export_as_marc()
            except (TypeError, KeyError):
                pass

        if isinstance(data, string_types):
            # We can try formatter!
            # If already XML, format_record does not like it.
            if of and of != 'xm':
                try:
                    from invenio.modules.formatter import format_record
                    formatted_data = format_record(
                        recID=None,
                        of=of,
                        xml_record=data
                    )
                except TypeError:
                    # Wrong kind of type
                    pass
            else:
                # So, XML then
                from xml.dom.minidom import parseString

                try:
                    unpretty_data = parseString(data)
                    formatted_data = unpretty_data.toprettyxml()
                except TypeError:
                    # Probably not proper XML string then
                    return "Data cannot be parsed: %s" % (data,)
                except Exception:
                    # Just return raw string
                    pass

        if not formatted_data:
            formatted_data = data

        if isinstance(formatted_data, dict):
            formatted_data = pformat(formatted_data)
        return formatted_data
Beispiel #16
0
def _entry_data_preview(data, of='default'):
    if format == 'hd' or format == 'xm':
        from invenio.modules.formatter import format_record
        try:
            data['record'] = format_record(recID=None, of=of,
                                           xml_record=data['record'])
            return data['record']
        except ValueError:
            print("This is not a XML string")
    return data
Beispiel #17
0
def _get_record_linking_fields(recid_b, recid_a, tag, ind1, ind2):
    """
    Returns the fields (defined by tag, ind1, ind2) in record (given
    by recid_b) that do not link to another given record (recid_a).
    """
    fields = []
    rec = create_record(format_record(recid_b, "xm"))[0]
    for field_instance in record_get_field_instances(rec, tag=tag, ind1=ind1, ind2=ind2):
        if not ('w', str(recid_a)) in field_instance[0]:
            fields.append(field_instance)
    return fields
Beispiel #18
0
 def _format_record(recid,
                    of='hd',
                    user_info=current_user,
                    *args,
                    **kwargs):
     from invenio.modules.formatter import format_record
     return format_record(recid,
                          of,
                          user_info=user_info,
                          *args,
                          **kwargs)
Beispiel #19
0
    def formatter(bwo, **kwargs):
        """Nicely format the record."""
        from pprint import pformat
        from invenio.modules.records.api import Record

        data = bwo.get_data()
        if not data:
            return ''

        formatter = kwargs.get("formatter", None)
        of = kwargs.get("of", None)
        if formatter:
            # A separate formatter is supplied
            return formatter(data)

        if isinstance(data, collections.Mapping):
            # Dicts are cool on its own, but maybe its SmartJson (record)
            try:
                data = Record(data.dumps()).legacy_export_as_marc()
            except (TypeError, KeyError):
                pass

        if isinstance(data, string_types):
            # We can try formatter!
            # If already XML, format_record does not like it.
            if of and of != 'xm':
                try:
                    from invenio.modules.formatter import format_record
                    formatted_data = format_record(recID=None,
                                                   of=of,
                                                   xml_record=data)
                except TypeError:
                    # Wrong kind of type
                    pass
            else:
                # So, XML then
                from xml.dom.minidom import parseString

                try:
                    unpretty_data = parseString(data)
                    formatted_data = unpretty_data.toprettyxml()
                except TypeError:
                    # Probably not proper XML string then
                    return "Data cannot be parsed: %s" % (data, )
                except Exception:
                    # Just return raw string
                    pass

        if not formatted_data:
            formatted_data = data

        if isinstance(formatted_data, dict):
            formatted_data = pformat(formatted_data)
        return formatted_data
Beispiel #20
0
def _get_formated_record(record_id, output_format, update_commands, language, outputTags="",
                         checked=True, displayed_records=None):
    """Returns a record in a given format

    @param record_id: the ID of record to format
    @param output_format: an output format code (or short identifier for the output format)
    @param update_commands: list of commands used to update record contents
    @param language: the language to use to format the record
    @param outputTags: the tags to be shown to the user
    @param checked: is the record checked by the user?
    @param displayed_records: records to be displayed on a given page

    @returns: record formated to be displayed or None
    """
    if update_commands and checked:
        # Modify the bibrecord object with the appropriate actions
        updated_record = _get_updated_record(record_id, update_commands)

    textmarc_options = {"aleph-marc":0, "correct-mode":1, "append-mode":0,
                        "delete-mode":0, "insert-mode":0, "replace-mode":0,
                        "text-marc":1}

    if record_id not in displayed_records:
        return

    old_record = search_engine.get_record(recid=record_id)
    old_record_textmarc = xmlmarc2textmarc.create_marc_record(old_record, sysno="", options=textmarc_options)
    if "hm" == output_format:
        if update_commands and checked:
            updated_record_textmarc = xmlmarc2textmarc.create_marc_record(updated_record, sysno="", options=textmarc_options)
            result = _get_record_diff(old_record_textmarc, updated_record_textmarc, outputTags, record_id)
        else:
            filter_tags = "All tags" not in outputTags and outputTags
            result = ['<pre>']
            for line in old_record_textmarc.splitlines():
                if not filter_tags or line.split()[0].replace('_', '') in outputTags:
                    result.append("%09d " % record_id + line.strip())
            result.append('</pre>')
            result = '\n'.join(result)
    else:
        if update_commands and checked:
            # No coloring of modifications in this case
            xml_record = bibrecord.record_xml_output(updated_record)
        else:
            xml_record = bibrecord.record_xml_output(old_record)
        result = bibformat.format_record(recID=None,
                                        of=output_format,
                                        xml_record=xml_record,
                                        ln=language)
    return result
Beispiel #21
0
def _get_record_linking_fields(recid_b, recid_a, tag, ind1, ind2):
    """
    Returns the fields (defined by tag, ind1, ind2) in record (given
    by recid_b) that do not link to another given record (recid_a).
    """
    fields = []
    rec = create_record(format_record(recid_b, "xm"))[0]
    for field_instance in record_get_field_instances(rec,
                                                     tag=tag,
                                                     ind1=ind1,
                                                     ind2=ind2):
        if not ('w', str(recid_a)) in field_instance[0]:
            fields.append(field_instance)
    return fields
Beispiel #22
0
def perform_request_display_linkbacks(status, return_code, ln=CFG_SITE_LANG):
    """
    Display linkbacks
    @param status: of CFG_WEBLINKBACK_STATUS, currently only CFG_WEBLINKBACK_STATUS['PENDING'] is supported
    """
    _ = gettext_set_language(ln)
    if status == CFG_WEBLINKBACK_STATUS['PENDING']:
        linkbacks = get_all_linkbacks(status=status, order=CFG_WEBLINKBACK_ORDER_BY_INSERTION_TIME['DESC'])
        entries = []

        for (linkbackid, origin_url, recid, additional_properties, linkback_type, linkback_status, insert_time) in linkbacks: # pylint: disable=W0612
            moderation_prefix = '<a href="moderatelinkback?action=%%s&linkbackid=%s&ln=%s">%%s</a>' % (linkbackid, ln)
            entries.append((linkback_type,
                            format_record(recID=recid, of='hs', ln=ln),
                            '<a href="%s">%s</a>' % (cgi.escape(origin_url), cgi.escape(get_url_title(origin_url))),
                            convert_datetext_to_dategui(str(insert_time)),
                            moderation_prefix % (CFG_WEBLINKBACK_ADMIN_MODERATION_ACTION['APPROVE'], 'Approve') + " / " + moderation_prefix % (CFG_WEBLINKBACK_ADMIN_MODERATION_ACTION['REJECT'], 'Reject')))

        header = ['Linkback type', 'Record', 'Origin', 'Submitted on', '']

        error_message = ""
        if return_code != CFG_WEBLINKBACK_ACTION_RETURN_CODE['OK']:
            error_message = _("Unknown error")
            if return_code == CFG_WEBLINKBACK_ACTION_RETURN_CODE['INVALID_ACTION']:
                error_message = _("Invalid action")

        error_message_html = ""
        if error_message != "":
            error_message_html = "<dt><b><font color=red>" + error_message + "</font></b></dt>" + "<br>"

        out = """
        <dl>
        %(error_message)s
        <dt>%(heading)s</dt>
        <dd>%(description)s</dd>
        </dl>
        """ % {'heading': _("Pending linkbacks"),
               'description': _("these linkbacks are not visible to users, they must be approved or rejected."),
               'error_message': error_message_html}

        if entries:
            out += tupletotable(header=header, tuple=entries, highlight_rows_p=True,
                                alternate_row_colors_p=True)
        else:
            out += "<i>There are no %s linkbacks.</i>" % status.lower()

        return addadminbox('<b>%s</b>'% _("Reduce the amount of currently pending linkback requests"), [out])
    else:
        return "<i>%s</i>" % _('Currently only pending linkbacks are supported.')
Beispiel #23
0
def _get_person_names_dicts_fallback(person_id):
    '''
    returns a dict with longest name, normalized names variations and db names variations.
    @param person_id: int personid
    @return [dict{},bool up_to_date]
    '''
    p = perform_request_search(rg=0, p='exactauthor:"%s"' % person_id)
    pcount = len(p)
    if p:
        formatted = format_record(p[0], 'XM')
        try:
            s = formatted.lower().index(person_id.lower())
            person_id = formatted[s:s + len(person_id)]
        except (IndexError, ValueError):
            pass
    return {'longest':person_id, 'names_dict':{person_id:pcount}, 'db_names_dict':{person_id:pcount}}
Beispiel #24
0
def print_record(recID, format='hb', ot='', ln=CFG_SITE_LANG, decompress=zlib.decompress,
                 search_pattern=None, user_info=None, verbose=0, sf='', so='d',
                 sp='', rm='', brief_links=True):
    """
    Print record 'recID' formatted according to 'format'.

    'sf' is sort field and 'rm' is ranking method that are passed here
    only for proper linking purposes: e.g. when a certain ranking
    method or a certain sort field was selected, keep it selected in
    any dynamic search links that may be printed.
    """
    from invenio.modules.formatter import format_record
    return format_record(
        recID, of=format, ln=ln, verbose=verbose,
        search_pattern=search_pattern
    ) if record_exists(recID) != 0 else ""
Beispiel #25
0
def datacite_register(recid):
    """
    Register a DOI for new publication

    If it fails, it will retry every 10 minutes for 1 hour.
    """
    record = get_record(recid)

    if record is None:
        logger.debug("Record %s not found" % recid)
        return

    doi_val = record.get(cfg['PIDSTORE_DATACITE_RECORD_DOI_FIELD'], None)
    logger.debug("Found DOI %s in record %s" % (doi_val, recid))

    pid = PersistentIdentifier.get("doi", doi_val)
    if not pid:
        logger.debug("DOI not locally managed.")
        return
    else:
        logger.debug("DOI locally managed.")

    if not pid.has_object("rec", recid):
        raise Exception(
            "DOI %s is not assigned to record %s." % (doi_val, recid))

    if pid.is_new() or pid.is_reserved():
        logger.info("Registering DOI %s for record %s" % (doi_val, recid))

        url = "%s/record/%s" % (
            cfg.get('PIDSTORE_DATACITE_SITE_URL', cfg['CFG_SITE_URL']),
            recid
        )
        doc = format_record(record, cfg['PIDSTORE_DATACITE_OUTPUTFORMAT'])

        if not pid.register(url=url, doc=doc):
            m = "Failed to register DOI %s" % doi_val
            logger.error(m + "\n%s\n%s" % (url, doc))
            if not datacite_register.request.is_eager:
                raise datacite_register.retry(exc=Exception(m))
        else:
            logger.info("Successfully registered DOI %s." % doi_val)
Beispiel #26
0
def datacite_register(recid):
    """
    Register a DOI for new publication

    If it fails, it will retry every 10 minutes for 1 hour.
    """
    record = get_record(recid)

    if record is None:
        logger.debug("Record %s not found" % recid)
        return

    doi_val = record.get(cfg['PIDSTORE_DATACITE_RECORD_DOI_FIELD'], None)
    logger.debug("Found DOI %s in record %s" % (doi_val, recid))

    pid = PersistentIdentifier.get("doi", doi_val)
    if not pid:
        logger.debug("DOI not locally managed.")
        return
    else:
        logger.debug("DOI locally managed.")

    if not pid.has_object("rec", recid):
        raise Exception(
            "DOI %s is not assigned to record %s." % (doi_val, recid))

    if pid.is_new() or pid.is_reserved():
        logger.info("Registering DOI %s for record %s" % (doi_val, recid))

        url = "%s/record/%s" % (
            cfg.get('PIDSTORE_DATACITE_SITE_URL', cfg['CFG_SITE_URL']),
            recid
        )
        doc = format_record(recid, cfg['PIDSTORE_DATACITE_OUTPUTFORMAT'])

        if not pid.register(url=url, doc=doc):
            m = "Failed to register DOI %s" % doi_val
            logger.error(m + "\n%s\n%s" % (url, doc))
            if not datacite_register.request.is_eager:
                raise datacite_register.retry(exc=Exception(m))
        else:
            logger.info("Successfully registered DOI %s." % doi_val)
Beispiel #27
0
    def formatter(bwo, **kwargs):
        """Return formatted data of object."""
        from invenio.modules.formatter import format_record
        try:
            deposit_object = Deposition(bwo)
        except InvalidDepositionType:
            return "This submission is disabled: {0}.".format(bwo.workflow.name)

        submission_data = deposit_object.get_latest_sip(deposit_object.submitted)

        if hasattr(submission_data, "package"):
            marcxml = submission_data.package
        else:
            return "No data found in submission (no package)."

        of = kwargs.get("of", "hd")
        if of == "xm":
            return marcxml
        else:
            return format_record(
                recID=None,
                of=of,
                xml_record=marcxml
            )
Beispiel #28
0
def print_record(recid,
                 prefix='marcxml',
                 verb='ListRecords',
                 set_spec=None,
                 set_last_updated=None):
    """Prints record 'recid' formatted according to 'prefix'.

    - if record does not exist, return nothing.

    - if record has been deleted and CFG_OAI_DELETED_POLICY is
      'transient' or 'deleted', then return only header, with status
      'deleted'.

    - if record has been deleted and CFG_OAI_DELETED_POLICY is 'no',
      then return nothing.

    """

    record_exists_result = record_exists(recid) == 1
    if record_exists_result:
        sets = get_field(recid, CFG_OAI_SET_FIELD)
        if set_spec is not None and not set_spec in sets and not [
                set_ for set_ in sets if set_.startswith("%s:" % set_spec)
        ]:
            ## the record is not in the requested set, and is not
            ## in any subset
            record_exists_result = False

    if record_exists_result:
        status = None
    else:
        status = 'deleted'

    if not record_exists_result and CFG_OAI_DELETED_POLICY not in (
            'persistent', 'transient'):
        return ""

    idents = get_field(recid, CFG_OAI_ID_FIELD)
    if not idents:
        return ""
    ## FIXME: Move these checks in a bibtask
    #try:
    #assert idents, "No OAI ID for record %s, please do your checks!" % recid
    #except AssertionError as err:
    #register_exception(alert_admin=True)
    #return ""
    #try:
    #assert len(idents) == 1, "More than OAI ID found for recid %s. Considering only the first one, but please do your checks: %s" % (recid, idents)
    #except AssertionError as err:
    #register_exception(alert_admin=True)
    ident = idents[0]

    header_body = EscapedXMLString('')
    header_body += X.identifier()(ident)
    if set_last_updated:
        header_body += X.datestamp()(max(get_modification_date(recid),
                                         set_last_updated))
    else:
        header_body += X.datestamp()(get_modification_date(recid))
    for set_spec in get_field(recid, CFG_OAI_SET_FIELD):
        if set_spec and set_spec != CFG_OAI_REPOSITORY_GLOBAL_SET_SPEC:
            # Print only if field not empty
            header_body += X.setSpec()(set_spec)

    header = X.header(status=status)(header_body)

    if verb == 'ListIdentifiers':
        return header
    else:
        if record_exists_result:
            metadata_body = format_record(recid,
                                          CFG_OAI_METADATA_FORMATS[prefix][0])
            metadata = X.metadata(body=metadata_body)
            provenance_body = get_record_provenance(recid)
            if provenance_body:
                provenance = X.about(body=provenance_body)
            else:
                provenance = ''
            rights_body = get_record_rights(recid)
            if rights_body:
                rights = X.about(body=rights_body)
            else:
                rights = ''
        else:
            metadata = ''
            provenance = ''
            rights = ''
        return X.record()(header, metadata, provenance, rights)
Beispiel #29
0
def move_drafts_articles_to_ready(journal_name, issue):
    """
    Move draft articles to their final "collection".

    To do so we rely on the convention that an admin-chosen keyword
    must be removed from the metadata
    """
    protected_datafields = ['100', '245', '246', '520', '590', '700']
    keyword_to_remove = get_journal_draft_keyword_to_remove(journal_name)
    collections_to_refresh = {}
    indexes_to_refresh = get_journal_index_to_refresh_on_release(journal_name)
    bibindex_indexes_params = []
    if indexes_to_refresh:
        bibindex_indexes_params = ['-w', ','.join(indexes_to_refresh)]

    categories = get_journal_categories(journal_name, issue)
    task_sequence_id = str(bibtask_allocate_sequenceid())
    for category in categories:
        articles = get_journal_articles(journal_name, issue, category)
        for order, recids in iteritems(articles):
            for recid in recids:
                record_xml = format_record(recid, of='xm')
                if not record_xml:
                    continue
                new_record_xml_path = os.path.join(CFG_TMPSHAREDDIR,
                                                   'webjournal_publish_' + \
                                                   str(recid) + '.xml')
                if os.path.exists(new_record_xml_path):
                    # Do not modify twice
                    continue
                record_struc = create_record(record_xml)
                record = record_struc[0]
                new_record = update_draft_record_metadata(record,
                                                          protected_datafields,
                                                          keyword_to_remove)
                new_record_xml = print_rec(new_record)
                if new_record_xml.find(keyword_to_remove) >= 0:
                    new_record_xml = new_record_xml.replace(keyword_to_remove, '')
                    # Write to file
                    new_record_xml_file = file(new_record_xml_path, 'w')
                    new_record_xml_file.write(new_record_xml)
                    new_record_xml_file.close()
                    # Submit
                    task_low_level_submission('bibupload',
                                              'WebJournal',
                                              '-c', new_record_xml_path,
                                              '-I', task_sequence_id)
                    task_low_level_submission('bibindex',
                                              'WebJournal',
                                              '-i', str(recid),
                                              '-I', task_sequence_id,
                                               *bibindex_indexes_params)
                    for collection in get_all_collections_of_a_record(recid):
                        collections_to_refresh[collection] = ''

    # Refresh collections
    collections_to_refresh.update([(c, '') for c in get_journal_collection_to_refresh_on_release(journal_name)])
    for collection in collections_to_refresh.keys():
        task_low_level_submission('webcoll',
                                  'WebJournal',
                                  '-f', '-P', '2', '-p', '1', '-c', collection,
                                  '-I', task_sequence_id)
Beispiel #30
0
def print_record(recid, prefix='marcxml', verb='ListRecords', set_spec=None, set_last_updated=None):
    """Prints record 'recid' formatted according to 'prefix'.

    - if record does not exist, return nothing.

    - if record has been deleted and CFG_OAI_DELETED_POLICY is
      'transient' or 'deleted', then return only header, with status
      'deleted'.

    - if record has been deleted and CFG_OAI_DELETED_POLICY is 'no',
      then return nothing.

    """

    record_exists_result = record_exists(recid) == 1
    if record_exists_result:
        sets = get_field(recid, CFG_OAI_SET_FIELD)
        if set_spec and not set_spec in sets and not [set_ for set_ in sets if set_.startswith("%s:" % set_spec)]:
            ## the record is not in the requested set, and is not
            ## in any subset
            record_exists_result = False

    if record_exists_result:
        status = None
    else:
        status = 'deleted'

    if not record_exists_result and CFG_OAI_DELETED_POLICY not in ('persistent', 'transient'):
        return ""

    idents = get_field(recid, CFG_OAI_ID_FIELD)
    if not idents:
        return ""
    ## FIXME: Move these checks in a bibtask
    #try:
        #assert idents, "No OAI ID for record %s, please do your checks!" % recid
    #except AssertionError as err:
        #register_exception(alert_admin=True)
        #return ""
    #try:
        #assert len(idents) == 1, "More than OAI ID found for recid %s. Considering only the first one, but please do your checks: %s" % (recid, idents)
    #except AssertionError as err:
        #register_exception(alert_admin=True)
    ident = idents[0]

    header_body = EscapedXMLString('')
    header_body += X.identifier()(ident)
    if set_last_updated:
        header_body += X.datestamp()(max(get_modification_date(recid), set_last_updated))
    else:
        header_body += X.datestamp()(get_modification_date(recid))
    for set_spec in get_field(recid, CFG_OAI_SET_FIELD):
        if set_spec and set_spec != CFG_OAI_REPOSITORY_GLOBAL_SET_SPEC:
            # Print only if field not empty
            header_body += X.setSpec()(set_spec)

    header = X.header(status=status)(header_body)

    if verb == 'ListIdentifiers':
        return header
    else:
        if record_exists_result:
            metadata_body = format_record(recid, CFG_OAI_METADATA_FORMATS[prefix][0])
            metadata = X.metadata(body=metadata_body)
            provenance_body = get_record_provenance(recid)
            if provenance_body:
                provenance = X.about(body=provenance_body)
            else:
                provenance = ''
            rights_body = get_record_rights(recid)
            if rights_body:
                rights = X.about(body=rights_body)
            else:
                rights = ''
        else:
            metadata = ''
            provenance = ''
            rights = ''
        return X.record()(header, metadata, provenance, rights)
 def test_format_record_no_recid(self):
     from invenio.modules.formatter import format_record
     result = format_record(recID=None, of="test6",
                            xml_record=self.no_001_record_xml)
     self.assertEqual(result, "helloworld\n")
Beispiel #32
0
def move_drafts_articles_to_ready(journal_name, issue):
    """
    Move draft articles to their final "collection".

    To do so we rely on the convention that an admin-chosen keyword
    must be removed from the metadata
    """
    protected_datafields = ['100', '245', '246', '520', '590', '700']
    keyword_to_remove = get_journal_draft_keyword_to_remove(journal_name)
    collections_to_refresh = {}
    indexes_to_refresh = get_journal_index_to_refresh_on_release(journal_name)
    bibindex_indexes_params = []
    if indexes_to_refresh:
        bibindex_indexes_params = ['-w', ','.join(indexes_to_refresh)]

    categories = get_journal_categories(journal_name, issue)
    task_sequence_id = str(bibtask_allocate_sequenceid())
    for category in categories:
        articles = get_journal_articles(journal_name, issue, category)
        for order, recids in iteritems(articles):
            for recid in recids:
                record_xml = format_record(recid, of='xm')
                if not record_xml:
                    continue
                new_record_xml_path = os.path.join(CFG_TMPSHAREDDIR,
                                                   'webjournal_publish_' + \
                                                   str(recid) + '.xml')
                if os.path.exists(new_record_xml_path):
                    # Do not modify twice
                    continue
                record_struc = create_record(record_xml)
                record = record_struc[0]
                new_record = update_draft_record_metadata(record,
                                                          protected_datafields,
                                                          keyword_to_remove)
                new_record_xml = print_rec(new_record)
                if new_record_xml.find(keyword_to_remove) >= 0:
                    new_record_xml = new_record_xml.replace(keyword_to_remove, '')
                    # Write to file
                    new_record_xml_file = file(new_record_xml_path, 'w')
                    new_record_xml_file.write(new_record_xml)
                    new_record_xml_file.close()
                    # Submit
                    task_low_level_submission('bibupload',
                                              'WebJournal',
                                              '-c', new_record_xml_path,
                                              '-I', task_sequence_id)
                    task_low_level_submission('bibindex',
                                              'WebJournal',
                                              '-i', str(recid),
                                              '-I', task_sequence_id,
                                               *bibindex_indexes_params)
                    for collection in get_all_collections_of_a_record(recid):
                        collections_to_refresh[collection] = ''

    # Refresh collections
    collections_to_refresh.update([(c, '') for c in get_journal_collection_to_refresh_on_release(journal_name)])
    for collection in collections_to_refresh.keys():
        task_low_level_submission('webcoll',
                                  'WebJournal',
                                  '-f', '-P', '2', '-p', '1', '-c', collection,
                                  '-I', task_sequence_id)
Beispiel #33
0
 def _format_record(recid, of='hd', user_info=current_user, *args, **kwargs):
     from invenio.modules.formatter import format_record
     return format_record(recid, of, user_info=user_info, *args, **kwargs)
 def test_format_2_passes(self):
     from invenio.modules.formatter import format_record
     result = format_record(recID=None, of="test6", xml_record=self.xml_text)
     self.assertEqual(result, "helloworld\n")
Beispiel #35
0
 def test_format_2_passes(self):
     from invenio.modules.formatter import format_record
     result = format_record(recID=None,
                            of="test6",
                            xml_record=self.xml_text)
     self.assertEqual(result, "helloworld\n")
Beispiel #36
0
def perform_request_display_linkbacks(status, return_code, ln=CFG_SITE_LANG):
    """
    Display linkbacks
    @param status: of CFG_WEBLINKBACK_STATUS, currently only CFG_WEBLINKBACK_STATUS['PENDING'] is supported
    """
    _ = gettext_set_language(ln)
    if status == CFG_WEBLINKBACK_STATUS['PENDING']:
        linkbacks = get_all_linkbacks(
            status=status,
            order=CFG_WEBLINKBACK_ORDER_BY_INSERTION_TIME['DESC'])
        entries = []

        for (linkbackid, origin_url, recid, additional_properties,
             linkback_type, linkback_status, insert_time) in linkbacks:  # pylint: disable=W0612
            moderation_prefix = '<a href="moderatelinkback?action=%%s&linkbackid=%s&ln=%s">%%s</a>' % (
                linkbackid, ln)
            entries.append((
                linkback_type, format_record(recID=recid, of='hs',
                                             ln=ln), '<a href="%s">%s</a>' %
                (cgi.escape(origin_url), cgi.escape(
                    get_url_title(origin_url))),
                convert_datetext_to_dategui(str(insert_time)),
                moderation_prefix %
                (CFG_WEBLINKBACK_ADMIN_MODERATION_ACTION['APPROVE'], 'Approve')
                + " / " + moderation_prefix %
                (CFG_WEBLINKBACK_ADMIN_MODERATION_ACTION['REJECT'], 'Reject')))

        header = ['Linkback type', 'Record', 'Origin', 'Submitted on', '']

        error_message = ""
        if return_code != CFG_WEBLINKBACK_ACTION_RETURN_CODE['OK']:
            error_message = _("Unknown error")
            if return_code == CFG_WEBLINKBACK_ACTION_RETURN_CODE[
                    'INVALID_ACTION']:
                error_message = _("Invalid action")

        error_message_html = ""
        if error_message != "":
            error_message_html = "<dt><b><font color=red>" + error_message + "</font></b></dt>" + "<br>"

        out = """
        <dl>
        %(error_message)s
        <dt>%(heading)s</dt>
        <dd>%(description)s</dd>
        </dl>
        """ % {
            'heading':
            _("Pending linkbacks"),
            'description':
            _("these linkbacks are not visible to users, they must be approved or rejected."
              ),
            'error_message':
            error_message_html
        }

        if entries:
            out += tupletotable(header=header,
                                tuple=entries,
                                highlight_rows_p=True,
                                alternate_row_colors_p=True)
        else:
            out += "<i>There are no %s linkbacks.</i>" % status.lower()

        return addadminbox(
            '<b>%s</b>' %
            _("Reduce the amount of currently pending linkback requests"),
            [out])
    else:
        return "<i>%s</i>" % _(
            'Currently only pending linkbacks are supported.')
Beispiel #37
0
 def test_format_record_no_recid(self):
     from invenio.modules.formatter import format_record
     result = format_record(recID=None,
                            of="test6",
                            xml_record=self.no_001_record_xml)
     self.assertEqual(result, "helloworld\n")
Beispiel #38
0
    def detailed_record_container_top(self,
                                      recid,
                                      tabs,
                                      ln=CFG_SITE_LANG,
                                      show_similar_rec_p=True,
                                      creationdate=None,
                                      modificationdate=None,
                                      show_short_rec_p=True,
                                      citationnum=-1,
                                      referencenum=-1,
                                      discussionnum=-1,
                                      include_jquery=False,
                                      include_mathjax=False):
        """Prints the box displayed in detailed records pages, with tabs at the top.

        Returns content as it is if the number of tabs for this record
        is smaller than 2

           Parameters:

        @param recid: int - the id of the displayed record
        @param tabs: ** - the tabs displayed at the top of the box.
        @param ln: *string* - the language of the page in which the box is displayed
        @param show_similar_rec_p: *bool* print 'similar records' link in the box
        @param creationdate: *string* - the creation date of the displayed record
        @param modificationdate: *string* - the last modification date of the displayed record
        @param show_short_rec_p: *boolean* - prints a very short version of the record as reminder.
        @param citationnum: show (this) number of citations in the citations tab
        @param referencenum: show (this) number of references in the references tab
        @param discussionnum: show (this) number of comments/reviews in the discussion tab
        """
        from invenio.legacy.search_engine import \
             get_restricted_collections_for_recid, \
             is_record_in_any_collection

        # load the right message language
        _ = gettext_set_language(ln)

        # Prepare restriction flag
        restriction_flag = ''
        if get_restricted_collections_for_recid(
                recid, recreate_cache_if_needed=False):
            restriction_flag = '<div class="restrictedflag"><span>%s</span></div>' % _(
                "Restricted")
        elif not is_record_in_any_collection(recid,
                                             recreate_cache_if_needed=False):
            restriction_flag = '<div class="restrictedflag restrictedflag-pending"><span>%s</span></div>' % _(
                "Restricted (Processing Record)")

        # If no tabs, returns nothing (excepted if restricted)
        if len(tabs) <= 1:
            return restriction_flag

        # Build the tabs at the top of the page
        out_tabs = ''
        if len(tabs) > 1:
            first_tab = True
            for (label, url, selected, enabled) in tabs:
                addnum = ""
                if (citationnum > -1) and url.count("/citation") == 1:
                    addnum = "(" + str(citationnum) + ")"
                if (referencenum > -1) and url.count("/references") == 1:
                    addnum = "(" + str(referencenum) + ")"
                if (discussionnum > -1) and url.count("/comments") == 1:
                    addnum = "(" + str(discussionnum) + ")"

                css_class = []
                if selected:
                    css_class.append('on')
                if first_tab:
                    css_class.append('first')
                    first_tab = False
                if not enabled:
                    css_class.append('disabled')
                css_class = ' class="%s"' % ' '.join(css_class)
                if not enabled:
                    out_tabs += '<li%(class)s><a>%(label)s %(addnum)s</a></li>' % \
                                {'class':css_class,
                                 'label':label,
                                 'addnum':addnum}
                else:
                    out_tabs += '<li%(class)s><a href="%(url)s">%(label)s %(addnum)s </a></li>' % \
                                {'class':css_class,
                                 'url':url,
                                 'label':label,
                                 'addnum':addnum}
        if out_tabs != '':
            out_tabs = '''        <div class="detailedrecordtabs">
            <div>
                <ul class="detailedrecordtabs">%s</ul>
            <div id="tabsSpacer" style="clear:both;height:0px">&nbsp;</div></div>
        </div>''' % out_tabs

        # Add the clip icon and the brief record reminder if necessary
        record_brief = ''
        if show_short_rec_p:
            record_brief = format_record(recID=recid, of='hs', ln=ln)
            record_brief = '''<div id="detailedrecordshortreminder">
                             <div id="clip">&nbsp;</div>
                             <div id="HB">
                                 %(record_brief)s
                             </div>
                         </div>
                         <div style="clear:both;height:1px">&nbsp;</div>
                         ''' % {
                'record_brief': record_brief
            }

        additional_scripts = ""
        if include_jquery:
            additional_scripts += """<script type="text/javascript" src="%s/js/jquery.min.js">' \
            '</script>\n""" % (CFG_BASE_URL, )
        if include_mathjax:

            additional_scripts += get_mathjax_header()

        # Print the content
        out = """
        %(additional_scripts)s<div class="detailedrecordbox">
        %(tabs)s
        <div class="detailedrecordboxcontent">
            <div class="top-left-folded"></div>
            <div class="top-right-folded"></div>
            <div class="inside">
                <!--<div style="height:0.1em;">&nbsp;</div>
                <p class="notopgap">&nbsp;</p>-->
                %(record_brief)s
                """ % {
            'additional_scripts': additional_scripts,
            'tabs': out_tabs,
            'record_brief': record_brief
        }

        out = restriction_flag + out
        return out
Beispiel #39
0
    def detailed_record_container_top(self, recid, tabs, ln=CFG_SITE_LANG,
                                      show_similar_rec_p=True,
                                      creationdate=None,
                                      modificationdate=None, show_short_rec_p=True,
                                      citationnum=-1, referencenum=-1, discussionnum=-1,
                                      include_jquery = False, include_mathjax = False):
        """Prints the box displayed in detailed records pages, with tabs at the top.

        Returns content as it is if the number of tabs for this record
        is smaller than 2

           Parameters:

        @param recid: int - the id of the displayed record
        @param tabs: ** - the tabs displayed at the top of the box.
        @param ln: *string* - the language of the page in which the box is displayed
        @param show_similar_rec_p: *bool* print 'similar records' link in the box
        @param creationdate: *string* - the creation date of the displayed record
        @param modificationdate: *string* - the last modification date of the displayed record
        @param show_short_rec_p: *boolean* - prints a very short version of the record as reminder.
        @param citationnum: show (this) number of citations in the citations tab
        @param referencenum: show (this) number of references in the references tab
        @param discussionnum: show (this) number of comments/reviews in the discussion tab
        """
        from invenio.modules.collections.cache import get_all_restricted_recids
        from invenio.modules.collections.cache import is_record_in_any_collection

        # load the right message language
        _ = gettext_set_language(ln)

        # Prepare restriction flag
        restriction_flag = ''
        if recid in get_all_restricted_recids():
            restriction_flag = '<div class="restrictedflag"><span>%s</span></div>' % _("Restricted")
        elif not is_record_in_any_collection(recid, recreate_cache_if_needed=False):
            restriction_flag = '<div class="restrictedflag restrictedflag-pending"><span>%s</span></div>' % _("Restricted (Processing Record)")

        # If no tabs, returns nothing (excepted if restricted)
        if len(tabs) <= 1:
            return restriction_flag

        # Build the tabs at the top of the page
        out_tabs = ''
        if len(tabs) > 1:
            first_tab = True
            for (label, url, selected, enabled) in tabs:
                addnum = ""
                if (citationnum > -1) and url.count("/citation") == 1:
                    addnum = "(" + str(citationnum) + ")"
                if (referencenum > -1) and url.count("/references") == 1:
                    addnum = "(" + str(referencenum) + ")"
                if (discussionnum > -1) and url.count("/comments") == 1:
                    addnum = "(" + str(discussionnum) + ")"

                css_class = []
                if selected:
                    css_class.append('on')
                if first_tab:
                    css_class.append('first')
                    first_tab = False
                if not enabled:
                    css_class.append('disabled')
                css_class = ' class="%s"' % ' '.join(css_class)
                if not enabled:
                    out_tabs += '<li%(class)s><a>%(label)s %(addnum)s</a></li>' % \
                                {'class':css_class,
                                 'label':label,
                                 'addnum':addnum}
                else:
                    out_tabs += '<li%(class)s><a href="%(url)s">%(label)s %(addnum)s </a></li>' % \
                                {'class':css_class,
                                 'url':url,
                                 'label':label,
                                 'addnum':addnum}
        if out_tabs != '':
            out_tabs = '''        <div class="detailedrecordtabs">
            <div>
                <ul class="detailedrecordtabs">%s</ul>
            <div id="tabsSpacer" style="clear:both;height:0px">&nbsp;</div></div>
        </div>''' % out_tabs


        # Add the clip icon and the brief record reminder if necessary
        record_brief = ''
        if show_short_rec_p:
            record_brief = format_record(recID=recid, of='hs', ln=ln)
            record_brief = '''<div id="detailedrecordshortreminder">
                             <div id="clip">&nbsp;</div>
                             <div id="HB">
                                 %(record_brief)s
                             </div>
                         </div>
                         <div style="clear:both;height:1px">&nbsp;</div>
                         ''' % {'record_brief': record_brief}

        additional_scripts = ""
        if include_jquery:
            additional_scripts += """<script type="text/javascript" src="%s/js/jquery.min.js">' \
            '</script>\n""" % (CFG_BASE_URL, )
        if include_mathjax:

            additional_scripts += get_mathjax_header()


        # Print the content
        out = """
        %(additional_scripts)s<div class="detailedrecordbox">
        %(tabs)s
        <div class="detailedrecordboxcontent">
            <div class="top-left-folded"></div>
            <div class="top-right-folded"></div>
            <div class="inside">
                <!--<div style="height:0.1em;">&nbsp;</div>
                <p class="notopgap">&nbsp;</p>-->
                %(record_brief)s
                """ % {'additional_scripts': additional_scripts,
                       'tabs':out_tabs,
                       'record_brief':record_brief}

        out = restriction_flag + out
        return out
Beispiel #40
0
def format_record_1st_pass(recID, of, ln=CFG_SITE_LANG, verbose=0,
                           search_pattern=None, xml_record=None,
                           user_info=None, on_the_fly=False,
                           save_missing=True, **kwargs):
    """
    Format a record in given output format.

    Return a formatted version of the record in the specified
    language, search pattern, and with the specified output format.
    The function will define which format template must be applied.

    The record to be formatted can be specified with its ID (with
    'recID' parameter) or given as XML representation (with
    'xml_record' parameter). If 'xml_record' is specified 'recID' is
    ignored (but should still be given for reference. A dummy recid 0
    or -1 could be used).

    'user_info' allows to grant access to some functionalities on a
    page depending on the user's priviledges. The 'user_info' object
    makes sense only in the case of on-the-fly formatting. 'user_info'
    is the same object as the one returned by
    'webuser.collect_user_info(req)'

    :param recID: the ID of record to format.
    @type recID: int
    :param of: an output format code (or short identifier for the output format)
    @type of: string
    :param ln: the language to use to format the record
    @type ln: string
    :param verbose: the level of verbosity from 0 to 9 (O: silent,
                                                       5: errors,
                                                       7: errors and warnings, stop if error in format elements
                                                       9: errors and warnings, stop if error (debug mode ))
    @type verbose: int
    :param search_pattern: list of strings representing the user request in web interface
    @type search_pattern: list(string)
    :param xml_record: an xml string represention of the record to format
    @type xml_record: string or None
    :param user_info: the information of the user who will view the formatted page (if applicable)
    :param on_the_fly: if False, try to return an already preformatted version of the record in the database
    @type on_the_fly: boolean
    @return: formatted record
    @rtype: string
    """
    if search_pattern is None:
        search_pattern = []

    out = ""

    try:
        out_, needs_2nd_pass = format_record(recID=recID,
                                             of=of,
                                             ln=ln,
                                             verbose=verbose,
                                             search_pattern=search_pattern,
                                             xml_record=xml_record,
                                             user_info=user_info,
                                             **kwargs)
        out += out_

        if of.lower() in ('xm', 'xoaimarc'):
            out = filter_hidden_fields(out, user_info, force_filtering=of.lower()=='xoaimarc')

        return out, needs_2nd_pass
    except Exception:
        current_app.logger.exception(
            "An error occured while formatting record {recid} in {of}".format(
                recid=recID, of=of
            ))
        raise
Beispiel #41
0
    def get(self):
        # Temporarily disable search until fully tested.
        abort(405)

        from invenio.legacy.search_engine import perform_request_search, \
            record_exists, check_user_can_view_record

        given_mimetype = request.headers.get('Accept', 'application/json')
        output_format = self.mimetypes.get(given_mimetype)
        if output_format is None:
            raise RecordUnsuppotedMediaTypeError(
                message="Output format {} is not supported.".format(
                    given_mimetype
                ))

        # get URL parameters
        query = request.args.get('query', '')
        sort_field = request.args.get('sort_field', 'title')
        sort_order = request.args.get('sort_order', 'a')
        page = int(request.args.get('page', 1))
        per_page = int(request.args.get('per_page', 5))

        if page < 0:
            raise RecordError(
                message="Invalid page {}".format(page),
                status=400
            )

        if per_page < 0:
            raise RecordError(
                message="Invalid per_page {}".format(per_page),
                status=400
            )

        rec_ids = perform_request_search(p=query, sf=sort_field,
                                         so=sort_order, of='id')
        rec_ids_to_keep = []
        for recid in rec_ids:
            if record_exists(recid) > 0:
                (auth_code, auth_mesg) = check_user_can_view_record(
                    current_user, recid)
                if auth_code == 0:
                    rec_ids_to_keep.append(recid)
        records_in_requested_format = []
        if rec_ids_to_keep:
            for recid in rec_ids_to_keep:
                result = format_record(recID=recid, of=output_format)
                records_in_requested_format.append(result)

        records_to_return = []
        headers = {}
        if records_in_requested_format:
            p = pagination.RestfulPagination(
                page=page,
                per_page=per_page,
                total_count=len(records_in_requested_format)
            )
            if (page > p.pages):
                raise RecordError(
                    message="Invalid page {}".format(page),
                    status=400
                )
            records_to_return = p.slice(records_in_requested_format)
            kwargs = {}
            kwargs['endpoint'] = request.endpoint
            kwargs['args'] = request.args
            link_header = p.link_header(**kwargs)
            headers[link_header[0]] = link_header[1]
        return (json.dumps(records_to_return), 200, headers)