first
This commit is contained in:
commit
89a5d70512
284 files changed
+8364
No files matched your search
Whitespace-only changes.
@@ -0,0 +1,19 @@
|
||||
from django.contrib import admin
|
||||
from apps.ticket.models import Ticket, TicketComment, TicketCommentFile
|
||||
|
||||
# Register your models here.
|
||||
|
||||
|
||||
@admin.register(Ticket)
|
||||
class TicketAdmin(admin.ModelAdmin):
|
||||
pass
|
||||
|
||||
|
||||
@admin.register(TicketComment)
|
||||
class TicketCommentAdmin(admin.ModelAdmin):
|
||||
pass
|
||||
|
||||
|
||||
@admin.register(TicketCommentFile)
|
||||
class TicketCommentFileAdmin(admin.ModelAdmin):
|
||||
pass
|
||||
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class TicketConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'apps.ticket'
|
||||
@@ -0,0 +1,47 @@
|
||||
# Generated by Django 4.2.1 on 2023-12-20 10:41
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
('workspace', '0003_alter_workspace_uuid'),
|
||||
('cuser', '0005_smsopt'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Ticket',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
('title', models.CharField(max_length=100)),
|
||||
('status', models.PositiveSmallIntegerField(choices=[(0, 'wait'), (1, 'customer'), (2, 'operator')])),
|
||||
('cuser_role', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='cuser.cuserrole')),
|
||||
('sys_ws', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='workspace.workspace')),
|
||||
],
|
||||
options={
|
||||
'abstract': False,
|
||||
},
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='TicketComment',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
('desc', models.CharField(max_length=500)),
|
||||
('cuser_role', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='cuser.cuserrole')),
|
||||
('sys_ws', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='workspace.workspace')),
|
||||
('ticket', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='ticket.ticket')),
|
||||
],
|
||||
options={
|
||||
'abstract': False,
|
||||
},
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,18 @@
|
||||
# Generated by Django 4.2.1 on 2023-12-20 11:32
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('ticket', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='ticket',
|
||||
name='status',
|
||||
field=models.PositiveSmallIntegerField(choices=[(0, 'wait'), (1, 'customer'), (2, 'operator')], default=0),
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,18 @@
|
||||
# Generated by Django 4.2.1 on 2023-12-24 06:54
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('ticket', '0002_alter_ticket_status'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='ticketcomment',
|
||||
name='file',
|
||||
field=models.FileField(blank=True, null=True, upload_to=''),
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,18 @@
|
||||
# Generated by Django 4.2.1 on 2023-12-24 06:55
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('ticket', '0003_ticketcomment_file'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='ticketcomment',
|
||||
name='file',
|
||||
field=models.FileField(blank=True, null=True, upload_to='fileha/'),
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,17 @@
|
||||
# Generated by Django 4.2.1 on 2023-12-24 07:04
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('ticket', '0004_alter_ticketcomment_file'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RemoveField(
|
||||
model_name='ticketcomment',
|
||||
name='file',
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,27 @@
|
||||
# Generated by Django 4.2.1 on 2023-12-24 07:09
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('ticket', '0005_remove_ticketcomment_file'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='TicketCommentFiles',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
('file', models.FileField(blank=True, null=True, upload_to='ticketFiles/')),
|
||||
('ticket_comment', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='ticket.ticketcomment')),
|
||||
],
|
||||
options={
|
||||
'abstract': False,
|
||||
},
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,17 @@
|
||||
# Generated by Django 4.2.1 on 2023-12-24 07:11
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('ticket', '0006_ticketcommentfiles'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RenameModel(
|
||||
old_name='TicketCommentFiles',
|
||||
new_name='TicketCommentFile',
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,19 @@
|
||||
# Generated by Django 4.2.1 on 2023-12-25 08:53
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('ticket', '0007_rename_ticketcommentfiles_ticketcommentfile'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='ticketcommentfile',
|
||||
name='ticket_comment',
|
||||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='ticketcommentfile_set', to='ticket.ticketcomment'),
|
||||
),
|
||||
]
|
||||
Whitespace-only changes.
@@ -0,0 +1,43 @@
|
||||
from django.db import models
|
||||
from utils.base_model import BaseModel
|
||||
from apps.workspace.models import Workspace
|
||||
from apps.cuser.models import CuserRole
|
||||
# Create your models here.
|
||||
|
||||
|
||||
class Ticket(BaseModel):
|
||||
|
||||
STATUS_CHOICES = [
|
||||
(0, "wait"),
|
||||
(1, "customer"),
|
||||
(2, "operator"),
|
||||
]
|
||||
|
||||
title = models.CharField(max_length=100)
|
||||
status = models.PositiveSmallIntegerField(choices=STATUS_CHOICES, default=0)
|
||||
cuser_role = models.ForeignKey(CuserRole, on_delete=models.CASCADE, null=False, blank=False)
|
||||
sys_ws = models.ForeignKey(Workspace, on_delete=models.CASCADE, null=False, blank=False)
|
||||
|
||||
def __str__(self):
|
||||
return f'{self.title}'
|
||||
|
||||
|
||||
class TicketComment(BaseModel):
|
||||
|
||||
ticket = models.ForeignKey(Ticket, on_delete=models.CASCADE, null=False, blank=False)
|
||||
desc = models.CharField(max_length=500)
|
||||
cuser_role = models.ForeignKey(CuserRole, on_delete=models.CASCADE, null=False, blank=False)
|
||||
sys_ws = models.ForeignKey(Workspace, on_delete=models.CASCADE, null=False, blank=False)
|
||||
|
||||
def __str__(self):
|
||||
return f'{self.ticket.title}'
|
||||
|
||||
|
||||
class TicketCommentFile(BaseModel):
|
||||
|
||||
ticket_comment = models.ForeignKey(TicketComment, on_delete=models.CASCADE, null=False, blank=False, related_name='ticketcommentfile_set')
|
||||
file = models.FileField(null=True, blank=True, upload_to='ticketFiles/')
|
||||
|
||||
def __str__(self):
|
||||
return f'{self.ticket_comment.ticket.title}'
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
from apps.ticket.models import Ticket as TicketModel, TicketComment as TicketCommentModel
|
||||
from apps.cuser.models import CuserRole as CuserRoleModel
|
||||
from rest_framework import exceptions
|
||||
|
||||
|
||||
def list_ticket(cuser_role_uuid: str, workspace_uuid: str) -> TicketModel:
|
||||
try:
|
||||
role_type_name = CuserRoleModel.objects.get(uuid=cuser_role_uuid).role_type.role_name_en
|
||||
if role_type_name == 'workspace_operator':
|
||||
tickets = TicketModel.objects.filter(sys_ws__uuid=workspace_uuid).order_by('-created_at')
|
||||
else:
|
||||
tickets = TicketModel.objects.filter(cuser_role__uuid=cuser_role_uuid, sys_ws__uuid=workspace_uuid).order_by('-created_at')
|
||||
return tickets
|
||||
except Exception as exp:
|
||||
message = f'Error Fetching List tickets: {exp}'
|
||||
raise exceptions.ValidationError({'error': message})
|
||||
|
||||
|
||||
def retrieve_ticket_comments(ticket_id: int, cuser_role_uuid: str, workspace_uuid: str) -> TicketCommentModel:
|
||||
try:
|
||||
role_type_name = CuserRoleModel.objects.get(uuid=cuser_role_uuid).role_type.role_name_en
|
||||
|
||||
if role_type_name == 'workspace_operator':
|
||||
ticket = TicketModel.objects.get(id=ticket_id, sys_ws__uuid=workspace_uuid)
|
||||
else:
|
||||
ticket = TicketModel.objects.get(id=ticket_id, cuser_role__uuid=cuser_role_uuid, sys_ws__uuid=workspace_uuid)
|
||||
|
||||
ticket_comments = TicketCommentModel.objects.filter(ticket=ticket, sys_ws__uuid=workspace_uuid)
|
||||
return ticket_comments
|
||||
except Exception as exp:
|
||||
message = f'Error Fetching List ticket comments: {exp}'
|
||||
raise exceptions.ValidationError({'error': message})
|
||||
@@ -0,0 +1,85 @@
|
||||
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})
|
||||
@@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
@@ -0,0 +1,9 @@
|
||||
from django.urls import path
|
||||
from apps.ticket.views import *
|
||||
|
||||
urlpatterns = [
|
||||
path('create/', TicketCreateApi.as_view(), name='create_ticket'),
|
||||
path('<int:ticket_id>/', TicketCommentsListApi.as_view(), name='comments_ticket'),
|
||||
path('<int:ticket_id>/new/', TicketCommentCreateApi.as_view(), name='add_comment_ticket'),
|
||||
path('list/', TicketListApi.as_view(), name='list_ticket'),
|
||||
]
|
||||
@@ -0,0 +1,162 @@
|
||||
from django.shortcuts import render
|
||||
from rest_framework.views import APIView
|
||||
from rest_framework.generics import ListAPIView
|
||||
from rest_framework.response import Response
|
||||
from rest_framework import status
|
||||
from rest_framework import serializers
|
||||
from utils.get_user_workspace import get_user_workspace_uuid, get_user_workspace
|
||||
from utils.get_cuser_role import get_cuser_role
|
||||
from apps.ticket.services import create_ticket, create_comment_ticket
|
||||
from apps.ticket.models import Ticket as TicketModel, TicketComment as TicketCommentModel, TicketCommentFile as TicketCommentFileModel
|
||||
from apps.cuser.models import CuserRole as CuserRoleModel
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from utils import custom_permissions
|
||||
from utils.get_cuser_role import get_cuser_role_uuid
|
||||
from utils.get_user_workspace import get_user_workspace_uuid
|
||||
from apps.ticket.selectors import list_ticket, retrieve_ticket_comments
|
||||
# Create your views here.
|
||||
|
||||
|
||||
class TicketCreateApi(APIView):
|
||||
|
||||
permission_classes = (IsAuthenticated, custom_permissions.WorkspaceCustomerPermission)
|
||||
|
||||
class InputSerializer(serializers.Serializer):
|
||||
title = serializers.CharField(max_length=100)
|
||||
desc = serializers.CharField(max_length=500)
|
||||
|
||||
files = serializers.ListField(
|
||||
child=serializers.FileField(required=False, max_length=10000000, allow_empty_file=False),
|
||||
required=False
|
||||
)
|
||||
|
||||
class OutputSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = TicketModel
|
||||
exclude = ('sys_ws', 'cuser_role')
|
||||
|
||||
def post(self, request):
|
||||
|
||||
serializer = self.InputSerializer(data=request.data)
|
||||
try:
|
||||
serializer.is_valid(raise_exception=True)
|
||||
except Exception as exp:
|
||||
print("exp...")
|
||||
print(exp)
|
||||
|
||||
user_workspace = get_user_workspace(request=request)
|
||||
cuser_role_obj = get_cuser_role(request=request)
|
||||
|
||||
print("3")
|
||||
|
||||
ticket = create_ticket(**serializer.validated_data, cuser_role=cuser_role_obj, workspace=user_workspace)
|
||||
|
||||
print("4")
|
||||
output = self.OutputSerializer(ticket)
|
||||
|
||||
return Response(data=output.data, status=status.HTTP_201_CREATED)
|
||||
|
||||
|
||||
class TicketListApi(ListAPIView):
|
||||
|
||||
permission_classes = (IsAuthenticated, custom_permissions.WorkspaceCustomerPermission)
|
||||
|
||||
class OutputSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = TicketModel
|
||||
exclude = ('sys_ws', )
|
||||
|
||||
serializer_class = OutputSerializer
|
||||
|
||||
def get_queryset(self):
|
||||
|
||||
cuser_role_uuid = get_cuser_role_uuid(request=self.request)
|
||||
user_workspace_uuid = get_user_workspace_uuid(request=self.request)
|
||||
|
||||
return list_ticket(cuser_role_uuid=cuser_role_uuid, workspace_uuid=user_workspace_uuid)
|
||||
|
||||
|
||||
class TicketCommentsListApi(APIView):
|
||||
|
||||
permission_classes = (IsAuthenticated, custom_permissions.WorkspaceCustomerPermission)
|
||||
|
||||
class OutputSerializer(serializers.ModelSerializer):
|
||||
|
||||
class TicketSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = TicketModel
|
||||
exclude = ('sys_ws', 'cuser_role')
|
||||
|
||||
class CuserRoleSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = CuserRoleModel
|
||||
fields = '__all__'
|
||||
|
||||
class TicketCommentFileSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = TicketCommentFileModel
|
||||
fields = '__all__'
|
||||
|
||||
ticket = TicketSerializer(many=False, read_only=True)
|
||||
comment_files = TicketCommentFileSerializer(many=True, read_only=True, source='ticketcommentfile_set')
|
||||
cuser_role = CuserRoleSerializer()
|
||||
|
||||
|
||||
class Meta:
|
||||
model = TicketCommentModel
|
||||
exclude = ('sys_ws',)
|
||||
|
||||
def get(self, request, ticket_id: int):
|
||||
|
||||
print("ok22")
|
||||
|
||||
cuser_role_uuid = get_cuser_role_uuid(request=request)
|
||||
user_workspace_uuid = get_user_workspace_uuid(request=request)
|
||||
|
||||
print("ok")
|
||||
|
||||
#return Response({"javad":"salam"})
|
||||
|
||||
ticket_comments = retrieve_ticket_comments(ticket_id=ticket_id, cuser_role_uuid=cuser_role_uuid,
|
||||
workspace_uuid=user_workspace_uuid)
|
||||
|
||||
output = self.OutputSerializer(ticket_comments, many=True)
|
||||
|
||||
return Response(data=output.data, status=status.HTTP_200_OK)
|
||||
|
||||
|
||||
class TicketCommentCreateApi(APIView):
|
||||
|
||||
permission_classes = (IsAuthenticated, custom_permissions.WorkspaceCustomerPermission)
|
||||
|
||||
class InputSerializer(serializers.Serializer):
|
||||
|
||||
files = serializers.ListField(
|
||||
child=serializers.FileField(
|
||||
max_length=100000,
|
||||
allow_empty_file=False,
|
||||
allow_null=True,
|
||||
), required=False
|
||||
)
|
||||
|
||||
desc = serializers.CharField(max_length=500)
|
||||
|
||||
class OutputSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = TicketCommentModel
|
||||
exclude = ('sys_ws', 'cuser_role')
|
||||
|
||||
def post(self, request, ticket_id):
|
||||
|
||||
serializer = self.InputSerializer(data=request.data)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
|
||||
user_workspace = get_user_workspace(request=request)
|
||||
cuser_role_obj = get_cuser_role(request=request)
|
||||
|
||||
ticket_comment = create_comment_ticket(**serializer.validated_data, ticket_id=ticket_id, cuser_role=cuser_role_obj, workspace=user_workspace)
|
||||
|
||||
output = self.OutputSerializer(ticket_comment)
|
||||
|
||||
return Response(data=output.data, status=status.HTTP_201_CREATED)
|
||||
|
||||
Reference in new issue
Block a user