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

"""
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
11
from plugin_dbui import UNDEF_ID
12 13


14
class Talks(Automaton):
15
    """Automaton for conference talks.
16 17

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

21 22
        Args:
            record (RecordConf): record describing a conference.
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 talk record"
34 35 36

        try:
            self.check.is_conference(record)
37 38
            self.check.country(record)
            self.check.conference_date(record)
39 40 41 42

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

43 44 45
            self.check.format_authors(record, format_author_fr)
            self.check.get_my_authors(record, family_name_fr)

46
        except CheckException as e:
47
            self.logs[-1].reject(e, record=record)
48 49 50
            return False

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

        return True

57 58
    def insert_record(self, record):
        """Insert a conference talk in the database.
59

60 61
        Args:
            record (RecordConf): record describing a conference.
62

63 64 65
        Returns:
            int: one when the record is inserted / updated in the database
                zero otherwise.
66 67 68 69 70 71 72 73 74 75

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

        # alias for the conference information
        conference_dates = record.conference_dates()
        conference_title = record.conference_title()
        first_author = record.first_author()
76
        id_country = self.search_country(record.conference_country())
77 78 79
        submitted = record.submitted()[0]
        title = record.title()

80
        # get the collaboration identifier
81
        id_collaboration = self.search_collaboration(record.collaboration())
82

83
        # get an already published talk
LE GAC Renaud's avatar
LE GAC Renaud committed
84 85
        fields = dict(conference_title=conference_title,
                      first_author=first_author,
86
                      title=title)
87

88
        rec_id, status = self.get_record_by_fields(oai_url, year, **fields)
89 90 91 92
        if rec_id:
            return status

        # eventually insert a new talk
93
        ret = 1
94
        if not self.dry_run:
LE GAC Renaud's avatar
LE GAC Renaud committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115

            fields = dict(authors=record.authors(),
                          authors_institute=record.my_authors,
                          conference_dates=conference_dates,
                          conference_speaker=first_author,
                          conference_title=conference_title,
                          conference_town=record.conference_town(),
                          conference_url=record.conference_url(),
                          first_author=first_author,
                          id_categories=self.id_category,
                          id_collaborations=id_collaboration,
                          id_countries=id_country,
                          id_projects=self.id_project,
                          id_status=UNDEF_ID,
                          id_teams=self.id_team,
                          origin=oai_url,
                          submitted=submitted,
                          title=title,
                          year=year)

            ret = self._insert_in_db(log_year=year, **fields)
116 117 118 119 120 121

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

        return 0