Skip to content

Commit

Permalink
Move authentication check into star service functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jjnesbitt committed Jan 3, 2025
1 parent 380a7eb commit fa9eb7f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
10 changes: 7 additions & 3 deletions dandiapi/api/services/dandiset/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
from dandiapi.api.services import audit
from dandiapi.api.services.dandiset.exceptions import DandisetAlreadyExistsError
from dandiapi.api.services.embargo.exceptions import DandisetUnembargoInProgressError
from dandiapi.api.services.exceptions import AdminOnlyOperationError, NotAllowedError
from dandiapi.api.services.exceptions import (
AdminOnlyOperationError,
NotAllowedError,
NotAuthenticatedError,
)
from dandiapi.api.services.version.metadata import _normalize_version_metadata


Expand Down Expand Up @@ -88,7 +92,7 @@ def star_dandiset(*, user, dandiset: Dandiset) -> int:
The new star count for the Dandiset.
"""
if not user.is_authenticated:
return dandiset.star_count
raise NotAuthenticatedError

with transaction.atomic():
DandisetStar.objects.get_or_create(user=user, dandiset=dandiset)
Expand All @@ -110,7 +114,7 @@ def unstar_dandiset(*, user, dandiset: Dandiset) -> int:
The new star count for the Dandiset.
"""
if not user.is_authenticated:
return dandiset.star_count
raise NotAuthenticatedError

with transaction.atomic():
DandisetStar.objects.filter(user=user, dandiset=dandiset).delete()
Expand Down
4 changes: 0 additions & 4 deletions dandiapi/api/views/dandiset.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,8 +582,6 @@ def clear_uploads(self, request, dandiset__pk):
)
@action(methods=['POST'], detail=True)
def star(self, request, dandiset__pk) -> Response:
if not request.user.is_authenticated:
raise NotAuthenticated
dandiset = self.get_object()
star_count = star_dandiset(user=request.user, dandiset=dandiset)
return Response({'count': star_count}, status=status.HTTP_200_OK)
Expand All @@ -601,8 +599,6 @@ def star(self, request, dandiset__pk) -> Response:
)
@action(methods=['POST'], detail=True)
def unstar(self, request, dandiset__pk) -> Response:
if not request.user.is_authenticated:
raise NotAuthenticated
dandiset = self.get_object()
star_count = unstar_dandiset(user=request.user, dandiset=dandiset)
return Response({'count': star_count}, status=status.HTTP_200_OK)

0 comments on commit fa9eb7f

Please sign in to comment.