from odoo import models, fields WORKFLOW_STATUS = [ ("pending", "Pending"), ("approved", "Approved"), ("rejected", "Rejected"), ] class EncoachApprovalWorkflow(models.Model): _name = "encoach.approval.workflow" _description = "EnCoach Exam Approval Workflow" exam_id = fields.Many2one("encoach.exam", ondelete="cascade", index=True) creator_id = fields.Many2one("res.users", default=lambda self: self.env.uid) entity_id = fields.Many2one("encoach.entity") name = fields.Char() modules = fields.Json(help='List of modules, e.g. ["reading","writing"]') steps = fields.Json( help="Array of workflow step objects with stepType, stepNumber, " "completed, rejected, completedBy, assignees, etc.", ) status = fields.Selection(WORKFLOW_STATUS, default="pending", required=True) start_date = fields.Datetime(default=fields.Datetime.now) legacy_id = fields.Char(index=True) def action_approve(self): for rec in self: rec.status = "approved" if rec.exam_id: rec.exam_id.approved = True def action_reject(self): for rec in self: rec.status = "rejected" if rec.exam_id: rec.exam_id.approved = False def action_reset(self): for rec in self: rec.status = "pending" def to_encoach_dict(self): self.ensure_one() return { "id": self.id, "name": self.name or "", "entityId": self.entity_id.id if self.entity_id else None, "requester": self.creator_id.id if self.creator_id else None, "startDate": ( int(self.start_date.timestamp() * 1000) if self.start_date else None ), "modules": self.modules or [], "examId": self.exam_id.id if self.exam_id else None, "status": self.status, "steps": self.steps or [], }