preprints.py 4.15 KB
Newer Older
1 2 3 4 5 6
""" harvest_tools.preprints

"""
import traceback


7 8 9
from .automaton import Automaton
from .base import MSG_CRASH, MSG_LOAD
from .checkandfix import CheckException
10
from invenio_tools import RecordConf, RecordThesis
11
from plugin_dbui import get_id, UNDEF_ID
12 13


14 15 16 17
MSG_PREPRINT_IS_PAPER = "Reject preprint is a published paper"
MSG_PREPRINT_IS_CONFERENCE = "Reject preprint is a conference"
MSG_PREPRINT_IS_THESIS = "Reject preprint is a thesis"
MSG_PREPRINT_NO_NUMBER = "Reject no preprint number"
18 19


20
class Preprints(Automaton):
21
    """Automaton for preprints.
22 23

    """
24 25 26
    def check_record(self, record):
        """Check the content of the preprint in order to fix non conformities.

27
        Args:
LE GAC Renaud's avatar
LE GAC Renaud committed
28 29
            record (RecordPubli):
                record describing a preprint.
30

31
        Returns:
LE GAC Renaud's avatar
LE GAC Renaud committed
32 33
            bool:
                ``False`` when a non conformity is found and can not be
34
                corrected.
35 36 37 38 39 40

        """
        if not Automaton.check_record(self, record):
            return False

        if self.dbg:
41
            print("check preprint record")
42 43

        if record.is_published():
44
            self.logs[-1].reject(MSG_PREPRINT_IS_PAPER, record=record)
45 46 47
            return False

        if isinstance(record, RecordConf):
48
            self.logs[-1].reject(MSG_PREPRINT_IS_CONFERENCE, record=record)
49 50 51
            return False

        if isinstance(record, RecordThesis):
52
            self.logs[-1].reject(MSG_PREPRINT_IS_THESIS, record=record)
53 54 55
            return False

        if not record.preprint_number():
56
            self.logs[-1].reject(MSG_PREPRINT_NO_NUMBER, record=record)
57 58 59 60 61
            return False

        try:
            self.check.submitted(record)

62 63
            self.check.format_authors(record, fmt="F. Last")
            self.check.get_my_authors(record, sort=True)
64

65
        except CheckException as e:
66
            self.logs[-1].reject(e, record=record)
67 68 69
            return False

        except Exception as e:
70
            self.logs[-1].reject(MSG_CRASH % e, record=record, translate=False)
71
            print(traceback.format_exc())
72 73 74
            return False

        return True
75

76 77
    def insert_record(self, record):
        """Insert a preprint in the database.
78

79
        Args:
LE GAC Renaud's avatar
LE GAC Renaud committed
80 81
            record (RecordPubli):
                record describing a preprint.
82

83
        Returns:
LE GAC Renaud's avatar
LE GAC Renaud committed
84 85
            int:
                one when the record is inserted / updated in the database
86
                zero otherwise.
87 88

        """
89 90
        db = self.db

91 92 93 94 95
        # alias
        first_author = record.first_author()
        oai_url = record.oai_url()
        preprint = record.preprint_number()
        title = record.title()
LE GAC Renaud's avatar
LE GAC Renaud committed
96 97
        submitted = record.submitted()
        year = submitted[0:4]
98

99
        # get the collaboration identifier
100 101
        id_collaboration = \
            get_id(db.collaborations, collaboration=record.collaboration())
102

103
        # get existing preprint or article
LE GAC Renaud's avatar
LE GAC Renaud committed
104 105 106 107 108
        fields = dict(first_author=first_author,
                      id_projects=self.id_project,
                      id_teams=self.id_team,
                      preprint=preprint,
                      submitted=submitted,
109
                      title=title)
LE GAC Renaud's avatar
LE GAC Renaud committed
110

111
        rec_id, status = self.get_record_by_fields(oai_url, year, **fields)
112 113 114 115
        if rec_id:
            return status

        # eventually insert a new preprint
116
        ret = 1
117
        if not self.dry_run:
LE GAC Renaud's avatar
LE GAC Renaud committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
            fields = dict(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)

            ret = self._insert_in_db(log_year=year, **fields)
134 135 136 137 138 139

        if ret == 1:
            self.logs[-1].load(MSG_LOAD, year)
            return 1

        return 0