Exemple #1
0
def get_current_repo_file(request, course_identifier, path):
    course = get_object_or_404(Course, identifier=course_identifier)
    role, participation = get_role_and_participation(request, course)

    from course.views import check_course_state
    check_course_state(course, role)

    from course.content import get_course_commit_sha
    commit_sha = get_course_commit_sha(course, participation)

    role, participation = get_role_and_participation(request, course)

    repo = get_course_repo(course)

    from course.content import is_repo_file_public
    if not is_repo_file_public(repo, commit_sha, path):
        raise PermissionDenied()

    from course.content import get_repo_blob_data_cached

    try:
        data = get_repo_blob_data_cached(repo, path, commit_sha.encode())
    except ObjectDoesNotExist:
        raise http.Http404()

    from mimetypes import guess_type
    content_type, _ = guess_type(path)

    return http.HttpResponse(data, content_type=content_type)
Exemple #2
0
    def __init__(self, request, course_identifier):
        # type: (http.HttpRequest, Text) -> None

        self.request = request
        self.course_identifier = course_identifier
        self._permissions_cache = None  # type: Optional[FrozenSet[Tuple[Text, Optional[Text]]]]  # noqa
        self._role_identifiers_cache = None  # type: Optional[List[Text]]
        self.old_language = None

        # using this to prevent nested using as context manager
        self._is_in_context_manager = False

        from course.models import Course  # noqa
        self.course = get_object_or_404(Course, identifier=course_identifier)

        from course.enrollment import get_participation_for_request
        self.participation = get_participation_for_request(
            request, self.course)

        from course.views import check_course_state
        check_course_state(self.course, self.participation)

        self.course_commit_sha = get_course_commit_sha(self.course,
                                                       self.participation)

        self.repo = get_course_repo(self.course)

        # logic duplicated in course.content.get_course_commit_sha
        sha = self.course.active_git_commit_sha.encode()

        if self.participation is not None:
            if self.participation.preview_git_commit_sha:
                preview_sha = self.participation.preview_git_commit_sha.encode(
                )

                with get_course_repo(self.course) as repo:
                    from relate.utils import SubdirRepoWrapper
                    if isinstance(repo, SubdirRepoWrapper):
                        true_repo = repo.repo
                    else:
                        true_repo = cast(dulwich.repo.Repo, repo)

                    try:
                        true_repo[preview_sha]
                    except KeyError:
                        from django.contrib import messages
                        messages.add_message(
                            request, messages.ERROR,
                            _("Preview revision '%s' does not exist--"
                              "showing active course content instead.") %
                            preview_sha.decode())

                        preview_sha = None
                    finally:
                        true_repo.close()

                if preview_sha is not None:
                    sha = preview_sha

        self.course_commit_sha = sha
Exemple #3
0
def get_current_repo_file(request, course_identifier, path):
    course = get_object_or_404(Course, identifier=course_identifier)
    role, participation = get_role_and_participation(
            request, course)

    from course.views import check_course_state
    check_course_state(course, role)

    from course.content import get_course_commit_sha
    commit_sha = get_course_commit_sha(course, participation)

    role, participation = get_role_and_participation(request, course)

    repo = get_course_repo(course)

    from course.content import is_repo_file_public
    if not is_repo_file_public(repo, commit_sha, path):
        raise PermissionDenied()

    from course.content import get_repo_blob_data_cached

    try:
        data = get_repo_blob_data_cached(
                repo, path, commit_sha.encode())
    except ObjectDoesNotExist:
        raise http.Http404()

    from mimetypes import guess_type
    content_type, _ = guess_type(path)

    return http.HttpResponse(data, content_type=content_type)
Exemple #4
0
def get_repo_file_backend(request, course, role, participation,
                          commit_sha, path):
    """
    Check if a file should be accessible.  Then call for it if
    the permission is not denied.

    Order is important here.  An in-exam request takes precedence.

    Note: an access_role of "public" is equal to "unenrolled"
    """

    # check to see if the course is hidden
    from course.views import check_course_state
    check_course_state(course, role)

    # retrieve local path for the repo for the course
    repo = get_course_repo(course)

    # set access to public (or unenrolled), student, etc
    access_kind = role
    if request.relate_exam_lockdown:
        access_kind = "in_exam"

    from course.content import is_repo_file_accessible_as
    if not is_repo_file_accessible_as(access_kind, repo, commit_sha, path):
        raise PermissionDenied()

    return get_repo_file_response(repo, path, commit_sha)
