Skip to content

Commit

Permalink
Add AllowAllCORSMixin
Browse files Browse the repository at this point in the history
Add AllowAllCORSMixin and apply it to legacy profile retrieval API and
package version APIs
  • Loading branch information
x753 committed Feb 14, 2025
1 parent eda0cbf commit f858125
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
9 changes: 9 additions & 0 deletions django/thunderstore/core/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,12 @@ def get_admin_url(self):

class Meta:
abstract = True


# AllowAllCORSMixin must be inherited before APIView / GenericAPIView
class AllowAllCORSMixin:
def finalize_response(self, request, response, *args, **kwargs):
response = super().finalize_response(request, response, *args, **kwargs)
response["Access-Control-Allow-Origin"] = "*"
response["Access-Control-Allow-Methods"] = "GET"
return response
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from rest_framework.throttling import UserRateThrottle
from rest_framework.views import APIView

from thunderstore.core.mixins import AllowAllCORSMixin
from thunderstore.core.utils import replace_cdn
from thunderstore.modpacks.models import LegacyProfile

Expand Down Expand Up @@ -53,7 +54,7 @@ def post(self, request, *args, **kwargs):
)


class LegacyProfileRetrieveApiView(APIView):
class LegacyProfileRetrieveApiView(AllowAllCORSMixin, APIView):
permission_classes = []

@swagger_auto_schema(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from thunderstore.cache.cache import ManualCacheCommunityMixin
from thunderstore.cache.enums import CacheBustCondition
from thunderstore.core.mixins import AllowAllCORSMixin
from thunderstore.repository.api.experimental.serializers import (
PackageSerializerExperimental,
)
Expand Down Expand Up @@ -64,7 +65,9 @@ def get_queryset(self):
return get_package_queryset()


class PackageDetailApiView(ManualCacheCommunityMixin, RetrieveAPIView):
class PackageDetailApiView(
AllowAllCORSMixin, ManualCacheCommunityMixin, RetrieveAPIView
):
"""
Get a single package
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from thunderstore.cache.cache import ManualCacheCommunityMixin
from thunderstore.cache.enums import CacheBustCondition
from thunderstore.core.mixins import AllowAllCORSMixin
from thunderstore.repository.api.experimental.serializers import (
MarkdownResponseSerializer,
PackageVersionSerializerExperimental,
Expand Down Expand Up @@ -54,7 +55,7 @@ def get_queryset(self):
)


class PackageVersionDetailApiView(PackageVersionDetailMixin):
class PackageVersionDetailApiView(AllowAllCORSMixin, PackageVersionDetailMixin):
"""
Get a single package version
"""
Expand All @@ -69,7 +70,7 @@ def get(self, *args, **kwargs):
return super().get(*args, **kwargs)


class PackageVersionChangelogApiView(PackageVersionDetailMixin):
class PackageVersionChangelogApiView(AllowAllCORSMixin, PackageVersionDetailMixin):
"""
Get a package verion's changelog
"""
Expand All @@ -89,7 +90,7 @@ def retrieve(self, request, *args, **kwargs):
return Response(serializer.data)


class PackageVersionReadmeApiView(PackageVersionDetailMixin):
class PackageVersionReadmeApiView(AllowAllCORSMixin, PackageVersionDetailMixin):
"""
Get a package verion's readme
"""
Expand Down

0 comments on commit f858125

Please sign in to comment.