Ejemplo n.º 1
0
def getUsername(request):
    """
    @api {get} /specificApis/user/getUsername getUsername
    @apiVersion 1.0.0
    @apiDescription get username
    @apiName getUsername
    @apiGroup user
    @apiSuccessExample {json} Success-Response:
        HTTP/1.1 200 OK
        {
            "error": 0,
            "result": "get username success"
        }
    @apiErrorExample {json} Error-Response:
        HTTP/1.1 200 OK
        {
            "error": 1,
            "reason": "error reason here"
        }
    """
    if not function.check_Session(request) and not function.check_gradeAdminSession(request):
        return function.retJson(error=-1, reason='have not login')
    if function.check_Session(request):
        username = request.session.get('username')
        role = 'user'
    elif function.check_gradeAdminSession(request):
        username = request.session.get('username_grade')
        role = 'gradeAdmin'
    return function.retJson(error=0, result=username, role=role)
Ejemplo n.º 2
0
def gradeAdminLogout(request):
    """
    @api {get} /specificApis/gradeAdmin/gradeAdminLogout gradeAdminLogout
    @apiVersion 1.0.0
    @apiDescription 年级管理员退出登录
    @apiName gradeAdminLogout
    @apiGroup gradeAdmin
    @apiSuccessExample {json} Success-Response:
        HTTP/1.1 200 OK
        {
            "error": 0,
            "result": "logout"
        }
    @apiErrorExample {json} Error-Response:
        HTTP/1.1 200 OK
        {
            "error": 1,
            "reason": "error reason here"
        }
    """
    try:
        if not function.check_gradeAdminSession(request):
            return function.retJson(error=-1, reason='have not login')
        request.session.flush()
        return function.retJson(error=0, resule='logout')
    except Exception as e:
        return function.retJson(error=1, reason=str(e))
Ejemplo n.º 3
0
def GAclassDelete(request):
    """
    @api {post} /specificApis/gradeAdmin/GAclassDelete GAclassDelete
    @apiVersion 1.0.0
    @apiDescription 删除班级,班级有同学的时候,拒绝删除
    @apiName GAclassDelete
    @apiGroup gradeAdmin
    @apiParam {string} classNumber classNumber
    @apiSuccessExample {json} Success-Response:
        HTTP/1.1 200 OK
        {
            "error": 0,
            "result": "delete class success"
        }
    @apiErrorExample {json} Error-Response:
        HTTP/1.1 200 OK
        {
            "error": 1,
            "reason": "error reason here"
        }
    """
    if not function.check_gradeAdminSession(request):
        return function.retJson(error=-1, reason='have not login')
    if request.method == "POST":
        try:
            classNumber = request.POST.get('classNumber')
            Classes.objects.get(classNumber=classNumber).delete()
            return function.retJson(error=0, result="delete class success")
        except Exception as e:
            return function.retJson(error=2, reason=str(e))
    else:
        return function.retJson(error=1, reason='needmethod: get')
Ejemplo n.º 4
0
def studentGet(request):
    """
    @api {get} /specificApis/student/get studentGet
    @apiVersion 1.0.0
    @apiDescription studentGet
    @apiName studentGet
    @apiGroup student
    @apiSuccessExample {json} Success-Response:
        HTTP/1.1 200 OK
        {
            "error": 0,
            "result": value
        }
    @apiErrorExample {json} Error-Response:
        HTTP/1.1 200 OK
        {
            "error": 1,
            "reason": "error reason here"
        }
    """
    if not function.check_Session(
            request) and not function.check_gradeAdminSession(request):
        return function.retJson(error=-1, reason='have not login')
    if request.method == "GET":
        value = list(Student.objects.all().values())
        return function.retJson(error=0,
                                result=value,
                                mycls=function.MyEncoder)
    else:
        return function.retJson(error=1, reason='needmethod: get')
