Beispiel #1
0
 def test_member_is_created(self):
     serial_user = UserSerializer(self.user2)
     serial = MemberSerializer(data={'user': serial_user.data, 'pointer': self.point.pk})
     self.assertTrue(serial.is_valid())
     serial.save()
     # 2 plus pointer author
     self.assertEqual(Member.objects.all().count(), 2)
Beispiel #2
0
 def put(self, request, pk, format=None):
     """
     Edit a Member.
     """
     member = self.get_member(pk)
     self.check_object_permissions(request, member)
     serializer = MemberSerializer(member, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Beispiel #3
0
def MemberList(request):

    if request.method == 'GET':
        contacts = Member.objects.all()
        serializer = MemberSerializer(contacts, many=True)
        return Response(serializer.data)
    elif request.method == 'POST':
        serializer = MemberSerializer(data=request.data)

    if serializer.is_valid():
        serializer.save()

        return Response(serializer.data, status=status.HTTP_201_CREATED)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Beispiel #4
0
def member_list(request):
    """
    Allows for GET requests for all members
    and POST requests to add member(s) to the
    db. Will return the http status code and 
    json data if applicable.
    """
    if request.method == 'GET':
        members = Member.objects.all()
        serializer = MemberSerializer(members, many=True)
        return JsonResponse(serializer.data, safe=False)

    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = MemberSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data, status=201)
        return JsonResponse(serializer.errors, status=400)
Beispiel #5
0
def member_detail(request, pk):
    """
    Allows operations on a specific member
    id such as GET, PUT, and DELETE. Will 
    return the http status and json data
    if applicable. 
    """
    try:
        member = Member.objects.get(pk=pk)
    except Member.DoesNotExist:
        return HttpResponse(status=404)

    if request.method == 'GET':
        serializer = MemberSerializer(member)
        return JsonResponse(serializer.data)

    elif request.method == 'PATCH':
        data = JSONParser().parse(request)
        response = member.update(data)
        if response != None:
            return HttpResponse(status=400)
        serializer = MemberSerializer(member)
        return JsonResponse(serializer.data)

    elif request.method == 'PUT':
        data = JSONParser().parse(request)
        serializer = MemberSerializer(member, data=data)
        print(data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data)
        return JsonResponse(serializer.errors, status=400)

    elif request.method == 'DELETE':
        member.delete()
        return HttpResponse(status=204)