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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
from rest_framework.serializers import ModelSerializer, PrimaryKeyRelatedField, ValidationError
from rest_framework_bulk import BulkSerializerMixin
from .models import (
BotSetting, DeletedMessage,
DocumentationLink, Infraction,
LogEntry, MessageDeletionContext,
Nomination, OffTopicChannelName,
Reminder, Role,
SnakeFact, SnakeIdiom,
SnakeName, SpecialSnake,
Tag, User
)
class BotSettingSerializer(ModelSerializer):
class Meta:
model = BotSetting
fields = ('name', 'data')
class DeletedMessageSerializer(ModelSerializer):
author = PrimaryKeyRelatedField(
queryset=User.objects.all()
)
deletion_context = PrimaryKeyRelatedField(
queryset=MessageDeletionContext.objects.all(),
# This will be overriden in the `create` function
# of the deletion context serializer.
required=False
)
class Meta:
model = DeletedMessage
fields = (
'id', 'author',
'channel_id', 'content',
'embeds', 'deletion_context'
)
class MessageDeletionContextSerializer(ModelSerializer):
deletedmessage_set = DeletedMessageSerializer(many=True)
class Meta:
model = MessageDeletionContext
fields = ('actor', 'creation', 'id', 'deletedmessage_set')
depth = 1
def create(self, validated_data):
messages = validated_data.pop('deletedmessage_set')
deletion_context = MessageDeletionContext.objects.create(**validated_data)
for message in messages:
DeletedMessage.objects.create(
deletion_context=deletion_context,
**message
)
return deletion_context
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 LogEntrySerializer(ModelSerializer):
class Meta:
model = LogEntry
fields = (
'application', 'logger_name', 'timestamp',
'level', 'module', 'line', 'message'
)
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 ReminderSerializer(ModelSerializer):
author = PrimaryKeyRelatedField(queryset=User.objects.all())
class Meta:
model = Reminder
fields = ('active', 'author', 'channel_id', 'content', 'expiration', 'id')
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
class NominationSerializer(ModelSerializer):
actor = PrimaryKeyRelatedField(queryset=User.objects.all())
user = PrimaryKeyRelatedField(queryset=User.objects.all())
class Meta:
model = Nomination
fields = (
'id', 'active', 'actor', 'reason', 'user',
'inserted_at', 'unnominate_reason', 'unwatched_at')
depth = 1
def validate(self, attrs):
active = attrs.get("active")
unnominate_reason = attrs.get("unnominate_reason")
if active and unnominate_reason:
raise ValidationError(
{'unnominate_reason': "An active nomination can't have an unnominate reason"}
)
return attrs
|