def get(self, request, course_id, format=None): user_character = request.user.character user_id = request.user.user_id # all within this class # TODO: change to match when comes to Python 3.10 if user_character == 1: # charging teacher pass elif user_character == 2 or user_character == 3: # teacher or teaching assistant # check if this teacher teaches this course if not is_teacher_teach_course(user_id, course_id): return Response(dict( {"msg": "Forbidden. You are not within course."}), status=403) elif user_character == 4: # student # check if student is within this course if not is_student_within_course(user_id, course_id): return Response(dict( {"msg": "Forbidden. You are not within course."}), status=403) response = { "courseId": course_id, "announcementCount": Announcement.objects.filter(course_id=course_id).count() } return Response(response)
def get(self, request, course_id, announcement_id, format=None): user_character = request.user.character user_id = request.user.user_id # all within this class # TODO: change to match when comes to Python 3.10 if user_character == 1: # charging teacher pass elif user_character == 2 or user_character == 3: # teacher or teaching assistant # check if this teacher teaches this course if not is_teacher_teach_course(user_id, course_id): return Response(dict( {"msg": "Forbidden. You are not within course."}), status=403) elif user_character == 4: # student # check if student is within this course if not is_student_within_course(user_id, course_id): return Response(dict( {"msg": "Forbidden. You are not within course."}), status=403) query_announcement = Announcement.objects.get( course_id=course_id, announcement_id=announcement_id) return Response(AnnouncementSerializer(query_announcement).data)
def get(self, request, course_id, homework_id, format=None): user_character = request.user.character user_id = request.user.user_id # all within this class # TODO: change to match when comes to Python 3.10 if user_character == 1: # charging teacher pass elif user_character == 2 or user_character == 3: # teacher or teaching assistant # check if this teacher teaches this course if not is_teacher_teach_course(user_id, course_id): return Response(dict({ "msg": "Forbidden. You are not within course." }), status=403) elif user_character == 4: # student # check if student is within this course if not is_student_within_course(user_id, course_id): return Response(dict({ "msg": "Forbidden. You are not within course." }), status=403) try: homework = Homework.objects.get(homework_id=homework_id) return Response(HomeworkSerializer(homework).data, status=status.HTTP_200_OK) except Homework.DoesNotExist: return Response(dict({ "msg": "No such homework found." }), status=status.HTTP_404_NOT_FOUND)
def get(self, request, course_id, file_id, format=None): user_character = request.user.character user_id = request.user.user_id # all within this class # TODO: change to match when comes to Python 3.10 if user_character == 1: # charging teacher pass elif user_character == 2 or user_character == 3: # teacher or teaching assistant # check if this teacher teaches this course if not is_teacher_teach_course(user_id, course_id): return Response(dict({ "msg": "Forbidden. You are not within course." }), status=403) elif user_character == 4: # student # check if student is within this course if not is_student_within_course(user_id, course_id): return Response(dict({ "msg": "Forbidden. You are not within course." }), status=403) file_queried: CourseDocument try: file_queried = CourseDocument.objects.get(course_id=course_id, file_course_document_id=file_id) except CourseDocument.DoesNotExist: return Response(dict({ "msg": "Requested course document does not exist.", "courseId": course_id, "fileId": file_id }), status=404) return Response(CourseDocumentSerializer(file_queried).data)
def get(self, request, course_id, homework_id, homework_file_id, format=None): user_character = request.user.character user_id = request.user.user_id # all within this class # TODO: change to match when comes to Python 3.10 if user_character == 1: # charging teacher pass elif user_character == 2 or user_character == 3: # teacher or teaching assistant # check if this teacher teaches this course if not is_teacher_teach_course(user_id, course_id): return Response(dict({ "msg": "Forbidden. You are not within course." }), status=403) elif user_character == 4: # student # check if student is within this course if not is_student_within_course(user_id, course_id): return Response(dict({ "msg": "Forbidden. You are not within course." }), status=403) try: file_queried = HomeworkScore.objects.get(homework_id=homework_id, student_id=request.user.user_id) except HomeworkScore.DoesNotExist: return Response(dict({ "msg": "Requested homework file does not exist.", "courseId": course_id, "homeworkId": homework_id }), status=404) return Response(HomeworkScoreSerializer(file_queried).data, status=status.HTTP_200_OK)
def get(self, request, course_id, course_chapter_id, format=None): user_character = request.user.character user_id = request.user.user_id # all within this class # TODO: change to match when comes to Python 3.10 if user_character == 1: # charging teacher pass elif user_character == 2 or user_character == 3: # teacher or teaching assistant # check if this teacher teaches this course if not is_teacher_teach_course(user_id, course_id): return Response(dict( {"msg": "Forbidden. You are not within course."}), status=403) elif user_character == 4: # student # check if student is within this course if not is_student_within_course(user_id, course_id): return Response(dict( {"msg": "Forbidden. You are not within course."}), status=403) try: selected_courseChapterDescrption = CourseChapterDescrption.objects.get( course_id=course_id, course_chapter_id=course_chapter_id) except CourseChapterDescrption.DoesNotExist: return Response(dict({ "msg": "Requested chapter does not exist.", "courseId": course_id, "course_chapter_id": course_chapter_id }), status=status.HTTP_404_NOT_FOUND) return Response(CourseChapterDescrptionSerializer( selected_courseChapterDescrption).data, status=status.HTTP_200_OK)
def get(self, request, course_id, format=None): user_character = request.user.character user_id = request.user.user_id # all within this class # TODO: change to match when comes to Python 3.10 if user_character == 1: # charging teacher pass elif user_character == 2 or user_character == 3: # teacher or teaching assistant # check if this teacher teaches this course if not is_teacher_teach_course(user_id, course_id): return Response(dict({ "msg": "Forbidden. You are not within course." }), status=403) elif user_character == 4: # student # check if student is within this course if not is_student_within_course(user_id, course_id): return Response(dict({ "msg": "Forbidden. You are not within course." }), status=403) query_dict = request.query_params need_pagination = False pagination_page_size = -1 pagination_page_num = -1 if len(query_dict) != 0: try: pagination_page_num = int(query_dict['pageIndex']) pagination_page_size = int(query_dict['itemCountOnOnePage']) need_pagination = True except KeyError: pass except ValueError: # not an int return Response(dict({ "msg": "Invaild pagination request." }), status=status.HTTP_400_BAD_REQUEST) except Exception as e: return Response(str(e), status=status.HTTP_400_BAD_REQUEST) response = [] all_homework = Homework.objects.filter(course_id=course_id)\ .order_by('homework_id') if need_pagination: pagination_start = (pagination_page_num - 1) * pagination_page_size pagination_end = pagination_page_num * pagination_page_size selected_homework = all_homework[pagination_start:pagination_end] else: selected_homework = all_homework for item in selected_homework: response.append(HomeworkSerializer(item).data) return Response(response, status=status.HTTP_200_OK)
def get(self, request, course_id, homework_id, homework_file_id, format=None): user_character = request.user.character user_id = request.user.user_id # all within this class # TODO: change to match when comes to Python 3.10 if user_character == 1: # charging teacher pass elif user_character == 2 or user_character == 3: # teacher or teaching assistant # check if this teacher teaches this course if not is_teacher_teach_course(user_id, course_id): return Response(dict( {"msg": "Forbidden. You are not within course."}), status=403) elif user_character == 4: # student # check if student is within this course if not is_student_within_course(user_id, course_id): return Response(dict( {"msg": "Forbidden. You are not within course."}), status=403) file_queried: HomeworkFile try: file_queried = HomeworkFile.objects.get( homework_id=homework_id, file_homework_id=homework_file_id) # check student if user_character == 4: if file_queried.file_uploader != user_id: return Response(dict({ "msg": "You can not read other student's submission :(" }), status=403) except HomeworkFile.DoesNotExist: return Response(dict({ "msg": "Requested homework file does not exist.", "courseId": course_id, "homeworkFileId": homework_file_id }), status=404) file_token = file_queried.file_token result_url = local_minio_client.presigned_url( "GET", DEFAULT_BUCKET, file_token, expires=DEFAULT_FILE_URL_TIMEOUT) return HttpResponseRedirect(redirect_to=result_url)
def get(self, request, course_id, format=None): user_character = request.user.character user_id = request.user.user_id # all within this class # TODO: change to match when comes to Python 3.10 if user_character == 1: # charging teacher pass elif user_character == 2 or user_character == 3: # teacher or teaching assistant # check if this teacher teaches this course if not is_teacher_teach_course(user_id, course_id): return Response(dict( {"msg": "Forbidden. You are not within course."}), status=403) elif user_character == 4: # student # check if student is within this course if not is_student_within_course(user_id, course_id): return Response(dict( {"msg": "Forbidden. You are not within course."}), status=403) query_dict = request.query_params need_pagination = False pagination_page_size = -1 pagination_page_num = -1 if query_dict: # find out whether the user requested for pagination try: pagination_page_size = int(query_dict["itemCountOnOnePage"]) pagination_page_num = int(query_dict["pageIndex"]) need_pagination = True except KeyError: pass except ValueError: # not an int return Response(dict({"msg": "Invaild pagination request."}), status=400) response = [] all_announcement = Announcement.objects.filter(course_id=course_id) all_announcement = all_announcement.order_by('announcement_id') if need_pagination: pagination_start = (pagination_page_num - 1) * pagination_page_size pagination_end = pagination_page_num * pagination_page_size selected_announcement = all_announcement[ pagination_start:pagination_end] else: selected_announcement = all_announcement for item in selected_announcement: response.append(AnnouncementSerializer(item).data) return Response(response)
def course_detail(request, course_id): """ Retrieve or update a course instance. """ try: course = Course.objects.get(pk=course_id) except Course.DoesNotExist: error_msg = {"detail": "object not exists"} return Response(generate_response(error_msg, False), status=status.HTTP_404_NOT_FOUND) user_id = request.user.user_id if request.user.character in [2, 3]: if not is_teacher_teach_course(teacher_id=user_id, course_id=course_id): response_data = {"error_msg": 'permission denied'} return Response(utils.generate_response(response_data, False), status=status.HTTP_400_BAD_REQUEST) elif request.user.character == 4: if not is_student_within_course(student_id=user_id, course_id=course_id): response_data = {"error_msg": 'permission denied'} return Response(utils.generate_response(response_data, False), status=status.HTTP_400_BAD_REQUEST) if request.method == 'GET': serializer = CourseSerializers(course) return Response(generate_response(serializer.data, True)) elif request.method == 'PUT': teacher = User.objects.get(user_id=request.user.user_id) if teacher.character not in [1]: error_msg = {"detail": "没有权限"} return Response(generate_response(error_msg, False), status=status.HTTP_400_BAD_REQUEST) serializer = CourseSerializers(course, data=request.data, partial=True) if serializer.is_valid(): serializer.save() return Response(generate_response(serializer.data, True)) return Response(generate_response(serializer.errors, False), status=status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE': if request.user.character in [1]: course.delete() response_data = {"detail": "have delete"} return Response(generate_response(response_data, True), status=status.HTTP_204_NO_CONTENT) else: error_msg = {"detail": "没有权限"} return Response(generate_response(error_msg, False), status=status.HTTP_400_BAD_REQUEST)
def get(self, request, course_id, homework_id, student_id, format=None): user_character = request.user.character user_id = request.user.user_id # all within this class # TODO: change to match when comes to Python 3.10 if user_character == 1: # charging teacher pass elif user_character == 2 or user_character == 3: # teacher or teaching assistant # check if this teacher teaches this course if not is_teacher_teach_course(user_id, course_id): return Response(dict( {"msg": "Forbidden. You are not within course."}), status=403) elif user_character == 4: # student # check if student is within this course if not is_student_within_course(user_id, course_id): return Response(dict( {"msg": "Forbidden. You are not within course."}), status=403) # check if student is query his own file id if not user_id == student_id: return Response(dict( {"msg": "You can not read other student's submission :("}), status=403) score_queried: HomeworkScore try: score_queried = HomeworkScore.objects.get(homework_id=homework_id, student_id=student_id) except HomeworkScore.DoesNotExist: return Response(dict({ "msg": "Requested homework score does not exist.", "courseId": course_id, "homeworkId": homework_id }), status=404) if score_queried.homework_is_grade_available_to_students == False and user_character == 4: return Response(dict({ "msg": "Requested homework score is not available to students now.", "courseId": course_id, "homeworkId": homework_id }), status=403) return Response(HomeworkScoreSerializer(score_queried).data, status=status.HTTP_200_OK)
def delete(self, request, course_id, homework_id, format=None): user_character = request.user.character user_id = request.user.user_id # all within this class # TODO: change to match when comes to Python 3.10 if user_character == 1: # charging teacher # he/she should not handin homework! return Response(dict({ "msg": "Forbidden. You should not delete homework." }), status=403) elif user_character == 2 or user_character == 3: # teacher or teaching assistant # these people should not handin homework! return Response(dict({ "msg": "Forbidden. You should not delete homework." }), status=403) elif user_character == 4: # student # check if student is within this course if not is_student_within_course(user_id, course_id): return Response(dict({ "msg": "Forbidden. You are not within course." }), status=403) try: file_to_delete = HomeworkFile.objects.get(homework_id=homework_id) # not this student if file_to_delete.file_uploader != user_id: return Response(dict({ "msg": "You can not change other student's submission :(" }), status=403) item_token_to_delete = file_to_delete.file_token local_minio_client.remove_object( DEFAULT_BUCKET, item_token_to_delete ) file_to_delete.delete() except HomeworkFile.DoesNotExist: return Response(dict({ "msg": "Requested homework file does not exist.", "courseId": course_id, "fileId": homework_id }), status=status.HTTP_404_NOT_FOUND) return Response(dict({ "msg": "Deleted." }))
def get(self, request, course_id, file_id, format=None): user_character = request.user.character user_id = request.user.user_id # all within this class # TODO: change to match when comes to Python 3.10 if user_character == 1: # charging teacher pass elif user_character == 2 or user_character == 3: # teacher or teaching assistant # check if this teacher teaches this course if not is_teacher_teach_course(user_id, course_id): return Response(dict( {"msg": "Forbidden. You are not within course."}), status=403) elif user_character == 4: # student # check if student is within this course if not is_student_within_course(user_id, course_id): return Response(dict( {"msg": "Forbidden. You are not within course."}), status=403) file_queried: CourseDocument try: file_queried = CourseDocument.objects.get( course_id=course_id, file_course_document_id=file_id) except CourseDocument.DoesNotExist: return Response(dict({ "msg": "Requested file does not exist.", "courseId": course_id, "fileId": file_id }), status=404) file_token = file_queried.file_token result_url = local_minio_client.presigned_url( "GET", DEFAULT_BUCKET, file_token, expires=DEFAULT_FILE_URL_TIMEOUT) return HttpResponseRedirect(redirect_to=result_url)
def get(self, request, course_id, homework_id, file_uploader, format=None): user_character = request.user.character user_id = request.user.user_id # all within this class # TODO: change to match when comes to Python 3.10 if user_character == 1: # charging teacher pass elif user_character == 2 or user_character == 3: # teacher or teaching assistant # check if this teacher teaches this course if not is_teacher_teach_course(user_id, course_id): return Response(dict( {"msg": "Forbidden. You are not within course."}), status=403) elif user_character == 4: # student # check if student is within this course if not is_student_within_course(user_id, course_id): return Response(dict( {"msg": "Forbidden. You are not within course."}), status=403) # check if student is query his own file id if not user_id == file_uploader: return Response(dict( {"msg": "You can not read other student's submission :("}), status=403) file_queried: HomeworkFile try: file_queried = HomeworkFile.objects.get( homework_id=homework_id, file_uploader=file_uploader) except HomeworkFile.DoesNotExist: return Response(dict({ "msg": "Requested homework file does not exist.", "courseId": course_id, "studentId": file_uploader }), status=404) return Response(HomeworkFileSerializer(file_queried).data, status=status.HTTP_200_OK)
def put(self, request, course_id, homework_id, format=None): user_character = request.user.character user_id = request.user.user_id # all within this class # TODO: change to match when comes to Python 3.10 if user_character == 1: # charging teacher # he/she should not handin homework! return Response(dict({ "msg": "Forbidden. You should not handin homework." }), status=403) elif user_character == 2 or user_character == 3: # teacher or teaching assistant # these people should not handin homework! return Response(dict({ "msg": "Forbidden. You should not handin homework." }), status=403) elif user_character == 4: # student # check if student is within this course if not is_student_within_course(user_id, course_id): return Response(dict({ "msg": "Forbidden. You are not within course." }), status=403) replace_flag = False try: homework = Homework.objects.get(homework_id=homework_id) except Homework.DoesNotExist: return Response(dict({ "msg": "No such homework found." }), status=status.HTTP_404_NOT_FOUND) if homework.homework_end_timestamp < datetime.now(): return Response(dict({ "msg": "You can not submit after the deadline." }), status=status.HTTP_400_BAD_REQUEST) try: file_to_delete = HomeworkFile.objects.get(file_uploader=request.user.user_id, homework_id=homework_id) # not this student if file_to_delete.file_uploader != user_id: return Response(dict({ "msg": "You can not change other student's submission :(" }), status=403) item_token_to_delete = file_to_delete.file_token local_minio_client.remove_object( DEFAULT_BUCKET, item_token_to_delete ) file_to_delete.delete() except HomeworkFile.DoesNotExist: replace_flag = True pass request_body_unicode = request.body.decode('utf-8') request_body = json.loads(request_body_unicode) file_display_name = request_body["homeworkFileDisplayName"] random_hex_string = ('%030x' % random.randrange(16**30)) file_token = f"{HOMEWORK_PREFIX}/{course_id}/{homework_id}/{request.user.user_id}/{random_hex_string}/{file_display_name}" new_course_file = HomeworkFile( homework_id=(Homework.objects.get(homework_id=homework_id)), file_comment=request_body["homeworkFileComment"], file_display_name=file_display_name, file_timestamp=datetime.now(), file_uploader=request.user.user_id, file_token=file_token) new_course_file.file_token = file_token if not local_minio_client.bucket_exists(DEFAULT_BUCKET): local_minio_client.make_bucket(DEFAULT_BUCKET) put_url = local_minio_client.presigned_url("PUT", DEFAULT_BUCKET, file_token, expires=DEFAULT_FILE_URL_TIMEOUT) file_put_url_dict = { "FILE_PUT_URL": put_url } new_course_file.save() # This method is for Python 3.9+ only. final_dict_to_return = dict(HomeworkFileSerializer(new_course_file).data) | file_put_url_dict if replace_flag: return Response(final_dict_to_return, status=status.HTTP_200_OK) else: return Response(final_dict_to_return, status=status.HTTP_201_CREATED)
def get(self, request, course_id, *args, **kwargs): user_character = request.user.character user_id = request.user.user_id # all within this class # TODO: change to match when comes to Python 3.10 if user_character == 1: # charging teacher pass elif user_character == 2 or user_character == 3: # teacher or teaching assistant # check if this teacher teaches this course if not is_teacher_teach_course(user_id, course_id): return Response(dict( {"msg": "Forbidden. You are not within course."}), status=403) elif user_character == 4: # student # check if student is within this course if not is_student_within_course(user_id, course_id): return Response(dict( {"msg": "Forbidden. You are not within course."}), status=403) query_dict = request.query_params request_body = None request_has_body = False need_pagination = False pagination_page_size = -1 pagination_page_num = -1 request_body_unicode = request.body.decode('utf-8') if len(request_body_unicode) != 0: try: request_body = json.loads(request_body_unicode) request_has_body = True except json.decoder.JSONDecodeError: return Response(dict({"msg": "Invalid JSON string provided."}), status=400) if request_has_body: # find out whether the user requested for pagination try: pagination_page_size = query_dict["itemCountOnOnePage"] pagination_page_num = query_dict["pageIndex"] need_pagination = True except KeyError: pass response = [] all_courseChapterDescrption = CourseChapterDescrption.objects.filter(course_id=course_id)\ .order_by('course_chapter_description_id') if need_pagination: pagination_start = (pagination_page_num - 1) * pagination_page_size pagination_end = pagination_page_num * pagination_page_size selected_courseChapterDescrption = all_courseChapterDescrption[ pagination_start:pagination_end] else: selected_courseChapterDescrption = all_courseChapterDescrption for item in selected_courseChapterDescrption: response.append(CourseChapterDescrptionSerializer(item).data) return Response(response, status=status.HTTP_200_OK)