Beispiel #1
0
    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)
Beispiel #2
0
    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)
Beispiel #3
0
 def delete(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
         # reject
         return Response(dict(
             {"msg": "Forbidden. You are not the teacher."}),
                         status=403)
     try:
         courseChapterDescrption_to_delete = CourseChapterDescrption.objects.get(
             course_id=course_id, course_chapter_id=course_chapter_id)
         courseChapterDescrption_to_delete.delete()
     except CourseChapterDescrption.DoesNotExist:
         return Response(dict({
             "msg": "Requested announcement does not exist.",
             "courseId": course_id,
             "announcement_id": id
         }),
                         status=status.HTTP_404_NOT_FOUND)
     return Response(status=status.HTTP_204_NO_CONTENT)
Beispiel #4
0
 def post(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
         # reject
         return Response(dict(
             {"msg": "Forbidden. You are not the teacher."}),
                         status=403)
     request_body_unicode = request.body.decode('utf-8')
     request_body = json.loads(request_body_unicode)
     new_announcement = Announcement(
         course_id=course_id,
         announcement_title=request_body["announcementTitle"],
         announcement_contents=request_body["announcementContents"],
         announcement_is_pinned=request_body["announcementIsPinned"],
         announcement_publish_time=datetime.datetime.now(),
         announcement_last_update_time=datetime.datetime.now(),
         announcement_sender_id=request.user.user_id,
     )
     new_announcement.save()
     return Response(AnnouncementSerializer(new_announcement).data)
Beispiel #5
0
 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)
Beispiel #6
0
 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)
Beispiel #7
0
 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)
Beispiel #8
0
 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
         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
         # reject
         return Response(dict({
             "msg": "Forbidden. You are not the teacher."
         }), status=403)
     try:
         homework = Homework.objects.get(course_id=course_id, homework_id=homework_id)
         homework.delete()
         # FIXME: 外码约束,删除文件和学生提交记录和分数
         return Response(status=status.HTTP_204_NO_CONTENT)
     except Homework.DoesNotExist:
         return Response(dict({
             "msg": "No such homework found."
         }), status=status.HTTP_404_NOT_FOUND)
Beispiel #9
0
 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)
Beispiel #10
0
    def put(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
            # reject
            return Response(dict({
                "msg": "Forbidden. You are not the teacher."
            }), status=403)
        request_body_unicode = request.body.decode('utf-8')
        request_body = None
        if len(request_body_unicode) != 0:
            try:
                request_body = json.loads(request_body_unicode)
            except json.decoder.JSONDecodeError:
                return Response(dict({
                    "msg": "Invalid JSON string provided."
                }), status=status.HTTP_400_BAD_REQUEST)

        try:
            file_queried = HomeworkFile.objects.get(file_homework_id=homework_file_id)
        except HomeworkFile.DoesNotExist as e:
            print(e.with_traceback)
            return Response(dict({
                "msg": "Requested homework file does not exist.",
                "courseId": course_id,
                "homeworkFileId": homework_file_id
            }), status=404)

        try:
            file_score_queried = HomeworkScore.objects.get(homework_id=homework_id, student_id=file_queried.file_uploader)
            file_score_queried.homework_score = request_body["homeworkScore"]
            file_score_queried.homework_teachers_comments = request_body["homeworkTeachersComment"]
            file_score_queried.homework_is_grade_available_to_students = request_body["homeworkIsGradeAvailable"]
            file_score_queried.save()
        except HomeworkScore.DoesNotExist:
            file_score_queried = HomeworkScore(
                homework_id=(Homework.objects.get(homework_id=homework_id)),
                student_id=file_queried.file_uploader,
                course_id=course_id,
                homework_score=request_body["homeworkScore"],
                homework_teachers_comments=request_body["homeworkTeachersComment"],
                homework_is_grade_available_to_students=request_body["homeworkIsGradeAvailable"],
            )
            file_score_queried.save()

        return Response(HomeworkScoreSerializer(file_score_queried).data, status=status.HTTP_200_OK)
Beispiel #11
0
    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)
