def put(self, request, *args, **kwargs): data = request.body if not is_json(data): return self.render_to_http_response(json.dumps( {'msg': 'pleas send me json data'}), status=400) pdata = json.loads(request.body) id = pdata.get('id', None) if id is None: return self.render_to_http_response(json.dumps( {'msg': "Id is required"}), status=400) obj = self.get_object_by_id(id) if obj is None: json_data = json.dumps({'msg': 'Object not exist'}) return self.render_to_http_response(json_data, status=404) new_data = pdata old_data = { 'eno': obj.eno, 'ename': obj.ename, 'esal': obj.esal, 'eaddr': obj.eaddr, } old_data.update(new_data) form = EmployeeForm(old_data, instance=obj) if form.is_valid(): form.save(commit=True) json_data = json.dumps({'msg': 'updated successfully'}) return self.render_to_http_response(json_data, status=201) 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 if not is_json(data): return self.render_to_http_response(json.dumps({'msg':'please send valid json'}),status=400) pdata=json.loads(request.body) id=pdata.get('id',None) if id is None: return self.render_to_http_response(json.dumps({'msg':'please send me id '}),status=400) emp=self.get_object_by_id(id) if emp is None: return self.render_to_http_response(json.dumps({'msg':'Object Does not DoesNotExistq1'}),status=400) original_data={ 'eno':emp.eno, 'ename':emp.ename, 'esal':emp.esal, 'passw':emp.passw, 'rpass':emp.rpass } original_data.update(pdata) form=EmployeeForm(original_data,instance=emp) if form.is_valid(): form.save(commit=True) return self.render_to_http_response(json.dumps({'msg':"Record updated successfully"})) 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): json_valid = is_json(request.body) if not json_valid: return render_to_http_response(json.dumps( {'msg': 'Pls send me valid data'}), status=400) pdata = json.loads(request.body) id = pdata.get('id', None) print(id) if id is None: return self.render_to_http_response(json.dumps( {'msg': 'Pls send me valid id'}), status=400) obj = Student.objects.get(id=id) if obj is None: return render_to_http_response(json.dumps( {'msg': 'Obj does not exist'}), status=400) old_obj = { "name": obj.name, "rollno": obj.rollno, "marks": obj.marks, "gf": obj.gf, "bf": obj.bf, } old_obj.update(pdata) form = StudentForm(old_obj, instance=obj) if form.is_valid(): form.save(commit=True) return self.render_to_http_response( json.dumps({'msg': 'Resource Updated successfully'})) if form.errors: json_data = json.dumps(form.errors) return selr.render_to_http_response(json_data, status=400)
def post(self,request,*args,**kwargs): data=request.body if not is_json(data): return self.render_to_http_response(json.dumps({'msg':'please send valid json'}),status=400) pdata=json.loads(request.body) form=EmployeeForm(pdata) if form.is_valid(): form.save(commit=True) return self.render_to_http_response(json.dumps({'msg':'employee save successfully'}),status=200) if form.error: json_data=json.dumps(form.errors) return self.render_to_http_response(json_data,status=400)
def get(self,request,*args,**kwargs): data=request.body if not is_json(data): return self.render_to_http_response(json.dumps({'msg':'Please provide json_data'}),status=400) pdata=json.loads(request.body) id=pdata.get('id',None) if id is not None: emp=self.get_object_by_id(id) if emp is None: return self.render_to_http_response(json.dumps({'msg':"Object does not exits"})) 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)
def post(self, request, *args, **kwargs): data = request.body if not is_json(data): return self.render_to_http_response(json.dumps( {'msg': 'pls send me json_data only'}), status=400) empdata = json.loads(request.body) print(empdata) form = EmployeeForm(empdata) if form.is_valid(): obj = form.save(commit=True) return self.render_to_http_response( json.dumps({'msg': 'resource created successfully'})) 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(request.body) if not valid_json: return render_to_http_response(json.dumps( {'msg': 'Pls send me valid data'}), status=400) pdata = json.loads(request.body) stuForm = StudentForm(pdata) if stuForm.is_valid(): stuForm.save(commit=True) return self.render_to_http_response(json.dumps( {'msg': 'Resource created successfully'}), status=201) if stuForm.errors: json_data = json.dumps(form.errors) return self.render_to_http_response(json_data, status=400)
def delete(self,request,*args,**kwargs): data=request.body if not is_json(data): return self.render_to_http_response(json.dumps({'msg':'pls send me valid data'}),status=400) data=json.loads(request.body) id=data.get('id',None) if id is None: return self.render_to_http_response(json.dumps({'msg':'pls send me id'}),status=400) obj=self.get_object_by_id(id) if obj is None: json_data=json.dumps({'msg':'object does not exits'}) return self.render_to_http_response(json_data,status=400) status,deleted_item=obj.delete() if status==1: json_data=json.dumps({'msg':'Record Deleted successfully'}) return self.render_to_http_response(json_data) json_data=json.dumps({'msg':'unable to belete the record'}) return self.render_to_http_response(json_data,status=500)
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 me json data'}) return self.render_to_http_response(json_data, status=400) pdata = json.loads(request.body) id = pdata.get('id', None) if id is not None: std = self.get_object_by_id(id) if std is None: json_data = json.dumps({'msg': 'object does not exist'}) return self.render_to_http_response(json_data, status=400) json_data = self.serialize([ std, ]) return self.render_to_http_response(json_data, status=200) qs = Student.objects.all() print("Query Set---: ", qs) json_data = self.serialize(qs) return self.render_to_http_response(json_data, status=200)
def get(self, request, *args, **kwargs): data = request.body if not is_json(data): return self.render_to_http_response(json.dumps( {'msg': "pls send valid json data only"}), status=400) data = json.loads(request.body) id = data.get('id', None) if id is not None: obj = self.get_object_by_id(id) if obj is None: return self.render_to_http_response(json.dumps( {"msg": "No Matched Record found with specified id"}), status=404) json_data = self.serialize([ obj, ]) 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)
def delete(self, request, *args, **kwargs): data = request.body if not is_json(data): return self.render_to_http_response(json.dumps( {'msg': 'pls send me dalid data'}), status=400) data = json.loads(request.body) id = data.get('id', None) if id is None: return self.render_to_http_response(json.dumps( {'msg': 'pls send me id'}), status=400) obj = self.get_object_by_id(id) if obj is None: json_data = json.dumps({"msg": 'No matching id found'}) return self.render_to_http_response(json_data, status=404) status, deleted_item = obj.delete() if status == 1: json_data = json.dumps({'msg': 'Resource Deleted successfully'}) return self.render_to_http_response(json_data, status=201) json_data = json.dumps({'msg': 'unable to delete '}) return self.render_to_http_response(json_data, status=500)
def delete(self, request, *args, **kwargs): valid_json = is_json(request.body) if not valid_json: return render_to_http_response(json.dumps( {'msg': 'Pls send me valid data'}), status=400) data = json.loads(request.body) id = data.get('id', None) if id is None: return self.render_to_http_response(json.dumps( {'msg': 'Require id'}), status=400) obj = Student.objects.get(id=id) if obj is None: return self.render_to_http_response(json.dumps( {'msg': 'Obj Doesnot Exist'}), status=400) status, deleted_item = obj.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': 'unable to delete'}) return self.render_to_http_response(json_data, status=500)