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)
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={})
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)
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={})