From e2cb881eb84da5b821bcdda5de37bfaebafd451a Mon Sep 17 00:00:00 2001 From: bbe Date: Thu, 31 Mar 2022 17:25:08 +0200 Subject: [PATCH] Create an API endpoint to list/create/update/delete tickets (ignoring custom fields) --- demo/demodesk/config/settings.py | 1 + demo/demodesk/config/urls.py | 1 + helpdesk/serializers.py | 22 ++++++++++++++-------- helpdesk/urls.py | 13 +++++++++++-- helpdesk/views/api.py | 14 ++++++++++++++ 5 files changed, 41 insertions(+), 10 deletions(-) create mode 100644 helpdesk/views/api.py diff --git a/demo/demodesk/config/settings.py b/demo/demodesk/config/settings.py index 7914d648..e2998c30 100644 --- a/demo/demodesk/config/settings.py +++ b/demo/demodesk/config/settings.py @@ -55,6 +55,7 @@ INSTALLED_APPS = [ 'pinax.teams', # team support 'reversion', # required by pinax-teams 'helpdesk', # This is us! + 'rest_framework', # required for the API ] MIDDLEWARE = [ diff --git a/demo/demodesk/config/urls.py b/demo/demodesk/config/urls.py index 0c37aac6..52f6f407 100644 --- a/demo/demodesk/config/urls.py +++ b/demo/demodesk/config/urls.py @@ -28,4 +28,5 @@ from django.conf.urls.static import static urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^', include('helpdesk.urls', namespace='helpdesk')), + url(r'^api/auth/', include('rest_framework.urls', namespace='rest_framework')) ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) diff --git a/helpdesk/serializers.py b/helpdesk/serializers.py index df0c6a5f..dac5e381 100644 --- a/helpdesk/serializers.py +++ b/helpdesk/serializers.py @@ -1,18 +1,15 @@ from rest_framework import serializers +from django.contrib.humanize.templatetags import humanize from .models import Ticket from .lib import format_time_spent -from django.contrib.humanize.templatetags import humanize - -""" -A serializer for the Ticket model, returns data in the format as required by -datatables for ticket_list.html. Called from staff.datatables_ticket_list. - -""" - class DatatablesTicketSerializer(serializers.ModelSerializer): + """ + A serializer for the Ticket model, returns data in the format as required by + datatables for ticket_list.html. Called from staff.datatables_ticket_list. + """ ticket = serializers.SerializerMethodField() assigned_to = serializers.SerializerMethodField() submitter = serializers.SerializerMethodField() @@ -68,3 +65,12 @@ class DatatablesTicketSerializer(serializers.ModelSerializer): def get_kbitem(self, obj): return obj.kbitem.title if obj.kbitem else "" + + +class TicketSerializer(serializers.ModelSerializer): + class Meta: + model = Ticket + fields = ( + 'id', 'queue', 'title', 'description', 'resolution', 'submitter_email', 'assigned_to', 'status', 'on_hold', + 'priority', 'due_date', 'last_escalation', 'merged_to' + ) diff --git a/helpdesk/urls.py b/helpdesk/urls.py index e9df6bb2..29a6654f 100644 --- a/helpdesk/urls.py +++ b/helpdesk/urls.py @@ -10,13 +10,14 @@ urls.py - Mapping of URL's to our various views. Note we always used NAMED from django.conf.urls import url from django.contrib.auth.decorators import login_required from django.contrib.auth import views as auth_views +from django.urls import include from django.views.generic import TemplateView +from rest_framework.routers import DefaultRouter from helpdesk.decorators import helpdesk_staff_member_required, protect_view -from helpdesk import settings as helpdesk_settings from helpdesk.views import feeds, staff, public, login from helpdesk import settings as helpdesk_settings - +from helpdesk.views.api import TicketViewSet if helpdesk_settings.HELPDESK_KB_ENABLED: from helpdesk.views import kb @@ -218,6 +219,14 @@ urlpatterns += [ ] +# API +router = DefaultRouter() +router.register(r'tickets', TicketViewSet, basename='ticket') +urlpatterns += [ + url(r'^api/', include(router.urls)) +] + + urlpatterns += [ url(r'^login/$', login.login, diff --git a/helpdesk/views/api.py b/helpdesk/views/api.py new file mode 100644 index 00000000..1bf362fb --- /dev/null +++ b/helpdesk/views/api.py @@ -0,0 +1,14 @@ +from rest_framework import viewsets +from rest_framework.permissions import IsAdminUser + +from helpdesk.models import Ticket +from helpdesk.serializers import TicketSerializer + + +class TicketViewSet(viewsets.ModelViewSet): + """ + A viewset that provides the standard actions to handle Ticket + """ + queryset = Ticket.objects.all() + serializer_class = TicketSerializer + permission_classes = [IsAdminUser]