-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathserializers.py
172 lines (132 loc) · 6.17 KB
/
serializers.py
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
import re
from django.utils import timezone
from datetime import datetime
from rest_framework import serializers
class EmojiSerializer(serializers.Serializer):
message_id = serializers.CharField(max_length=128)
sender_id = serializers.CharField(max_length=128)
data = serializers.CharField(max_length=128)
category = serializers.CharField(max_length=128)
aliases = serializers.ListField(
child=serializers.CharField(max_length=128), required=False, default=[]
)
count = serializers.IntegerField(default=0, required=False)
created_at = serializers.DateTimeField(default=timezone.now)
def __str__(self):
return str(self.data)
class ReminderSerializer(serializers.Serializer):
message_id = serializers.CharField(max_length=500)
current_date = serializers.CharField(max_length=500)
scheduled_date = serializers.CharField(max_length=500)
notes = serializers.CharField(required=False,max_length=500, allow_blank=True)
class Meta:
fields = ['__all__']
class ThreadSerializer(serializers.Serializer):
# """
# this will server as a serializer to hold threads
# under a particular message.
# The threads serializer will be used in the
# Message serializer
# """
message_id = serializers.CharField(max_length=128, required=False)
sender_id = serializers.CharField(max_length=128, required=False)
message = serializers.CharField(required=False)
media = serializers.ListField(
child=serializers.URLField(), allow_empty=True, required=False, default=[]
)
read = serializers.BooleanField(default=False, required=False)
pinned = serializers.BooleanField(default=False, required=False)
reactions = serializers.ListField(
required=False, default=[], child=EmojiSerializer()
)
created_at = serializers.DateTimeField(default=timezone.now)
def __str__(self):
return str(self.message)
class MessageSerializer(serializers.Serializer):
sender_id = serializers.CharField(max_length=128)
room_id = serializers.CharField(max_length=128)
message = serializers.CharField()
media = serializers.ListField(
child=serializers.URLField(), allow_empty=True, required=False, default=[]
)
read = serializers.BooleanField(default=False, required=False)
pinned = serializers.BooleanField(default=False, required=False)
saved_by = serializers.ListField(
child=serializers.CharField(max_length=128), required=False, default=[]
)
threads = serializers.ListField(
required=False, default=[], child=ThreadSerializer()
)
replied_message = serializers.ListField(
required=False, default=[]
)
reactions = serializers.ListField(
required=False, default=[], child=EmojiSerializer()
)
sent_from_thread = serializers.BooleanField(default=False, required=False)
created_at = serializers.DateTimeField(default=timezone.now)
def __str__(self):
return str(self.message)
def update(self, instance, validated_data):
print(validated_data)
instance["sender_id"] = validated_data.get(
"sender_id", instance["sender_id"])
instance["room_id"] = validated_data.get(
"room_id", instance["room_id"])
instance["message"] = validated_data.get(
"message", instance["message"])
# instance["created_at"] = validated_data.get('created_at', instance["created_at"] ) read only
return instance
class RoomSerializer(serializers.Serializer):
org_id = serializers.CharField(max_length=128, required=True)
room_member_ids = serializers.ListField(
child=serializers.CharField(max_length=128), allow_empty=False, required=True
)
room_name = serializers.CharField(max_length=128, required=True)
private = serializers.BooleanField(default=True, read_only=True)
created_at = serializers.DateTimeField(
default=timezone.now, read_only=True)
def __str__(self):
return str()
class RoomInfoSerializer(serializers.Serializer):
room_id = serializers.CharField(max_length=128)
class GetMessageSerializer(serializers.Serializer):
date = serializers.DateField(
format="%d-%m-%Y", input_formats=["%d-%m-%Y", "iso-8601"], required=False
)
class UserRoomsSerializer(serializers.Serializer):
user_id = serializers.CharField(max_length=128)
class BookmarkSerializer(serializers.Serializer):
link = serializers.CharField()
name = serializers.CharField()
created_at = serializers.DateTimeField(default=timezone.now)
def validate_link(self, value):
pattern = r"(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,})"
if not re.match(pattern, value):
raise serializers.ValidationError("Invalid link for bookmark")
return value
class ReadSerializer(serializers.Serializer):
message_id = serializers.CharField(max_length=128)
class CookieSerializer(serializers.Serializer):
cookie = serializers.CharField(max_length=150)
class DeleteMessageSerializer(serializers.Serializer):
message_id = serializers.CharField(max_length=128)
class ScheduleMessageSerializer(serializers.Serializer):
sender_id = serializers.CharField(max_length=128)
room_id = serializers.CharField(max_length=128)
message = serializers.CharField()
timer = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S")
def validate_timer(self, timer):
if datetime.now() > timer.replace(tzinfo=None):
raise serializers.ValidationError("Date cannot be in the past.")
return timer
class AddMemberSerializer(serializers.Serializer):
# member_id = serializers.CharField(max_length=128)
members_id = serializers.ListField(
child=serializers.CharField(max_length=128), allow_empty=False, required=True
)
room_id = serializers.CharField(max_length=128)
room_name = serializers.CharField(max_length=128, required=True)
private = serializers.BooleanField(default=True, read_only=True)
created_at = serializers.DateTimeField(
default=timezone.now, read_only=True)