Exemple #5
0
    def __init__(self, request, course_identifier):
        # type: (http.HttpRequest, Text) -> None

        self.request = request
        self.course_identifier = course_identifier
        self._permissions_cache = None  # type: Optional[FrozenSet[Tuple[Text, Optional[Text]]]]  # noqa
        self._role_identifiers_cache = None  # type: Optional[List[Text]]
        self.old_language = None

        # using this to prevent nested using as context manager
        self._is_in_context_manager = False

        from course.models import Course  # noqa
        self.course = get_object_or_404(Course, identifier=course_identifier)

        from course.enrollment import get_participation_for_request
        self.participation = get_participation_for_request(
                request, self.course)

        from course.views import check_course_state
        check_course_state(self.course, self.participation)

        self.course_commit_sha = get_course_commit_sha(
                self.course, self.participation)

        self.repo = get_course_repo(self.course)

        # logic duplicated in course.content.get_course_commit_sha
        sha = self.course.active_git_commit_sha.encode()

        if self.participation is not None:
            if self.participation.preview_git_commit_sha:
                preview_sha = self.participation.preview_git_commit_sha.encode()

                with get_course_repo(self.course) as repo:
                    from relate.utils import SubdirRepoWrapper
                    if isinstance(repo, SubdirRepoWrapper):
                        true_repo = repo.repo
                    else:
                        true_repo = cast(dulwich.repo.Repo, repo)

                    try:
                        true_repo[preview_sha]
                    except KeyError:
                        from django.contrib import messages
                        messages.add_message(request, messages.ERROR,
                                _("Preview revision '%s' does not exist--"
                                "showing active course content instead.")
                                % preview_sha.decode())

                        preview_sha = None
                    finally:
                        true_repo.close()

                if preview_sha is not None:
                    sha = preview_sha

        self.course_commit_sha = sha
Exemple #6
0
def current_repo_file_etag_func(request, course_identifier, path):
    course = get_object_or_404(Course, identifier=course_identifier)
    role, participation = get_role_and_participation(request, course)

    from course.views import check_course_state
    check_course_state(course, role)

    from course.content import get_course_commit_sha
    commit_sha = get_course_commit_sha(course, participation)

    return ":".join([course_identifier, commit_sha, path])
Exemple #7
0
def current_repo_file_etag_func(request, course_identifier, path):
    course = get_object_or_404(Course, identifier=course_identifier)
    role, participation = get_role_and_participation(
            request, course)

    from course.views import check_course_state
    check_course_state(course, role)

    from course.content import get_course_commit_sha
    commit_sha = get_course_commit_sha(course, participation)

    return ":".join([course_identifier, commit_sha.decode(), path])
Exemple #8
0
def get_repo_file_backend(request, course, role, participation, commit_sha, path):
    from course.views import check_course_state
    check_course_state(course, role)

    repo = get_course_repo(course)

    access_kind = "public"
    if request.relate_exam_lockdown:
        access_kind = "in_exam"

    from course.content import is_repo_file_accessible_as
    if not is_repo_file_accessible_as(access_kind, repo, commit_sha, path):
        raise PermissionDenied()

    return get_repo_file_response(repo, path, commit_sha)
Exemple #9
0
    def __init__(self, request, course_identifier):
        self.request = request
        self.course_identifier = course_identifier

        self.course = get_object_or_404(Course, identifier=course_identifier)
        self.role, self.participation = get_role_and_participation(
                request, self.course)

        from course.views import check_course_state
        check_course_state(self.course, self.role)

        self.course_commit_sha = get_course_commit_sha(
                self.course, self.participation)

        self.repo = get_course_repo(self.course)
Exemple #10
0
    def __init__(self, request, course_identifier):
        self.request = request
        self.course_identifier = course_identifier

        self.course = get_object_or_404(Course, identifier=course_identifier)
        self.role, self.participation = get_role_and_participation(
            request, self.course)

        from course.views import check_course_state
        check_course_state(self.course, self.role)

        self.course_commit_sha = get_course_commit_sha(self.course,
                                                       self.participation)

        self.repo = get_course_repo(self.course)
