from odoo import api, fields, models, _ from odoo.exceptions import UserError import time import requests import json import hmac import base64 from hashlib import sha256 class DetailOrder(models.Model): _name = "detail.order" _inherit = ['mail.thread'] detail_order = fields.Text() execute_status = fields.Selection([ ('success', 'Success'), ('failed', 'Failed'), ('done', 'Done'), ('not_found', 'Record not found') ], 'Execute Status') def get_order_id(self): try: if self.json_ginee: json_data = json.loads(self.json_ginee) order_id = json_data.get('data', {}).get('orderId') order_status = json_data.get('data', {}).get('orderStatus') print_info = json_data.get('data', {}).get('printInfo', {}).get('labelPrintStatus') if not order_id: raise UserError(_("Order ID not found in JSON data")) return order_id raise UserError(_("No JSON data available")) except json.JSONDecodeError: raise UserError(_("Invalid JSON format in json_ginee field")) except Exception as e: raise UserError(_("Error extracting order ID: %s") % str(e)) def process_queue_item(self, limit=100, max_exec_time=30): domain = [('execute_status', '=', 'success')] records = self.search(domain, order='create_date asc', limit=limit) start_time = time.time() for rec in records: end_time = time.time() elapsed_time = end_time - start_time if elapsed_time > max_exec_time: break rec.execute_queue() def execute_queue(self): try: order_id, order_status, print_info = self.get_order_id() except Exception as e: self.write({ 'execute_status': 'failed', 'json_ginee': json.dumps({ 'error': str(e) }) })