Ejemplo n.º 1
0
    def partial_update(self, request, pk=None):
        """

        :param request:
        :param pk:
        :return:
        """
        request.data["changed_by"] = self.request.user.username
        instance = get_or_none(ContactBook, id=pk)
        if not isinstance(instance, ContactBook):
            return error_response(
                data={"error": "Contact Book does not exist"},
                msg='ContactBook does not exists',
                status=status.HTTP_404_NOT_FOUND)
        serializer = ContactBookSerializer(instance=instance,
                                           data=request.data,
                                           partial=True)
        if serializer.is_valid():
            serializer.save()
            return success_response(status=status.HTTP_202_ACCEPTED,
                                    msg='Contact Book is updated',
                                    data=serializer.data)
        return error_response(status=status.HTTP_400_BAD_REQUEST,
                              msg='Invalid data',
                              data=serializer.errors)
Ejemplo n.º 2
0
 def soft_delete(self, request, pk):
     contact = get_or_none(Contact, id=pk)
     if isinstance(contact, Contact):
         if contact.deleted:
             return error_response(status=status.HTTP_400_BAD_REQUEST,
                                   msg="Already deleted",
                                   data={})
         contact.deleted = True
         contact.deleted_on = datetime.now()
         contact.save()
         return success_response(msg="id: {} deleted successful".format(pk),
                                 data={},
                                 status=status.HTTP_202_ACCEPTED)
     return error_response(status=status.HTTP_400_BAD_REQUEST,
                           msg="{} does not exists".format(pk),
                           data={})
Ejemplo n.º 3
0
 def hard_delete(self, request, pk):
     try:
         contact_book = ContactBook.objects.get(id=pk)
     except ContactBook.DoesNotExist:
         contact_book = None
         return error_response(status=status.HTTP_400_BAD_REQUEST,
                               msg="{} does not exists".format(pk),
                               data={})
     if isinstance(contact_book, ContactBook):
         ContactBook.objects.filter(id=pk).delete()
         print(ContactBook.objects.filter(id=pk))
         return success_response(msg='{} is deleted'.format(pk),
                                 status=status.HTTP_202_ACCEPTED,
                                 data={})
     return error_response(status=status.HTTP_400_BAD_REQUEST,
                           msg="{} does not exists".format(pk),
                           data={})
Ejemplo n.º 4
0
 def hard_delete(self, request, pk):
     contact = get_or_none(Contact, id=pk)
     if isinstance(contact, Contact):
         Contact.objects.filter(id=pk).delete()
         return success_response(msg='{} is deleted'.format(pk),
                                 status=status.HTTP_202_ACCEPTED,
                                 data={})
     return error_response(status=status.HTTP_400_BAD_REQUEST,
                           msg="{} does not exists".format(pk),
                           data={})
Ejemplo n.º 5
0
 def retrieve(request, pk=None):
     """
     :param request:
     :param pk:
     :return:
     """
     instance = get_or_none(Contact, id=pk)
     if isinstance(instance, Contact):
         serializer = ContactSerializer(instance=instance)
         return success_response(data=serializer.data,
                                 status=status.HTTP_200_OK,
                                 msg='success')
     return error_response(data={"error": "Contact Book does not exist"},
                           msg='Contact does not exists',
                           status=status.HTTP_404_NOT_FOUND)
Ejemplo n.º 6
0
 def create(self, request):
     if 'name' not in request.data:
         return Response(
             data={"error": "Please specify name of contact book"},
             status=status.HTTP_400_BAD_REQUEST)
     request.data["created_by"] = self.request.user.username
     request.data["changed_by"] = self.request.user.username
     serializer = ContactBookSerializer(data=request.data)
     if serializer.is_valid():
         serializer.save()
         return success_response(status=status.HTTP_201_CREATED,
                                 msg='Contact Book is created',
                                 data=serializer.data)
     return error_response(status=status.HTTP_400_BAD_REQUEST,
                           msg='Invalid data',
                           data=serializer.errors)
Ejemplo n.º 7
0
    def create(self, request):
        """

        :param request:
        :return:
        """
        request.data["created_by"] = self.request.user.username
        request.data["changed_by"] = self.request.user.username
        serializer = ContactSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return success_response(status=status.HTTP_201_CREATED,
                                    msg='Contact  is created',
                                    data=serializer.data)
        return error_response(status=status.HTTP_400_BAD_REQUEST,
                              msg='Invalid data',
                              data=serializer.errors)