def get(request): # 记录次数 try: count = RequestRecord.objects.get(name='score') except RequestRecord.DoesNotExist: count = RequestRecord(name='score', count=0) count.count += 1 count.save() # 查询 req = request.query_params.dict() result = Score.objects.all() if len(req) == 2: for key, value in req.items(): if key == 'semester': result = result.filter(semester=value) elif key == 'student_id': # 检查学生是否存在 try: Student.objects.get(id=req['student_id']) except Student.DoesNotExist: raise UnAuthorizedError() result = result.filter(student_id=value) else: raise ArgumentError() else: raise ArgumentError() score_serializer = ScoreSerializer(result, many=True) return Response(score_serializer.data)
def put(request): req = request.data if len(req) == 4: try: bid = req['bid'] text = req['text'] score = req['score'] student_id = req['student_id'] except KeyError: raise ArgumentError() if not 1 <= score <= 5: raise ArgumentError(detail="评分只能为1-5分") try: student = Student.objects.get(id=student_id) except Student.DoesNotExist: raise UnAuthorizedError() try: course = Course.objects.get(bid=bid) except Course.DoesNotExist: raise NotFoundError(detail="没有这门课程") try: evaluation = CourseEvaluation.objects.get(student=student, course=course) evaluation.evaluation = text evaluation.score = score except CourseEvaluation.DoesNotExist: evaluation = CourseEvaluation(student=student, course=course, score=score, evaluation=text) evaluation.save() return HttpResponse(status=201) raise ArgumentError()
def get(request): """ 功能:前端获取校历和该学生未完成的ddl 调用方法:http://127.0.0.1:8000/ddl/Calendar/?student_id=17373349&school_year=2019-2020 返回参数:具体学期,日期,节假日,ddl事件 具体json格式见接口文档,两个列表,一个ddl,一个节假日 :param request: :return: """ try: # 保存前端请求数据 record = RequestRecord.objects.get(name="SchoolCalendar") record.count = record.count + 1 record.save() except RequestRecord.DoesNotExist: RequestRecord(name="SchoolCalendar", count=1).save() req = request.query_params.dict() if (len(req) != 2) or ("student_id" not in req) or ("school_year" not in req): raise ArgumentError() try: Student.objects.get(id=req['student_id']) ddl_req = DDL.objects.filter(student_id=req['student_id']) except Student.DoesNotExist: raise UnAuthorizedError() # 该学生未完成的ddl ddl = ddl_req.filter((Q(state="尚未提交") | Q(state="草稿 - 进行中"))).values( "course", "homework", "ddl") # 第一学期开始日期+寒假开始日期+第二学期开始日期+第三学期开始日期 try: semester = SchoolYear.objects.get(school_year=req["school_year"]) except SchoolYear.DoesNotExist: raise DatabaseNotExitError # 节假日信息 holiday = SchoolCalendar.objects.filter( Q(semester__contains=req["school_year"]) & ~Q(holiday__contains=req["school_year"])) content = { "school_year": req["school_year"], "first_semester": semester.first_semester, "winter_semester": semester.winter_semester, "second_semester": semester.second_semester, "third_semester": semester.third_semester, "end_semester": semester.end_semester } content.update({ "holiday": holiday.values("year", "semester", "date", "holiday"), "ddl": ddl }) return Response(content)
def default(request): req = request.query_params.dict() teacher_courses = [] if 'student_id' in req.keys(): output = {} student_id = req['student_id'] try: student = Student.objects.get(id=student_id) except Student.DoesNotExist: raise UnAuthorizedError() courses = StudentCourse.objects.filter(student_id=student) for course in courses: select_course = course.course_id teacher_course = TeacherCourse.objects.filter( course_id=select_course)[0] teacher_courses.append(teacher_course) result = TeacherCourseSerializer(teacher_courses, many=True).data info = format_search(result) total = len(info) output['total'] = total output['cur_page'] = 1 output['total_page'] = 1 output['info'] = info return Response(output) raise ArgumentError()
def add_request(req_type, student_id): global REQ_ID, REQ_QUEUE, PENDING_WORK exist = 0 try: student = Student.objects.get(id=student_id) except Student.DoesNotExist: raise UnAuthorizedError() for item in REQ_QUEUE: if item['usr_name'] == student.usr_name \ and item['password'] == student.usr_password \ and item['req_type'] == req_type: exist = 1 break if not exist: REQ_ID += 1 new_request = { 'req_id': REQ_ID, 'usr_name': student.usr_name, 'password': student.usr_password, 'req_type': req_type } REQ_QUEUE.append(new_request) log = open('log.txt', 'a') log.write('new request: ') log.write(str(new_request)) log.write('\n') PENDING_WORK.append(REQ_ID) return REQ_ID return -1
def post(request): req = request.data if len(req) == 4: teacher_name = req['teacher'] bid = req['bid'] actor = req['actor'] action = req['action'] try: teacher = Teacher.objects.get(name=teacher_name) except Teacher.DoesNotExist: raise NotFoundError(detail="没有这个老师") try: course = Course.objects.get(bid=bid) except Course.DoesNotExist: raise NotFoundError(detail="没有这门课程") try: student = Student.objects.get(id=actor) except Student.DoesNotExist: raise UnAuthorizedError() try: teacher_course = TeacherCourse.objects.get(teacher_id=teacher, course_id=course) except TeacherCourse.DoesNotExist: raise NotFoundError(detail="没有这个课程评价") if action == 'up': try: # 已经点过赞 TeacherEvaluationRecord.objects.get( teacher_course=teacher_course, student=student) return Response({"up": teacher_course.up}, status=202) except TeacherEvaluationRecord.DoesNotExist: # 没点过赞 up_record = TeacherEvaluationRecord( teacher_course=teacher_course, student=student) up_record.save() teacher_course.up = up_count_teacher(teacher_course) teacher_course.save() return Response({"up": teacher_course.up}, status=201) if action == 'cancel_up': try: up_record = TeacherEvaluationRecord.objects.get( teacher_course=teacher_course, student=student) up_record.delete() teacher_course.up = up_count_teacher(teacher_course) teacher_course.save() except TeacherEvaluationRecord.DoesNotExist: raise NotFoundError(detail="不存在这个点赞记录") return Response({"up": teacher_course.up}, status=201) raise ArgumentError()
def post(request): req = request.data # 确保数据库中有此学生的记录 try: student = Student.objects.get(id=req['student_id']) except Student.DoesNotExist: raise UnAuthorizedError() # 爬虫的数据库插入请求 if len(req) == 3: semester = req['semester'] for key in req['info']: insert_score(student=student, semester=semester, key=key) return HttpResponse(status=201) # 其他非法请求 raise ArgumentError()
def get(request): """ 输入:学号,输出:作业,dll,提交状态,课程 参数1:学生学号 e.g. 17373349 例:http://127.0.0.1:8000/ddl/?student_id=17373349 返回作业,dll,提交状态,课程 没有提供参数,参数数量错误,返回500错误; 参数错误,返回400错误; 查询用户不存在返回401 """ try: # 保存前端请求数据 record = RequestRecord.objects.get(name="ddl") record.count = record.count + 1 record.save() except RequestRecord.DoesNotExist: RequestRecord(name="ddl", count=1).save() req = request.query_params.dict() if (len(req) != 1) or ("student_id" not in req): raise ArgumentError() student_id = req["student_id"] content = [] try: Student.objects.get(id=req['student_id']) req = DDL.objects.filter(student_id=student_id) except Student.DoesNotExist: raise UnAuthorizedError() course_re = req.values("course").distinct() for i in course_re: cr_re_1 = req.filter( Q(course=i["course"]) & (Q(state="尚未提交") | Q(state="草稿 - 进行中"))).values( "homework", "ddl", "state").distinct() cr_re_2 = req.filter( Q(course=i["course"]) & (~Q(state="尚未提交") & ~Q(state="草稿 - 进行中"))).values( "homework", "ddl", "state").distinct() cr_re = chain(cr_re_1, cr_re_2) content.append({"name": i["course"], "content": cr_re}) return Response(content)
def get(request): req = request.query_params.dict() student_id = req['student_id'] score_sum = 0.0 credit_sum = 0.0 try: Student.objects.get(id=student_id) except Student.DoesNotExist: raise UnAuthorizedError() if len(req) == 1 and 'student_id' in req.keys(): scores = Score.objects.filter(student_id=student_id) for score in scores: score_sum += score.score * score.credit credit_sum += score.credit if credit_sum == 0: return Response({'score': 0.00000}) return Response({'score': score_sum / credit_sum}) raise ArgumentError()
def get(request): req = request.query_params.dict() student_id = req['student_id'] gpa_sum = 0.0 credit_sum = 0.0 try: Student.objects.get(id=student_id) except Student.DoesNotExist: raise UnAuthorizedError() if len(req) == 1 and 'student_id' in req.keys(): scores = Score.objects.filter(student_id=student_id) for score in scores: if score.origin_score != '通过' and score.origin_score != '不通过': gpa_sum += get_gpa(score.origin_score, score.credit) credit_sum += score.credit if credit_sum == 0: return Response({'gpa': 0.0000}) return Response({'gpa': gpa_sum / credit_sum}) raise ArgumentError()
def get(request): start_day = '2020-2-24' req = request.query_params.dict() result = StudentCourse.objects.all() # 记录查询次数 try: count = RequestRecord.objects.get(name='timetable') except RequestRecord.DoesNotExist: count = RequestRecord(name='timetable', count=0) count.count += 1 count.save() # 课表查询请求 if len(req) == 2: for key, value in req.items(): if key == 'student_id': try: Student.objects.get(id=req['student_id']) except Student.DoesNotExist: raise UnAuthorizedError() result = result.filter(student_id__id=value) elif key == 'week': if value != 'all': value += ',' result = result.filter(week__icontains=value) else: raise ArgumentError() course_serializer = StudentCourseSerializer(result, many=True) return Response(course_serializer.data) # 当前周查询请求 if len(req) == 1: if 'date' in req.keys(): content = [] date1 = datetime.strptime(req['date'], "%Y-%m-%d") date2 = datetime.strptime(start_day, "%Y-%m-%d") total_week = min(16, int((date1 - date2).days / 7 + 1)) value = str(total_week) content.append({"week": value}) return Response(content) raise ArgumentError() # 其他非法请求 raise ArgumentError()
def post(request): """ 前端调用插入用户反馈信息 http://127.0.0.1:8000/feedback/ 参数:学生id,反馈类别,具体内容 :param request: :return: """ req = request.data if (len(req) != 3) | (("student_id" not in req) | ("kind" not in req) | ("content" not in req)): raise ArgumentError() try: student = Student.objects.get(id=req['student_id']) except Student.DoesNotExist: raise UnAuthorizedError() Feedback(student_id=student, kind=req["kind"], content=req["content"]).save() return Response({"state": "成功"})
def get_evaluation(req): try: student_id = req['student_id'] actor = req['actor'] bid = req['bid'] except KeyError: raise ArgumentError() try: student = Student.objects.get(id=student_id) actor = Student.objects.get(id=actor) except Student.DoesNotExist: raise UnAuthorizedError() try: course = Course.objects.get(bid=bid) except Course.DoesNotExist: raise NotFoundError(detail="没有这门课程") try: evaluation = CourseEvaluation.objects.get(student=student, course=course) except CourseEvaluation.DoesNotExist: raise NotFoundError(detail="没有这条评价") return actor, evaluation
def get(request): req = request.query_params.dict() result = CourseEvaluation.objects.all() teachers = TeacherCourse.objects.all() info_dict = {} if len(req) == 2: try: bid = req['bid'] student_id = req['student_id'] except KeyError: raise ArgumentError() try: student = Student.objects.get(id=student_id) except Student.DoesNotExist: raise UnAuthorizedError() try: course = Course.objects.get(bid=bid) except Course.DoesNotExist: raise NotFoundError(detail="没有这门课程") result = result.filter(course__bid=bid) teachers = teachers.filter(course_id__bid=bid) teacher_info = TeacherEvaluationSerializer(teachers, many=True).data teacher_info = format_serializer_teacher(teacher_info, student) info = CourseEvaluationSerializer(result, many=True).data info = format_serializer(info, student) avg_score = result.aggregate(Avg('score'))['score__avg'] avg_score = 0.0 if avg_score is None else round(avg_score, 1) evaluation_num = evaluator_count(course) info_dict["course_name"] = course.name info_dict["evaluation_num"] = evaluation_num info_dict["avg_score"] = avg_score info_dict["teacher_info"] = teacher_info info_dict["info"] = info info_dict["score_info"] = get_score_info(course) return Response(info_dict) raise ArgumentError()
def delete(request): req = request.data if len(req) == 2: try: bid = req['bid'] student_id = req['student_id'] except KeyError: raise ArgumentError() try: student = Student.objects.get(id=student_id) except Student.DoesNotExist: raise UnAuthorizedError() try: course = Course.objects.get(bid=bid) except Course.DoesNotExist: raise NotFoundError(detail="没有这门课程") try: evaluation = CourseEvaluation.objects.get(student=student, course=course) evaluation.delete() return HttpResponse(status=204) except CourseEvaluation.DoesNotExist: raise NotFoundError(detail="没有这条评价") raise ArgumentError()
def post(request): """ 根据post的json文件来将相关数据插入数据库; 格式:{student_id:(id), semester:(sm), info:[[课程名称1,地点1...],[课程名称2,地点2...]} """ req = request.data # 确保数据库中有这个同学的信息 student_id = req['student_id'] try: student = Student.objects.get(id=student_id) except Student.DoesNotExist: raise UnAuthorizedError() # 爬虫的数据库插入请求 if len(req) == 2: semester = '2020_Spring' # 更新则默认将原记录删除 StudentCourse.objects.filter(student_id=student_id).delete() # 将爬虫爬取的数据写入数据库 for lists in req['info']: for info in lists: add_student_course(student, semester, info) return HttpResponse(status=201) # 其他非法请求 raise ArgumentError()
def post(request): """ 输入:用户名,用户密码,输出:登录状态(1代表成功,2代表无该账号,3代表密码错误) 参数1:用户名 e.g. mushan,用户密码 e.g. h1010 例:http --form POST http://127.0.0.1:8000/login/ usr_name="mushan" usr_password="******" 获取学生基本信息 返回: [state, student_id, name] 没有提供参数,参数数量错误,返回400错误; 参数错误,返回400错误; 密码账号错误401 账号被锁返回460 服务器ip返回461 """ try: # 保存前端请求数据 record = RequestRecord.objects.get(name="login") record.count = record.count + 1 record.save() except RequestRecord.DoesNotExist: RequestRecord(name="login", count=1).save() req = request.data if (len(req) != 2) | ("usr_name" not in req) | ("usr_password" not in req): raise ArgumentError() usr_name = request.data["usr_name"] usr_password = request.data["usr_password"] ans = get_student_info(usr_name, usr_password) state = ans if (ans == 0) | (ans == -1) | (ans == -2) | (ans == -3) | (ans == -4) | (ans == -5): raise InternalServerError() if ans == -6: # IP is banned from the buaa raise IPBannedError() if (ans == -7) | (ans == -8): raise UnAuthorizedError() if ans == -9: raise ArgumentError() if ans == -10: raise AccountLockedError() state = 1 student_id = str(ans[0]) password_d = Aescrypt(KEY, MODEL, IV, ENCODE_) student_id = password_d.aesencrypt(student_id) name = ans[2] grade = ans[3] if Student.objects.filter(id=student_id).count() == 0: Student(usr_name=usr_name, usr_password=usr_password, id=student_id, name=name, grade=grade).save() add_request('l', student_id) else: Student(usr_name=usr_name, usr_password=usr_password, id=student_id, name=name, grade=grade).save() try: # 学生更新数据最新时间 student = Student.objects.get(id=student_id) PostRecord.objects.get(student_id=student, name="login").delete() PostRecord.objects.get(student_id=student, name="login").save() except PostRecord.DoesNotExist: PostRecord(student_id=student, name="login").save() if len(StudentCourse.objects.filter(student_id_id=student_id)) == 0 or len( Score.objects.filter(student_id_id=student_id)) == 0: add_request('j', student_id) if len(DDL.objects.filter(student_id_id=student_id)) == 0: add_request('d', student_id) content = {"state": state, "student_id": ans[0], "name": name} return Response(content)
def post(request): """ 访问方法 POST http://127.0.0.1:8000/ddl/ { "student_id":"17373349", "ddl":[ { "content":[ { "ddl":"2020-3-19 下午11:55", "homework":"第一次作业", "state":"提交" }, { "ddl":"2020-3-19 下午11:55", "homework":"第二次作业", "state":"未提交" } ], "name":"计算机科学方法论" }, { "content":[ { "ddl":"2020-3-19 下午11:55", "homework":"第一次作业", "state":"提交" } ], "name":"计算机图形学" } ] } 错误:400 """ req = request.data try: student = Student.objects.get(id=req['student_id']) DDL.objects.filter(student_id=req['student_id']).delete() except Student.DoesNotExist: raise UnAuthorizedError() try: # 学生更新数据最新时间 PostRecord.objects.get(student_id=student, name="ddl").delete() PostRecord.objects.get(student_id=student, name="ddl").save() except PostRecord.DoesNotExist: PostRecord(student_id=student, name="ddl").save() StudentError.objects.filter(student_id=student.id).update(number=0) if "ddl" not in req: raise ArgumentError() for key in req['ddl']: if len(key) == 2: content = key["content"] name = key["name"] for i in content: if i["ddl"] == "": time = "" else: try: time = standard_time(i["ddl"]) except ValueError: text = "ddl时间格式错误 " + i["ddl"] logging.warning(text) raise ArgumentError() DDL(student_id=student, ddl=time, homework=i["homework"], state=i["state"], course=name).save() else: raise ArgumentError() content = {"state": 1} return Response(content)