def get_openid(self, code, db_name): result = {} result['is_success'] = True cr = Registry(db_name).cursor() env = api.Environment(cr, SUPERUSER_ID, {}) is_success, token = env['wechat.mini.program.session'].get_token(code) result['token'] = token result['is_get_token'] = is_success cr.commit() cr.close() return json.dumps(result)
def cron_folder_auto_classification(self, path=False, processing_path=False, limit=False): if not path: path = (self.env["ir.config_parameter"].sudo().get_param( "document_quick_access_auto_classification.path", default=False)) if not path: return False if not processing_path and not self.env.context.get( "ignore_process_path"): processing_path = ( self.env["ir.config_parameter"].sudo().get_param( "document_quick_access_auto_classification.process_path", default=False, )) elements = [ os.path.join(path, f) for f in os.listdir(path) if os.path.isfile(os.path.join(path, f)) ] if limit: elements = elements[:limit] for element in elements: obj = self new_element = element if processing_path: new_cr = Registry(self.env.cr.dbname).cursor() try: if processing_path: new_element = os.path.join(processing_path, os.path.basename(element)) shutil.copy(element, new_element) obj = (api.Environment( new_cr, self.env.uid, self.env.context)[self._name].browse().with_delay( **self._delay_vals())) obj._process_document(new_element) if processing_path: new_cr.commit() except Exception: if processing_path: os.unlink(new_element) new_cr.rollback() raise finally: if processing_path: new_cr.close() if processing_path: os.unlink(element) return True
def cron_move_documents(self, limit=False, path=False): if not path: path = (self.env["ir.config_parameter"].sudo().get_param( "hash_search_document_scanner_queue.preprocess_path", default=False, )) dest_path = (self.env["ir.config_parameter"].sudo().get_param( "hash_search_document_scanner.path", default=False)) if not path or not dest_path: return False elements = [ os.path.join(path, f) for f in os.listdir(path) if os.path.isfile(os.path.join(path, f)) ] if limit: elements = elements[:limit] min_time = int(time.time()) - 60 single_commit = self.env.context.get("scanner_single_commit", False) for element in elements: if os.path.getmtime( element) > min_time and not self.env.context.get( "scanner_ignore_time", False): continue filename = os.path.basename(element) new_element = os.path.join(dest_path, filename) shutil.copy(element, new_element) if not single_commit: new_cr = Registry(self.env.cr.dbname).cursor() try: if not single_commit: obj = (api.Environment( new_cr, self.env.uid, self.env.context)[self._name].browse().with_delay()) else: obj = self.env[self._name].browse() obj.process_document(new_element) if not single_commit: new_cr.commit() except Exception: os.unlink(new_element) if not single_commit: new_cr.rollback() # error, rollback everything atomically raise finally: if not single_commit: new_cr.close() os.unlink(element) return True
def _next(self): if (getattr(threading.currentThread(), "testing", False) or self.env.context.get("install_mode") or self.env.context.get("ignore_safe", not self.safe)): return super()._next() new_cr = Registry(self.env.cr.dbname).cursor() try: env = api.Environment(new_cr, self.env.uid, self.env.context) res = env[self._name].browse(self.id) result = res.with_context(ignore_safe=True)._next() new_cr.commit() except Exception: new_cr.rollback() # error, rollback everything atomically raise finally: new_cr.close() return result
def process(self): self.ensure_one() # Decide when/how to send lroe_operation queue_obj = self.env["queue.job"].sudo() company = self.company_id if not company.use_connector: try: lroe_response = self.send() if lroe_response.state == LROEOperationResponseState.CORRECT.value: self.mark_as_recorded() elif (lroe_response.state == LROEOperationResponseState.PARTIALLY_CORRECT.value): self.mark_as_warning() elif lroe_response.state in ( LROEOperationResponseState.BUILD_ERROR.value, LROEOperationResponseState.INCORRECT.value, ): self.mark_as_error() except Exception: new_cr = Registry(self.env.cr.dbname).cursor() env = api.Environment(new_cr, self.env.uid, self.env.context) lroe_operation = env["lroe.operation"].browse(self.id) lroe_operation.write( {"state": LROEOperationStateEnum.ERROR.value}) # If an operation has been sent successfully to the Tax Agency we need # to make sure that the current state is saved in case an exception # occurs in the following invoices. new_cr.commit() new_cr.close() raise else: eta = company._get_lroe_eta() new_delay = (self.sudo().with_context( company_id=company.id).with_delay( eta=eta).send_one_operation_job()) job = queue_obj.search([("uuid", "=", new_delay.uuid)], limit=1) self.sudo().jobs_ids |= job
def cron_ssh_move_documents( self, host=False, port=False, user=False, password=False, ssh_path=False, ): dest_path = (self.env["ir.config_parameter"].sudo().get_param( "hash_search_document_scanner.path", default=False)) connection = SSHClient() connection.load_system_host_keys() if not dest_path: return False if not host: host = self.env["ir.config_parameter"].get_param( "hash_search_document_scanner_queue_ssh.host", default=False) if not port: port = int(self.env["ir.config_parameter"].get_param( "hash_search_document_scanner_queue_ssh.port", default="0")) if not user: user = self.env["ir.config_parameter"].get_param( "hash_search_document_scanner_queue_ssh.user", default=False) if not password: password = self.env["ir.config_parameter"].get_param( "hash_search_document_scanner_queue_ssh.password", default=False, ) if not ssh_path: ssh_path = self.env["ir.config_parameter"].get_param( "hash_search_document_scanner_queue_ssh.ssh_path", default=False, ) connection.connect(hostname=host, port=port, username=user, password=password) sftp = connection.open_sftp() if ssh_path: sftp.chdir(ssh_path) elements = sftp.listdir_attr(".") min_time = int(time.time()) - 60 single_commit = self.env.context.get("scanner_single_commit", False) for element in elements: if element.st_atime > min_time and not self.env.context.get( "scanner_ignore_time", False): continue filename = element.filename new_element = os.path.join(dest_path, filename) if not single_commit: new_cr = Registry(self.env.cr.dbname).cursor() try: sftp.get(filename, new_element) if single_commit: obj = self.env[self._name].browse() else: obj = (api.Environment( new_cr, self.env.uid, self.env.context)[self._name].browse().with_delay()) obj.process_document(new_element) if not single_commit: new_cr.commit() except Exception: if os.path.exists(new_element): os.unlink(new_element) if not single_commit: new_cr.rollback() # error, rollback everything atomically raise finally: if not single_commit: new_cr.close() sftp.remove(element.filename) sftp.close() connection.close() return True