Ejemplo n.º 5
0
def classes(request):
    if not function.check_Session(
            request) and not function.check_gradeAdminSession(request):
        return HttpResponseRedirect("/login")
    if 'classNumber' in request.GET:
        classNumber = request.GET['classNumber']
        return render(request, 'classes.html', {'classNumber': classNumber})
    else:
        return index(request)
Ejemplo n.º 6
0
def student(request):
    if not function.check_Session(
            request) and not function.check_gradeAdminSession(request):
        return HttpResponseRedirect("/login")
    if 'studentId' in request.GET:
        studentId = request.GET['studentId']
        return render(request, 'student.html', {'studentId': studentId})
    else:
        return index(request)
Ejemplo n.º 7
0
def export(request):
    """
    @api {get} /specificApis/export export
    @apiVersion 1.0.0
    @apiDescription export student info
    @apiName export
    @apiGroup export
    @apiSuccessExample {json} Success-Response:
        HTTP/1.1 200 OK
        {
            "error": 0,
            "result": "get all class info success"
        }
    @apiErrorExample {json} Error-Response:
        HTTP/1.1 200 OK
        {
            "error": 1,
            "reason": "error reason here"
        }
    """
    if not function.check_gradeAdminSession(request):
        return function.retJson(error=-1, reason='have not login')
    if request.method == "GET":
        try:
            now = datetime.now().strftime('%Y_%m_%d')
            filename = 'student_info_' + now + '.xlsx'
            base_dir = os.path.dirname(
                os.path.dirname(os.path.abspath(__file__)))
            file_path = os.path.join(base_dir, 'file', 'download',
                                     filename)  # 下载文件的绝对路径

            # 写入
            students = list(Student.objects.all().values())
            for stuInfo in students:
                info = function.getStudentData(stuInfo["studentId"])[0]
                stuInfo.update(info)
            ret = function.write_excel_xlsx(file_path, students)
            if ret != 'success':
                return function.retJson(error=3, reason=ret)

            # 输出
            file = open(file_path, 'rb')
            response = FileResponse(file)
            response['Content-Type'] = 'application/octet-stream'
            response[
                'Content-Disposition'] = 'attachment;filename="{}"'.format(
                    filename).encode('utf-8')
            return response
        except Exception as e:
            return function.retJson(error=2, reason=str(e))
    else:
        return function.retJson(error=1, reason='needmethod: get')
Ejemplo n.º 8
0
def userAdd(request):
    """
    @api {post} /specificApis/user/add userAdd
    @apiVersion 1.0.0
    @apiDescription userAdd
    @apiName userAdd
    @apiGroup user
    @apiParam {string} username username unique
    @apiParam {string} password password
    @apiParam {string} classNumber classNumber unique
    @apiParam {string} grade grade
    @apiSuccessExample {json} Success-Response:
        HTTP/1.1 200 OK
        {
            "error": 0,
            "result": "create user success"
        }
    @apiErrorExample {json} Error-Response:
        HTTP/1.1 200 OK
        {
            "error": 1,
            "reason": "error reason here"
        }
    """
    if not function.check_gradeAdminSession(request):
        return function.retJson(error=-1, reason='have not login')
    if request.method == "POST":
        username = request.POST.get('username')
        password = request.POST.get('password')
        classNumber = request.POST.get('classNumber')
        grade = request.POST.get('grade')
        # TODO password decode
        password = function.hash(password)
        try:
            gradeAccount = Grade.objects.get(grade=grade)
            user_Account = User(username=username,
                                password=password,
                                classNumber=classNumber)
            user_Account.save()
            try:
                class_Account = Classes(classNumber=user_Account,
                                        grade=gradeAccount)
                class_Account.save()
                return function.retJson(error=0, result="create user success")
            except Exception as e:
                return function.retJson(error=1, reason=str(e))
        except Exception as e:
            return function.retJson(error=2, reason=str(e))
    else:
        return function.retJson(error=3, reason='needmethod: post')
