preprints.py 4.41 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
# -*- coding: utf-8 -*-
""" harvest_tools.preprints

"""
import traceback


from base import family_name_fr, MSG_CRASH, MSG_LOAD
from gluon import current
from invenio_tools import CheckException
from publicationstool import PublicationsTool
from plugin_dbui import UNDEF_ID


MSG_PREPRINT_IS_PAPER = \
    current.T("Reject preprint is a published paper", lazy=False)
MSG_PREPRINT_IS_CONFERENCE = \
    current.T("Reject preprint is a conference", lazy=False)
MSG_PREPRINT_IS_THESIS = current.T("Reject preprint is a thesis", lazy=False)
MSG_PREPRINT_NO_NUMBER = current.T("Reject no preprint number", lazy=False)


class Preprints(PublicationsTool):
    """Publications tool for preprints.

    """

    def load_db(self, record):
        """Load a preprint in the database.

        @type record: L{Record}
        @param record:

        @rtype: int
        @return: one when the record is inserted / updated in the database
        zero otherwise.

        """
        db = self.db

        # alias
        first_author = record.first_author()
        oai_url = record.oai_url()
        preprint = record.preprint_number()
        title = record.title()
        submitted = record.submitted()[0]
        year = record.year()

        # check the collaboration
        id_collaboration = self.check_collaboration(record.collaboration())

        # check against preprint or article already published
        rec_id, status = self.check_by_origin(oai_url=oai_url, year=year)
        if rec_id:
            return status

        rec_id, status = self.check_by_fields(first_author=first_author,
                                              id_projects=self.id_project,
                                              id_teams=self.id_team,
                                              oai_url=oai_url,
                                              preprint=preprint,
                                              submitted=submitted,
                                              title=title,
                                              year=year)
        if rec_id:
            return status

        # eventually insert a new preprint
        if not self.dry_run:

            db.publications.insert(authors=record.authors(),
                                   authors_institute=record.my_authors,
                                   first_author=first_author,
                                   id_categories=self.id_category,
                                   id_collaborations=id_collaboration,
                                   id_projects=self.id_project,
                                   id_status=UNDEF_ID,
                                   id_teams=self.id_team,
                                   origin=oai_url,
                                   preprint=preprint,
                                   publication_url=record.paper_url(),
                                   submitted=submitted,
                                   title=title,
                                   year=year)

        self.logs[-1].load(MSG_LOAD, year)
        return 1

    def select_record(self, record):
        """C{True} when the preprint is valid.

        @type record: L{Record}
        @param record:

        """
        if not PublicationsTool.select_record(self, record):
            return False

        if self.dbg:
            print "select preprint record"

        if record.is_published():
            self.logs[-1].reject(MSG_PREPRINT_IS_PAPER, record.year())
            return False

        if record.is_conference_data():
            self.logs[-1].reject(MSG_PREPRINT_IS_CONFERENCE, record.year())
            return False

        if record.is_thesis():
            self.logs[-1].reject(MSG_PREPRINT_IS_THESIS, record.year())
            return False

        if not record.preprint_number():
            self.logs[-1].reject(MSG_PREPRINT_NO_NUMBER, record.year())
            return False

        try:
            self.check.my_authors(record,
                                  reference=self._my_author_list(record),
                                  cmpFct=family_name_fr)

            self.check.oai(record)
            self.check.submitted(record)
            self.check.year(record)

        except CheckException as e:
            self.logs[-1].reject(e, record.year())
            return False

        except BaseException as e:
            self.logs[-1].reject(MSG_CRASH % e, record.year())
            print traceback.format_exc()
            return False

        return True