@@ -111,25 +111,23 @@ def to_representation(self, instance):
111111 data ["label_ids" ] = label_ids if label_ids else []
112112 return data
113113
114- def validate (self , data ):
114+ def validate (self , attrs ):
115115 if (
116- data .get ("start_date" , None ) is not None
117- and data .get ("target_date" , None ) is not None
118- and data .get ("start_date" , None ) > data .get ("target_date" , None )
116+ attrs .get ("start_date" , None ) is not None
117+ and attrs .get ("target_date" , None ) is not None
118+ and attrs .get ("start_date" , None ) > attrs .get ("target_date" , None )
119119 ):
120120 raise serializers .ValidationError ("Start date cannot exceed target date" )
121- return data
122121
123- def get_valid_assignees (self , assignees , project_id ):
124- if not assignees :
125- return []
122+ if attrs .get ("assignee_ids" , []):
123+ attrs ["assignee_ids" ] = ProjectMember .objects .filter (
124+ project_id = self .context ["project_id" ],
125+ role__gte = 15 ,
126+ is_active = True ,
127+ member_id__in = attrs ["assignee_ids" ],
128+ ).values_list ("member_id" , flat = True )
126129
127- return ProjectMember .objects .filter (
128- project_id = project_id ,
129- role__gte = 15 ,
130- is_active = True ,
131- member_id__in = assignees
132- ).values_list ('member_id' , flat = True )
130+ return attrs
133131
134132 def create (self , validated_data ):
135133 assignees = validated_data .pop ("assignee_ids" , None )
@@ -146,33 +144,35 @@ def create(self, validated_data):
146144 created_by_id = issue .created_by_id
147145 updated_by_id = issue .updated_by_id
148146
149- valid_assignee_ids = self .get_valid_assignees (assignees , project_id )
150- if valid_assignee_ids is not None and len (valid_assignee_ids ):
147+ if assignees is not None and len (assignees ):
151148 try :
152149 IssueAssignee .objects .bulk_create (
153150 [
154151 IssueAssignee (
155- assignee_id = user_id ,
152+ assignee_id = assignee_id ,
156153 issue = issue ,
157154 project_id = project_id ,
158155 workspace_id = workspace_id ,
159156 created_by_id = created_by_id ,
160157 updated_by_id = updated_by_id ,
161158 )
162- for user_id in valid_assignee_ids
159+ for assignee_id in assignees
163160 ],
164161 batch_size = 10 ,
165162 )
166163 except IntegrityError :
167164 pass
168165 else :
169166 # Then assign it to default assignee, if it is a valid assignee
170- if default_assignee_id is not None and ProjectMember .objects .filter (
171- member_id = default_assignee_id ,
172- project_id = project_id ,
173- role__gte = 15 ,
174- is_active = True
175- ).exists ():
167+ if (
168+ default_assignee_id is not None
169+ and ProjectMember .objects .filter (
170+ member_id = default_assignee_id ,
171+ project_id = project_id ,
172+ role__gte = 15 ,
173+ is_active = True ,
174+ ).exists ()
175+ ):
176176 try :
177177 IssueAssignee .objects .create (
178178 assignee_id = default_assignee_id ,
@@ -216,21 +216,20 @@ def update(self, instance, validated_data):
216216 created_by_id = instance .created_by_id
217217 updated_by_id = instance .updated_by_id
218218
219- valid_assignee_ids = self .get_valid_assignees (assignees , project_id )
220- if valid_assignee_ids is not None :
219+ if assignees is not None :
221220 IssueAssignee .objects .filter (issue = instance ).delete ()
222221 try :
223222 IssueAssignee .objects .bulk_create (
224223 [
225224 IssueAssignee (
226- assignee_id = user_id ,
225+ assignee_id = assignee_id ,
227226 issue = instance ,
228227 project_id = project_id ,
229228 workspace_id = workspace_id ,
230229 created_by_id = created_by_id ,
231230 updated_by_id = updated_by_id ,
232231 )
233- for user_id in valid_assignee_ids
232+ for assignee_id in assignees
234233 ],
235234 batch_size = 10 ,
236235 ignore_conflicts = True ,
0 commit comments