Ejemplo n.º 1
0
def upload(request):
    '''
    Upload a csv and store the students' imformation in the database.
    '''
    if request.method == "POST":  # form submitted
        try:
            csv = request.FILES['csv']
            if csv.size > 4096:
                return render(request, 'error.html', {'str':'文件尺寸过大'})
            else:
                for line in csv:
                    line = line.strip('\r\n\xef\xbb\xbf')  # delete the useless characters
                    words = line.split(',')
                    if len(words) == 0:
                        continue
                        
                    paras = dict()
                    paras['num'] = int(words[0])
                    paras['tot'] = 0  # total score
                    for i in xrange(1,6):
                        paras[subjects[i]] = int(words[i+1])  # scores of the subjects
                        paras['tot'] += paras[subjects[i]]
                    paras['name']=words[1]
                    
                    stu = Student(**paras)
                    try:
                        stu.clean_fields()  # check the scores of the students 
                    except:
                        return render(request, 'error.html', {'str':'学生信息错误'})
                    stu.save()
                return render(request, 'notify.html', {"str":"导入csv文件成功"})  # upload succeeded
        except:
            return render(request, 'error.html', {'str':'您没有上传文件或文件格式错误,具体文件格式详见帮助'})
    else:
        return render(request, 'upload.html')
Ejemplo n.º 2
0
def modify1(request):
    '''
    Receive the form containing a student's information
    and update the student's infromation in the database.
    '''
    try:
        cmd = request.POST.get('cmd')
        paras = dict()
        paras['num'] = int(request.POST.get('num'))
        if cmd == 'edit':  # edit the student's infromation
            paras['name'] = request.POST.get('name')
            paras['tot'] = 0

            for subject in subjects[1:]:
                paras[subject] = int(request.POST.get(subject))
                paras['tot'] += paras[subject]

            stu = Student(**paras)
            stu.clean_fields(
            )  # validate the fields of the students, if not, an error will be raised
            stu.save()

            return render(
                request, 'query.html', {
                    "table": [[
                        Student.objects.get(num=paras['num']),
                        Student.objects.filter(tot__gt=paras['tot']).count() +
                        1
                    ]]
                })
        else:  # delete the student from the database
            Student.objects.filter(num=paras['num']).delete()
            return render(request, 'notify.html', {"str": "学生信息成功删除"})
    except:
        return render(request, 'error.html', {"str": "学生信息错误,请正确填写"})
Ejemplo n.º 3
0
def modify1(request):
    '''
    Receive the form containing a student's information
    and update the student's infromation in the database.
    '''
    try:
        cmd = request.POST.get('cmd')
        paras = dict()
        paras['num'] = int(request.POST.get('num'))
        if cmd == 'edit':  # edit the student's infromation
            paras['name'] = request.POST.get('name')
            paras['tot'] = 0
            
            for subject in subjects[1:]:
                paras[subject] = int(request.POST.get(subject))
                paras['tot'] += paras[subject]
                
            stu=Student(**paras)
            stu.clean_fields()  # validate the fields of the students, if not, an error will be raised
            stu.save()

            return render(request, 'query.html', {"table":[[Student.objects.get(num=paras['num']), Student.objects.filter(tot__gt=paras['tot']).count()+1]]})
        else:  # delete the student from the database
            Student.objects.filter(num=paras['num']).delete()
            return render(request, 'notify.html', {"str":"学生信息成功删除"})
    except:
        return render(request, 'error.html', {"str":"学生信息错误,请正确填写"});
Ejemplo n.º 4
0
def upload(request):
    '''
    Upload a csv and store the students' imformation in the database.
    '''
    if request.method == "POST":  # form submitted
        try:
            csv = request.FILES['csv']
            if csv.size > 4096:
                return render(request, 'error.html', {'str': '文件尺寸过大'})
            else:
                for line in csv:
                    line = line.strip(
                        '\r\n\xef\xbb\xbf')  # delete the useless characters
                    words = line.split(',')
                    if len(words) == 0:
                        continue

                    paras = dict()
                    paras['num'] = int(words[0])
                    paras['tot'] = 0  # total score
                    for i in xrange(1, 6):
                        paras[subjects[i]] = int(
                            words[i + 1])  # scores of the subjects
                        paras['tot'] += paras[subjects[i]]
                    paras['name'] = words[1]

                    stu = Student(**paras)
                    try:
                        stu.clean_fields()  # check the scores of the students
                    except:
                        return render(request, 'error.html', {'str': '学生信息错误'})
                    stu.save()
                return render(request, 'notify.html',
                              {"str": "导入csv文件成功"})  # upload succeeded
        except:
            return render(request, 'error.html',
                          {'str': '您没有上传文件或文件格式错误,具体文件格式详见帮助'})
    else:
        return render(request, 'upload.html')