Beispiel #12
0
    def post(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
            # reject
            return Response(dict({
                "msg": "Forbidden. You are not the teacher."
            }), status=403)
        request_body_unicode = request.body.decode('utf-8')
        if len(request_body_unicode) != 0:
            try:
                request_body = json.loads(request_body_unicode)
            except json.decoder.JSONDecodeError:
                return Response(dict({
                    "msg": "Invalid JSON string provided."
                }), status=status.HTTP_400_BAD_REQUEST)

        request_body = json.loads(request_body_unicode)
        new_homework = Homework(
            course_id=course_id,
            homework_creator=request.request.user.user_id,
            homework_title=request_body["homeworkTitle"],
            homework_description=request_body["homeworkDescription"],
            homework_start_timestamp=request_body["homeworkStartTime"],
            homework_end_timestamp=request_body["homeworkEndTime"],
            homework_create_timestamp=datetime.now(),
            homework_update_timestamp=datetime.now(),
        )

        if new_homework.homework_start_timestamp > new_homework.homework_end_timestamp:
            return Response(dict({
                "msg": "End before start."
            }), status=status.HTTP_400_BAD_REQUEST)

        try:
            new_homework.save()
        except ValidationError as e:
            return Response(dict({
                "msg": f"Invalid parameter. Detail: {e.message}"
            }), status=status.HTTP_400_BAD_REQUEST)

        return Response(HomeworkSerializer(new_homework).data, status=status.HTTP_201_CREATED)
Beispiel #13
0
    def put(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
            # reject
            return Response(dict(
                {"msg": "Forbidden. You are not the teacher."}),
                            status=403)
        request_body = None
        request_has_body = False

        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=status.HTTP_400_BAD_REQUEST)
        else:
            return Response(dict(
                {"msg": "Expect a JSON, but got empty contents instead."}),
                            status=status.HTTP_400_BAD_REQUEST)
        try:
            query_courseChapterDescrption = CourseChapterDescrption.objects.get(
                course_id=course_id, course_chapter_id=course_chapter_id)
            query_courseChapterDescrption.course_chapter_title = request_body[
                "courseChapterTitle"]
            query_courseChapterDescrption.course_chapter_mooc_link = request_body[
                "courseChapterMoocLink"]
            query_courseChapterDescrption.save()
        except CourseChapterDescrption.DoesNotExist:
            return Response(dict({
                "msg": "Requested course chapter does not exist.",
                "courseId": course_id,
                "announcement_id": id
            }),
                            status=status.HTTP_404_NOT_FOUND)

        return Response(CourseChapterDescrptionSerializer(
            query_courseChapterDescrption).data,
                        status=status.HTTP_200_OK)
Beispiel #14
0
 def put(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
         # reject
         return Response(dict(
             {"msg": "Forbidden. You are not the teacher."}),
                         status=403)
     request_has_body = False
     request_body = None
     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)
     else:
         return Response(dict(
             {"msg": "Expect a JSON, but got empty contents instead."}),
                         status=400)
     try:
         query_announcement = Announcement.objects.get(
             course_id=course_id, announcement_id=announcement_id)
     except Announcement.DoesNotExist:
         return Response(dict({
             "msg": "Requested announcement does not exist.",
             "courseId": course_id,
             "announcement_id": announcement_id
         }),
                         status=404)
     query_announcement.announcement_title = request_body[
         "announcementTitle"]
     query_announcement.announcement_contents = request_body[
         "announcementContents"]
     query_announcement.announcement_is_pinned = request_body[
         "announcementIsPinned"]
     query_announcement.announcement_last_update_time = datetime.datetime.now(
     )
     query_announcement.save()
     return Response(AnnouncementSerializer(query_announcement).data)
Beispiel #15
0
    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)
Beispiel #16
0
    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)
Beispiel #17
0
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)
Beispiel #18
0
    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
            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
            # reject
            return Response(dict({
                "msg": "Forbidden. You are not the teacher."
            }), status=403)
        request_body_unicode = request.body.decode('utf-8')
        if len(request_body_unicode) != 0:
            try:
                request_body = json.loads(request_body_unicode)
            except json.decoder.JSONDecodeError:
                return Response(dict({
                    "msg": "Invalid JSON string provided."
                }), status=status.HTTP_400_BAD_REQUEST)

        try:
            homework = Homework.objects.get(course_id=course_id, homework_id=homework_id)
        except Homework.DoesNotExist:
            return Response(dict({
                "msg": "No such homework found."
            }), status=status.HTTP_404_NOT_FOUND)

        request_body = json.loads(request_body_unicode)
        homework.homework_title = request_body["homeworkTitle"]
        homework.homework_description = request_body["homeworkDescription"]
        homework.homework_start_timestamp = request_body["homeworkStartTime"]
        homework.homework_end_timestamp = request_body["homeworkEndTime"]
        homework.homework_update_timestamp = datetime.now()

        if homework.homework_start_timestamp > homework.homework_end_timestamp:
            return Response(dict({
                "msg": "End before start."
            }), status=status.HTTP_400_BAD_REQUEST)

        homework.save()

        return Response(HomeworkSerializer(homework).data, status=status.HTTP_200_OK)
