
from apps.wf.models import Ticket
# TicketFlow, Transition, Workflow, CustomField, State, 
from apps.ofm.models import SealManage, LendingSeal
from rest_framework.exceptions import ParseError


def seal_submit_validate(ins: LendingSeal):
    if ins.submit_time:
        raise ParseError('该日志已提交!')
    if ins.mtask and ins.mtask.state == LendingSeal.MTASK_STOP:
        raise ParseError('该任务已停止!')
            
def bind_lendingseal(ticket: Ticket, transition, new_ticket_data: dict):
    ins = SealManage.objects.get(id=new_ticket_data['t_id'])
    if ins.submit_time is not None:
        raise ParseError('该印章申请不可提交审批')
    seal_submit_validate(ins)
    ticket_data = ticket.ticket_data
    ticket_data.update({
        't_model': 'LendingSeal',
        't_id': ins.id,
    })
    ticket.ticket_data = ticket_data
    ticket.create_by = ins.create_by
    ticket.save()
    if ins.ticket is None:
        ins.ticket = ticket
        ins.save()

