diff --git a/kepi/trilby_api/tests/test_status.py b/kepi/trilby_api/tests/test_status.py index 24d2688..a1ce455 100644 --- a/kepi/trilby_api/tests/test_status.py +++ b/kepi/trilby_api/tests/test_status.py @@ -22,7 +22,7 @@ class TestStatus(TrilbyTestCase): content = self.get( path = '/api/v1/statuses/'+str(self._alice.id), as_user = self._alice, - )[0] + ) for field, expected in STATUS_EXPECTED.items(): self.assertIn(field, content) @@ -616,36 +616,26 @@ class TestGetStatus(TrilbyTestCase): content = 'Daisies are our silver.', ) - c = APIClient() - c.force_authenticate(self._alice.local_user) - - result = c.get( + details = self.get( '/api/v1/statuses/{}'.format( self._alice_status.id, ), + as_user = self._alice, + expect_result = 200, ) - self.assertEqual(result.status_code, - 200) - - try: - details = json.loads(result.content.decode('UTF-8')) - except JSON.decoder.JSONDecodeError: - self.fail("Response was not JSON") - return - self.assertEqual( - details[0]['id'], + details['id'], str(self._alice_status.id), ) self.assertEqual( - details[0]['account']['username'], + details['account']['username'], 'alice', ) self.assertEqual( - details[0]['content'], + details['content'], '

Daisies are our silver.

', ) diff --git a/kepi/trilby_api/urls.py b/kepi/trilby_api/urls.py index 8e65dde..29098a4 100644 --- a/kepi/trilby_api/urls.py +++ b/kepi/trilby_api/urls.py @@ -13,26 +13,26 @@ urlpatterns = [ path('api/v1/accounts/search', AccountsSearch.as_view()), - path('api/v1/accounts/', User.as_view()), - path('api/v1/accounts//statuses', Statuses.as_view()), - path('api/v1/accounts//following', Following.as_view()), - path('api/v1/accounts//followers', Followers.as_view()), - path('api/v1/accounts//follow', Follow.as_view()), - path('api/v1/accounts//unfollow', Unfollow.as_view()), + path('api/v1/accounts/', User.as_view()), + path('api/v1/accounts//statuses', Statuses.as_view()), + path('api/v1/accounts//following', Following.as_view()), + path('api/v1/accounts//followers', Followers.as_view()), + path('api/v1/accounts//follow', Follow.as_view()), + path('api/v1/accounts//unfollow', Unfollow.as_view()), path('api/v1/statuses', Statuses.as_view()), - path('api/v1/statuses/', Statuses.as_view()), - path('api/v1/statuses//context', StatusContext.as_view()), + path('api/v1/statuses/', SpecificStatus.as_view()), + path('api/v1/statuses//context', StatusContext.as_view()), # Favourite, aka like - path('api/v1/statuses//favourite', Favourite.as_view()), - path('api/v1/statuses//unfavourite', Unfavourite.as_view()), - path('api/v1/statuses//favourited_by', StatusFavouritedBy.as_view()), + path('api/v1/statuses//favourite', Favourite.as_view()), + path('api/v1/statuses//unfavourite', Unfavourite.as_view()), + path('api/v1/statuses//favourited_by', StatusFavouritedBy.as_view()), # Reblog, aka boost - path('api/v1/statuses//reblog', Reblog.as_view()), - path('api/v1/statuses//unreblog', Unreblog.as_view()), - path('api/v1/statuses//reblogged_by', StatusRebloggedBy.as_view()), + path('api/v1/statuses//reblog', Reblog.as_view()), + path('api/v1/statuses//unreblog', Unreblog.as_view()), + path('api/v1/statuses//reblogged_by', StatusRebloggedBy.as_view()), path('api/v1/notifications', Notifications.as_view()), path('api/v1/filters', Filters.as_view()), diff --git a/kepi/trilby_api/views.py b/kepi/trilby_api/views.py index 36c2025..b7352e0 100644 --- a/kepi/trilby_api/views.py +++ b/kepi/trilby_api/views.py @@ -13,7 +13,7 @@ import kepi.trilby_api.models as trilby_models import kepi.trilby_api.utils as trilby_utils from .serializers import * import kepi.trilby_api.signals as kepi_signals -from rest_framework import generics, response +from rest_framework import generics, response, mixins from rest_framework.permissions import IsAuthenticated, \ IsAuthenticatedOrReadOnly from rest_framework.response import Response @@ -74,7 +74,7 @@ class DoSomethingWithStatus(generics.GenericAPIView): try: the_status = get_object_or_404( self.get_queryset(), - id = int(kwargs['id']), + id = int(kwargs['status']), ) except ValueError: return error_response(404, 'Non-decimal ID') @@ -212,7 +212,7 @@ class DoSomethingWithPerson(generics.GenericAPIView): try: the_person = get_object_or_404( self.get_queryset(), - id = int(kwargs['id']), + id = int(kwargs['user']), ) except ValueError: return error_response(404, 'Non-decimal ID') @@ -427,7 +427,7 @@ class User(generics.GenericAPIView): try: whoever = get_object_or_404( self.get_queryset(), - id = int(kwargs['id']), + id = int(kwargs['user']), ) except ValueError: return error_response(404, 'Non-decimal ID') @@ -435,9 +435,59 @@ class User(generics.GenericAPIView): serializer = UserSerializer(whoever) return JsonResponse(serializer.data) +class SpecificStatus(generics.GenericAPIView): + + queryset = trilby_models.Status.objects.filter(remote_url=None) + serializer_class = StatusSerializer + lookup_field = 'status' + permission_classes = (IsAuthenticatedOrReadOnly,) + + def get(self, request, *args, **kwargs): + + the_status = get_object_or_404( + self.get_queryset(), + id = int(kwargs['status']), + ) + + serializer = StatusSerializer( + the_status, + context = { + 'request': request, + }, + ) + + response = JsonResponse(serializer.data) + + return response + + def delete(self, request, *args, **kwargs): + + if 'status' not in kwargs: + return error_response(404, 'Can\'t delete all statuses at once') + + the_status = get_object_or_404( + self.get_queryset(), + id = int(kwargs['status']), + ) + + if the_status.account != request.user.person: + return error_response(404, # sic + 'That isn\'t yours to delete') + + serializer = StatusSerializer( + the_status, + context = { + 'request': request, + }, + ) + + response = JsonResponse(serializer.data) + + the_status.delete() + + return response + class Statuses(generics.ListCreateAPIView, - generics.CreateAPIView, - generics.DestroyAPIView, ): queryset = trilby_models.Status.objects.filter(remote_url=None) @@ -452,7 +502,7 @@ class Statuses(generics.ListCreateAPIView, try: the_person = get_object_or_404( trilby_models.Person, - id = int(kwargs['id']), + id = int(kwargs['user']), ) except ValueError: return error_response(404, 'Non-decimal ID') @@ -515,32 +565,6 @@ class Statuses(generics.ListCreateAPIView, reason = 'Hot off the press', ) - def delete(self, request, *args, **kwargs): - - if 'id' not in kwargs: - return error_response(404, 'Can\'t delete all statuses at once') - - the_status = get_object_or_404( - self.get_queryset(), - id = int(kwargs['id']), - ) - - if the_status.account != request.user.person: - return error_response(404, # sic - 'That isn\'t yours to delete') - - serializer = StatusSerializer( - the_status, - context = { - 'request': request, - }, - ) - - response = JsonResponse(serializer.data) - - the_status.delete() - - return response class StatusContext(generics.ListCreateAPIView): @@ -550,7 +574,7 @@ class StatusContext(generics.ListCreateAPIView): queryset = self.get_queryset() - status = queryset.get(id=int(kwargs['id'])) + status = queryset.get(id=int(kwargs['status'])) serializer = StatusContextSerializer(status) return JsonResponse(serializer.data) @@ -563,7 +587,7 @@ class StatusFavouritedBy(generics.ListCreateAPIView): queryset = self.get_queryset() - status = trilby_models.Status.objects.get(id=int(kwargs['id'])) + status = trilby_models.Status.objects.get(id=int(kwargs['status'])) status.save() @@ -586,7 +610,7 @@ class StatusRebloggedBy(generics.ListCreateAPIView): queryset = self.get_queryset() - status = trilby_models.Status.objects.get(id=int(kwargs['id'])) + status = trilby_models.Status.objects.get(id=int(kwargs['status'])) people = queryset.filter( poster__reblog_of = status, @@ -684,7 +708,7 @@ class UserFeed(View): try: the_person = get_object_or_404( self.get_queryset(), - id = int(kwargs['id']), + id = int(kwargs['user']), ) except ValueError: return error_response(404, 'Non-decimal ID') @@ -782,7 +806,7 @@ class Followers_or_Following(generics.GenericAPIView): try: the_person = get_object_or_404( self.get_queryset(), - id = int(kwargs['id']), + id = int(kwargs['user']), ) except ValueError: return error_response(404, 'Non-decimal ID')