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 28, 2025
1 parent 75e51a1 commit 2f82708
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 @@ -7,7 +7,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.permissions.dandiset import add_dandiset_owner, is_dandiset_owner
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

DandisetStar.objects.get_or_create(user=user, dandiset=dandiset)
return dandiset.star_count
Expand All @@ -106,7 +110,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

DandisetStar.objects.filter(user=user, dandiset=dandiset).delete()
return dandiset.star_count
4 changes: 0 additions & 4 deletions dandiapi/api/views/dandiset.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,8 +583,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 @@ -602,8 +600,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 2f82708

Please sign in to comment.