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

"""
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
from invenio_tools import CheckException
LE GAC Renaud's avatar
LE GAC Renaud committed
11
from plugin_dbui import UNDEF_ID
12 13


14
class Notes(Automaton):
15
    """Automaton for notes.
16 17

    """
18 19 20
    def check_record(self, record):
        """Check the content of the note in order to fix non conformities.

21 22
        Args:
            record (RecordPubli): record describing a note
23

24 25 26
        Returns:
            bool: ``False`` when a non conformity is found and can not be
                corrected.
27 28 29 30 31 32

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

        if self.dbg:
LE GAC Renaud's avatar
LE GAC Renaud committed
33
            print "check note record"
34 35 36 37 38

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

39 40 41
            self.check.format_authors(record, format_author_fr)
            self.check.get_my_authors(record, family_name_fr)

42
        except CheckException as e:
43
            self.logs[-1].reject(e, record=record)
44 45 46
            return False

        except Exception as e:
47
            self.logs[-1].reject(MSG_CRASH % e, record=record, translate=False)
48 49 50 51
            print traceback.format_exc()
            return False

        return True
52

53
    def insert_record(self, record):
54
        """Insert a public note in the database.
55

56 57
        Args:
            record (RecordPubli): record describing a note.
58

59 60 61
        Returns:
            int: one when the record is inserted / updated in the database
                zero otherwise.
62 63 64 65 66 67 68 69

        """
        # alias
        first_author = record.first_author()
        oai_url = record.oai_url()
        title = record.title()
        year = record.year()

70
        # get existing notes
LE GAC Renaud's avatar
LE GAC Renaud committed
71 72 73 74
        fields = dict(first_author=first_author,
                      id_categories=self.id_category,
                      id_projects=self.id_project,
                      id_teams=self.id_team,
75
                      title=title)
LE GAC Renaud's avatar
LE GAC Renaud committed
76

77
        rec_id, status = self.get_record_by_fields(oai_url, year, **fields)
78 79 80 81
        if rec_id:
            return status

        # eventually insert a new report
82
        ret = 1
83
        if not self.dry_run:
LE GAC Renaud's avatar
LE GAC Renaud committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
            fields = dict(authors=record.authors(),
                          authors_institute=record.my_authors,
                          first_author=first_author,
                          id_categories=self.id_category,
                          id_projects=self.id_project,
                          id_status=UNDEF_ID,
                          id_teams=self.id_team,
                          origin=oai_url,
                          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)
99 100 101 102 103 104

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

        return 0