Skip to content

Commit

Permalink
Merge pull request #6125 from stsewd/stsewd/refactor-footer-html-view
Browse files Browse the repository at this point in the history
 Refactor footer_html view to class
  • Loading branch information
stsewd authored Aug 30, 2019
2 parents 00ab116 + 9c5cf6f commit 34d1a15
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 111 deletions.
2 changes: 1 addition & 1 deletion readthedocs/api/v2/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@

function_urls = [
url(r'docurl/', core_views.docurl, name='docurl'),
url(r'footer_html/', footer_views.footer_html, name='footer_html'),
url(r'footer_html/', footer_views.FooterHTML.as_view(), name='footer_html'),
]

task_urls = [
Expand Down
258 changes: 151 additions & 107 deletions readthedocs/api/v2/views/footer_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
from django.conf import settings
from django.shortcuts import get_object_or_404
from django.template import loader as template_loader
from rest_framework import decorators, permissions
from rest_framework.permissions import AllowAny
from rest_framework.renderers import JSONRenderer
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework_jsonp.renderers import JSONPRenderer

from readthedocs.api.v2.signals import footer_response
Expand Down Expand Up @@ -63,113 +64,156 @@ def get_version_compare_data(project, base_version=None):
return ret_val


@decorators.api_view(['GET'])
@decorators.permission_classes((permissions.AllowAny,))
@decorators.renderer_classes((JSONRenderer, JSONPRenderer))
def footer_html(request):
"""Render and return footer markup."""
# TODO refactor this function
# pylint: disable=too-many-locals
project_slug = request.GET.get('project', None)
version_slug = request.GET.get('version', None)
page_slug = request.GET.get('page', '')
theme = request.GET.get('theme', False)
docroot = request.GET.get('docroot', '')
subproject = request.GET.get('subproject', False)
source_suffix = request.GET.get('source_suffix', '.rst')

# Hack in a fix for missing version slug deploy that went out a while back
if version_slug == '':
version_slug = LATEST

new_theme = (theme == 'sphinx_rtd_theme')
using_theme = (theme == 'default')
project = get_object_or_404(Project, slug=project_slug)
version = get_object_or_404(
Version.objects.public(
request.user,
project=project,
only_active=False,
),
slug__iexact=version_slug,
)
main_project = project.main_language_project or project
class FooterHTML(APIView):

if page_slug and page_slug != 'index':
if main_project.documentation_type == 'sphinx_htmldir':
path = page_slug + '/'
else:
path = page_slug + '.html'
else:
path = ''

version_compare_data = get_version_compare_data(project, version)

context = {
'project': project,
'version': version,
'path': path,
'downloads': version.get_downloads(pretty=True),
'current_version': version.verbose_name,
'versions': project.ordered_active_versions(user=request.user),
'main_project': main_project,
'translations': main_project.translations.all(),
'current_language': project.language,
'using_theme': using_theme,
'new_theme': new_theme,
'settings': settings,
'subproject': subproject,
'github_edit_url': version.get_github_url(
docroot,
page_slug,
source_suffix,
'edit',
),
'github_view_url': version.get_github_url(
docroot,
page_slug,
source_suffix,
'view',
),
'gitlab_edit_url': version.get_gitlab_url(
docroot,
page_slug,
source_suffix,
'edit',
),
'gitlab_view_url': version.get_gitlab_url(
docroot,
page_slug,
source_suffix,
'view',
),
'bitbucket_url': version.get_bitbucket_url(
docroot,
page_slug,
source_suffix,
),
'theme': theme,
}
"""
Render and return footer markup.
html = template_loader.get_template('restapi/footer.html').render(
context,
request,
)
resp_data = {
'html': html,
'show_version_warning': project.show_version_warning,
'version_active': version.active,
'version_compare': version_compare_data,
'version_supported': version.supported,
}
.. note::
# Allow folks to hook onto the footer response for various information
# collection, or to modify the resp_data.
footer_response.send(
sender=None,
request=request,
context=context,
resp_data=resp_data,
)
The methods `_get_project` and `_get_version`
are called many times, so a basic cache is implemented.
"""

http_method_names = ['get']
permission_classes = [AllowAny]
renderer_classes = [JSONRenderer, JSONPRenderer]

def _get_project(self):
cache_key = '_cached_project'
project = getattr(self, cache_key, None)

if not project:
project_slug = self.request.GET.get('project', None)
project = get_object_or_404(Project, slug=project_slug)
setattr(self, cache_key, project)

return project

return Response(resp_data)
def _get_version(self):
cache_key = '_cached_version'
version = getattr(self, cache_key, None)

if not version:
version_slug = self.request.GET.get('version', None)

# Hack in a fix for missing version slug deploy
# that went out a while back
if version_slug == '':
version_slug = LATEST

version = get_object_or_404(
Version.objects.public(
user=self.request.user,
project=self._get_project(),
only_active=False,
),
slug__iexact=version_slug,
)
setattr(self, cache_key, version)

return version

def _get_context(self):
theme = self.request.GET.get('theme', False)
docroot = self.request.GET.get('docroot', '')
subproject = self.request.GET.get('subproject', False)
source_suffix = self.request.GET.get('source_suffix', '.rst')

new_theme = (theme == 'sphinx_rtd_theme')
using_theme = (theme == 'default')

project = self._get_project()
main_project = project.main_language_project or project
version = self._get_version()

page_slug = self.request.GET.get('page', '')
if page_slug and page_slug != 'index':
if main_project.documentation_type == 'sphinx_htmldir':
path = page_slug + '/'
else:
path = page_slug + '.html'
else:
path = ''

context = {
'project': project,
'version': version,
'path': path,
'downloads': version.get_downloads(pretty=True),
'current_version': version.verbose_name,
'versions': project.ordered_active_versions(
user=self.request.user,
),
'main_project': main_project,
'translations': main_project.translations.all(),
'current_language': project.language,
'using_theme': using_theme,
'new_theme': new_theme,
'settings': settings,
'subproject': subproject,
'github_edit_url': version.get_github_url(
docroot,
page_slug,
source_suffix,
'edit',
),
'github_view_url': version.get_github_url(
docroot,
page_slug,
source_suffix,
'view',
),
'gitlab_edit_url': version.get_gitlab_url(
docroot,
page_slug,
source_suffix,
'edit',
),
'gitlab_view_url': version.get_gitlab_url(
docroot,
page_slug,
source_suffix,
'view',
),
'bitbucket_url': version.get_bitbucket_url(
docroot,
page_slug,
source_suffix,
),
'theme': theme,
}
return context

def get(self, request, format=None):
project = self._get_project()
version = self._get_version()
version_compare_data = get_version_compare_data(
project,
version,
)

context = self._get_context()
html = template_loader.get_template('restapi/footer.html').render(
context,
request,
)

resp_data = {
'html': html,
'show_version_warning': project.show_version_warning,
'version_active': version.active,
'version_compare': version_compare_data,
'version_supported': version.supported,
}

# Allow folks to hook onto the footer response for various information
# collection, or to modify the resp_data.
footer_response.send(
sender=None,
request=request,
context=context,
resp_data=resp_data,
)

return Response(resp_data)
6 changes: 3 additions & 3 deletions readthedocs/rtd_tests/tests/test_footer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from rest_framework.test import APIRequestFactory, APITestCase

from readthedocs.api.v2.views.footer_views import (
footer_html,
FooterHTML,
get_version_compare_data,
)
from readthedocs.builds.constants import BRANCH, LATEST, TAG
Expand All @@ -26,7 +26,7 @@ def setUpTestData(cls):

def render(self):
request = self.factory.get(self.url)
response = footer_html(request)
response = FooterHTML.as_view()(request)
response.render()
return response

Expand Down Expand Up @@ -243,7 +243,7 @@ def setUp(self):

def render(self):
request = self.factory.get(self.url)
response = footer_html(request)
response = FooterHTML.as_view()(request)
response.render()
return response

Expand Down

0 comments on commit 34d1a15

Please sign in to comment.