Skip to content
Permalink
Browse files
Refactor api
 * move api under its own path
 * add urls to model views to help with api navigation
 * add user and group to api
 * experiment with change events as a subpath of tickets

git-svn-id: https://svn.apache.org/repos/asf/bloodhound/branches/bh_core_experimental@1853879 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
Gary Martin committed Feb 19, 2019
1 parent ae3966a commit ac3d18794ac0566a8f28f2fb19bac0843d3c8c29
Showing 5 changed files with 78 additions and 20 deletions.
@@ -142,3 +142,9 @@
# https://docs.djangoproject.com/en/2.0/howto/static-files/

STATIC_URL = '/static/'

REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly',
]
}
@@ -35,11 +35,12 @@ class Meta:


class Ticket(ModelCommon):
title = models.CharField(max_length=200, null=True)
description = models.TextField(null=True)

def api_url(self):
return reverse('ticket_view', args=(self.id,))
return reverse('ticket-detail', args=(self.id,))

def api_events_url(self):
return reverse('changeevent-list', args=(self.id,))

def last_update(self):
last_event = self.changeevent_set.order_by('created').last()
@@ -67,6 +68,9 @@ def get_field_value(self, field):
class TicketField(ModelCommon):
name = models.CharField(max_length=32)

def api_url(self):
return reverse('ticketfield-detail', args=(self.id,))


class ChangeEvent(ModelCommon):
ticket = models.ForeignKey(Ticket, models.CASCADE, null=False)
@@ -81,3 +85,9 @@ def value(self, which=2):
def __str__(self):
return "Change to: {}; Field: {}; Diff: {}".format(
self.ticket, self.field, self.diff)

def api_url(self):
return reverse('changeevent-detail', args=(self.ticket.id, self.id,))

def api_ticket_url(self):
return reverse('ticket-detail', args=(self.ticket.id,))
@@ -1,9 +1,23 @@
from django.contrib.auth.models import User, Group
from rest_framework import serializers
from trackers import models


class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = User
fields = ('url', 'username', 'email', 'is_staff')


class GroupSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Group
fields = ('url', 'name')


class TicketSerializer(serializers.ModelSerializer):
api_url = serializers.SerializerMethodField()
api_events_url = serializers.SerializerMethodField()

class Meta:
model = models.Ticket
@@ -12,14 +26,31 @@ class Meta:
def get_api_url(self, obj):
return self.context['request'].build_absolute_uri(obj.api_url())

def get_api_events_url(self, obj):
return self.context['request'].build_absolute_uri(obj.api_events_url())


class TicketFieldSerializer(serializers.ModelSerializer):
api_url = serializers.SerializerMethodField()

class Meta:
model = models.TicketField
fields = '__all__'

def get_api_url(self, obj):
return self.context['request'].build_absolute_uri(obj.api_url())


class ChangeEventSerializer(serializers.ModelSerializer):
api_url = serializers.SerializerMethodField()
api_ticket_url = serializers.SerializerMethodField()

class Meta:
model = models.ChangeEvent
fields = '__all__'

def get_api_url(self, obj):
return self.context['request'].build_absolute_uri(obj.api_url())

def get_api_ticket_url(self, obj):
return self.context['request'].build_absolute_uri(obj.api_ticket_url())
@@ -16,13 +16,22 @@
# under the License.

from django.urls import path
from django.conf.urls import include
from rest_framework import routers
from . import views

router = routers.DefaultRouter()
router.register('users', views.UserViewSet)
router.register('groups', views.GroupViewSet)
router.register('tickets', views.TicketViewSet)
router.register('ticketfields', views.TicketFieldViewSet)

ticket_router = routers.DefaultRouter()
ticket_router.register('ticketevents', views.ChangeEventViewSet)

urlpatterns = [
path('', views.home, name='home'),
path('api/', include(router.urls)),
path('api/tickets/<uuid:id>/', include(ticket_router.urls)),
path('schema_view/', views.schema_view),
path('field/', views.TicketFieldListCreate.as_view()),
path('ticket/', views.TicketListCreate.as_view()),
path('ticket/<uuid:id>', views.TicketViewUpdate.as_view(), name='ticket_view'),
path('ticket/<uuid:id>/event/', views.ChangeEventListCreate.as_view()),
]
@@ -15,9 +15,9 @@
# specific language governing permissions and limitations
# under the License.

from django.contrib.auth.models import User, Group
from django.http import HttpResponse
from django.shortcuts import render
from rest_framework import generics
from rest_framework import viewsets
from . import serializers
from . import models

@@ -30,24 +30,26 @@ def home(request):
return HttpResponse('<html><title>Bloodhound Trackers</title></html>')


class TicketListCreate(generics.ListCreateAPIView):
queryset = models.Ticket.objects.all()
serializer_class = serializers.TicketSerializer
class UserViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
serializer_class = serializers.UserSerializer


class TicketViewUpdate(generics.RetrieveUpdateAPIView):
queryset = models.Ticket.objects.all()
serializer_class = serializers.TicketSerializer
lookup_field = 'id'
class GroupViewSet(viewsets.ModelViewSet):
queryset = Group.objects.all()
serializer_class = serializers.GroupSerializer


class TicketFieldListCreate(generics.ListCreateAPIView):
class TicketFieldViewSet(viewsets.ModelViewSet):
queryset = models.TicketField.objects.all()
serializer_class = serializers.TicketFieldSerializer
lookup_field = 'ticket'


class ChangeEventListCreate(generics.ListCreateAPIView):
class TicketViewSet(viewsets.ModelViewSet):
queryset = models.Ticket.objects.all()
serializer_class = serializers.TicketSerializer


class ChangeEventViewSet(viewsets.ModelViewSet):
queryset = models.ChangeEvent.objects.all()
serializer_class = serializers.ChangeEventSerializer
lookup_field = 'ticket'

0 comments on commit ac3d187

Please sign in to comment.