Ejemplo n.º 9
0
def GAclassAdd(request):
    """
    @api {post} /specificApis/gradeAdmin/GAclassAdd GAclassAdd
    @apiVersion 1.0.0
    @apiDescription 添加班级,并将会创建一个默认班级负责人,此管理员用户名和初始密码与班号相同。
    @apiName GAclassAdd
    @apiGroup gradeAdmin
    @apiParam {string} classNumber classNumber
    @apiSuccessExample {json} Success-Response:
        HTTP/1.1 200 OK
        {
            "error": 0,
            "result": "create class and user success"
        }
    @apiErrorExample {json} Error-Response:
        HTTP/1.1 200 OK
        {
            "error": 1,
            "reason": "error reason here"
        }
    """
    if not function.check_gradeAdminSession(request):
        return function.retJson(error=-1, reason='have not login')
    if request.method == "POST":
        try:
            grade = GradeAdmin.objects.get(
                username=request.session.get('username_grade')).grade.grade
            classNumber = request.POST.get('classNumber')
            username = classNumber
            password = classNumber
            password = function.hash(password)

            gradeAccount = Grade.objects.get(grade=grade)
            user_Account = User(username=username,
                                password=password,
                                classNumber=classNumber)
            user_Account.save()
            try:
                class_Account = Classes(classNumber=user_Account,
                                        grade=gradeAccount)
                class_Account.save()
                return function.retJson(error=0,
                                        result="create class and user success")
            except Exception as e:
                return function.retJson(error=1, reason=str(e))
        except Exception as e:
            return function.retJson(error=2, reason=str(e))
    else:
        return function.retJson(error=3, reason='needmethod: post')
Ejemplo n.º 10
0
def getAllClass(request):
    """
    @api {get} /specificApis/show/getAllClass getAllClass
    @apiVersion 1.0.0
    @apiDescription get all class info
    @apiName getAllClass
    @apiGroup show
    @apiSuccessExample {json} Success-Response:
        HTTP/1.1 200 OK
        {
            "error": 0,
            "result": "get all class info success"
        }
    @apiErrorExample {json} Error-Response:
        HTTP/1.1 200 OK
        {
            "error": 1,
            "reason": "error reason here"
        }
    """
    if not function.check_gradeAdminSession(request):
        return function.retJson(error=-1, reason='have not login')
    if request.method == "GET":
        try:
            classes = Classes.objects.filter().values()
            people = 0
            requiredPeople = 0
            classInfo = []
            for cl in classes:
                classNumber = cl['classNumber_id']
                info = function.getClassData(classNumber)[0]
                people += info['number']
                requiredPeople += info['requiredPeople']
                classInfo.append(info)
            result = {'people': people, 'requiredPeople': requiredPeople}
            return function.retJson(error=0,
                                    result=result,
                                    classInfo=classInfo,
                                    mycls=function.MyEncoder)
        except Exception as e:
            return function.retJson(error=2, reason=str(e))
    else:
        return function.retJson(error=1, reason='needmethod: get')
Ejemplo n.º 11
0
def GAchangePass(request):
    """
    @api {post} /specificApis/gradeAdmin/GAchangePass GAchangePass
    @apiVersion 1.0.0
    @apiDescription 修改密码
    @apiName GAchangePass
    @apiGroup gradeAdmin
    @apiParam {string} password_new password_new
    @apiParam {string} password_old password_old
    @apiSuccessExample {json} Success-Response:
        HTTP/1.1 200 OK
        {
            "error": 0,
            "result": "change grade_admin's password success"
        }
    @apiErrorExample {json} Error-Response:
        HTTP/1.1 200 OK
        {
            "error": 1,
            "reason": "error reason here"
        }
    """
    if not function.check_gradeAdminSession(request):
        return function.retJson(error=-1, reason='have not login')
    if request.method == "POST":
        try:
            username = request.session.get('username_grade')
            password_new = request.POST.get('password_new')
            password_new = function.hash(password_new)
            password_old = request.POST.get('password_old')
            password_old = function.hash(password_old)

            if not function.check_gradeAdminPass(username, password_old):
                return function.retJson(error=3, reason='wrong password')
            else:
                admin_Account = GradeAdmin.objects.filter(username=username)
                admin_Account.update(password=password_new)
                return function.retJson(
                    error=0, result="change grade_admin's password success")
        except Exception as e:
            return function.retJson(error=2, reason=str(e))
    else:
        return function.retJson(error=1, reason='needmethod: post')
