Skip to content

Commit

Permalink
Remove obviated rest_framework code
Browse files Browse the repository at this point in the history
  • Loading branch information
zachmullen committed Aug 21, 2023
1 parent f8d7eb4 commit 76326a4
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 292 deletions.
21 changes: 0 additions & 21 deletions isic/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from django.test.client import Client
import pytest
from pytest_factoryboy import register
from rest_framework.test import APIClient

from isic.core.tests.factories import CollectionFactory, ImageFactory
from isic.ingest.tests.factories import (
Expand Down Expand Up @@ -38,12 +37,6 @@ def setup_groups(request):
public.user_set.set(User.objects.all())


@pytest.fixture
def api_client() -> APIClient:
# TODO have this return a django.test.Client instead of DRF's APIClient
return APIClient()


@pytest.fixture
def client() -> Client:
return Client()
Expand All @@ -58,13 +51,6 @@ def authenticated_client(user):
return client


@pytest.fixture
def authenticated_api_client(user) -> APIClient:
api_client = APIClient()
api_client.force_authenticate(user=user)
return api_client


@pytest.fixture
def staff_user(user_factory):
return user_factory(is_staff=True)
Expand All @@ -77,13 +63,6 @@ def staff_client(staff_user):
return client


@pytest.fixture
def staff_api_client(staff_user) -> APIClient:
api_client = APIClient()
api_client.force_authenticate(user=staff_user)
return api_client


@pytest.fixture
def eager_celery(settings):
settings.CELERY_TASK_ALWAYS_EAGER = True
Expand Down
7 changes: 0 additions & 7 deletions isic/core/api/exceptions.py

This file was deleted.

27 changes: 0 additions & 27 deletions isic/core/pagination.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from base64 import b64decode, b64encode
from collections import OrderedDict
from dataclasses import dataclass
from typing import Any
from urllib import parse
Expand All @@ -8,32 +7,6 @@
from django.http.request import HttpRequest
from ninja import Field, Schema
from ninja.pagination import PaginationBase
from rest_framework.pagination import CursorPagination
from rest_framework.response import Response


class CursorWithCountPagination(CursorPagination):
page_size_query_param = "limit"
max_page_size = 100

def paginate_queryset(self, queryset, request, view=None):
self.count = queryset.count()
return super().paginate_queryset(queryset, request, view)

def get_paginated_response(self, data):
return Response(
OrderedDict(
[
("count", self.count),
("next", self.get_next_link()),
("previous", self.get_previous_link()),
("results", data),
]
)
)


##################


@dataclass
Expand Down
22 changes: 6 additions & 16 deletions isic/core/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from django.shortcuts import get_object_or_404, resolve_url
from django.utils.functional import wraps
from ninja.security.session import SessionAuth
from rest_framework.filters import BaseFilterBackend


class SessionAuthStaffUser(SessionAuth):
Expand All @@ -25,6 +24,12 @@ def authenticate(self, request: HttpRequest, key: str | None) -> User | None:
return None


class IsicObjectPermissionsBackend(BaseBackend):
def has_perm(self, user_obj, perm, obj=None):
if ISIC_PERMS_MAP.get(perm):
return ISIC_PERMS_MAP[perm](user_obj, obj)


class UserPermissions:
model = User
perms = ["view_staff"]
Expand Down Expand Up @@ -52,21 +57,6 @@ def view_staff(user_obj, obj=None):
)


class IsicObjectPermissionsBackend(BaseBackend):
def has_perm(self, user_obj, perm, obj=None):
if ISIC_PERMS_MAP.get(perm):
return ISIC_PERMS_MAP[perm](user_obj, obj)


class IsicObjectPermissionsFilter(BaseFilterBackend):
def filter_queryset(self, request, queryset, view):
return get_visible_objects(
request.user,
f"{queryset.model._meta.app_label}.view_{queryset.model._meta.model_name}",
queryset,
)


def get_visible_objects(user, perm, qs=None):
filter = ISIC_FILTERS_MAP.get(perm)

Expand Down
28 changes: 0 additions & 28 deletions isic/core/tests/test_serializers.py

This file was deleted.

80 changes: 0 additions & 80 deletions isic/ingest/serializers.py

This file was deleted.

16 changes: 0 additions & 16 deletions isic/login/views.py

This file was deleted.

37 changes: 0 additions & 37 deletions isic/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,6 @@
TestingBaseConfiguration,
)
from configurations import values
from django.core.exceptions import PermissionDenied, ValidationError as DjangoValidationError
from django.http import Http404
from rest_framework import exceptions
from rest_framework.serializers import as_serializer_error


def drf_default_with_modifications_exception_handler(exc, ctx):
# TODO: importing this at the top level causes weird errors in test cases with list
# endpoints.
from rest_framework.views import exception_handler

if isinstance(exc, DjangoValidationError):
exc = exceptions.ValidationError(as_serializer_error(exc))

if isinstance(exc, Http404):
exc = exceptions.NotFound()

if isinstance(exc, PermissionDenied):
exc = exceptions.PermissionDenied()

response = exception_handler(exc, ctx)

# If unexpected error occurs (server error, etc.)
if response is None:
return response

if isinstance(exc.detail, (list, dict)):
response.data = {"detail": response.data}

return response


def _oauth2_pkce_required(client_id):
Expand Down Expand Up @@ -117,13 +87,6 @@ def mutate_configuration(configuration: ComposedConfiguration) -> None:
"isic.core.context_processors.placeholder_images",
]

configuration.REST_FRAMEWORK[
"EXCEPTION_HANDLER"
] = "isic.settings.drf_default_with_modifications_exception_handler"
configuration.REST_FRAMEWORK[
"DEFAULT_PAGINATION_CLASS"
] = "isic.core.pagination.CursorWithCountPagination"

AUTHENTICATION_BACKENDS = [
"allauth.account.auth_backends.AuthenticationBackend",
"isic.core.permissions.IsicObjectPermissionsBackend",
Expand Down
44 changes: 0 additions & 44 deletions isic/studies/serializers.py

This file was deleted.

Loading

0 comments on commit 76326a4

Please sign in to comment.