1414from lib .core .enums import CustomFieldType
1515from lib .core .enums import ProjectStatus
1616from lib .core .enums import UserRole
17+ from lib .core .enums import WMUserStateEnum
1718from lib .core .exceptions import AppException
1819from lib .core .jsx_conditions import EmptyQuery
1920from lib .core .jsx_conditions import Filter
@@ -95,6 +96,60 @@ def handle(self, filters: Dict[str, Any], query: Query = None) -> Query:
9596 return super ().handle (filters , query )
9697
9798
99+ class ItemFilterHandler (AbstractQueryHandler ):
100+ def __init__ (
101+ self ,
102+ project : ProjectEntity ,
103+ entity : BaseItemEntity ,
104+ service_provider : BaseServiceProvider ,
105+ ):
106+ self ._service_provider = service_provider
107+ self ._entity = entity
108+ self ._project = project
109+
110+ def handle (self , filters : Dict [str , Any ], query : Query = None ) -> Query :
111+ if query is None :
112+ query = EmptyQuery ()
113+ for key , val in filters .items ():
114+ _keys = key .split ("__" )
115+ val = self ._handle_special_fields (_keys , val )
116+ condition , _key = determine_condition_and_key (_keys )
117+ query &= Filter (_key , val , condition )
118+ return super ().handle (filters , query )
119+
120+ @staticmethod
121+ def _extract_value_from_mapping (data , extractor = lambda x : x ):
122+ if isinstance (data , (list , tuple , set )):
123+ return [extractor (i ) for i in data ]
124+ return extractor (data )
125+
126+ def _handle_special_fields (self , keys : List [str ], val ):
127+ """
128+ Handle special fields like 'approval_status', 'assignments', 'user_role' and 'annotation_status'.
129+ """
130+ if keys [0 ] == "approval_status" :
131+ val = (
132+ [ApprovalStatus (i ).value for i in val ]
133+ if isinstance (val , (list , tuple , set ))
134+ else ApprovalStatus (val ).value
135+ )
136+ elif keys [0 ] == "annotation_status" :
137+ val = self ._extract_value_from_mapping (
138+ val ,
139+ lambda x : self ._service_provider .get_annotation_status_value (
140+ self ._project , x
141+ ),
142+ )
143+ elif keys [0 ] == "assignments" and keys [1 ] == "user_role" :
144+ if isinstance (val , list ):
145+ val = [
146+ self ._service_provider .get_role_id (self ._project , i ) for i in val
147+ ]
148+ else :
149+ val = self ._service_provider .get_role_id (self ._project , val )
150+ return val
151+
152+
98153class BaseCustomFieldHandler (AbstractQueryHandler ):
99154 def __init__ (
100155 self , service_provider : BaseServiceProvider , entity : CustomFieldEntityEnum
@@ -151,8 +206,16 @@ def _determine_condition_and_key(keys: List[str]) -> Tuple[OperatorEnum, str]:
151206 key = "." .join (keys )
152207 return condition , key
153208
154- @staticmethod
155- def _handle_special_fields (keys : List [str ], val ):
209+ def _handle_special_fields (self , keys : List [str ], val ):
210+ if keys [0 ] == "custom_field" :
211+ component_id = self ._service_provider .get_custom_field_component_id (
212+ field_id = int (keys [1 ]), entity = self ._entity
213+ )
214+ if component_id == CustomFieldType .DATE_PICKER .value :
215+ try :
216+ val = val * 1000
217+ except Exception :
218+ raise AppException ("Invalid custom field value provided." )
156219 return val
157220
158221 def handle (self , filters : Dict [str , Any ], query : Query = None ) -> Query :
@@ -168,75 +231,12 @@ def handle(self, filters: Dict[str, Any], query: Query = None) -> Query:
168231 return super ().handle (filters , query )
169232
170233
171- class ItemFilterHandler (AbstractQueryHandler ):
172- def __init__ (
173- self ,
174- project : ProjectEntity ,
175- entity : BaseItemEntity ,
176- service_provider : BaseServiceProvider ,
177- ):
178- self ._service_provider = service_provider
179- self ._entity = entity
180- self ._project = project
181-
182- def handle (self , filters : Dict [str , Any ], query : Query = None ) -> Query :
183- if query is None :
184- query = EmptyQuery ()
185- for key , val in filters .items ():
186- _keys = key .split ("__" )
187- val = self ._handle_special_fields (_keys , val )
188- condition , _key = determine_condition_and_key (_keys )
189- query &= Filter (_key , val , condition )
190- return super ().handle (filters , query )
191-
192- @staticmethod
193- def _extract_value_from_mapping (data , extractor = lambda x : x ):
194- if isinstance (data , (list , tuple , set )):
195- return [extractor (i ) for i in data ]
196- return extractor (data )
197-
198- def _handle_special_fields (self , keys : List [str ], val ):
199- """
200- Handle special fields like 'approval_status', 'assignments', 'user_role' and 'annotation_status'.
201- """
202- if keys [0 ] == "approval_status" :
203- val = (
204- [ApprovalStatus (i ).value for i in val ]
205- if isinstance (val , (list , tuple , set ))
206- else ApprovalStatus (val ).value
207- )
208- elif keys [0 ] == "annotation_status" :
209- val = self ._extract_value_from_mapping (
210- val ,
211- lambda x : self ._service_provider .get_annotation_status_value (
212- self ._project , x
213- ),
214- )
215- elif keys [0 ] == "assignments" and keys [1 ] == "user_role" :
216- if isinstance (val , list ):
217- val = [
218- self ._service_provider .get_role_id (self ._project , i ) for i in val
219- ]
220- else :
221- val = self ._service_provider .get_role_id (self ._project , val )
222- return val
223-
224-
225234class UserFilterHandler (BaseCustomFieldHandler ):
226235 def _handle_special_fields (self , keys : List [str ], val ):
227236 """
228237 Handle special fields like 'custom_fields__'.
229238 """
230- if keys [0 ] == "custom_field" :
231- component_id = self ._service_provider .get_custom_field_component_id (
232- field_id = int (keys [1 ]), entity = self ._entity
233- )
234- if component_id == CustomFieldType .DATE_PICKER .value :
235- try :
236- val = val * 1000
237- except Exception :
238- raise AppException ("Invalid custom field value provided." )
239- elif keys [0 ] == "role" :
239+ if keys [0 ] == "role" :
240240 try :
241241 if isinstance (val , list ):
242242 val = [UserRole .__getitem__ (i .upper ()).value for i in val ]
@@ -247,14 +247,14 @@ def _handle_special_fields(self, keys: List[str], val):
247247 except (KeyError , AttributeError ):
248248 raise AppException ("Invalid user role provided." )
249249 elif keys [0 ] == "state" :
250- valid_states = [ "PENDING" , "CONFIRMED" ]
251- if isinstance (val , list ):
252- for state in val :
253- if state not in valid_states :
254- raise AppException ( "Invalid user state provided." )
255- elif val not in valid_states :
250+ try :
251+ if isinstance (val , list ):
252+ val = [ WMUserStateEnum [ i ]. value for i in val ]
253+ else :
254+ val = WMUserStateEnum [ val ]. value
255+ except ( TypeError , KeyError ) :
256256 raise AppException ("Invalid user state provided." )
257- return val
257+ return super (). _handle_special_fields ( keys , val )
258258
259259
260260class ProjectFilterHandler (BaseCustomFieldHandler ):
@@ -268,16 +268,7 @@ def _handle_special_fields(self, keys: List[str], val):
268268 if isinstance (val , (list , tuple , set ))
269269 else ProjectStatus (val ).value
270270 )
271- elif keys [0 ] == "custom_field" :
272- component_id = self ._service_provider .get_custom_field_component_id (
273- field_id = int (keys [1 ]), entity = self ._entity
274- )
275- if component_id == CustomFieldType .DATE_PICKER .value :
276- try :
277- val = val * 1000
278- except Exception :
279- raise AppException ("Invalid custom field value provided." )
280- return val
271+ return super ()._handle_special_fields (keys , val )
281272
282273
283274class QueryBuilderChain :
0 commit comments