예제 #1
0
 def update(self, request, *args, **kwargs):
     serializer = ClassSerializer(data=request.data,
                                  context={'request': request})
     if serializer.is_valid():
         Class.objects.filter(pk=self.get_object().pk).update(
             **serializer.validated_data)
         class_obj = self.get_object()
         students = []
         assignments = []
         if len(request.data.get('students', [])) > 0:
             students = request.POST.getlist('students')
         if len(request.data.get('assignments', [])) > 0:
             assignments = request.POST.getlist('assignments')
         if len(students) > 0:
             class_obj.students.clear()
         if len(assignments) > 0:
             class_obj.assignments.clear()
         for student in Student.objects.filter(pk__in=students):
             class_obj.students.add(student)
         for assignment in Assignment.objects.filter(pk__in=assignments):
             class_obj.assignments.add(assignment)
             class_obj.save()
         class_obj.save()
         return Response(status=status.HTTP_200_OK)
     else:
         return Response(status=status.HTTP_400_BAD_REQUEST)
예제 #2
0
    def post(self, request):
        params = {}

        class_id = request.data.get("id")

        class_obj = class_exists(class_id)
        if class_obj is None:
            params["message"] = f"Class with id {class_id} does not exist"
            params["status"] = 404
            return Response(params, status=params["status"])

        class_dict = model_to_dict(class_obj)
        # Droplet count not updating....why?
        class_dict.update(request.data)
        clas = ClassSerializer(class_obj, data=class_dict)
        if clas.is_valid() is True:
            class_obj_save = clas.save()
            params["status"] = 200
            params["message"] = "Class was updated successfully"
            params["info"] = model_to_dict(class_obj_save)
        else:
            params[
                "message"] = "Invalid parameters passed. You must data for every class field."
            params["status"] = 400
            params["errors"] = clas.errors
        return Response(params, status=params.get("status", 200))
예제 #3
0
 def destroy(self, request, pk):
     try:
         cl = Class.objects.get(name=pk)
         serializer = ClassSerializer(cl)
         cl.delete()
         if serializer.is_valid():
             return Response(serializer.data,
                             status=status.HTTP_202_ACCEPTED)
         else:
             return Response(status=status.HTTP_400_BAD_REQUEST)
     except Class.DoesNotExist:
         return Response(status=status.HTTP_404_NOT_FOUND)
예제 #4
0
    def post(self, request):
        params = {}

        clas = ClassSerializer(data=request.data)
        if clas.is_valid() is True:
            class_obj = clas.save()
            params["message"] = "Class successfully created"
            params["status"] = 200
            params["info"] = model_to_dict(class_obj)
        else:
            params["message"] = "Invalid parameters passed"
            params["status"] = 400
            params["errors"] = clas.errors
        return Response(params, status=params.get("status", 200))
예제 #5
0
 def partial_update(self, request, pk):
     try:
         data = JSONParser().parse(request)
         cl = Class.objects.get(name=pk)
         serializer = ClassSerializer(cl, data=data, partial=True)
         if serializer.is_valid():
             serializer.save()
             return Response(serializer.data,
                             status=status.HTTP_202_ACCEPTED)
         else:
             return Response(serializer.errors,
                             status=status.HTTP_400_BAD_REQUEST)
     except Class.DoesNotExist:
         return Response(status=status.HTTP_404_NOT_FOUND)
예제 #6
0
 def list(self, request):
     queryset = Class.objects.all()
     name = request.query_params.get('name')
     type = request.query_params.get('type')
     age = request.query_params.get('age')
     difficulty = request.query_params.get('difficulty')
     workarea = request.query_params.get('muscle')
     categories = request.query_params.get('categories')
     if name is not None:
         queryset = queryset.filter(name=name)
     elif type is not None:
         queryset = queryset.filter(type=type)
     elif age is not None:
         queryset = queryset.filter(age=age)
     elif difficulty is not None:
         queryset = queryset.filter(difficulty=difficulty)
     elif workarea is not None:
         queryset = queryset.filter(workearea=workarea)
     elif categories is not None:
         queryset = queryset.filter(categories=categories)
     if queryset.count() > 0:
         serializer = ClassSerializer(queryset, many=True)
         return Response(serializer.data, status=status.HTTP_200_OK)
     else:
         return Response(status=status.HTTP_404_NOT_FOUND)
예제 #7
0
 def destroy(self, request, *args, **kwargs):
     instance = self.get_object()
     data = ClassSerializer(instance).data
     instance.delete()
     return Response(data, status=status.HTTP_204_NO_CONTENT)