Skip to content

Commit 60b121f

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

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

readthedocs/proxito/views/mixins.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,38 @@ 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 ServeDocsPermissionsMixin:
160+
161+
"""
162+
Mixin to handle documentation permissions.
163+
164+
To use this mixin, the method that serve the docs should call
165+
``allowed_user`` first to check if the user requesting to read the page is
166+
allowed. If allowed, the flow should continue.
167+
168+
On the other hand, if the user is not allowed, the serve docs method MUST
169+
break the flow and return a call to ``get_unauthed_response``.
170+
"""
171+
172+
def allowed_user(self, request, project, version_slug):
173+
"""
174+
Return whether the user is allowed to read this documentation's version.
175+
176+
:returns: ``True`` if allowed. ``False`` otherwise.
177+
:rtype: bool
178+
"""
179+
return False
180+
181+
def get_unauthed_response(self, request, project):
182+
"""
183+
Return response for an unauthed hit at a documentation's version.
184+
185+
When a user does not have access to read documentation, we need to
186+
return a response showing the proper message. This method produces that
187+
reponse.
188+
189+
:rtype: django.http.HttpResponse
190+
"""
191+
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)