예제 #1
0
    def process(self):

        # create the unicity key for the database
        self._diff_externalid = diff_id = Tools.generate_random_string()

        # fetch database
        database = Database.get_database()

        try:
            # create JSON request file
            self.create_request_file()
            # create the first database record
            diffusion = Diffusion(diff_externalid=diff_id,
                                  fullrequestId=self.req_id+self.hostname,
                                  requestStatus=REQ_STATUS.ongoing,
                                  Date=self._to_datetime(self.date_reception),
                                  rxnotif=True,
                                  message="Created record in SQL database",)
            with Database.get_app().app_context():
                database.session.add(diffusion)
                database.session.commit()
            LOGGER.debug("Committed %s dissemination status "
                         "into database.", REQ_STATUS.ongoing)
            status = REQ_STATUS.ongoing
        except Exception as exc:
            LOGGER.exception("Error during notification processing. "
                             "Dissemination failed.")
            status = self.commit_failure(database, diff_id)
        return status
예제 #2
0
 def check_zip(item, destination_dir):
     if item == "tmp.zip":
         ext = "." + Tools.generate_random_string(5)
         destination_path = os.path.join(destination_dir, item + ext)
     else:
         destination_path = os.path.join(destination_dir, item)
     return destination_path
예제 #3
0
    def update_filename(self, filename):
        """
        Register a new entry in the database in case
        there are multiples files for one request_id. If one request id
        is requesting N files, there should be N entries in the database, each with its own diff_external_id.
        """

        # fetch database
        database = Database.get_database()

        # get the base record to duplicate
        with Database.get_app().app_context():
            base_record = Diffusion.query.filter_by(
            fullrequestId=self.req_id).first()

            if base_record.original_file is None:
                # if there is only the one record created by
                # receiver module, job is done
                base_record.original_file = filename
                database.session.commit()
            # otherwise, we create a new record
            else:
                diffusion = Diffusion(diff_externalid=Tools.generate_random_string(),
                                      fullrequestId=base_record.fullrequestId,
                                      original_file=filename,
                                      requestStatus=base_record.requestStatus,
                                      message=base_record.message,
                                      Date=base_record.Date,
                                      rxnotif=base_record.rxnotif)

                database.session.add(diffusion)
                database.session.commit()
예제 #4
0
    def setUp(self):

        # Configuring repertories
        file_manager.manager.TEST_SFTP = True
        self.tmpdir = mkdtemp(prefix='harnais_')
        os.environ["TMPDIR"] = self.tmpdir
        self.staging_post = join(self.tmpdir, "staging_post")
        os.mkdir(self.staging_post)
        # # prepare settings
        SettingsManager.load_settings()
        SettingsManager.update(dict(harnaisLogdir=self.tmpdir,
                                    harnaisDir=self.tmpdir,
                                    harnaisAckDir=self.tmpdir,
                                    openwisStagingPath=gettempdir(),
                                    openwisHost="localhost",
                                    openwisSftpUser="******",
                                    openwisSftpPassword="******",
                                    openwisSftpPort=3373),
                               testing=True)

        os.environ[ENV.settings] = join(self.tmpdir, "settings_testing.yaml")

        with open(os.environ[ENV.settings], "w") as file_:
            yaml.dump(SettingsManager._parameters, file_)

        setup_logging()

        # Start sftp server
        SFTPserver.create_server(self.staging_post)

        # create json file to process
        self.dir_a = HarnessTree.get("temp_dissRequest_A")
        self.json_file = json_file = join(self.dir_a,
                                          "test_instruction_file.json")
        instr = {
            'hostname': socket.gethostname(),
            'uri': self.staging_post,
            'req_id': '123456',
            'diffpriority': 81,
            'date': datetime.now().strftime("%Y%m%d%H%M%S"),
            'diffusion': {
                'fileName': None,
                'attachmentMode': 'AS_ATTACHMENT',
                'dispatchMode': 'TO',
                'DiffusionType': 'EMAIL',
                'subject': 'dummySubject',
                'headerLine': 'dummyHeaderLine',
                'address': '*****@*****.**'
            }
        }
        # put it in cache/A_dissreq
        with open(json_file, "w") as file_:
            json.dump(instr, file_)
        # create corresponding record in database:
        ext_id = Tools.generate_random_string()
        diffusion = Diffusion(diff_externalid=ext_id,
                              fullrequestId="123456" + socket.gethostname(),
                              requestStatus=REQ_STATUS.ongoing,
                              Date=datetime.now(),
                              rxnotif=True,
                              message="Created record in SQL database")

        with Database.get_app().app_context():
            database = Database.get_database()
            database.session.add(diffusion)
            database.session.commit()