Beispiel #19
0
    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 put(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
            # reject
            return Response(dict(
                {"msg": "Forbidden. You are not the teacher."}),
                            status=403)

        request_has_body = False
        request_body = None
        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)
        else:
            return Response(dict(
                {"msg": "Expect a JSON, but got empty contents instead."}),
                            status=400)
        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)
        file_queried.file_comment = request_body["fileComment"]
        file_queried.save()
        return Response(CourseDocumentSerializer(file_queried).data)
Beispiel #21
0
    def post(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
            # reject
            return Response(dict({
                "msg": "Forbidden. You are not the teacher."
            }), status=403)
        request_body_unicode = request.body.decode('utf-8')
        request_body = json.loads(request_body_unicode)
        file_display_name = request_body["fileDisplayName"]
        random_hex_string = ('%030x' % random.randrange(16**30))
        file_token = f"{COURSE_DOCUMENT_PREFIX}/{course_id}/{random_hex_string}/{file_display_name}"
        new_course_file = CourseDocument(
            course_id=course_id,
            file_display_name=file_display_name,
            file_comment=request_body["fileComment"],
            file_create_timestamp=datetime.datetime.now(),
            file_update_timestamp=datetime.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(CourseDocumentSerializer(new_course_file).data) | file_put_url_dict
        return Response(final_dict_to_return)
Beispiel #22
0
    def post(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
            # reject
            return Response(dict(
                {"msg": "Forbidden. You are not the teacher."}),
                            status=403)
        request_body_unicode = request.body.decode('utf-8')
        request_body = json.loads(request_body_unicode)
        new_courseChapterDescrption = CourseChapterDescrption(
            course_id=course_id,
            course_chapter_id=request_body["courseChapterId"],
            course_chapter_title=request_body["courseChapterTitle"],
            course_chapter_mooc_link=request_body["courseChapterMoocLink"],
        )

        if CourseChapterDescrption.objects\
                .filter(course_id=new_courseChapterDescrption.course_id, course_chapter_id=new_courseChapterDescrption.course_chapter_id).exists():
            return Response(dict({
                "error":
                "Course chapter already existed, use another API if you want to modify it."
            }),
                            status=400)
        else:
            new_courseChapterDescrption.save()

        return Response(CourseChapterDescrptionSerializer(
            new_courseChapterDescrption).data,
                        status=status.HTTP_201_CREATED)
    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)
Beispiel #24
0
    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)
Beispiel #25
0
    def delete(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
            # reject
            return Response(dict({
                "msg": "Forbidden. You are not the teacher."
            }), status=403)

        try:
            file_to_delete = CourseDocument.objects.get(course_id=course_id, file_course_document_id=file_id)
            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 CourseDocument.DoesNotExist:
            return Response(dict({
                "msg": "Requested course document does not exist.",
                "courseId": course_id,
                "fileId": file_id
            }), status=404)
        return Response(dict({
            "msg": "Deleted."
        }))
Beispiel #26
0
    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
            # reject
            return Response(dict({
                "msg": "Forbidden. You are not the teacher."
            }), 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)

        response = []
        all_homeworkFiles = HomeworkFile.objects.filter(homework_id=homework_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_homeworkFiles = all_homeworkFiles[pagination_start:pagination_end]
        else:
            selected_homeworkFiles = all_homeworkFiles
        for item in selected_homeworkFiles:
            tmp_file_info = HomeworkFileSerializer(item).data
            student_id = tmp_file_info['file_uploader']
            score_queried: HomeworkScore
            try:
                score_queried = HomeworkScore.objects.get(homework_id=homework_id, student_id=student_id)
                homework_score = score_queried.homework_score
            except HomeworkScore.DoesNotExist:
                homework_score = 0
            tmp_file_info['homework_score'] = homework_score
            response.append(tmp_file_info)

        return Response(response, status=status.HTTP_200_OK)
Beispiel #27
0
    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)