Refactor BuildOrder API classes with a fancy mixin

This commit is contained in:
Oliver Walters 2022-05-04 15:59:30 +10:00
parent a510ca89f7
commit 768e23c7b8

View File

@ -233,7 +233,24 @@ class BuildUnallocate(generics.CreateAPIView):
return ctx
class BuildOutputCreate(generics.CreateAPIView):
class BuildOrderContextMixin:
""" Mixin class which adds build order as serializer context variable """
def get_serializer_context(self):
ctx = super().get_serializer_context()
ctx['request'] = self.request
ctx['to_complete'] = True
try:
ctx['build'] = Build.objects.get(pk=self.kwargs.get('pk', None))
except:
pass
return ctx
class BuildOutputCreate(BuildOrderContextMixin, generics.CreateAPIView):
"""
API endpoint for creating new build output(s)
"""
@ -242,21 +259,8 @@ class BuildOutputCreate(generics.CreateAPIView):
serializer_class = build.serializers.BuildOutputCreateSerializer
def get_serializer_context(self):
ctx = super().get_serializer_context()
ctx['request'] = self.request
ctx['to_complete'] = True
try:
ctx['build'] = Build.objects.get(pk=self.kwargs.get('pk', None))
except:
pass
return ctx
class BuildOutputComplete(generics.CreateAPIView):
class BuildOutputComplete(BuildOrderContextMixin, generics.CreateAPIView):
"""
API endpoint for completing build outputs
"""
@ -265,21 +269,8 @@ class BuildOutputComplete(generics.CreateAPIView):
serializer_class = build.serializers.BuildOutputCompleteSerializer
def get_serializer_context(self):
ctx = super().get_serializer_context()
ctx['request'] = self.request
ctx['to_complete'] = True
try:
ctx['build'] = Build.objects.get(pk=self.kwargs.get('pk', None))
except:
pass
return ctx
class BuildOutputDelete(generics.CreateAPIView):
class BuildOutputDelete(BuildOrderContextMixin, generics.CreateAPIView):
"""
API endpoint for deleting multiple build outputs
"""
@ -288,20 +279,8 @@ class BuildOutputDelete(generics.CreateAPIView):
serializer_class = build.serializers.BuildOutputDeleteSerializer
def get_serializer_context(self):
ctx = super().get_serializer_context()
ctx['request'] = self.request
try:
ctx['build'] = Build.objects.get(pk=self.kwargs.get('pk', None))
except:
pass
return ctx
class BuildFinish(generics.CreateAPIView):
class BuildFinish(BuildOrderContextMixin, generics.CreateAPIView):
"""
API endpoint for marking a build as finished (completed)
"""
@ -310,20 +289,8 @@ class BuildFinish(generics.CreateAPIView):
serializer_class = build.serializers.BuildCompleteSerializer
def get_serializer_context(self):
ctx = super().get_serializer_context()
ctx['request'] = self.request
try:
ctx['build'] = Build.objects.get(pk=self.kwargs.get('pk', None))
except:
pass
return ctx
class BuildAutoAllocate(generics.CreateAPIView):
class BuildAutoAllocate(BuildOrderContextMixin, generics.CreateAPIView):
"""
API endpoint for 'automatically' allocating stock against a build order.
@ -337,24 +304,8 @@ class BuildAutoAllocate(generics.CreateAPIView):
serializer_class = build.serializers.BuildAutoAllocationSerializer
def get_serializer_context(self):
"""
Provide the Build object to the serializer context
"""
context = super().get_serializer_context()
try:
context['build'] = Build.objects.get(pk=self.kwargs.get('pk', None))
except:
pass
context['request'] = self.request
return context
class BuildAllocate(generics.CreateAPIView):
class BuildAllocate(BuildOrderContextMixin, generics.CreateAPIView):
"""
API endpoint to allocate stock items to a build order
@ -370,22 +321,6 @@ class BuildAllocate(generics.CreateAPIView):
serializer_class = build.serializers.BuildAllocationSerializer
def get_serializer_context(self):
"""
Provide the Build object to the serializer context
"""
context = super().get_serializer_context()
try:
context['build'] = Build.objects.get(pk=self.kwargs.get('pk', None))
except:
pass
context['request'] = self.request
return context
class BuildItemDetail(generics.RetrieveUpdateDestroyAPIView):
"""