11import logging
2+ from typing import Generic , Type , TypeVar
23
34from common .environments .permissions import (
45 TAG_SUPPORTED_PERMISSIONS ,
56 VIEW_ENVIRONMENT ,
67)
7- from django .db .models import Count , Q
8+ from django .db .models import Count , Q , QuerySet
89from django .utils .decorators import method_decorator
910from drf_yasg import openapi # type: ignore[import-untyped]
1011from drf_yasg .utils import no_body , swagger_auto_schema # type: ignore[import-untyped]
1516from rest_framework .permissions import IsAuthenticated
1617from rest_framework .request import Request
1718from rest_framework .response import Response
19+ from rest_framework .serializers import BaseSerializer
1820
21+ from core .models import AbstractBaseExportableModel
1922from environments .permissions .permissions import (
2023 EnvironmentAdminPermission ,
2124 EnvironmentPermissions ,
3538)
3639from projects .models import Project
3740from users .models import FFAdminUser
38- from webhooks .mixins import TriggerSampleWebhookMixin
3941from webhooks .webhooks import WebhookType
4042
4143from .identities .traits .models import Trait
5759 WebhookSerializer ,
5860)
5961
62+ T = TypeVar ("T" , bound = AbstractBaseExportableModel )
63+
6064logger = logging .getLogger (__name__ )
6165
6266
@@ -307,43 +311,44 @@ def disable_v2_versioning(self, request: Request, api_key: str) -> Response:
307311 return Response (status = status .HTTP_202_ACCEPTED )
308312
309313
310- class NestedEnvironmentViewSet (viewsets .GenericViewSet ): # type: ignore[type-arg]
311- model_class = None
314+ class NestedEnvironmentViewSet (Generic [ T ], viewsets .GenericViewSet [ T ]):
315+ model_class : Type [ T ]
312316 webhook_type = WebhookType .ENVIRONMENT
313317
314- def get_queryset (self ): # type: ignore[no-untyped-def]
315- return self .model_class .objects .filter ( # type: ignore[attr-defined]
318+ def get_queryset (self ) -> QuerySet [ T ]:
319+ return self .model_class .objects .filter (
316320 environment__api_key = self .kwargs .get ("environment_api_key" )
317321 )
318322
319- def perform_create (self , serializer ): # type: ignore[no-untyped-def]
320- serializer .save (environment = self ._get_environment ()) # type: ignore[no-untyped-call]
323+ def perform_create (self , serializer : BaseSerializer [ T ]) -> None :
324+ serializer .save (environment = self ._get_environment ())
321325
322- def perform_update (self , serializer ): # type: ignore[no-untyped-def]
323- serializer .save (environment = self ._get_environment ()) # type: ignore[no-untyped-call]
326+ def perform_update (self , serializer : BaseSerializer [ T ]) -> None :
327+ serializer .save (environment = self ._get_environment ())
324328
325- def _get_environment (self ): # type: ignore[no-untyped-def]
326- return Environment .objects .get (api_key = self .kwargs .get ("environment_api_key" ))
329+ def _get_environment (self ) -> Environment :
330+ environment : Environment = Environment .objects .get (
331+ api_key = self .kwargs .get ("environment_api_key" )
332+ )
333+ return environment
327334
328335
329336class WebhookViewSet (
330- NestedEnvironmentViewSet ,
337+ NestedEnvironmentViewSet [ Webhook ] ,
331338 mixins .ListModelMixin ,
332339 mixins .CreateModelMixin ,
333340 mixins .UpdateModelMixin ,
334341 mixins .DestroyModelMixin ,
335- TriggerSampleWebhookMixin ,
336342):
337343 serializer_class = WebhookSerializer
338344 pagination_class = None
339345 permission_classes = [IsAuthenticated , NestedEnvironmentPermissions ]
340- model_class = Webhook # type: ignore[assignment]
341-
342- webhook_type = WebhookType .ENVIRONMENT # type: ignore[assignment]
346+ model_class : Type [Webhook ] = Webhook
347+ webhook_type : WebhookType = WebhookType .ENVIRONMENT
343348
344349
345350class EnvironmentAPIKeyViewSet (
346- NestedEnvironmentViewSet ,
351+ NestedEnvironmentViewSet [ EnvironmentAPIKey ] ,
347352 mixins .ListModelMixin ,
348353 mixins .CreateModelMixin ,
349354 mixins .UpdateModelMixin ,
@@ -352,4 +357,4 @@ class EnvironmentAPIKeyViewSet(
352357 serializer_class = EnvironmentAPIKeySerializer
353358 pagination_class = None
354359 permission_classes = [IsAuthenticated , EnvironmentAdminPermission ]
355- model_class = EnvironmentAPIKey # type: ignore[assignment]
360+ model_class : Type [ EnvironmentAPIKey ] = EnvironmentAPIKey
0 commit comments