def put(request): contact = Contact.objects.get(id=request.data['id']) payload = request.data serializer = ContactSerializer(contact, data=payload, partial=True) if serializer.is_valid(raise_exception=True): serializer.save() return Response(serializer.data, status=HTTP_200_OK)
def put(self, request, pk): contact = self.my_get_object(pk) incoming = ContactSerializer(instance=contact, data=request.data, partial=True) if not incoming.is_valid(raise_exception=True): return Response(incoming.errors, status=status.HTTP_400_BAD_REQUEST) new_contact = incoming.save() outgoing = ContactSerializer(instance=new_contact) return Response(outgoing.data, status=status.HTTP_200_OK)
def post(self, request): request.data['owner'] = request.user.id incoming = ContactSerializer(data=request.data) if not incoming.is_valid(raise_exception=True): return Response(incoming.errors, status=status.HTTP_400_BAD_REQUEST) new_contact = incoming.create(incoming.validated_data) outgoing = ContactSerializer(instance=new_contact) return Response(outgoing.data, status=status.HTTP_201_CREATED)
def post(self, request, *args, **kwargs): data = request.data first_name = data.get('first_name') last_name = data.get('last_name') phone_number = data.get('phone_number') """ Check Json """ if not data: return Response({'error': 'Json not found'}, status=400) if not first_name: return Response({'error': 'First_name not found in json'}, status=400) if not last_name: return Response({'error': 'Last_name not found in json'}, status=400) if not phone_number: return Response({'error': 'Phone_number not found in json'}, status=400) user = get_user_model().objects.filter(pk=request.user.pk).first() if not user: return Response({'error': 'user not found in json'}, status=404) contactObj = Contact.objects.filter(phone_number=phone_number).first() if not contactObj: """ Create new contact """ contactSerializer = ContactSerializer(data=request.data) if contactSerializer.is_valid(): contactSerializer.save() else: """ Update contact """ contactSerializer = ContactSerializer(contactObj, data=request.data) if contactSerializer.is_valid(): contactSerializer.save() user.contacts.add(contactObj) user.save() return Response(status=201)
def contact_list(request): if request.method == 'GET': contacts = Contact.objects.all() serializer = ContactSerializer(contacts, many=True) return Response(serializer.data) elif request.method == 'POST': serializer = ContactSerializer(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) elif request.method == 'DELETE': contact = Contact.objects.get(name=request.data.name) contact.delete() return Response(status.HTTP_204_NO_CONTENT)
def post(request): token = request.META.get('HTTP_AUTHORIZATION') token = token.split(" ")[1] _id = Token.objects.get(key=str(token)) data = { "first_name": request.data['first_name'], "last_name": request.data['last_name'], "note": request.data['note'], "dob": request.data['dob'], "user_id": _id.user_id } serializer = ContactSerializer(data=data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) print(serializer.errors) return Response(serializer.errors)
def contact_detail(request, pk): try: contact = Contact.objects.get(pk=pk) except Contact.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': serializer = ContactSerializer(contact) return Response(serializer.data) elif request.method == 'PUT': serializer = ContactSerializer(contact, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE': contact.delete() return Response(status=status.HTTP_204_NO_CONTENT)