1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
from rest_framework.serializers import ModelSerializer, PrimaryKeyRelatedField, ValidationError
from rest_framework_bulk import BulkSerializerMixin
from .models import (
DocumentationLink, Infraction,
OffTopicChannelName,
Role, SnakeFact,
SnakeIdiom, SnakeName,
SpecialSnake, Tag, User
)
class DocumentationLinkSerializer(ModelSerializer):
class Meta:
model = DocumentationLink
fields = ('package', 'base_url', 'inventory_url')
class InfractionSerializer(ModelSerializer):
class Meta:
model = Infraction
fields = (
'id', 'inserted_at', 'expires_at', 'active', 'user', 'actor', 'type', 'reason', 'hidden'
)
def validate(self, attrs):
infr_type = attrs.get('type')
expires_at = attrs.get('expires_at')
if expires_at and infr_type in ('kick', 'warning'):
raise ValidationError({'expires_at': [f'{infr_type} infractions cannot expire.']})
hidden = attrs.get('hidden')
if hidden and infr_type in ('superstar',):
raise ValidationError({'hidden': [f'{infr_type} infractions cannot be hidden.']})
return attrs
class ExpandedInfractionSerializer(InfractionSerializer):
def to_representation(self, instance):
ret = super().to_representation(instance)
user = User.objects.get(id=ret['user'])
user_data = UserSerializer(user).data
ret['user'] = user_data
actor = User.objects.get(id=ret['actor'])
actor_data = UserSerializer(actor).data
ret['actor'] = actor_data
return ret
class OffTopicChannelNameSerializer(ModelSerializer):
class Meta:
model = OffTopicChannelName
fields = ('name',)
def to_representation(self, obj):
return obj.name
class SnakeFactSerializer(ModelSerializer):
class Meta:
model = SnakeFact
fields = ('fact',)
class SnakeIdiomSerializer(ModelSerializer):
class Meta:
model = SnakeIdiom
fields = ('idiom',)
class SnakeNameSerializer(ModelSerializer):
class Meta:
model = SnakeName
fields = ('name', 'scientific')
class SpecialSnakeSerializer(ModelSerializer):
class Meta:
model = SpecialSnake
fields = ('name', 'images', 'info')
class RoleSerializer(ModelSerializer):
class Meta:
model = Role
fields = ('id', 'name', 'colour', 'permissions')
class TagSerializer(ModelSerializer):
class Meta:
model = Tag
fields = ('title', 'embed')
class UserSerializer(BulkSerializerMixin, ModelSerializer):
roles = PrimaryKeyRelatedField(many=True, queryset=Role.objects.all(), required=False)
class Meta:
model = User
fields = ('id', 'avatar_hash', 'name', 'discriminator', 'roles', 'in_guild')
depth = 1
|