|
| 1 | +from typing import Dict, Type, Optional |
| 2 | + |
| 3 | +from asgiref.sync import async_to_sync |
| 4 | +from channels.db import database_sync_to_async |
| 5 | +from django.db.models import QuerySet, Model |
| 6 | +from rest_framework.generics import get_object_or_404, GenericAPIView |
| 7 | +from rest_framework.serializers import Serializer |
| 8 | + |
| 9 | +from channels_api.views import AsyncWebsocketAPIView |
| 10 | + |
| 11 | +GenericAPIView |
| 12 | + |
| 13 | +class GenericAsyncWebsocketAPIView(AsyncWebsocketAPIView): |
| 14 | + """ |
| 15 | + Base class for all other generic views. |
| 16 | + """ |
| 17 | + |
| 18 | + # You'll need to either set these attributes, |
| 19 | + # or override `get_queryset()`/`get_serializer_class()`. |
| 20 | + # If you are overriding a view method, it is important that you call |
| 21 | + # `get_queryset()` instead of accessing the `queryset` property directly, |
| 22 | + # as `queryset` will get evaluated only once, and those results are cached |
| 23 | + # for all subsequent requests. |
| 24 | + |
| 25 | + queryset = None # type: QuerySet |
| 26 | + serializer_class = None # type: Type[Serializer] |
| 27 | + |
| 28 | + # If you want to use object lookups other than pk, set 'lookup_field'. |
| 29 | + # For more complex lookup requirements override `get_object()`. |
| 30 | + lookup_field = 'pk' # type: str |
| 31 | + lookup_url_kwarg = None # type: Optional[str] |
| 32 | + |
| 33 | + # TODO filter_backends |
| 34 | + |
| 35 | + # TODO pagination_class |
| 36 | + |
| 37 | + async def get_queryset(self, action: str, **kwargs) -> QuerySet: |
| 38 | + """ |
| 39 | + Get the list of items for this view. |
| 40 | + This must be an iterable, and may be a queryset. |
| 41 | + Defaults to using `self.queryset`. |
| 42 | +
|
| 43 | + This method should always be used rather than accessing `self.queryset` |
| 44 | + directly, as `self.queryset` gets evaluated only once, and those results |
| 45 | + are cached for all subsequent requests. |
| 46 | +
|
| 47 | + You may want to override this if you need to provide different |
| 48 | + querysets depending on the incoming request. |
| 49 | +
|
| 50 | + (Eg. return a list of items that is specific to the user) |
| 51 | + """ |
| 52 | + assert self.queryset is not None, ( |
| 53 | + "'%s' should either include a `queryset` attribute, " |
| 54 | + "or override the `get_queryset()` method." |
| 55 | + % self.__class__.__name__ |
| 56 | + ) |
| 57 | + |
| 58 | + queryset = self.queryset |
| 59 | + if isinstance(queryset, QuerySet): |
| 60 | + # Ensure queryset is re-evaluated on each request. |
| 61 | + queryset = queryset.all() |
| 62 | + return queryset |
| 63 | + |
| 64 | + async def get_object(self, action: str, **kwargs) ->Model: |
| 65 | + """ |
| 66 | + Returns the object the view is displaying. |
| 67 | +
|
| 68 | + You may want to override this if you need to provide non-standard |
| 69 | + queryset lookups. Eg if objects are referenced using multiple |
| 70 | + keyword arguments in the url conf. |
| 71 | + """ |
| 72 | + queryset = await self.filter_queryset( |
| 73 | + queryset=await self.get_queryset(action=action, **kwargs), |
| 74 | + action=action, |
| 75 | + **kwargs |
| 76 | + ) |
| 77 | + |
| 78 | + # Perform the lookup filtering. |
| 79 | + lookup_url_kwarg = self.lookup_url_kwarg or self.lookup_field |
| 80 | + |
| 81 | + assert lookup_url_kwarg in kwargs, ( |
| 82 | + 'Expected view %s to be called with a URL keyword argument ' |
| 83 | + 'named "%s". Fix your URL conf, or set the `.lookup_field` ' |
| 84 | + 'attribute on the view correctly.' % |
| 85 | + (self.__class__.__name__, lookup_url_kwarg) |
| 86 | + ) |
| 87 | + |
| 88 | + filter_kwargs = {self.lookup_field: kwargs[lookup_url_kwarg]} |
| 89 | + |
| 90 | + obj = await database_sync_to_async(get_object_or_404)(queryset, **filter_kwargs) |
| 91 | + # TODO check_object_permissions |
| 92 | + |
| 93 | + return obj |
| 94 | + |
| 95 | + async def get_serializer( |
| 96 | + self, action: str, |
| 97 | + action_kwargs: Dict=None, |
| 98 | + *args, **kwargs): |
| 99 | + """ |
| 100 | + Return the serializer instance that should be used for validating and |
| 101 | + deserializing input, and for serializing output. |
| 102 | + """ |
| 103 | + serializer_class = await self.get_serializer_class( |
| 104 | + action=action, **action_kwargs |
| 105 | + ) |
| 106 | + |
| 107 | + kwargs['context'] = await self.get_serializer_context( |
| 108 | + action=action, **action_kwargs |
| 109 | + ) |
| 110 | + |
| 111 | + return serializer_class(*args, **kwargs) |
| 112 | + |
| 113 | + async def get_serializer_class(self, action: str, **kwargs) -> Type[Serializer]: |
| 114 | + """ |
| 115 | + Return the class to use for the serializer. |
| 116 | + Defaults to using `self.serializer_class`. |
| 117 | +
|
| 118 | + You may want to override this if you need to provide different |
| 119 | + serializations depending on the incoming request. |
| 120 | +
|
| 121 | + (Eg. admins get full serialization, others get basic serialization) |
| 122 | + """ |
| 123 | + assert self.serializer_class is not None, ( |
| 124 | + "'%s' should either include a `serializer_class` attribute, " |
| 125 | + "or override the `get_serializer_class()` method." |
| 126 | + % self.__class__.__name__ |
| 127 | + ) |
| 128 | + |
| 129 | + return self.serializer_class |
| 130 | + |
| 131 | + async def get_serializer_context(self, action: str, **kwargs): |
| 132 | + """ |
| 133 | + Extra context provided to the serializer class. |
| 134 | + """ |
| 135 | + return { |
| 136 | + 'scope': self.scope, |
| 137 | + 'consumer': self |
| 138 | + } |
| 139 | + |
| 140 | + async def filter_queryset(self, queryset: QuerySet, action: str, **kwargs): |
| 141 | + """ |
| 142 | + Given a queryset, filter it with whichever filter backend is in use. |
| 143 | +
|
| 144 | + You are unlikely to want to override this method, although you may need |
| 145 | + to call it either from a list view, or from a custom `get_object` |
| 146 | + method if you want to apply the configured filtering backend to the |
| 147 | + default queryset. |
| 148 | + """ |
| 149 | + # TODO filter_backends |
| 150 | + |
| 151 | + return queryset |
0 commit comments