from apps.ticket.models import Ticket, TicketComment, TicketCommentFile from apps.cuser.models import CuserRole from apps.workspace.models import Workspace from apps.cuser.models import CuserRole as CuserRoleModel from django.db import transaction from rest_framework import exceptions import base64 from abc import ABC, abstractmethod def create_ticket(title: str, desc: str, cuser_role: CuserRole, workspace: Workspace, files=None) -> Ticket: with transaction.atomic(): try: ticket = Ticket() ticket.title = title ticket.cuser_role = cuser_role ticket.status = 0 ticket.sys_ws = workspace ticket.save() ticket_comment = TicketComment() ticket_comment.ticket = ticket ticket_comment.desc = desc ticket_comment.cuser_role = cuser_role ticket_comment.sys_ws = workspace ticket_comment.save() if files: for file in files: ticket_file = TicketCommentFile() ticket_file.ticket_comment = ticket_comment ticket_file.file = file ticket_file.save() return ticket except Exception as exp: transaction.set_rollback(True) message = f'Error Create Ticket: {exp}' raise exceptions.ValidationError({'error': message}) def create_comment_ticket(ticket_id: int, desc: str, cuser_role: CuserRole, workspace: Workspace, files=None) -> TicketComment: with transaction.atomic(): try: role_type_name = cuser_role.role_type.role_name_en ticket_status = None if role_type_name == 'workspace_operator': ticket = Ticket.objects.get(id=ticket_id) if ticket.status == 0: ticket.status = 2 ticket.save() else: ticket = Ticket.objects.get(id=ticket_id, cuser_role=cuser_role, sys_ws=workspace) if ticket.status == 0: ticket.status = 1 ticket.save() ticket_comment = TicketComment() ticket_comment.ticket = ticket ticket_comment.desc = desc ticket_comment.cuser_role = cuser_role ticket_comment.sys_ws = workspace ticket_comment.save() if files: for file in files: ticket_file = TicketCommentFile() ticket_file.ticket_comment = ticket_comment ticket_file.file = file ticket_file.save() return ticket_comment except Exception as exp: transaction.set_rollback(True) message = f'Error Create TicketComment: {exp}' raise exceptions.ValidationError({'error': message})