Ejemplo n.º 12
0
def GAgetClasses(request):
    """
    @api {get} /specificApis/gradeAdmin/GAgetClasses GAgetClasses
    @apiVersion 1.0.0
    @apiDescription 获取当前年级管理员管理的年级下的所有班级
    @apiName GAgetClasses
    @apiGroup gradeAdmin
    @apiSuccessExample {json} Success-Response:
        HTTP/1.1 200 OK
        {
            "error": 0,
            "result": ""
        }
    @apiErrorExample {json} Error-Response:
        HTTP/1.1 200 OK
        {
            "error": 1,
            "reason": "error reason here"
        }
    """
    if not function.check_gradeAdminSession(request):
        return function.retJson(error=-1, reason='have not login')
    if request.method == "GET":
        try:
            username = request.session.get('username_grade')
            grade = GradeAdmin.objects.get(username=username).grade.grade
            classes = list(Classes.objects.filter(grade=grade).values())
            users = []
            for item in classes:
                info = item['classNumber_id']
                userInfo = list(User.objects.filter(classNumber=info).values())
                users.append({'user': userInfo[0]['username'], 'class': info})
            return function.retJson(error=0,
                                    classes=classes,
                                    users=users,
                                    grade=grade,
                                    mycls=function.MyEncoder)
        except Exception as e:
            return function.retJson(error=2, reason=str(e))
    else:
        return function.retJson(error=1, reason='needmethod: get')
Ejemplo n.º 13
0
def GAcheckPass(request):
    """
    @api {post} /specificApis/gradeAdmin/GAcheckPass GAcheckPass
    @apiVersion 1.0.0
    @apiDescription 修改密码时检查原密码正确性
    @apiName GAcheckPass
    @apiGroup gradeAdmin
    @apiParam {string} password password
    @apiSuccessExample {json} Success-Response:
        HTTP/1.1 200 OK
        {
            "error": 0,
            "result": "check password success"
        }
    @apiErrorExample {json} Error-Response:
        HTTP/1.1 200 OK
        {
            "error": 1,
            "reason": "error reason here"
        }
    """
    if not function.check_gradeAdminSession(request):
        return function.retJson(error=-1, reason='have not login')
    if request.method == "POST":
        try:
            username = request.session.get('username_grade')
            password = request.POST.get('password')
            password = function.hash(password)
            if function.check_gradeAdminPass(username, password):
                return function.retJson(error=0,
                                        result='check password success')
            else:
                return function.retJson(error=3, reason='wrong password')
        except Exception as e:
            return function.retJson(error=2, reason=str(e))
    else:
        return function.retJson(error=1, reason='please use post')
Ejemplo n.º 14
0
def getClassData(request):
    """
    @api {post} /specificApis/show/getClassData getClassData
    @apiVersion 1.0.0
    @apiDescription getClassData
    @apiName getClassData
    @apiGroup show
    @apiParam {string} classNumber classNumber
    @apiSuccessExample {json} Success-Response:
        HTTP/1.1 200 OK
        {
            "error": 0,
            "result": "get all class info success"
        }
    @apiErrorExample {json} Error-Response:
        HTTP/1.1 200 OK
        {
            "error": 1,
            "reason": "error reason here"
        }
    """
    if not function.check_Session(
            request) and not function.check_gradeAdminSession(request):
        return function.retJson(error=-1, reason='have not login')
    if request.method == "POST":
        try:
            classNumber = request.POST.get('classNumber')
            classs, students = function.getClassData(classNumber)

            return function.retJson(error=0,
                                    classs=classs,
                                    data=list(students),
                                    mycls=function.MyEncoder)
        except Exception as e:
            return function.retJson(error=2, reason=str(e))
    else:
        return function.retJson(error=1, reason='needmethod: get')
