Skip to content

Commit

Permalink
feat: API + Vuejs3 improved
Browse files Browse the repository at this point in the history
  • Loading branch information
francesco-filicetti committed Sep 26, 2024
1 parent d28aa8c commit f942e1d
Show file tree
Hide file tree
Showing 11 changed files with 282 additions and 291 deletions.
45 changes: 21 additions & 24 deletions uniticket/api_rest/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,30 @@
from rest_framework.renderers import JSONOpenAPIRenderer
from rest_framework.schemas import get_schema_view

from api_rest.views import *
from api_rest.views import generic, manager, operator, user


# Routers provide an easy way of automatically determining the URL conf.
router = routers.DefaultRouter()
router.register('users', UserViewSet)
router.register('groups', GroupViewSet)
router.register('users', generic.UserViewSet)
router.register('groups', generic.GroupViewSet)


urlpatterns = [
# path('api/', include(router.urls)),
# path('api-auth/', include(rest_framework.urls, 'rest_framework',)),

re_path('^openapi$', get_schema_view(**{}), name='openapi-schema'),
re_path('^openapi.json$', get_schema_view(renderer_classes = [JSONOpenAPIRenderer], **{}), name='openapi-schema-json'),
path('api/<slug:structure_slug>/<slug:category_slug>/ticket/new', TicketAPIView.as_view(), name='api-new-ticket'),
path('api/ticket/<str:ticket_uid>', TicketAPIDetail.as_view(), name='api-view-ticket'),
path('api/strutture/list', TicketAPIStruttureList.as_view(), name='api-strutture-list'),
path('api/ticket/category/list', TicketAPITicketCategoryList.as_view(),name='api-ticket-category-list'),
path('api/ticket/user/list', TicketAPIListCreated.as_view(), name='api-ticket-user-list'),
path('api/ticket/close/<str:ticket_id>', TicketAPIClose.as_view(), name='api-ticket-close'),

path('api/manager/<slug:structure_slug>/tickets/unassigned/count/', TicketAPIManagerUnassignedCount.as_view(), name='api-manager-tickets-unassigned-count'),
path('api/manager/<slug:structure_slug>/tickets/open/count/', TicketAPIManagerOpenCount.as_view(), name='api-manager-tickets-open-count'),
path('api/manager/<slug:structure_slug>/tickets/my-open/count/', TicketAPIManagerMyOpenCount.as_view(), name='api-manager-tickets-my-open-count'),

path('api/operator/<slug:structure_slug>/tickets/unassigned/count/', TicketAPIOperatorUnassignedCount.as_view(), name='api-operator-tickets-unassigned-count'),
path('api/operator/<slug:structure_slug>/tickets/open/count/', TicketAPIOperatorOpenCount.as_view(), name='api-operator-tickets-open-count'),
path('api/operator/<slug:structure_slug>/tickets/my-open/count/', TicketAPIOperatorMyOpenCount.as_view(), name='api-operator-tickets-my-open-count'),

]
# path('api/', include(router.urls)),
# path('api-auth/', include(rest_framework.urls, 'rest_framework',)),

re_path('^openapi$', get_schema_view(**{}), name='openapi-schema'),
re_path('^openapi.json$', get_schema_view(renderer_classes = [JSONOpenAPIRenderer], **{}), name='openapi-schema-json'),
path('api/<slug:structure_slug>/<slug:category_slug>/ticket/new', user.TicketAPIView.as_view(), name='api-new-ticket'),
path('api/ticket/<str:ticket_uid>', user.TicketAPIDetail.as_view(), name='api-view-ticket'),
path('api/strutture/list', generic.TicketAPIStruttureList.as_view(), name='api-strutture-list'),
path('api/ticket/category/list', generic.TicketAPITicketCategoryList.as_view(),name='api-ticket-category-list'),
path('api/ticket/user/list', user.TicketAPIListCreated.as_view(), name='api-ticket-user-list'),
path('api/ticket/close/<str:ticket_id>', user.TicketAPIClose.as_view(), name='api-ticket-close'),

# manager
path('api/manager/<slug:structure_slug>/tickets/count/', manager.TicketAPICounter.as_view(), name='api-manager-tickets-count'),

# operator
path('api/operator/<slug:structure_slug>/tickets/count/', operator.TicketAPICounter.as_view(), name='api-operator-tickets-count'),]
Empty file.
97 changes: 97 additions & 0 deletions uniticket/api_rest/views/generic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
from copy import deepcopy
import json
import logging

from django.contrib import messages
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group
from django.core import serializers
from django.core.exceptions import BadRequest
from django.db.models import Q
from django.http import Http404
from django.shortcuts import get_object_or_404

from rest_framework.authentication import SessionAuthentication
from rest_framework.exceptions import PermissionDenied
from rest_framework import generics
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import permissions, viewsets

from uni_ticket.dynamic_form import serialize_form
from uni_ticket.models import Ticket, TicketAssignment, TicketCategory, TicketReply
from uni_ticket.views.user import TicketAddNew, TicketClose, TicketDetail
from uni_ticket.settings import TICKET_CREATE_BUTTON_NAME
from uni_ticket.utils import user_is_manager, user_is_operator, visible_tickets_to_user

from organizational_area.models import OrganizationalStructure
from api_rest.authorizations import AuthorizationToken


