talks.py 3.98 KB
Newer Older
1 2 3 4 5 6
""" harvest_tools.talks

"""
import traceback


7
<<<<<<< HEAD
8 9 10
from .automaton import Automaton
from .base import MSG_CRASH, MSG_LOAD
from .checkandfix import CheckException
11
from plugin_dbui import UNDEF_ID
12 13 14 15 16 17
=======
from automaton import Automaton
from base import MSG_CRASH, MSG_LOAD
from checkandfix import CheckException
from plugin_dbui import get_id, UNDEF_ID
>>>>>>> Modify the logic to deal with synonyms.
18 19


20
class Talks(Automaton):
21
    """Automaton for conference talks.
22 23

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

27
        Args:
LE GAC Renaud's avatar
LE GAC Renaud committed
28 29
            record (RecordConf):
                record describing a conference.
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 talk record")
42 43 44

        try:
            self.check.is_conference(record)
45
            self.check.country(record)
LE GAC Renaud's avatar
LE GAC Renaud committed
46
            self.check.conference_date(record)
47 48 49

            self.check.submitted(record)

50 51
            self.check.format_authors(record, fmt="F. Last")
            self.check.get_my_authors(record, sort=True)
52

53
        except CheckException as e:
54
            self.logs[-1].reject(e, record=record)
55 56 57
            return False

        except Exception as e:
58
            self.logs[-1].reject(MSG_CRASH % e, record=record, translate=False)
59
            print(traceback.format_exc())
60 61 62 63
            return False

        return True

64 65
    def insert_record(self, record):
        """Insert a conference talk in the database.
66

67
        Args:
LE GAC Renaud's avatar
LE GAC Renaud committed
68 69
            record (RecordConf):
                record describing a conference.
70

71
        Returns:
LE GAC Renaud's avatar
LE GAC Renaud committed
72 73
            int:
                one when the record is inserted / updated in the database
74
                zero otherwise.
75 76

        """
77 78
        db = self.db

79 80
        # alias
        oai_url = record.oai_url()
LE GAC Renaud's avatar
LE GAC Renaud committed
81
        year = record.conference_year()
82 83 84 85 86

        # alias for the conference information
        conference_dates = record.conference_dates()
        conference_title = record.conference_title()
        first_author = record.first_author()
87
        id_country = get_id(db.countries, country=record.conference_country())
LE GAC Renaud's avatar
LE GAC Renaud committed
88
        submitted = record.submitted()
89 90
        title = record.title()

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

95
        # get an already published talk
LE GAC Renaud's avatar
LE GAC Renaud committed
96 97
        fields = dict(conference_title=conference_title,
                      first_author=first_author,
98
                      title=title)
99

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

        # eventually insert a new talk
105
        ret = 1
106
        if not self.dry_run:
LE GAC Renaud's avatar
LE GAC Renaud committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127

            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)
128 129 130 131 132 133

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

        return 0