Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions readthedocs/api/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ class CDNCacheTagsMixin:
Add cache tags for project and version to the response of this view.

The view inheriting this mixin should implement the
`self._get_project` and `self._get_version` methods.
`self._get_project` and `self._get_version` methods,
or set `self._cache_tags` to a list of cache tags if the project/version is
not a class/instance variable.

If `self._get_version` returns `None`,
only the project level tags are added.
Expand All @@ -30,7 +32,7 @@ class CDNCacheTagsMixin:

def dispatch(self, request, *args, **kwargs):
response = super().dispatch(request, *args, **kwargs)
cache_tags = self._get_cache_tags()
cache_tags = getattr(self, "_cache_tags", self._get_cache_tags())
if cache_tags:
add_cache_tags(response, cache_tags)
return response
Expand Down
10 changes: 9 additions & 1 deletion readthedocs/projects/views/public.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from django.views.generic import ListView
from taggit.models import Tag

from readthedocs.api.mixins import CDNCacheTagsMixin
from readthedocs.builds.constants import BUILD_STATE_FINISHED
from readthedocs.builds.constants import EXTERNAL
from readthedocs.builds.constants import INTERNAL
Expand All @@ -32,6 +33,7 @@
from readthedocs.core.mixins import CDNCacheControlMixin
from readthedocs.core.permissions import AdminPermission
from readthedocs.core.resolver import Resolver
from readthedocs.core.utils import get_cache_tag
from readthedocs.core.utils.extend import SettingsOverrideObject
from readthedocs.notifications.models import Notification
from readthedocs.projects.filters import ProjectVersionListFilterSet
Expand Down Expand Up @@ -299,7 +301,7 @@ def verify_project_token(cls, token, project_slug):
project_badge = never_cache(ProjectBadgeView.as_view())


class ProjectDownloadMediaBase(CDNCacheControlMixin, ServeDocsMixin, View):
class ProjectDownloadMediaBase(CDNCacheControlMixin, CDNCacheTagsMixin, ServeDocsMixin, View):
# Use new-style URLs (same domain as docs) or old-style URLs (dashboard URL)
same_domain_url = False

Expand Down Expand Up @@ -382,6 +384,12 @@ def get(
slug=version_slug,
)

# For cache tag mixin
self._cache_tags = [
project.slug,
get_cache_tag(project.slug, version.slug),
]

return self._serve_dowload(
request=request,
project=version.project,
Expand Down
3 changes: 3 additions & 0 deletions readthedocs/proxito/tests/test_full.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,7 @@ def test_download_files_public_version(self):
headers={"host": "project.dev.readthedocs.io"},
)
self.assertEqual(resp.status_code, 200)
self.assertEqual(resp["Cache-Tag"], "project,project:latest")
extension = "zip" if type_ == MEDIA_TYPE_HTMLZIP else type_
self.assertEqual(
resp["X-Accel-Redirect"],
Expand All @@ -560,6 +561,7 @@ def test_download_files_public_version(self):
headers={"host": "project.dev.readthedocs.io"},
)
self.assertEqual(resp.status_code, 200)
self.assertEqual(resp["Cache-Tag"], "translation,translation:latest")
extension = "zip" if type_ == MEDIA_TYPE_HTMLZIP else type_
self.assertEqual(
resp["X-Accel-Redirect"],
Expand Down Expand Up @@ -604,6 +606,7 @@ def test_download_files_private_version(self):
headers={"host": "project.dev.readthedocs.io"},
)
self.assertEqual(resp.status_code, 200)
self.assertEqual(resp["Cache-Tag"], "project,project:latest")
extension = "zip" if type_ == MEDIA_TYPE_HTMLZIP else type_
self.assertEqual(
resp["X-Accel-Redirect"],
Expand Down