def delete(self, request, *args, **kwargs): data = request.body if not is_json(data): return self.render_to_http_response(json.dumps( {'msg': 'plz send valid json data only'}), 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': 'To perform delete,id is mandatory,you should provide' }), status=400) obj = self.get_object_by_id(id) if obj is None: json_data = json.dumps({ 'msg': 'No matched record found, Not possible to perform delete operation' }) 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 ...plz try again'}) return self.render_to_http_response(json_data, status=500)
def put(self,request,*args,**kwargs): data=request.body if not is_json(data): return self.render_to_http_response(json.dumps({'msg':'plz send valid json data only'}),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':'To perform updation id is mandatory,you should provide'}),status=400) obj=self.get_object_by_id(id) if obj is None: json_data=json.dumps({'msg':'No matched record found, Not possible to perform updataion'}) return self.render_to_http_response(json_data,status=404) new_data=data old_data={ 'eno':obj.eno, 'ename':obj.ename, 'esal':obj.esal, 'eaddr':obj.eaddr, } # for k,v in new_data.items(): # old_data[k]=v 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 valid_json = is_json(data) if not valid_json: json_data = json.dumps( {"msg": "Please provide the valid Json data only"}) return self.get_http_response(json_data, status=400) pdata = json.loads(data) # print(pdata,"data is") new_json_data = pdata['id'] emp = get_object_id(new_json_data) if emp is None: json_data = json.dumps({ "msg": "No data is found with this id,so there is no updation" }) return self.get_http_response(json_data, status=400) orginal_data = { 'eno': emp.eno, 'ename': emp.ename, 'esal': emp.esal, 'eaddr': emp.eaddr } orginal_data.update(pdata) form = EmployeeForm(orginal_data, instance=emp) if form.is_valid(): form.save() json_data = json.dumps({"msg": "Resource updated Successfully"}) return self.get_http_response(json_data) if form.errors: json_data = json.dumps(form.errors) return self.get_http_response(json_data)
def put(self, request, id, *args, **kwargs): emp = self.get_object_by_id(id) if emp is None: json_data = json.dumps({ "msg": "No matched record found not possible to perform 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 a valid json only"}) return self.render_to_http_response(json_data, status=404) provided_data = json.loads(data) original_data = { 'eno': emp.eno, 'ename': emp.ename, 'esal': emp.esal, 'eaddr': emp.eaddr } original_data.update(provided_data) form = EmployeeForm(original_data, instance=emp) if form.is_valid(): form.save(commit=True) json_data = json.dumps({"msg": 'Resource Created Succesfully'}) 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 not valid_json: json_data = json.dumps( {'msg': 'please provide valid json data only'}) return self.render_to_http_response(json_data, status=400) provided_data = json.loads(data) id = provided_data.get('id', None) if id is None: json_data = json.dumps( {'msg': 'To perform updation, id is mandatory,plz provide'}) return self.render_to_http_response(json_data, status=400) std = self.get_object_by_id(id) if std is None: json_data = json.dumps( {'msg': 'No matched record found with the given id'}) return self.render_to_http_response(json_data, status=400) orginal_data = { 'name': std.name, 'rollno': std.rollno, 'marks': std.marks, 'gf': std.gf, 'bf': std.bf } orginal_data.update(provided_data) form = StudentForm(orginal_data, instance=std) if form.is_valid(): form.save() json_data = json.dumps({'msg': 'resources update successfully'}) return self.render_to_http_response(json_data, status=400) 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 not valid_json: json_data = json.dumps({"msg": "Please send valid json data only"}) return self.render_to_http_response(json_data, status=400) pdata = json.loads(data) id = pdata.get('id', None) if id is None: json_data = json.dumps({"msg": "To perform updation id is mandatory so please provide"}) return self.render_to_http_response(json_data, status=400) emp = self.get_object_by_id(id) if emp is None: json_data = json.dumps({"msg": "No resource with matched id,not possible tp perform updation"}) return self.render_to_http_response(json_data, status=400) provided_data = json.loads(data) original_data = { "eno": emp.eno, "ename": emp.ename, "esal": emp.esal, "eaddr": emp.eaddr, } original_data.update(provided_data) form = EmployeeForm(original_data, instance=emp) if form.is_valid(): form.save(commit=True) json_data = json.dumps({"msg": "Resource is updated sucessfully"}) return self.render_to_http_response(json_data) if form.errors: json_data = json.dumps(form.errors) # form.errors is dictonary only return self.render_to_http_response(json_data, status=400)
def delete(self, request, *args, **kwargs): data = request.body valid_json = is_json(data) if not valid_json: json_data = json.dumps({'msg': 'Provide some valid json data'}) return self.render_to_http_response(json_data) stu_data = json.loads(data) id = stu_data.get('id', None) if id is None: json_data = json.dumps({ 'msg': 'ID is mandatory for Deletion Operation, please provide ID' }) return self.render_to_http_response(json_data) stu = self.get_object_by_id(id) if stu is None: json_data = json.dumps({ 'msg': 'Given ID is Not Matched With exsiting record, Please Give some valid ID' }) return self.render_to_http_response(json_data) status, delete_items = stu.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 delete record.....please try again'}) return self.render_to_http_response(json_data)
def put(self, request, id, *args, **kwargs): # try: # emp = Employee.objects.get(id=id) # except Employee.DoesNotExist: # emp = None # return emp emp = self.get_object_by_id(id) if emp is None: json_data = json.dumps({"msg": "No Matched Resource Found, Not possible to perform 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 only"}) return self.render_to_http_response(json_data, status=400) provided_data = json.loads(data) original_data = { "eno": emp.eno, "ename": emp.ename, "esal": emp.esal, "eaddr": emp.eaddr, } original_data.update(provided_data) form = EmployeeForm(original_data, instance=emp) if form.is_valid(): form.save(commit=True) json_data = json.dumps({"msg": "Resource is updated sucessfully"}) return self.render_to_http_response(json_data) if form.errors: json_data = json.dumps(form.errors) # form.errors is dictonary only 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 not valid_json: return self.render_to_http_response(json.dumps({'msg':'please provide valid json'}),status=400) provided_data = json.loads(data) id = provided_data.get('id',None) if id is None: return self.render_to_http_response(json.dumps({'msg':' to perform updation id is medatory '}),status=200) std = self.get_object_by_id(id) if std is None: return self.render_to_http_response(json.dumps({'msg':'fields for matched found'}),status=400) original_data = { 'name':std.name, 'rollno':std.rollno, 'marks':std.marks } original_data.update(provided_data) form = StudentForm(original_data,instance=std) if form.is_valid(): form.save(commit=True) return self.render_to_http_response(json.dumps({'msg':'resource updated successfully'}),status=200) if form.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 valid_json = is_json(data) if not valid_json: json_data = json.dumps({'msg': 'please send valid json data only'}) 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 available with matched id' }) return self.render_to_http_response(json_data, status=404) status, deleted_item = emp.delete() if status == 1: json_data = json.dumps( {'msg': 'Resources Deleted Successfully'}) return self.render_to_http_response(json_data, status=404) json_data = json.dumps( {'msg': 'Unable to delete....plz try again'}) return self.render_to_http_response(json_data) json_data = json.dumps( {'msg': ' to perform delation ID is mandatory,please provide'}) 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 not valid_json: json_data = json.dumps({'msg': 'please send valid json data only'}) return self.render_to_http_response(json_data, status=400) pdata = json.loads(data) id = pdata.get('id', None) if id is None: json_data = json.dumps( {'msg': ' to perform updation ID is mandatory,please provide'}) return self.render_to_http_response(json_data, status=400) emp = self.get_object_by_id(id) if emp is None: json_data = json.dumps({ 'msg': 'No resources with matched id,not possible to perform updation' }) return self.render_to_http_response(json_data, status=404) provided_data = json.loads(data) orginal_data = { 'eno': emp.eno, 'ename': emp.ename, 'esal': emp.esal, 'eaddr': emp.eaddr, } orginal_data.update(provided_data) form = EmployeeForm(orginal_data, instance=emp) if form.is_valid(): form.save(commit=True) json_data = json.dumps({'msg': 'Resources 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 get(self, request, *args, **kwargs): data = request.body if is_json(data): json_data = json.dumps( {'msg': 'please provide the valid json data '}) return self.render_to_http_response(json_data, status=404) pdata = json.loads(data) id = pdata.get('id', None) print(id, '###################') if id is not None: emp = self.get_object_by_id(id) if emp is None: json_data = json.dumps( {'msg': 'No Matched resource is not found '}) 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)
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 only'}) return self.render_to_http_response(json_data, status=400) pdata = json.loads(data) id = pdata.get('id', None) if id is not None: stud = self.get_object_by_id(id) if stud is None: json_data = json.dumps({ 'msg': 'The requested resource is not available with matched id' }) return self.render_to_http_response(json_data, status=400) status, deleted_item = stud.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....Please try again.'}) return self.render_to_http_response(json_data) json_data = json.dumps({ 'msg': 'To perform deletion id is mandatory....Please provide id' }) 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 not valid_json: return self.render_to_http_response(json.dumps( {'msg': 'please send valid json only'}), status=400) provided_data = json.loads(data) id = provided_data.get('id', None) if id is None: return self.render_to_http_response(json.dumps( {'msg': 'To perform updation id is mandatory,plz provide id'}), status=400) std = self.get_object_by_id(id) original_data = { 'name': std.name, 'rollno': std.rollno, 'marks': std.marks, 'gf': std.gf, 'bf': std.bf } original_data.update(provided_data) form = StudentForm(original_data, instance=std) 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 self.render_to_http_response(json_data, status=400)
def put(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 only'}) return self.render_to_http_response(json_data,status=400) p_data = json.loads(data) id = p_data.get('id',None) # if id is available than return id if not return None if id is None: json_data = json.dumps({'msg':'to prtform data id is mendatory '}) return self.render_to_http_response(json_data,status=400) emp = self.get_oject_by_id(id) if emp is None: json_data = json.dumps({'msg':'id is not available'}) return self.render_to_http_response(json_data,status=400) provided_data = json.loads(data) original_data = { 'eno':emp.eno, 'ename': emp.ename, 'esal':emp.esal, 'eadd':emp.eadd, } original_data.update(provided_data) form = EmployeeForm(original_data,instance=emp) if form.is_valid(): form.save(commit=True) json_data = json.dumps({'msg': 'resource updated 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 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 only'}) return self.render_to_http_response(json_data,status=400) p_data = json.loads(data) id = p_data.get('id',None) # if id is available than return id if not return None #if id is provided by client than this part if id is not None: emp = self.get_oject_by_id(id) #if employee is not available if emp is None: json_data = json.dumps({'msg':'record not available'}) return self.render_to_http_response(json_data,status=400) 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': 'unable to delete please try again'}) return self.render_to_http_response(json_data) json_data = json.dumps({'msg':'to perfomr deletion provide id'}) 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 not valid_json: json_data=json.dumps({'msg':'please send valid json data'}) return HttpResponse(json_data,content_type='application/json') pdata=json.loads(data) id=pdata.get('id',None) if id is None: json_data=json.dumps({'msg':'to perform update id is required'}) return HttpResponse(json_data,content_type='application/json') emp=self.get_by_object_id(id) if emp is None: json_data=json.dumps({'msg':'the Matched resource not found'}) return HttpResponse(json_data,content_type='application/json') provided_data=json.loads(data) original_data={ 'eno':emp.eno, 'ename':emp.ename, 'esal':emp.esal, 'eaddr':emp.eaddr, } original_data.update(provided_data) form=EmployeeForm(original_data,instance=emp) if form.is_valid(): form.save(commit=True) json_data=json.dumps({'msg':'data saved in database'}) return HttpResponse(json_data,content_type='application/json') if form.errors: json_data=json.dumps(form.errors) return HttpResponse(json_data,content_type='application/json')
def put(self, request, *args, **kwargs): data = request.body json_data = is_json(data) if not json_data: json_data = json.dumps({'msg': 'Please enter valid json data'}) return self.render_to_response(json_data, status=404) pdata = json.loads(data) id = pdata.get('id', None) if id is None: json_data = json.dumps({'msg': 'Please provide id'}) return self.render_to_response(json_data, status=404) emp = self.get_object_id(id) if emp is None: json_data = json.dumps( {'msg': 'The requested id is not available'}) return self.render_to_response(json_data, status=404) provided_data = json.loads(data) original_data = { 'ename': emp.ename, 'eaddr': emp.eaddr, 'esal': emp.esal, 'eno': emp.eno } original_data.update(provided_data) form = EmployeeForm(original_data, instance=emp) if form.is_valid(): form.save(commit=True) json_data = json.dumps({'msg': 'Data added successfully'}) return self.render_to_response(json_data) if form.errors: json_data = json.dumps(form.errors) return self.render_to_response(json_data, status=404)
def post(self,request,*args,**kwargs): data=request.body if not is_json(data): return self.render_to_http_response(json.dumps({'msg':'plz send valid json data only'}),status=400) empdata=json.loads(request.body) 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 get(self, request, *args, **kwargs): ##################################################### # try: # emp=Employee.objects.get(id=id) # except Employee.DoesNotExist: # json_data=json.dumps({'msg':'The requested data is not available'}) # return self.render_to_response(json_data,status=404) # else: # emp_data={ # 'ename':emp.ename, # 'eaddr':emp.eaddr, # 'esal' :emp.esal, # 'eno' :emp.eno # } # json_data=json.dumps(emp_data) # return self.render_to_response(json_data) ####################################################### data = request.body json_data = is_json(data) if not json_data: json_data = json.dumps({'msg': 'Please enter valid json data'}) return self.render_to_response(json_data, status=404) pdata = json.loads(data) id = pdata.get('id', None) if id is None: empl = Employee.objects.all() emp_data = [] for emp in empl: tmp_data = { 'ename': emp.ename, 'eaddr': emp.eaddr, 'esal': emp.esal, 'eno': emp.eno } emp_data.append(tmp_data) json_data = json.dumps(emp_data) return self.render_to_response(json_data, status=200) emp = self.get_object_id(id) if emp is None: json_data = json.dumps( {'msg': 'The requested id is not available'}) return self.render_to_response(json_data, status=404) tmp_data = { 'ename': emp.ename, 'eaddr': emp.eaddr, 'esal': emp.esal, 'eno': emp.eno } json_data = json.dumps(tmp_data) return self.render_to_response(json_data, status=200)
def post(self, request, *args, **kwargs): data = request.body valid_json = is_json(data) if not valid_json: json_data = json.dumps({'msg': 'Provide some valid json data'}) return self.render_to_http_response(json_data) stu_data = json.loads(data) form = StudentForm(stu_data) if form.is_valid(): form.save(commit=True) json_data = json.dumps({'msg': 'Record 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)
def get(self,request,*args,**kwargs): data=request.body if not is_json(data): return self.render_to_http_response(json.dumps({'msg':'plz 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 post(self, request, *args, **kwargs): data = request.body json_data = is_json(data) if not json_data: json_data = json.dumps({'msg': 'Please enter valid json data'}) return self.render_to_response(json_data, status=404) empdata = json.loads(data) form = EmployeeForm(empdata) if form.is_valid(): form.save(commit=True) json_data = json.dumps({'msg': 'Data added successfully'}) return self.render_to_response(json_data) if form.errors: json_data = json.dumps(form.errors) return self.render_to_response(json_data, status=404)
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 HttpResponse(json_data,content_type='application/json') empdata=json.loads(data) form=EmployeeForm(empdata) if form.is_valid(): form.save(commit=True) json_data=json.dumps({'msg':'data saved in database'}) return HttpResponse(json_data,content_type='application/json') if form.errors: json_data=json.dumps(form.errors) return HttpResponse(json_data,content_type='application/json')
def post(self, request, *args, **kwargs): data = request.body valid_json = is_json(data) if not valid_json: return self.render_to_http_response(json.dumps( {'msg': 'please send valid json only'}), status=400) std_data = json.loads(data) form = StudentForm(std_data) if form.is_valid(): 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(data) if not valid_json: return self.render_to_http_response(json.dumps({'msg':'please provide valid json data only'}),status=400) std_data = json.loads(data) #to save in db form must be requied form = StudentForm(std_data) if form.is_valid(): form.save(commit=True) return self.render_to_http_response(json.dumps({'msg':'resource insert successfully'}),status=200) 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 not valid_json: json_data = json.dumps( {"msg": "Please send me the valid Json data only"}) return self.get_http_response(json_data, status=400) pdata = json.loads(data) form = EmployeeForm(pdata) if form.is_valid(): form.save() json_data = json.dumps({"msg": "Resource created successfully"}) return self.get_http_response(json_data) if form.errors: json_data = json.dumps(form.errors) return self.get_http_response(json_data)
def post(self,request,*args,**kwargs): data = request.body valid_json = is_json(data) if not valid_json: return self.returnResponse({'Message':"Please send valid json data"},status_code=400) empdata = json.loads(data) # to save form data of post request you must have forms in django so create a forms.py file and extend models in that. form = EmployeeForm(empdata) if form.is_valid(): form.save(commit=True) json_data = json.dumps({'msg':'Resource created successfully'}) return self.returnResponse(json_data,status_code=200) if form.errors: json_data = json.dumps(form.errors) return self.returnResponse(json_data, status_code=400)
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 only"}) 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 available"}) return self.render_to_http_response(json_data, status=400) 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 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 HttpResponse(json_data,content_type='application/json') pdata=json.loads(data) id=pdata.get('id',None) if id is not None: emp=self.get_by_object_id(id) if emp is None: json_data=json.dumps({'msg':'the Matched resource not found'}) return HttpResponse(json_data,content_type='application/json') json_data=self.serialize([emp,]) return HttpResponse(json_data,content_type='application/json') qs=Employee.objects.all() json_data=self.serialize(qs) return HttpResponse(json_data,content_type='application/json')