Exemplo n.º 1
0
 def put(self, request, pk, format=None):
     contact = self.get_object(pk)
     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)
Exemplo n.º 2
0
 def put(self, request, pk):
     contact = self.get_object(pk)
     serializer = ContactSerializer(instance=contact, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response(serializer.errors,
                     status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Exemplo n.º 3
0
 def put(self, request, contact_id):
     contact = Contact.objects.get(Contact, id=contact_id)
     data = request.data
     serializer = ContactSerializer(data=data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data, status=status.HTTP_204_NO_CONTENT)
     else:
         return Response(serializer.errors,
                         status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 4
0
    def create(self, request, user_pk=None):
        """
        Creates a Connection instance from two given User instances: the
        user itself and its associate.

        Parameters:
            request (rest_framework.response.Response):
                HTTP request.
            user_pk (uuid.UUID): Primary key for the user instance.
            pk (uuid.UUID): Primary key for the associated user instance.
        """
        data = request.data
        data['user'] = user_pk

        serializer = ContactSerializer(data=data)
        serializer.is_valid(raise_exception=True)
        serializer.save()

        return Response(serializer.data, status=status.HTTP_201_CREATED)
Exemplo n.º 5
0
def contacts_detail(request,pk):
    if request.method == 'PUT':
        contact = Contact.objects.get(id=pk)
        data = json.loads(request.body)
        serializer = ContactSerializer(instance=contact, data=data)
        if serializer.is_valid():
            serializer.save()  # update function in serializer class
            return JsonResponse(serializer.data, status=200)
        return JsonResponse(serializer.errors)
    elif request.method == 'DELETE':
        contact.delete()
        return JsonResponse({}, status=204)
Exemplo n.º 6
0
def contactlist(request):
    if request.method == 'GET':
        contactlist = Contact.objects.all()
        serializers = ContactSerializer(contactlist, many=True)
        return JsonResponse(serializers.data, safe=False)
    elif request.method == 'POST':
        data = json.loads(request.body)
        serializer = ContactSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data)
        return JsonResponse(serializer.errors)
Exemplo n.º 7
0
def contact_list(request):
    if (request.method == "GET"):
        contacts = Contact.objects.all()
        ser = ContactSerializer(contacts, many=True)
        return JsonResponse(ser.data, safe=False)
    elif (request.method == "POST"):
        contactToAdd = JSONParser().parse(request)
        ser = ContactSerializer(data=contactToAdd)
        if (ser.is_valid()):
            ser.save()
            return JsonResponse(ser.data, status=201)
        return JsonResponse(ser.errors, status=400)
Exemplo n.º 8
0
def contact_list(request):
    if request.method == 'GET':
        contacts = Contact.objects.all()
        serializer = ContactSerializer(contacts, many=True)
        return JsonResponse(serializer.data, safe=False, status=200)

    elif request.method == 'POST':
        data = json.loads(request.body)
        serializer = ContactSerializer2(data=data)
        if serializer.is_valid():
            serializer.save()  # create function in serializer class
            return JsonResponse(serializer.data, status=201)
        return JsonResponse(serializer.errors)
Exemplo n.º 9
0
def contacts(request):  # List of all contacts
    if request.method == 'GET':
        contactss = Contact.objects.all()
        serializer = ContactSerializer(contactss, many=True)
        return Response(serializer.data, status=status.HTTP_200_OK)

    elif request.method == 'POST':  # Create contact
        serializer = ContactSerializer2(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_500_INTERNAL_SERVER_ERROR)
Exemplo n.º 10
0
def contacts(request):
    if request.method == 'GET':
        contacts = Contacts.objects.all()
        serializer = ContactSerializer(contacts, many=True)
        return JsonResponse(serializer.data, safe=False)
    elif request.method == 'POST':
        body = json.loads(request.body)
        serializer = ContactSerializer(data=body)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data)
        return JsonResponse(serializer.errors)
    return JsonResponse({'error': 'bad request'})
Exemplo n.º 11
0
def contact_list(request):
    # permission_classes = (IsAuthenticated,)

    if request.method == 'GET':
        contacts = Contact.objects.all()
        serializer = ContactSerializer(contacts, many=True)
        return Response(serializer.data, status=status.HTTP_200_OK)
    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_500_INTERNAL_SERVER_ERROR)
