| 1 |
import uuid |
| 2 |
from datetime import timedelta |
| 3 |
|
| 4 |
from django.core.validators import ( |
| 5 |
DecimalValidator, MaxLengthValidator, MaxValueValidator, |
| 6 |
MinLengthValidator, MinValueValidator, RegexValidator |
| 7 |
) |
| 8 |
from django.db import models |
| 9 |
|
| 10 |
from rest_framework import generics, permissions, serializers |
| 11 |
from rest_framework.decorators import action |
| 12 |
from rest_framework.response import Response |
| 13 |
from rest_framework.schemas.openapi import AutoSchema |
| 14 |
from rest_framework.views import APIView |
| 15 |
from rest_framework.viewsets import GenericViewSet, ViewSet |
| 16 |
|
| 17 |
|
| 18 |
class ExampleListView(APIView): |
| 19 |
permission_classes = [permissions.IsAuthenticatedOrReadOnly] |
| 20 |
|
| 21 |
def get(self, *args, **kwargs): |
| 22 |
pass |
| 23 |
|
| 24 |
def post(self, request, *args, **kwargs): |
| 25 |
pass |
| 26 |
|
| 27 |
|
| 28 |
class ExampleDetailView(APIView): |
| 29 |
permission_classes = [permissions.IsAuthenticatedOrReadOnly] |
| 30 |
|
| 31 |
def get(self, *args, **kwargs): |
| 32 |
pass |
| 33 |
|
| 34 |
|
| 35 |
class DocStringExampleListView(APIView): |
| 36 |
""" |
| 37 |
get: A description of my GET operation. |
| 38 |
post: A description of my POST operation. |
| 39 |
""" |
| 40 |
permission_classes = [permissions.IsAuthenticatedOrReadOnly] |
| 41 |
|
| 42 |
def get(self, *args, **kwargs): |
| 43 |
pass |
| 44 |
|
| 45 |
def post(self, request, *args, **kwargs): |
| 46 |
pass |
| 47 |
|
| 48 |
|
| 49 |
class DocStringExampleDetailView(APIView): |
| 50 |
permission_classes = [permissions.IsAuthenticatedOrReadOnly] |
| 51 |
|
| 52 |
def get(self, *args, **kwargs): |
| 53 |
""" |
| 54 |
A description of my GET operation. |
| 55 |
""" |
| 56 |
pass |
| 57 |
|
| 58 |
|
| 59 |
# Generics. |
| 60 |
class ExampleSerializer(serializers.Serializer): |
| 61 |
date = serializers.DateField() |
| 62 |
datetime = serializers.DateTimeField() |
| 63 |
duration = serializers.DurationField(default=timedelta()) |
| 64 |
hstore = serializers.HStoreField() |
| 65 |
uuid_field = serializers.UUIDField(default=uuid.uuid4) |
| 66 |
|
| 67 |
|
| 68 |
class ExampleGenericAPIView(generics.GenericAPIView): |
| 69 |
serializer_class = ExampleSerializer |
| 70 |
|
| 71 |
def get(self, *args, **kwargs): |
| 72 |
from datetime import datetime |
| 73 |
now = datetime.now() |
| 74 |
|
| 75 |
serializer = self.get_serializer(data=now.date(), datetime=now) |
| 76 |
return Response(serializer.data) |
| 77 |
|
| 78 |
|
| 79 |
class ExampleGenericViewSet(GenericViewSet): |
| 80 |
serializer_class = ExampleSerializer |
| 81 |
|
| 82 |
def get(self, *args, **kwargs): |
| 83 |
from datetime import datetime |
| 84 |
now = datetime.now() |
| 85 |
|
| 86 |
serializer = self.get_serializer(data=now.date(), datetime=now) |
| 87 |
return Response(serializer.data) |
| 88 |
|
| 89 |
@action(detail=False) |
| 90 |
def new(self, *args, **kwargs): |
| 91 |
pass |
| 92 |
|
| 93 |
@action(detail=False) |
| 94 |
def old(self, *args, **kwargs): |
| 95 |
pass |
| 96 |
|
| 97 |
|
| 98 |
# Validators and/or equivalent Field attributes. |
| 99 |
class ExampleValidatedSerializer(serializers.Serializer): |
| 100 |
integer = serializers.IntegerField( |
| 101 |
validators=( |
| 102 |
MaxValueValidator(limit_value=99), |
| 103 |
MinValueValidator(limit_value=-11), |
| 104 |
) |
| 105 |
) |
| 106 |
string = serializers.CharField( |
| 107 |
validators=( |
| 108 |
MaxLengthValidator(limit_value=10), |
| 109 |
MinLengthValidator(limit_value=2), |
| 110 |
) |
| 111 |
) |
| 112 |
regex = serializers.CharField( |
| 113 |
validators=( |
| 114 |
RegexValidator(regex=r'[ABC]12{3}'), |
| 115 |
), |
| 116 |
help_text='must have an A, B, or C followed by 1222' |
| 117 |
) |
| 118 |
lst = serializers.ListField( |
| 119 |
validators=( |
| 120 |
MaxLengthValidator(limit_value=10), |
| 121 |
MinLengthValidator(limit_value=2), |
| 122 |
) |
| 123 |
) |
| 124 |
decimal1 = serializers.DecimalField(max_digits=6, decimal_places=2, coerce_to_string=False) |
| 125 |
decimal2 = serializers.DecimalField(max_digits=5, decimal_places=0, coerce_to_string=False, |
| 126 |
validators=(DecimalValidator(max_digits=17, decimal_places=4),)) |
| 127 |
decimal3 = serializers.DecimalField(max_digits=8, decimal_places=2, coerce_to_string=True) |
| 128 |
decimal4 = serializers.DecimalField(max_digits=8, decimal_places=2, coerce_to_string=True, |
| 129 |
validators=(DecimalValidator(max_digits=17, decimal_places=4),)) |
| 130 |
decimal5 = serializers.DecimalField(max_digits=6, decimal_places=2) |
| 131 |
email = serializers.EmailField(default='[email protected]') |
| 132 |
url = serializers.URLField(default='http://www.example.com', allow_null=True) |
| 133 |
uuid = serializers.UUIDField() |
| 134 |
ip4 = serializers.IPAddressField(protocol='ipv4') |
| 135 |
ip6 = serializers.IPAddressField(protocol='ipv6') |
| 136 |
ip = serializers.IPAddressField() |
| 137 |
duration = serializers.DurationField( |
| 138 |
validators=( |
| 139 |
MinValueValidator(timedelta(seconds=10)), |
| 140 |
) |
| 141 |
) |
| 142 |
|
| 143 |
|
| 144 |
class ExampleValidatedAPIView(generics.GenericAPIView): |
| 145 |
serializer_class = ExampleValidatedSerializer |
| 146 |
|
| 147 |
def get(self, *args, **kwargs): |
| 148 |
serializer = self.get_serializer(integer=33, string='hello', regex='foo', decimal1=3.55, |
| 149 |
decimal2=5.33, email='[email protected]', |
| 150 |
url='http://localhost', uuid=uuid.uuid4(), ip4='127.0.0.1', ip6='::1', |
| 151 |
ip='192.168.1.1') |
| 152 |
return Response(serializer.data) |
| 153 |
|
| 154 |
|
| 155 |
# Serializer with model. |
| 156 |
class OpenAPIExample(models.Model): |
| 157 |
first_name = models.CharField(max_length=30) |
| 158 |
|
| 159 |
|
| 160 |
class ExampleSerializerModel(serializers.Serializer): |
| 161 |
date = serializers.DateField() |
| 162 |
datetime = serializers.DateTimeField() |
| 163 |
hstore = serializers.HStoreField() |
| 164 |
uuid_field = serializers.UUIDField(default=uuid.uuid4) |
| 165 |
|
| 166 |
class Meta: |
| 167 |
model = OpenAPIExample |
| 168 |
|
| 169 |
|
| 170 |
class ExampleOperationIdDuplicate1(generics.GenericAPIView): |
| 171 |
serializer_class = ExampleSerializerModel |
| 172 |
|
| 173 |
def get(self, *args, **kwargs): |
| 174 |
pass |
| 175 |
|
| 176 |
|
| 177 |
class ExampleOperationIdDuplicate2(generics.GenericAPIView): |
| 178 |
serializer_class = ExampleSerializerModel |
| 179 |
|
| 180 |
def get(self, *args, **kwargs): |
| 181 |
pass |
| 182 |
|
| 183 |
|
| 184 |
class ExampleGenericAPIViewModel(generics.GenericAPIView): |
| 185 |
serializer_class = ExampleSerializerModel |
| 186 |
|
| 187 |
def get(self, *args, **kwargs): |
| 188 |
from datetime import datetime |
| 189 |
now = datetime.now() |
| 190 |
|
| 191 |
serializer = self.get_serializer(data=now.date(), datetime=now) |
| 192 |
return Response(serializer.data) |
| 193 |
|
| 194 |
|
| 195 |
class ExampleAutoSchemaComponentName(generics.GenericAPIView): |
| 196 |
serializer_class = ExampleSerializerModel |
| 197 |
schema = AutoSchema(component_name="Ulysses") |
| 198 |
|
| 199 |
def get(self, *args, **kwargs): |
| 200 |
from datetime import datetime |
| 201 |
now = datetime.now() |
| 202 |
|
| 203 |
serializer = self.get_serializer(data=now.date(), datetime=now) |
| 204 |
return Response(serializer.data) |
| 205 |
|
| 206 |
|
| 207 |
class ExampleAutoSchemaDuplicate1(generics.GenericAPIView): |
| 208 |
serializer_class = ExampleValidatedSerializer |
| 209 |
schema = AutoSchema(component_name="Duplicate") |
| 210 |
|
| 211 |
def get(self, *args, **kwargs): |
| 212 |
from datetime import datetime |
| 213 |
now = datetime.now() |
| 214 |
|
| 215 |
serializer = self.get_serializer(data=now.date(), datetime=now) |
| 216 |
return Response(serializer.data) |
| 217 |
|
| 218 |
|
| 219 |
class ExampleAutoSchemaDuplicate2(generics.GenericAPIView): |
| 220 |
serializer_class = ExampleSerializerModel |
| 221 |
schema = AutoSchema(component_name="Duplicate") |
| 222 |
|
| 223 |
def get(self, *args, **kwargs): |
| 224 |
from datetime import datetime |
| 225 |
now = datetime.now() |
| 226 |
|
| 227 |
serializer = self.get_serializer(data=now.date(), datetime=now) |
| 228 |
return Response(serializer.data) |
| 229 |
|
| 230 |
|
| 231 |
class ExampleViewSet(ViewSet): |
| 232 |
serializer_class = ExampleSerializerModel |
| 233 |
|
| 234 |
def list(self, request): |
| 235 |
pass |
| 236 |
|
| 237 |
def create(self, request): |
| 238 |
pass |
| 239 |
|
| 240 |
def retrieve(self, request, pk=None): |
| 241 |
pass |
| 242 |
|
| 243 |
def update(self, request, pk=None): |
| 244 |
pass |
| 245 |
|
| 246 |
def partial_update(self, request, pk=None): |
| 247 |
pass |
| 248 |
|
| 249 |
def destroy(self, request, pk=None): |
| 250 |
pass |