Skip to content
This repository has been archived by the owner on Oct 1, 2020. It is now read-only.

Commit

Permalink
update docs for template items, add permissions for templates
Browse files Browse the repository at this point in the history
  • Loading branch information
dmorina committed Nov 30, 2017
1 parent 203ae3b commit e5c741b
Show file tree
Hide file tree
Showing 5 changed files with 387 additions and 66 deletions.
35 changes: 32 additions & 3 deletions crowdsourcing/viewsets/template.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
from django.db import transaction
from rest_framework import mixins
from rest_framework import viewsets, status, serializers
from rest_framework.decorators import detail_route
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from django.db import transaction

from crowdsourcing.serializers.template import TemplateItemSerializer, TemplateItemPropertiesSerializer, \
TemplateSerializer


class TemplateViewSet(viewsets.ModelViewSet):
class TemplateViewSet(mixins.RetrieveModelMixin, mixins.CreateModelMixin, mixins.UpdateModelMixin,
mixins.ListModelMixin,
viewsets.GenericViewSet):
from crowdsourcing.models import Template

queryset = Template.objects.all()
serializer_class = TemplateSerializer
permission_classes = [IsAuthenticated]

def list(self, request, *args, **kwargs):
return Response(self.serializer_class(instance=request.user.templates.all().order_by('-id'), many=True).data)

@detail_route(methods=['get'])
def items(self, request, *args, **kwargs):
instance = self.queryset.filter(owner=request.user, id=kwargs.get('pk')).first()
if instance is None:
return Response({"message": "Template not found!"}, status=status.HTTP_404_NOT_FOUND)
return Response(TemplateItemSerializer(instance=instance.items.all(), many=True).data)


class TemplateItemViewSet(viewsets.ModelViewSet):
Expand All @@ -19,6 +35,17 @@ class TemplateItemViewSet(viewsets.ModelViewSet):
queryset = TemplateItem.objects.all()
serializer_class = TemplateItemSerializer

def list(self, request, *args, **kwargs):
template_id = request.query_params.get('template_id')
items = self.queryset.filter(template_id=template_id, template__owner=request.user)
response = {
"count": len(self.serializer_class(instance=items, many=True).data),
"next": None,
"previous": None,
"results": self.serializer_class(instance=items, many=True).data
}
return Response(response)

def update(self, request, *args, **kwargs):
with transaction.atomic():
instance = self.queryset.select_for_update().filter(id=kwargs.get('pk')).first()
Expand All @@ -31,7 +58,9 @@ def update(self, request, *args, **kwargs):
return Response(data={"message": "Item updated successfully"}, status=status.HTTP_200_OK)

def destroy(self, request, *args, **kwargs):
item = self.get_object()
item = self.queryset.filter(template__owner=request.user, id=kwargs.get('pk')).first()
if item is None:
return Response({"message": "Item not found!"}, status.HTTP_404_NOT_FOUND)
if item.successors.all().count() > 0:
item.successors.all().update(predecessor=item.predecessor)

Expand Down
2 changes: 2 additions & 0 deletions csp/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
router.register(r'projects', ProjectViewSet)
router.register(r'tasks', TaskViewSet)
router.register(r'assignments', TaskWorkerViewSet)
router.register(r'templates', TemplateViewSet)
router.register(r'template-items', TemplateItemViewSet)

router.register(r'profile', UserProfileViewSet)
router.register(r'user', UserViewSet)
Expand Down
Loading

0 comments on commit e5c741b

Please sign in to comment.