Ejemplo n.º 15
0
def gradeAdminIndex(request):
    if not function.check_gradeAdminSession(request):
        return HttpResponseRedirect("/adminLogin")
    return render(request, 'gradeAdminIndex.html')
Ejemplo n.º 16
0
def index(request):
    if not function.check_Session(
            request) and not function.check_gradeAdminSession(request):
        return HttpResponseRedirect("/login")
    return render(request, 'index.html')
Ejemplo n.º 17
0
def adminLogin(request):
    if function.check_adminSession(request):
        return HttpResponseRedirect("/adminIndex")
    if function.check_gradeAdminSession(request):
        return HttpResponseRedirect("/gradeAdminIndex")
    return render(request, 'login.html', {"type": "admin"})
Ejemplo n.º 18
0
def uploadStudentInfo(request):
    """
    @api {get} /specificApis/uploadStudentInfo uploadStudentInfo
    @apiVersion 1.0.0
    @apiDescription upload student info & write student infos
    @apiName uploadStudentInfo
    @apiGroup export
    @apiParam {file} file file
    @apiSuccessExample {json} Success-Response:
        HTTP/1.1 200 OK
        {
            "error": 0,
            "result": "get all class info success"
        }
    @apiErrorExample {json} Error-Response:
        HTTP/1.1 200 OK
        {
            "error": 1,
            "reason": "error reason here"
        }
    """
    if not function.check_gradeAdminSession(request):
        return function.retJson(error=-1, reason='have not login')
    if request.method == "POST":
        try:
            studentInfoFile = request.FILES.get("studentInfo", None)
            if not studentInfoFile:
                return function.retJson(error=-2,
                                        reason="no files for upload!")
            if not str(studentInfoFile.name).split('.')[-1] == 'xlsx':
                return function.retJson(error=-3,
                                        reason="need file with .xlsx")

            # 保存文件
            now = datetime.now().strftime('%Y_%m_%d')
            filename = 'student_info_' + now + '_' + studentInfoFile.name
            base_dir = os.path.dirname(
                os.path.dirname(os.path.abspath(__file__)))
            file_path = os.path.join(base_dir, 'file', 'upload',
                                     filename)  # 下载文件的绝对路径

            with open(file_path, 'wb') as f:
                for chunk in studentInfoFile.chunks():  # 分块写入文件
                    f.write(chunk)

            # 读取文件
            studentInfo = function.read_excel_xlsx(file_path)

            # 检查studentId 是否重复
            studentIdList1 = [x['studentId'] for x in studentInfo]
            studentIdList2 = set(studentIdList1)
            if len(studentIdList1) != len(studentIdList2):
                return function.retJson(error=-4, reason='stdudent id repeat')

            # 检查class 是否已存在
            classTmp = list(Classes.objects.filter().values())
            classList1 = list(set([x['class'] for x in studentInfo]))
            classList2 = [x['classNumber_id'] for x in classTmp]
            if set(classList1) <= set(classList2):
                return function.retJson(error=-5,
                                        reason='need create class first')

            # 原子操作
            with transaction.atomic():
                for std in studentInfo:
                    name = std['name']
                    state = std['state']
                    studentId = std['studentId']
                    initPoints = std['initPoints']
                    classNumber = std['class']
                    class_Account = Classes.objects.get(
                        classNumber=classNumber)
                    student_Account__ = Student(classNumber=class_Account,
                                                sex=0,
                                                studentId=studentId,
                                                name=name,
                                                state=state,
                                                initPoints=initPoints)
                    student_Account__.save()

            return function.retJson(error=0, result="upload file success")
        except Exception as e:
            return function.retJson(error=2, reason=str(e))
    else:
        return function.retJson(error=1, reason='need method: post')
Ejemplo n.º 19
0
def login(request):
    if function.check_Session(request) or function.check_gradeAdminSession(
            request):
        return HttpResponseRedirect("/index")
    return render(request, 'login.html', {"type": "user"})