def delete(self, request, *args, **kwargs): data = request.body valid_json = is_json(data) if valid_json == False: data = json.dumps({'Msg': 'Please provide valid json format'}) return self.render_to_http_response(data, status_code=400) pdata = json.loads(data) id = pdata.get('id', None) if not id: data = json.dumps({'id': 'id is must to delete resource'}) return self.render_to_http_response(data, status_code=400) emp = get_object_by_id(id) if not emp: data = json.dumps( {"message": "The requested resource is not found to delete"}) return self.render_to_http_response(data, status_code=400) status, deleted_item = emp.delete() if status == 1: # status 1 means DB successfully processed the request data = json.dumps( {"message": "The resource deleted successfully."}) return self.render_to_http_response(data) data = json.dumps({"message": "uanble to delete recored, try again"}) return self.render_to_http_response(data, status_code=400)
def put(self, request, id, *args, **kwargs): emp = self.get_object_by_id(id=id) if emp is None: json_data = json.dumps( {'msg': 'No Matched record found not possible to updation '}) return self.render_to_http_response(json_data, status=404) data = request.body valid_json = is_json(data) if not valid_json: json_data = json.dumps({'msg': 'please send valid json data'}) return self.render_to_http_response(json_data, status=400) provided_data = json.loads(data) origial_data = { 'eno': emp.eno, 'ename': emp.ename, 'esal': emp.esal, 'eaddr': emp.eaddr, } origial_data.update(provided_data) form = EmployeeForm(origial_data, instance=emp) if form.is_valid(): form.save() json_data = json.dumps({'msg': 'Resource update successfully'}) return self.render_to_http_response(json_data) if form.errors: json_data = json.dumps(form.errors) return self.render_to_http_response(json_data, status=400)
def put(self, request, *args, **kwargs): data = request.body valid_json = is_json(data) if valid_json == False: data = json.dumps({'Msg': 'Please provide valid json format'}) return self.render_to_http_response(data, status_code=400) pdata = json.loads(data) id = pdata.get('id', None) if id: student = get_object_by_id(id=id) if student is None: data = json.dumps({'Msg': 'student record not found'}) return self.render_to_http_response(data, status_code=404) form = StudentForm(pdata, instance=student) print(form) if form.is_valid(): form.save(commit=True) data = json.dumps({'message': "Record updated successfully"}) return self.render_to_http_response(data) if form.errors: json_data = json.dumps(form.errors) return self.render_to_http_response(json_data, status_code=400) data = json.dumps({'id': 'id is mandatory to update'}) return self.render_to_http_response(data, status_code=400)
def get(self, request, *args, **kwargs): data = request.body valid_json = is_json(data) if valid_json == False: data = json.dumps({'Msg': 'Please provide valid json'}) return self.render_to_http_response(data, status_code=404) pdata = json.loads(data) print(pdata) id = pdata.get('id', None) print(id) if id: student = get_object_by_id(id=id) print(student) if student is None: data = json.dumps({'Msg': 'student record not found'}) return self.render_to_http_response(data, status_code=404) json_data = self.serialize([ student, ]) return self.render_to_http_response(json_data) qs = Student.objects.all() json_data = self.serialize(qs) return self.render_to_http_response(json_data)
def post(self, request, *args, **kwargs): data = request.body valid_json = is_json(data) if not valid_json: json_data = json.dumps({'msg': 'please send valid json data'}) return self.render_to_http_response(json_data, status=400) empdata = json.loads(data) form = EmployeeForm(empdata) if form.is_valid(): form.save() json_data = json.dumps({'msg': 'resource created successfully'}) return self.render_to_http_response(json_data) if form.errors: json_data = json.dumps(form.errors) return self.render_to_http_response(json_data, status=400)
def post(self, request, *args, **kwargs): data = request.body valid_json = is_json(data) if valid_json == False: data = json.dumps({'Msg': 'Please provide valid json format'}) return self.render_to_http_response(data, status_code=400) pdata = json.loads(data) form = StudentForm(pdata) if form.is_valid(): form.save(commit=True) json.dumps({'message': "Record inserted successfully"}) return self.render_to_http_response(data) if form.errors: json_data = json.dumps(form.errors) return self.render_to_http_response(json_data, status_code=400)
def delete(self, request, *args, **kwargs): data = request.body valid_json = is_json(data) if not valid_json: json_data = json.dumps({'msg': 'please send valid json data'}) return self.render_to_http_response(json_data, status=400) pdata = json.loads(data) id = pdata.get('id', None) if id is not None: emp = self.get_object_by_id(id) if emp is None: json_data = json.dumps({ 'msg': 'No resource with matched id, not possible to delete' }) return self.render_to_http_response(json_data, status=404) status, deleted_item = emp.delete() if status == 1: json_data = json.dumps( {'msg': 'resource deleted successfully'}) return self.render_to_http_response(json_data) json_data = json.dumps({'msg': 'unale to delete'}) return self.render_to_http_response(json_data)
def get(self, request, *args, **kwargs): data = request.body valid_json = is_json(data) if not valid_json: json_data = json.dumps({'msg': 'please send valid json data'}) return self.render_to_http_response(json_data, status=400) pdata = json.loads(data) id = pdata.get('id', None) if id is not None: emp = self.get_object_by_id(id) if emp is None: json_data = json.dumps({ 'msg': 'The requested resource not availale with matched id' }) return self.render_to_http_response(json_data, status=404) json_data = self.serialize([ emp, ]) return self.render_to_http_response(json_data) qs = Employee.objects.all() json_data = self.serialize(qs) return self.render_to_http_response(json_data)