reports.py 3.9 KB
Newer Older
1 2 3 4 5 6 7
# -*- coding: utf-8 -*-
""" harvest_tools.reports

"""
import traceback


8
from automaton import Automaton
LE GAC Renaud's avatar
LE GAC Renaud committed
9
from base import family_name_fr, format_author_fr, MSG_CRASH, MSG_LOAD
10 11 12 13
from invenio_tools import CheckException
from plugin_dbui import get_id, UNDEF_ID, UNKNOWN


14
MSG_REPORT_NO_NUMBER = "Reject no report number"
15 16


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

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

24 25
        Args:
            record (RecordPubli): record describing a report.
26

27 28 29
        Returns:
            bool: ``False`` when a non conformity is found and can not be
                corrected.
30 31 32 33 34 35

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

        if self.dbg:
LE GAC Renaud's avatar
LE GAC Renaud committed
36
            print "check report record"
37 38

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

        try:
            self.check.submitted(record)
            self.check.year(record)

46 47 48
            self.check.format_authors(record, format_author_fr)
            self.check.get_my_authors(record, family_name_fr)

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 56 57 58
            print traceback.format_exc()
            return False

        return True
59

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

63 64
        Args:
            record (RecordPubli): record describing a report.
65

66 67 68
        Returns:
            int: one when the record is inserted / updated in the database
            zero otherwise.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

        """
        db = self.db

        # alias
        authors = record.authors()
        first_author = record.first_author()
        id_status = UNDEF_ID
        oai_url = record.oai_url()
        title = record.title()
        year = record.year()

        # allow undefined institute authors
        try:
            self.check.my_authors(record,
                                  reference=self._my_author_list(record),
                                  cmpFct=family_name_fr)
            authors_institute = record.my_authors

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

92
        # get the collaboration identifier
93
        id_collaboration = self.search_collaboration(record.collaboration())
94

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

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

        # eventually insert a new report
106
        ret = 1
107
        if not self.dry_run:
LE GAC Renaud's avatar
LE GAC Renaud committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
            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)
125 126 127 128 129 130

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

        return 0