1
- from rest_framework import serializers
1
+ from rest_framework import serializers , status
2
2
3
3
from users .models import User
4
4
from users .serializers import UserSafeSerializer
@@ -12,41 +12,47 @@ class Meta:
12
12
model = Diary
13
13
fields = '__all__'
14
14
15
+
15
16
class DiarySimpleSerializer (serializers .ModelSerializer ):
16
17
user = UserSafeSerializer (read_only = True )
17
18
18
19
class Meta :
19
20
model = Diary
20
21
fields = ['id' , 'user' , 'title' ]
21
22
23
+
22
24
class SentenceSerializer (serializers .ModelSerializer ):
23
25
diay = DiarySerializer (read_only = True )
24
26
25
27
class Meta :
26
28
model = Sentences
27
29
fields = '__all__'
28
30
31
+
29
32
class SentenceSimpleSerializer (serializers .ModelSerializer ):
30
33
diary = DiarySimpleSerializer (read_only = True )
31
34
32
35
class Meta :
33
36
model = Sentences
34
37
fields = ['id' , 'diary' , 'sentence' ]
35
38
39
+
36
40
class KeywordSerializer (serializers .ModelSerializer ):
37
41
sentence = SentenceSerializer (read_only = True )
38
42
39
43
class Meta :
40
44
model = Keywords
41
45
fields = '__all__'
42
46
47
+
43
48
class QuestionSerializer (serializers .ModelSerializer ):
44
49
keyword = KeywordSerializer (read_only = True )
45
50
46
51
class Meta :
47
52
model = Questions
48
53
fields = '__all__'
49
54
55
+
50
56
class DiaryCreateRequest (serializers .ModelSerializer ):
51
57
userId = serializers .IntegerField ()
52
58
@@ -60,19 +66,91 @@ def to_diary(self, user: User) -> DiarySerializer:
60
66
newDiary .save (user = user )
61
67
return newDiary
62
68
69
+
63
70
class WriteRequest (serializers .Serializer ):
64
71
userId = serializers .IntegerField ()
65
72
title = serializers .CharField (max_length = 100 )
66
73
content = serializers .CharField ()
67
74
75
+ def is_valid (self , raise_exception = False ):
76
+ super_valid = super ().is_valid ()
77
+ # 유효하지 않다면 False, 400 반환
78
+ if not super_valid :
79
+ return False , status .HTTP_400_BAD_REQUEST
80
+
81
+ # 유효하다면 userId가 존재하는지 확인
82
+ is_user_exist = User .objects .filter (id = self .data ['userId' ]).exists ()
83
+
84
+ # 존재하지 않는다면 False, 404 반환
85
+ if not is_user_exist :
86
+ self ._errors ['userId' ] = [f'userId: { self .data .get ("userId" )} 가 존재하지 않습니다.' ]
87
+ return False , status .HTTP_404_NOT_FOUND
88
+
89
+ return True , status .HTTP_200_OK
90
+
91
+
68
92
class UpdateRequest (serializers .Serializer ):
69
93
diaryId = serializers .IntegerField ()
70
94
userId = serializers .IntegerField ()
71
95
title = serializers .CharField (max_length = 100 )
72
96
content = serializers .CharField ()
73
97
98
+ def is_valid (self , raise_exception = False ):
99
+ super_valid = super ().is_valid ()
100
+ # 유효하지 않다면 False, 400 반환
101
+ if not super_valid :
102
+ return False , status .HTTP_400_BAD_REQUEST
103
+
104
+ # userId가 존재하는지 확인
105
+ is_user_exist = User .objects .filter (id = self .data ['userId' ]).exists ()
106
+ # 존재하지 않는다면 False, 404 반환
107
+ if not is_user_exist :
108
+ self ._errors ['userId' ] = [f'userId: { self .data .get ("userId" )} 가 존재하지 않습니다.' ]
109
+ return False , status .HTTP_404_NOT_FOUND
110
+
111
+ # diaryId가 존재하는지 확인
112
+ is_diary_exist = Diary .objects .filter (id = self .data ['diaryId' ]).exists ()
113
+ # 존재하지 않는다면 False, 404 반환
114
+ if not is_diary_exist :
115
+ self ._errors ['diaryId' ] = [f'diaryId: { self .data .get ("diaryId" )} 가 존재하지 않습니다.' ]
116
+ return False , status .HTTP_404_NOT_FOUND
117
+
118
+ return True , status .HTTP_200_OK
119
+
120
+
74
121
class GetUserRequest (serializers .Serializer ):
75
122
userId = serializers .IntegerField ()
76
123
124
+ def is_valid (self , raise_exception = False ):
125
+ super_valid = super ().is_valid ()
126
+ # 유효하지 않다면 False, 400 반환
127
+ if not super_valid :
128
+ return False , status .HTTP_400_BAD_REQUEST
129
+
130
+ # userId가 존재하는지 확인
131
+ is_user_exist = User .objects .filter (id = self .data ['userId' ]).exists ()
132
+ # 존재하지 않는다면 False, 404 반환
133
+ if not is_user_exist :
134
+ self ._errors ['userId' ] = [f'userId: { self .data .get ("userId" )} 가 존재하지 않습니다.' ]
135
+ return False , status .HTTP_404_NOT_FOUND
136
+
137
+ return True , status .HTTP_200_OK
138
+
139
+
77
140
class GetDiaryRequest (serializers .Serializer ):
78
- diaryId = serializers .IntegerField ()
141
+ diaryId = serializers .IntegerField ()
142
+
143
+ def is_valid (self , raise_exception = False ):
144
+ super_valid = super ().is_valid ()
145
+ # 유효하지 않다면 False, 400 반환
146
+ if not super_valid :
147
+ return False , status .HTTP_400_BAD_REQUEST
148
+
149
+ # diaryId가 존재하는지 확인
150
+ is_diary_exist = Diary .objects .filter (id = self .data ['diaryId' ]).exists ()
151
+ # 존재하지 않는다면 False, 404 반환
152
+ if not is_diary_exist :
153
+ self ._errors ['diaryId' ] = [f'diaryId: { self .data .get ("diaryId" )} 가 존재하지 않습니다.' ]
154
+ return False , status .HTTP_404_NOT_FOUND
155
+
156
+ return True , status .HTTP_200_OK
0 commit comments