Skip to content

Commit 3689847

Browse files
committed
Use ServeDocsPermissionsMixin to isolate this methods and be more clear
1 parent 5f22355 commit 3689847

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

readthedocs/proxito/views/mixins.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,37 @@ def get_redirect_response(self, request, redirect_path, http_status):
154154
return HttpResponsePermanentRedirect(new_path)
155155

156156
return HttpResponseRedirect(new_path)
157+
158+
159+
class ServeDocPermissionsMixin:
160+
"""
161+
Mixin to handle documentation permissions.
162+
163+
To use this mixin, the method that serve the docs should call
164+
``allowed_user`` first to check if the user requesting to read the page is
165+
allowed. If allowed, the flow should continue.
166+
167+
On the other hand, if the user is not allowed, the serve docs method MUST
168+
break the flow and return a call to ``get_unauthed_response``.
169+
"""
170+
171+
def allowed_user(self, request, project, version_slug):
172+
"""
173+
Return whether the user is allowed to read this documentation's version.
174+
175+
:returns: ``True`` if allowed. ``False`` otherwise.
176+
:rtype: bool
177+
"""
178+
return False
179+
180+
def get_unauthed_response(self, request, project):
181+
"""
182+
Return response for an unauthed hit at a documentation's version.
183+
184+
When a user does not have access to read documentation, we need to
185+
return a response showing the proper message. This method produces that
186+
reponse.
187+
188+
:rtype: django.http.HttpResponse
189+
"""
190+
return self._serve_401(request, project)

readthedocs/proxito/views/serve.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020
from readthedocs.projects import constants
2121
from readthedocs.projects.templatetags.projects_tags import sort_version_aware
2222

23-
from .mixins import ServeDocsMixin, ServeRedirectMixin
23+
from .mixins import (
24+
ServeDocsMixin,
25+
ServeRedirectMixin,
26+
ServeDocsPermissionsMixin,
27+
)
2428

2529
from .decorators import map_project_slug
2630
from .redirects import redirect_project_slug
@@ -30,7 +34,12 @@
3034
log = logging.getLogger(__name__) # noqa
3135

3236

33-
class ServeDocsBase(ServeRedirectMixin, ServeDocsMixin, View):
37+
class ServeDocsBase(
38+
ServeRedirectMixin,
39+
ServeDocsPermissionsMixin,
40+
ServeDocsMixin,
41+
View,
42+
):
3443

3544
def get(self,
3645
request,
@@ -108,7 +117,7 @@ def get(self,
108117
path=path,
109118
)
110119

111-
def allowed_user(self, *args, **kwargs):
120+
def allowed_user(self, *args, **kwargs): # noqa
112121
return True
113122

114123

0 commit comments

Comments
 (0)