1+ from lxml import html
2+
13# Django imports
24from django .utils import timezone
35
1618 DraftIssueLabel ,
1719 DraftIssueCycle ,
1820 DraftIssueModule ,
21+ ProjectMember ,
22+ EstimatePoint ,
1923)
24+ from plane .app .permissions import ROLE
2025
2126
2227class DraftIssueCreateSerializer (BaseSerializer ):
@@ -57,14 +62,77 @@ def to_representation(self, instance):
5762 data ["label_ids" ] = label_ids if label_ids else []
5863 return data
5964
60- def validate (self , data ):
65+ def validate (self , attrs ):
6166 if (
62- data .get ("start_date" , None ) is not None
63- and data .get ("target_date" , None ) is not None
64- and data .get ("start_date" , None ) > data .get ("target_date" , None )
67+ attrs .get ("start_date" , None ) is not None
68+ and attrs .get ("target_date" , None ) is not None
69+ and attrs .get ("start_date" , None ) > attrs .get ("target_date" , None )
6570 ):
6671 raise serializers .ValidationError ("Start date cannot exceed target date" )
67- return data
72+
73+ try :
74+ if attrs .get ("description_html" , None ) is not None :
75+ parsed = html .fromstring (attrs ["description_html" ])
76+ parsed_str = html .tostring (parsed , encoding = "unicode" )
77+ attrs ["description_html" ] = parsed_str
78+
79+ except Exception :
80+ raise serializers .ValidationError ("Invalid HTML passed" )
81+
82+ # Validate assignees are from project
83+ if attrs .get ("assignee_ids" , []):
84+ attrs ["assignee_ids" ] = ProjectMember .objects .filter (
85+ project_id = self .context ["project_id" ],
86+ role__gte = ROLE .MEMBER .value ,
87+ is_active = True ,
88+ member_id__in = attrs ["assignee_ids" ],
89+ ).values_list ("member_id" , flat = True )
90+
91+ # Validate labels are from project
92+ if attrs .get ("label_ids" ):
93+ label_ids = [label .id for label in attrs ["label_ids" ]]
94+ attrs ["label_ids" ] = list (
95+ Label .objects .filter (
96+ project_id = self .context .get ("project_id" ), id__in = label_ids
97+ ).values_list ("id" , flat = True )
98+ )
99+
100+ # # Check state is from the project only else raise validation error
101+ if (
102+ attrs .get ("state" )
103+ and not State .objects .filter (
104+ project_id = self .context .get ("project_id" ),
105+ pk = attrs .get ("state" ).id ,
106+ ).exists ()
107+ ):
108+ raise serializers .ValidationError (
109+ "State is not valid please pass a valid state_id"
110+ )
111+
112+ # # Check parent issue is from workspace as it can be cross workspace
113+ if (
114+ attrs .get ("parent" )
115+ and not Issue .objects .filter (
116+ project_id = self .context .get ("project_id" ),
117+ pk = attrs .get ("parent" ).id ,
118+ ).exists ()
119+ ):
120+ raise serializers .ValidationError (
121+ "Parent is not valid issue_id please pass a valid issue_id"
122+ )
123+
124+ if (
125+ attrs .get ("estimate_point" )
126+ and not EstimatePoint .objects .filter (
127+ project_id = self .context .get ("project_id" ),
128+ pk = attrs .get ("estimate_point" ).id ,
129+ ).exists ()
130+ ):
131+ raise serializers .ValidationError (
132+ "Estimate point is not valid please pass a valid estimate_point_id"
133+ )
134+
135+ return attrs
68136
69137 def create (self , validated_data ):
70138 assignees = validated_data .pop ("assignee_ids" , None )
@@ -89,14 +157,14 @@ def create(self, validated_data):
89157 DraftIssueAssignee .objects .bulk_create (
90158 [
91159 DraftIssueAssignee (
92- assignee = user ,
160+ assignee_id = assignee_id ,
93161 draft_issue = issue ,
94162 workspace_id = workspace_id ,
95163 project_id = project_id ,
96164 created_by_id = created_by_id ,
97165 updated_by_id = updated_by_id ,
98166 )
99- for user in assignees
167+ for assignee_id in assignees
100168 ],
101169 batch_size = 10 ,
102170 )
@@ -105,14 +173,14 @@ def create(self, validated_data):
105173 DraftIssueLabel .objects .bulk_create (
106174 [
107175 DraftIssueLabel (
108- label = label ,
176+ label_id = label_id ,
109177 draft_issue = issue ,
110178 project_id = project_id ,
111179 workspace_id = workspace_id ,
112180 created_by_id = created_by_id ,
113181 updated_by_id = updated_by_id ,
114182 )
115- for label in labels
183+ for label_id in labels
116184 ],
117185 batch_size = 10 ,
118186 )
@@ -163,14 +231,14 @@ def update(self, instance, validated_data):
163231 DraftIssueAssignee .objects .bulk_create (
164232 [
165233 DraftIssueAssignee (
166- assignee = user ,
234+ assignee_id = assignee_id ,
167235 draft_issue = instance ,
168236 workspace_id = workspace_id ,
169237 project_id = project_id ,
170238 created_by_id = created_by_id ,
171239 updated_by_id = updated_by_id ,
172240 )
173- for user in assignees
241+ for assignee_id in assignees
174242 ],
175243 batch_size = 10 ,
176244 )
0 commit comments