Exemplo n.º 1
0
 def put(self,request,id,*args, **kwargs):
     emp = self.get_object_by_id(id)
     if emp is None:
         json_data = json.dumps({'msg':'Matched Resource Not Found'})
         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 appropriate data'})
         return self.render_to_http_response(json_data, status=400)
     empdata = json.loads(data)
     original_data ={
         'name':emp.name,
         'roll':emp.roll,
         'marks':emp.marks,
         'addr':emp.addr,
         }
     original_data.update(empdata)
     form = EmployeeForm(original_data, instance=emp)
     if form.is_valid():
         form.save(commit=True)
         json_data = json.dumps({'msg':'resource added 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)
Exemplo n.º 2
0
 def post(self, request, *args, **kwargs):
     data = requst.body
     valid_json = is_json(data)
     if not valid_json:
         json_data = json.dumps({'msg':'please send appropriate data'})
         return self.render_to_http_response(json_data, status=400)
     empdata = json.loads(data)
     form = EmployeeForm(empdata)
     if form.is_valid():
         form.save(commit=True)
         json_data = json.dumps({'msg':'resource added 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)
Exemplo n.º 3
0
 def get(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'})
         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=id)
         if emp is None:
             json_data = json.dumps({'msg':'Matched Resource 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)