reports.py 3.79 KB
Newer Older
1 2 3 4 5 6
""" harvest_tools.reports

"""
import traceback


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


13
MSG_REPORT_NO_NUMBER = "Reject no report number"
14 15


16
class Reports(Automaton):
17
    """Automaton for reports to committee.
18 19

    """
20 21 22
    def check_record(self, record):
        """Check the content of the report in order to fix non conformities.

23
        Args:
LE GAC Renaud's avatar
LE GAC Renaud committed
24 25
            record (RecordPubli):
                record describing a report.
26

27
        Returns:
LE GAC Renaud's avatar
LE GAC Renaud committed
28 29
            bool:
                ``False`` when a non conformity is found and can not be
30
                corrected.
31 32 33 34 35 36

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

        if self.dbg:
37
            print("check report record")
38 39

        if not record.report_number():
40
            self.logs[-1].reject(MSG_REPORT_NO_NUMBER, record=record)
41 42 43 44 45
            return False

        try:
            self.check.submitted(record)

46 47
            self.check.format_authors(record, fmt="F. Last")
            self.check.get_my_authors(record, sort=True)
48

49
        except CheckException as e:
50
            self.logs[-1].reject(e, record=record)
51 52 53
            return False

        except Exception as e:
54
            self.logs[-1].reject(MSG_CRASH % e, record=record, translate=False)
55
            print(traceback.format_exc())
56 57 58
            return False

        return True
59

60
    def insert_record(self, record):
61
        """Insert a report in the database.
62

63
        Args:
LE GAC Renaud's avatar
LE GAC Renaud committed
64 65
            record (RecordPubli):
                record describing a report.
66

67
        Returns:
LE GAC Renaud's avatar
LE GAC Renaud committed
68 69 70
            int:
                one when the record is inserted / updated in the database
                zero otherwise.
71 72 73 74 75 76 77 78 79 80

        """
        db = self.db

        # alias
        authors = record.authors()
        first_author = record.first_author()
        id_status = UNDEF_ID
        oai_url = record.oai_url()
        title = record.title()
LE GAC Renaud's avatar
LE GAC Renaud committed
81
        year = record.submitted()[0:4]
82 83 84

        # allow undefined institute authors
        try:
85
            self.check.get_my_authors(record, sort=True)
86 87 88 89 90 91
            authors_institute = record.my_authors

        except CheckException:
            authors_institute = UNKNOWN
            id_status = get_id(db.status, code=UNKNOWN)

92
        # get the collaboration identifier
93 94
        id_collaboration = \
            get_id(db.collaborations, collaboration=record.collaboration())
95

96
        # get an already published reports
LE GAC Renaud's avatar
LE GAC Renaud committed
97 98 99
        fields = dict(id_categories=self.id_category,
                      id_projects=self.id_project,
                      id_teams=self.id_team,
100
                      title=title)
LE GAC Renaud's avatar
LE GAC Renaud committed
101

102
        rec_id, status = self.get_record_by_fields(oai_url, year, **fields)
103 104 105 106
        if rec_id:
            return status

        # eventually insert a new report
107
        ret = 1
108
        if not self.dry_run:
LE GAC Renaud's avatar
LE GAC Renaud committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
            fields = dict(authors=authors,
                          authors_institute=authors_institute,
                          first_author=first_author,
                          id_categories=self.id_category,
                          id_collaborations=id_collaboration,
                          id_projects=self.id_project,
                          id_status=id_status,
                          id_teams=self.id_team,
                          origin=oai_url,
                          preprint=record.preprint_number(),
                          publication_url=record.paper_url(),
                          report_numbers=record.report_number(),
                          submitted=record.submitted()[0],
                          title=title,
                          year=year)

            ret = self._insert_in_db(log_year=year, **fields)
126 127 128 129 130 131

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

        return 0