""" harvest_tools.reports """ import traceback from .automaton import Automaton from .base import MSG_CRASH, MSG_LOAD from .checkandfix import CheckException from plugin_dbui import get_id, UNDEF_ID, UNKNOWN MSG_REPORT_NO_NUMBER = "Reject no report number" class Reports(Automaton): """Automaton for reports to committee. """ def check_record(self, record): """Check the content of the report in order to fix non conformities. Args: record (RecordPubli): record describing a report. Returns: bool: ``False`` when a non conformity is found and can not be corrected. """ if not Automaton.check_record(self, record): return False if self.dbg: print("check report record") if not record.report_number(): self.logs[-1].reject(MSG_REPORT_NO_NUMBER, record=record) return False try: self.check.submitted(record) self.check.format_authors(record, fmt="F. Last") self.check.get_my_authors(record, sort=True) except CheckException as e: self.logs[-1].reject(e, record=record) return False except Exception as e: self.logs[-1].reject(MSG_CRASH % e, record=record, translate=False) print(traceback.format_exc()) return False return True def insert_record(self, record): """Insert a report in the database. Args: record (RecordPubli): record describing a report. Returns: int: one when the record is inserted / updated in the database zero otherwise. """ 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.submitted()[0:4] # allow undefined institute authors try: self.check.get_my_authors(record, sort=True) authors_institute = record.my_authors except CheckException: authors_institute = UNKNOWN id_status = get_id(db.status, code=UNKNOWN) # get the collaboration identifier id_collaboration = \ get_id(db.collaborations, collaboration=record.collaboration()) # get an already published reports fields = dict(id_categories=self.id_category, id_projects=self.id_project, id_teams=self.id_team, title=title) rec_id, status = self.get_record_by_fields(oai_url, year, **fields) if rec_id: return status # eventually insert a new report ret = 1 if not self.dry_run: 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) if ret == 1: self.logs[-1].load(MSG_LOAD, year) return 1 return 0