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