コード例 #1
0
 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)
コード例 #2
0
 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)
コード例 #3
0
    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)
コード例 #4
0
    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)
コード例 #5
0
def add_student_view(request):
    form = StudentForm()
    if request.method == 'POST':
        form = StudentForm(request.POST)
        if form.is_valid():
            form.save(commit=True)
        return index_view(request)
    return render(request, 'testapp/addstudent.html', {'form': form})
コード例 #6
0
def createview(request):
    form = StudentForm()
    if request.method == 'POST':
        form = StudentForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('/')
    return render(request, 'testapp/create.html', {'form': form})
コード例 #7
0
def update_view(request, id):
    student = Student.objects.get(id=id)
    if request.method == 'POST':
        form = StudentForm(request.POST, instance=student)
        if form.is_valid():
            form.save(commit=True)
            return redirect('/testapp')
    return render(request, 'testapp/update.html', {'student': student})
コード例 #8
0
def updateview(request, id):
    s = Student.objects.get(id=id)
    if request.method == 'POST':
        form = StudentForm(request.POST, instance=s)
        if form.is_valid():
            form.save()
            return redirect('/')
    return render(request, 'testapp/update.html', {'s': s})
コード例 #9
0
def home_view(request):
    form = StudentForm()
    if request.method == 'POST':
        form = StudentForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            return HttpResponse('done')
    return render(request, 'testapp/home.html', {'form': form})
コード例 #10
0
    def put(self, request, *args, **kwargs):
        data = request.body
        valid_json_data = is_data_json(data)
        '''checking whether the data is Json or not'''
        if not valid_json_data:
            json_data = json.dumps({'msg': 'Please send the valid json data'})
            return self.render_to_http_response(json_data, status=400)
        '''This is the data coming from python application inorder to update'''
        provided_data = json.loads(data)
        id = provided_data.get('id', None)
        '''if id is None'''
        if id is None:
            json_data = json.dumps({
                'msg':
                'To perform Updation id is mandatory... Please provide the id'
            })
            return self.render_to_http_response(json_data, status=400)

        stud = self.get_object_data_by_id(id)
        '''This is used to check stud is None'''
        if stud is None:
            json_data = json.dumps(
                {'msg': 'The required resource is not available'})
            return self.render_to_http_response(json_data, status=404)
        '''This is the data which is been stored within the database'''
        original_data = {
            'sname': stud.sname,
            'semail': stud.semail,
            'sphone_no': stud.sphone_no,
            'saddress': stud.saddress
        }
        print('Data before Updation')
        print(original_data)

        print('Data After Updation')
        '''Performing updation on the existing original data'''
        original_data.update(provided_data)
        print(original_data)

        form = StudentForm(original_data, instance=stud)
        '''This is used for checking form valid or not'''
        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)
コード例 #11
0
def student_input_view(request):
    markssubmitted = False
    name = ''
    if request.method == 'POST':
        form = StudentForm(request.POST)
        if form.is_valid():
            name = form.cleaned_data['name']
            form.save()
            markssubmitted = True
    form = StudentForm()
    return render(request, 'testapp/input.html', {
        'form': form,
        'markssubmitted': markssubmitted,
        'name': name
    })
コード例 #12
0
 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)
コード例 #13
0
 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)
コード例 #14
0
    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)
コード例 #15
0
ファイル: views.py プロジェクト: Pavitrabiradar25/api
    def put(self, request, *args, **kwargs):
        data = request.body
        valid_json_data = is_data_json(data)
        if not valid_json_data:
            json_data = json.dumps({'msg': 'Please send the valid json data'})
            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': 'Please Provide the id'})
            return self.render_to_http_response(json_data, status=400)

        student = self.get_object_data_by_id(id)

        if student is None:
            json_data = json.dumps({'msg': 'The Record is not available'})
            return self.render_to_http_response(json_data, status=404)

        original_data = {
            'name': student.name,
            'email': student.email,
            'phone_num': student.phone_num,
            'address': student.address
        }

        print('Data before Updation')
        print(original_data)

        print('Data After updation')

        original_data.update(provided_data)
        print(original_data)

        form = StudentForm(original_data, instance=student)
        if form.is_valid():
            form.save(commit=True)
            json_data = json.dumps({'msg': 'Record 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)
コード例 #16
0
    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)
コード例 #17
0
    def post(self, request, *args, **kwargs):
        data = request.body
        valid_json_data = is_data_json(data)
        '''checking whether the data is JSON or not'''
        if not valid_json_data:
            json_data = json.dumps({'msg': 'Please send the valid json data'})
            return self.render_to_http_response(json_data, status=400)
        '''variable used to store json data to dictionary'''
        stud_data = json.loads(data)
        '''variable used to call form'''
        form = StudentForm(stud_data)
        '''checking whether form is valid or not'''
        if form.is_valid():
            form.save(commit=True)
            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)
コード例 #18
0
 def put(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)
     provided_data = json.loads(data)
     id = provided_data.get('id', None)
     if id is None:
         json_data = json.dumps({
             'msg':
             'ID is mandatory for Update 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)
     original_data = {
         'name': stu.name,
         'rollno': stu.rollno,
         'mark': stu.mark,
         'division': stu.division,
         'addrs': stu.addrs,
     }
     original_data.update(provided_data)
     form = StudentForm(original_data, instance=stu)
     if form.is_valid():
         form.save(commit=True)
         json_data = json.dumps({'msg': 'Record 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)
コード例 #19
0
 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 id'
         })
         return render_to_http_response(json_data, status=400)
     stud = self.get_object_by_id(id)
     if stud is None:
         json_data = json.dumps(
             {'msg': 'No resource with matched id can not update'})
         return self.render_to_http_response(json_data, status=400)
     provided_data = json.loads(data)
     original_data = {
         'name': stud.name,
         'email': stud.email,
         'dob': stud.dob,
         'profile_pic': stud.profile_pic,
         'fees': stud.fees
     }
     original_data.update(provided_data)
     form = StudentForm(original_data, instance=stud)
     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)