Exemplo n.º 12
0
def contacts(request):
    if request.method == 'GET':
        contacts_list = TaskList.objects.all()
        serializer = ContactSerializer(contacts_list, many=True)
        return Response(serializer.data, status=status.HTTP_200_OK)

    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_500_INTERNAL_SERVER_ERRORS)
    return Response(status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 13
0
def contactformCreate(request):
    serializer = ContactSerializer(data=request.data)
    if serializer.is_valid():
        serializer.save()
        #Please uncomment the following lines if you want to get e-mail triggered when you get a message
        # send_mail(
        #   'Portfolio Contact Form Message',
        #   'Please check your contact form',
        #   '*****@*****.**',
        #   ['*****@*****.**'],
        #   fail_silently = False,

        # )
    return Response(serializer.data)
Exemplo n.º 14
0
    def post(self, request, *args, **kwargs):
        if {'city', 'street', 'phone'}.issubset(request.data):
            request.data._mutable = True
            request.data.update({'user': request.user.id})
            serializer = ContactSerializer(data=request.data)

            if serializer.is_valid():
                serializer.save()
                return Response({'status': True}, status=status.HTTP_201_CREATED)
            else:
                Response({'status': False, 'error': serializer.errors}, status=status.HTTP_400_BAD_REQUEST)

        return Response({'status': False, 'error': 'Не указаны все необходимые аргументы'},
                        status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 15
0
def contact_list(request):

    if request.method == 'GET':
        contact = Contact.objects.all()
        serializer = ContactSerializer(contact, 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)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 16
0
def contact_list(request):
    if request.method == 'GET':
        contacts = Contact.objects.all()
        ser = ContactSerializer(contacts, many=True)
        return JsonResponse(ser.data, safe=False)

    elif request.method == 'POST':
        data = JSONParser().parse(request)
        ser = ContactSerializer(data=data)
    if ser.is_valid():
        ser.save()
        return JsonResponse(ser.data, status=201)

    return JsonResponse(ser.errors, status=400)
Exemplo n.º 17
0
def contactdetail(request, pk):
    try:
        contactlist = Contact.objects.get(id=pk)
    except Contact.DoesNotExist as e:
        return JsonResponse({'error': str(e)})
    if request.method == 'PUT':
        data = json.loads(request.body)
        serializer = ContactSerializer(instance=contactlist, data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data)
        return JsonResponse(serializer.errors)
    elif request.method == 'DELETE':
        contactlist.delete()
        return JsonResponse({})
    return JsonResponse({'error': 'bad request'})
Exemplo n.º 18
0
    def post(self, request, *args, **kwargs):
        if not request.user.is_authenticated:
            return JsonResponse({'Status': False, 'Error': 'Log in required'}, status=403)

        if {'city', 'street', 'phone'}.issubset(request.data):
            request.data._mutable = True
            request.data.update({'user': request.user.id})
            serializer = ContactSerializer(data=request.data)

            if serializer.is_valid():
                serializer.save()
                return JsonResponse({'Status': True})
            else:
                JsonResponse({'Status': False, 'Errors': serializer.errors})

        return JsonResponse({'Status': False, 'Errors': 'Не указаны все необходимые аргументы'})
Exemplo n.º 19
0
def contact(request, pk):  # Get TaskList
    try:
        contacti = Contact.objects.get(id=pk)
    except Contact.DoesNotExist as e:
        return Response({'error doesn exist'},
                        status=status.HTTP_404_NOT_FOUND)

    if request.method == 'PUT':
        serializer = ContactSerializer(instance=contacti, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_200_OK)
        return Response(serializer.errors,
                        status=status.HTTP_500_INTERNAL_SERVER_ERROR)
    elif request.method == 'DELETE':
        contacti.delete()
        return Response({}, status=status.HTTP_204_NO_CONTENT)
Exemplo n.º 20
0
def contact_update_delete(request, pk):
    try:
        contact = Contact.objects.filter(created_by=request.user).get(pk=pk)
    except Contact.DoesNotExist as e:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method == 'DELETE':
        contact.delete()
        serializer = ContactSerializer(contact)
        return Response(serializer.data)

    if request.method == 'PUT':
        serializer = ContactSerializer(contact, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors)
Exemplo n.º 21
0
    def test_serializer_updates_only_affect_correct_fields(self):
        """
        Updates done through serializer should only affect fields included in
        serializers data and leave others unaffected.
        """

        serializer = ContactSerializer(
            self.contact, data=self.data, context=self.context, partial=True
        )
        self.assertTrue(serializer.is_valid())
        serializer.save()

        contact = Contact.objects.latest('contact_date')
        self.assertEqual(self.data['github'], contact.github)
        self.assertEqual(self.data['facebook'], contact.facebook)
        self.assertEqual('testcontact', contact.title)
        self.assertEqual('www.linkedin.com', contact.linkedin)
Exemplo n.º 22
0
    def put(self, request, *args, **kwargs):
        if not request.user.is_authenticated:
            return JsonResponse({'Status': False, 'Error': 'Log in required'}, status=403)

        if 'id' in request.data:
            if request.data['id'].isdigit():
                contact = Contact.objects.filter(id=request.data['id'], user_id=request.user.id).first()
                print(contact)
                if contact:
                    serializer = ContactSerializer(contact, data=request.data, partial=True)
                    if serializer.is_valid():
                        serializer.save()
                        return JsonResponse({'Status': True})
                    else:
                        JsonResponse({'Status': False, 'Errors': serializer.errors})

        return JsonResponse({'Status': False, 'Errors': 'Не указаны все необходимые аргументы'})
Exemplo n.º 23
0
def contact_detail(request, contact_id):
    try:
        contact = Contact.objects.get(pk=contact_id)
    except Exception as e:
        return JsonResponse({"error": str(e)}, status=404)
    if (request.method == "GET"):
        ser = ContactSerializer(contact)
        return JsonResponse(ser.data)
    elif (request.method == "PUT"):
        contactToUpdate = JSONParser().parse(request)
        ser = ContactSerializer(contact, contactToUpdate)
        if (ser.is_valid()):
            ser.save()
            return JsonResponse(ser.data)
    elif (request.method == "DELETE"):
        contact.delete()
        ser = ContactSerializer(contact)
        return JsonResponse(ser.data)
Exemplo n.º 24
0
def contact_detail(request, pk) :
     try:
         print(pk)
         res = Contact.objects.get(id=pk)
     except Contact.DoesNotExist:
         return Response('Post list not found',status=404)
     if(request.method == 'GET'):
         ser = ContactSerializer(res)
         return Response(ser.data)
     if(request.method == 'PUT'):
         ser = ContactSerializer(instance=res,data=request.data)
         if(ser.is_valid()):
             ser.save()
             return Response(ser.data,status=201)
         return Response('some error occured')
     if(request.method == 'DELETE'):
         res.delete()
         return Response('done:')
Exemplo n.º 25
0
def contact_detail(request, pk):
    try:
        taskList = Contact.objects.get(id=pk)
    except Contact.DoesNotExist:
        return Response(status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        serializer = ContactSerializer(taskList)
        return Response(serializer.data)
    elif request.method == 'PUT':
        serializer = ContactSerializer(instance=taskList, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors)
    elif request.method == 'DELETE':
        taskList.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)
Exemplo n.º 26
0
def contact_detail(request, pk):
    try:
        contact = Contact.objects.get(id=pk)
    except Contact.DoesNotExist as e:
        return JsonResponse({'error': str(e)})

    if request.method == 'GET':
        serializer = ContactSerializer(contact)
        return JsonResponse(serializer.data, status=200)
    elif request.method == 'PUT':
        data = json.loads(request.body)
        serializer = ContactSerializer(instance=contact, data=data)
        if serializer.is_valid():
            serializer.save()  # update function in serializer class
            return JsonResponse(serializer.data, status=200)
        return JsonResponse(serializer.errors)
    elif request.method == 'DELETE':
        contact.delete()
        return JsonResponse({}, status=204)
Exemplo n.º 27
0
def contact_detail(request, pk):
    try:
        contacts = Contact.objects.get(id=pk)
    except Contact.DoesNotExist as e:
        return Response({'error', f'{e}'}, status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        serializer = ContactSerializer(contacts)
        return Response(serializer.data)
    elif request.method == 'PUT':
        serializer = ContactSerializer(instance=contacts, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors,
                        status=status.HTTP_500_INTERNAL_SERVER_ERROR)
    elif request.method == 'DELETE':
        contacts.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)
Exemplo n.º 28
0
    def put(self, request, *args, **kwargs):
        if {'id'}.issubset(request.data):
            try:
                contact = Contact.objects.get(pk=int(request.data["id"]))
            except ValueError:
                return Response(
                    {'status': False, 'error': 'Неверный тип аргумента ID.'}, status=status.HTTP_400_BAD_REQUEST)
            except ObjectDoesNotExist:
                return Response(
                    {'status': False, 'error': f"Контакта с ID={request.data['id']} не существует."},
                    status=status.HTTP_400_BAD_REQUEST
                )
            serializer = ContactSerializer(contact, data=request.data, partial=True)
            if serializer.is_valid():
                serializer.save()
                return Response({'status': True}, status=status.HTTP_200_OK)
            return Response({'status': False, 'error': serializer.errors}, status=status.HTTP_400_BAD_REQUEST)

        return Response({'status': False, 'error': 'Не указаны все необходимые аргументы'},
                        status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 29
0
def show_contact_detail(request, pk):
    try:
        contacts = Contact.objects.get(id=pk)
    except Contact.DoesNotExist as e:
        return JsonResponse({'error': str(e)})

    if request.method == "GET":
        serializer = ContactSerializer(tasks)
        return JsonResponse(serializer.data)

    elif request.method == "PUT":
        data = json.loads(request.body)
        serializer = ContactSerializer(instance=tasks, data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data)
        return JsonResponse(serializer.errors)

    elif request.method == "DELETE":
        contacts.delete()
        return JsonResponse({})
Exemplo n.º 30
0
def selected_contact(request, pk):
    try:
        contact = Contact.objects.get(id=pk)
    except TaskList.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(instance=task_list, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors)

    elif request.method == 'DELETE':
        contact.delete()
        return Response(status.HTTP_204_NO_CONTENT)

    return Response(status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 31
0
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':
        contact_detail1 = Contact.objects.get(pk=pk)
        serializer = ContactSerializer(contact_detail1)
        return Response(serializer.data)

    elif request.method == 'PUT':
        serializer = ContactSerializer(contact, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_200_OK)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    elif request.method == 'DELETE':
        contact.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)
Exemplo n.º 32
0
 def post(self, request, format=None):
     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)