Debian Codegraph @

testproj/users/serializers.py

raw
1 import typing # noqa: F401
2
3 from django.contrib.auth.models import User
4 from rest_framework import serializers
5
6 from drf_yasg.utils import swagger_serializer_method
7 from snippets.models import Snippet
8
9 from .method_serializers import MethodFieldExampleSerializer
10
11
12 class OtherStuffSerializer(serializers.Serializer):
13 foo = serializers.CharField()
14
15
16 class UserSerializer(serializers.ModelSerializer):
17 snippets = serializers.PrimaryKeyRelatedField(
18 many=True, queryset=Snippet.objects.all()
19 )
20 article_slugs = serializers.SlugRelatedField(
21 read_only=True, slug_field="slug", many=True, source="articles"
22 )
23 last_connected_ip = serializers.IPAddressField(
24 help_text="i'm out of ideas", protocol="ipv4", read_only=True
25 )
26 last_connected_at = serializers.DateField(help_text="really?", read_only=True)
27
28 other_stuff = serializers.SerializerMethodField(
29 help_text="the decorator should determine the serializer class for this"
30 )
31
32 hint_example = MethodFieldExampleSerializer()
33
34 @swagger_serializer_method(serializer_or_field=OtherStuffSerializer)
35 def get_other_stuff(self, obj):
36 """
37 method_field that uses a serializer internally.
38
39 By using the decorator, we can tell drf-yasg how to represent this in Swagger
40 :param obj:
41 :return:
42 """
43 return OtherStuffSerializer().data
44
45 help_text_example_1 = serializers.SerializerMethodField(
46 help_text="help text on field is set, so this should appear in swagger"
47 )
48
49 @swagger_serializer_method(
50 serializer_or_field=serializers.IntegerField(
51 help_text=(
52 "decorated instance help_text shouldn't appear in swagger because "
53 "field has priority"
54 )
55 )
56 )
57 def get_help_text_example_1(self):
58 """
59 method docstring shouldn't appear in swagger because field has priority
60 :return:
61 """
62 return 1
63
64 help_text_example_2 = serializers.SerializerMethodField()
65
66 @swagger_serializer_method(
67 serializer_or_field=serializers.IntegerField(
68 help_text="instance help_text is set, so should appear in swagger"
69 )
70 )
71 def get_help_text_example_2(self):
72 """
73 method docstring shouldn't appear in swagger because decorator has priority
74 :return:
75 """
76 return 1
77
78 help_text_example_3 = serializers.SerializerMethodField()
79
80 @swagger_serializer_method(serializer_or_field=serializers.IntegerField())
81 def get_help_text_example_3(self):
82 """
83 docstring is set so should appear in swagger as fallback
84 :return:
85 """
86 return 1
87
88 class Meta:
89 model = User
90 fields = (
91 "id",
92 "username",
93 "email",
94 "articles",
95 "snippets",
96 "last_connected_ip",
97 "last_connected_at",
98 "article_slugs",
99 "other_stuff",
100 "hint_example",
101 "help_text_example_1",
102 "help_text_example_2",
103 "help_text_example_3",
104 )
105
106 ref_name = "UserSerializer"
107
108
109 class UserListQuerySerializer(serializers.Serializer):
110 username = serializers.CharField(
111 help_text="this field is generated from a query_serializer", required=False
112 )
113 is_staff = serializers.BooleanField(help_text="this one too!", required=False)
114 styles = serializers.MultipleChoiceField(
115 help_text="and this one is fancy!", choices=("a", "b", "c", "d")
116 )