Exemple #11
0
def get_repo_file_backend(request, course, role, participation, commit_sha,
                          path):
    from course.views import check_course_state
    check_course_state(course, role)

    repo = get_course_repo(course)

    access_kind = "public"
    if request.relate_exam_lockdown:
        access_kind = "in_exam"

    from course.content import is_repo_file_accessible_as
    if not is_repo_file_accessible_as(access_kind, repo, commit_sha, path):
        raise PermissionDenied()

    return get_repo_file_response(repo, path, commit_sha)
Exemple #12
0
    def __init__(self, request, course_identifier):
        self.request = request
        self.course_identifier = course_identifier

        from course.models import Course
        self.course = get_object_or_404(Course, identifier=course_identifier)

        from course.views import get_role_and_participation
        self.role, self.participation = get_role_and_participation(
                request, self.course)

        from course.views import check_course_state
        check_course_state(self.course, self.role)

        self.course_commit_sha = get_course_commit_sha(
                self.course, self.participation)

        self.repo = get_course_repo(self.course)

        # logic duplicated in course.content.get_course_commit_sha
        sha = self.course.active_git_commit_sha.encode()

        if (self.participation is not None
                and self.participation.preview_git_commit_sha):
            preview_sha = self.participation.preview_git_commit_sha.encode()

            repo = get_course_repo(self.course)

            from course.content import SubdirRepoWrapper
            if isinstance(repo, SubdirRepoWrapper):
                repo = repo.repo

            try:
                repo[preview_sha]
            except KeyError:
                from django.contrib import messages
                messages.add_message(request, messages.ERROR,
                        _("Preview revision '%s' does not exist--"
                        "showing active course content instead.")
                        % preview_sha.decode())

                preview_sha = None

            if preview_sha is not None:
                sha = preview_sha

        self.course_commit_sha = sha
Exemple #13
0
    def __init__(self, request, course_identifier):
        # type: (http.HttpRequest, Text) -> None

        self.request = request
        self.course_identifier = course_identifier
        self._permissions_cache = None  # type: Optional[FrozenSet[Tuple[Text, Optional[Text]]]]  # noqa
        self._role_identifiers_cache = None  # type: Optional[List[Text]]
        self.old_language = None

        # using this to prevent nested using as context manager
        self._is_in_context_manager = False

        from course.models import Course  # noqa
        self.course = get_object_or_404(Course, identifier=course_identifier)

        from course.enrollment import get_participation_for_request
        self.participation = get_participation_for_request(
            request, self.course)

        from course.views import check_course_state
        check_course_state(self.course, self.participation)

        self.repo = get_course_repo(self.course)

        try:
            sha = get_course_commit_sha(
                self.course,
                self.participation,
                repo=self.repo,
                raise_on_nonexistent_preview_commit=True)
        except CourseCommitSHADoesNotExist as e:
            from django.contrib import messages
            messages.add_message(request, messages.ERROR, str(e))

            sha = self.course.active_git_commit_sha.encode()

        self.course_commit_sha = sha
Exemple #14
0
    def __init__(self, request, course_identifier):
        # type: (http.HttpRequest, Text) -> None

        self.request = request
        self.course_identifier = course_identifier
        self._permissions_cache = None  # type: Optional[FrozenSet[Tuple[Text, Optional[Text]]]]  # noqa
        self._role_identifiers_cache = None  # type: Optional[List[Text]]
        self.old_language = None

        # using this to prevent nested using as context manager
        self._is_in_context_manager = False

        from course.models import Course  # noqa
        self.course = get_object_or_404(Course, identifier=course_identifier)

        from course.enrollment import get_participation_for_request
        self.participation = get_participation_for_request(
                request, self.course)

        from course.views import check_course_state
        check_course_state(self.course, self.participation)

        self.repo = get_course_repo(self.course)

        try:
            sha = get_course_commit_sha(
                self.course, self.participation,
                repo=self.repo,
                raise_on_nonexistent_preview_commit=True)
        except CourseCommitSHADoesNotExist as e:
            from django.contrib import messages
            messages.add_message(request, messages.ERROR, str(e))

            sha = self.course.active_git_commit_sha.encode()

        self.course_commit_sha = sha