from .. serializers import (
GroupSerializer,
OrganizationalStructureSerializer,
TicketCategorySerializer,
TicketSerializer,
UserSerializer
)

logger = logging.getLogger(__name__)


# ViewSets define the view behavior.
class UserViewSet(viewsets.ModelViewSet):
permission_classes = [permissions.IsAdminUser]
queryset = get_user_model().objects.all()
serializer_class = UserSerializer


class GroupViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows groups to be viewed or edited.
"""
permission_classes = [permissions.IsAdminUser]
queryset = Group.objects.all()
serializer_class = GroupSerializer


class uniTicketROGenericList(generics.ListAPIView):
authentication_classes = [AuthorizationToken, SessionAuthentication]
permission_classes = [permissions.IsAuthenticated]


def message_level(level:int):
levels = {v:k for k,v in messages.DEFAULT_LEVELS.items()}
return levels.get(level, level)


class TicketAPIBaseView(APIView):
"""
base class to port in the API all the legacy code
"""

# TODO: AgID MoDI also here?
authentication_classes = [
AuthorizationToken, SessionAuthentication
]
permission_classes = [permissions.IsAuthenticated]

def get_messages(self):
return [
{
message_level(i.level): i.message
for i in messages.get_messages(self.request)
}
]


class TicketAPIStruttureList(uniTicketROGenericList):
queryset = OrganizationalStructure.objects.filter(is_active=True)
lookup_field = 'pk'
serializer_class = OrganizationalStructureSerializer


class TicketAPITicketCategoryList(uniTicketROGenericList):
queryset = TicketCategory.objects.filter(is_active=True)
lookup_field = 'pk'
serializer_class = TicketCategorySerializer
56 changes: 56 additions & 0 deletions uniticket/api_rest/views/manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import logging

from django.contrib import messages
from django.core import serializers
from django.db.models import Q
from django.http import Http404
from django.shortcuts import get_object_or_404

from rest_framework.authentication import SessionAuthentication
from rest_framework.exceptions import PermissionDenied
from rest_framework import generics
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import permissions, viewsets

from uni_ticket.models import Ticket, TicketAssignment, TicketCategory, TicketReply
from uni_ticket.utils import user_is_manager, user_is_operator, visible_tickets_to_user

from organizational_area.models import OrganizationalStructure
from api_rest.authorizations import AuthorizationToken

from . generic import *


logger = logging.getLogger(__name__)


class TicketAPICounter(TicketAPIBaseView):
def get(self, request, structure_slug, *args, **kwargs):
structure = get_object_or_404(OrganizationalStructure, slug=structure_slug)
if not user_is_manager(request.user, structure): raise PermissionDenied

unassigned_tickets = TicketAssignment.get_ticket_per_structure(structure=structure,
closed=False,
taken=False)

open_tickets = TicketAssignment.get_ticket_per_structure(structure=structure,
closed=False,
taken=True)

my_open_tickets = TicketAssignment.get_ticket_per_structure(structure=structure,
closed=False,
taken=True,
taken_by=request.user)
ticket_ids = TicketAssignment.objects.filter(
office__organizational_structure=structure,
office__is_active=True,
follow=True,
ticket__is_closed=False
).values_list('ticket__pk', flat=True).distinct()
messages = TicketReply.get_unread_messages_count(ticket_ids=ticket_ids)

return Response({'unassigned': len(unassigned_tickets),
'open': len(open_tickets),
'my_open': len(my_open_tickets),
'new_messages': messages})
65 changes: 65 additions & 0 deletions uniticket/api_rest/views/operator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import logging

from django.contrib import messages
from django.core import serializers
from django.db.models import Q
from django.http import Http404
from django.shortcuts import get_object_or_404

from rest_framework.authentication import SessionAuthentication
from rest_framework.exceptions import PermissionDenied
from rest_framework import generics
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import permissions, viewsets

from uni_ticket.models import Ticket, TicketAssignment, TicketCategory, TicketReply
from uni_ticket.utils import user_is_manager, user_is_operator, visible_tickets_to_user

from organizational_area.models import OrganizationalStructure
from api_rest.authorizations import AuthorizationToken

from . generic import *


logger = logging.getLogger(__name__)


class TicketAPICounter(TicketAPIBaseView):
def get(self, request, structure_slug, *args, **kwargs):
structure = get_object_or_404(OrganizationalStructure, slug=structure_slug)
oe = user_is_operator(request.user, structure)
if not oe: raise PermissionDenied

unassigned_tickets = visible_tickets_to_user(user=request.user,
structure=structure,
office_employee=oe,
closed=False,
taken=False)

open_tickets = visible_tickets_to_user(user=request.user,
structure=structure,
office_employee=oe,
closed=False,
taken=True)

my_open_tickets = visible_tickets_to_user(user=request.user,
structure=structure,
office_employee=oe,
closed=False,
taken=True,
taken_by=request.user)

ticket_ids = visible_tickets_to_user(
user=request.user,
structure=structure,
office_employee=oe,
closed=False
)

messages = TicketReply.get_unread_messages_count(ticket_ids=ticket_ids)

return Response({'unassigned': len(unassigned_tickets),
'open': len(open_tickets),
'my_open': len(my_open_tickets),
'new_messages': messages})
Loading

0 comments on commit f942e1d

Please sign in to comment.