Add API endpoint for viewing and deleting a PartStar entry

This commit is contained in:
Oliver Walters 2019-05-05 10:36:48 +10:00
parent d245e58990
commit 3bd7c28558
2 changed files with 21 additions and 5 deletions

View File

@ -153,6 +153,13 @@ class PartList(generics.ListCreateAPIView):
] ]
class PartStarDetail(generics.RetrieveDestroyAPIView):
""" API endpoint for viewing or removing a PartStar object """
queryset = PartStar.objects.all()
serializer_class = PartStarSerializer
class PartStarList(generics.ListCreateAPIView): class PartStarList(generics.ListCreateAPIView):
""" API endpoint for accessing a list of PartStar objects. """ API endpoint for accessing a list of PartStar objects.
@ -165,13 +172,20 @@ class PartStarList(generics.ListCreateAPIView):
def create(self, request, *args, **kwargs): def create(self, request, *args, **kwargs):
# Ensure the 'user' field is the authenticated user # Automatically add the user information
user_id = request.data['user'] data = request.data.copy()
data['user'] = str(request.user.id)
if not str(user_id) == str(request.user.id): serializer = self.get_serializer(data=data)
raise ValidationError({'user': 'Parts can only be starred for the currently authenticated user'})
return super(generics.ListCreateAPIView, self).create(request, *args, **kwargs) print(serializer)
print(data)
print(request.user)
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)
headers = self.get_success_headers(serializer.data)
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
permission_classes = [ permission_classes = [
permissions.IsAuthenticatedOrReadOnly, permissions.IsAuthenticatedOrReadOnly,
@ -310,6 +324,7 @@ supplier_part_api_urls = [
] ]
part_star_api_urls = [ part_star_api_urls = [
url(r'^(?P<pk>\d+)/?', PartStarDetail.as_view(), name='api-part-star-detail'),
# Catchall # Catchall
url(r'^.*$', PartStarList.as_view(), name='api-part-star-list'), url(r'^.*$', PartStarList.as_view(), name='api-part-star-list'),

View File

@ -86,6 +86,7 @@ class PartStarSerializer(InvenTreeModelSerializer):
class Meta: class Meta:
model = PartStar model = PartStar
fields = [ fields = [
'pk',
'part', 'part',
'partname', 'partname',
'user', 'user',