|
1 | 1 | import asyncio |
2 | 2 | import logging |
3 | 3 | import time |
| 4 | +import typing |
4 | 5 | from abc import ABC |
5 | 6 | from abc import abstractmethod |
6 | 7 | from functools import wraps |
|
24 | 25 | logger = logging.getLogger("sa") |
25 | 26 |
|
26 | 27 |
|
| 28 | +class EntityContext(typing.TypedDict, total=False): |
| 29 | + team_id: int |
| 30 | + project_id: Optional[int] |
| 31 | + |
| 32 | + |
27 | 33 | def divide_to_chunks(it, size): |
28 | 34 | it = iter(it) |
29 | 35 | return iter(lambda: tuple(islice(it, size)), ()) |
@@ -324,78 +330,104 @@ def get_annotation_status_name(self, project, status_value: int) -> str: |
324 | 330 |
|
325 | 331 | def get_custom_field_id( |
326 | 332 | self, |
327 | | - team_id: int, |
| 333 | + context: EntityContext, |
328 | 334 | field_name: str, |
329 | 335 | entity: CustomFieldEntityEnum, |
330 | 336 | parent: CustomFieldEntityEnum, |
331 | 337 | ) -> int: |
332 | 338 | if entity == CustomFieldEntityEnum.PROJECT: |
333 | | - custom_field_data = self._project_custom_field_cache.get(team_id) |
| 339 | + custom_field_data = self._project_custom_field_cache.get(context["team_id"]) |
334 | 340 | else: |
335 | 341 | if parent == CustomFieldEntityEnum.TEAM: |
336 | | - custom_field_data = self._team_user_custom_field_cache.get(team_id) |
| 342 | + custom_field_data = self._team_user_custom_field_cache.get( |
| 343 | + context["team_id"] |
| 344 | + ) |
337 | 345 | else: |
338 | | - custom_field_data = self._project_user_custom_field_cache.get(team_id) |
| 346 | + custom_field_data = self._project_user_custom_field_cache.get( |
| 347 | + context["project_id"] |
| 348 | + ) |
339 | 349 | if field_name in custom_field_data["custom_fields_name_id_map"]: |
340 | 350 | return custom_field_data["custom_fields_name_id_map"][field_name] |
341 | 351 | raise AppException("Invalid custom field name provided.") |
342 | 352 |
|
343 | 353 | def get_custom_field_name( |
344 | 354 | self, |
345 | | - team_id: int, |
| 355 | + context: EntityContext, |
346 | 356 | field_id: int, |
347 | 357 | entity: CustomFieldEntityEnum, |
348 | 358 | parent: CustomFieldEntityEnum, |
349 | 359 | ) -> str: |
350 | 360 | if entity == CustomFieldEntityEnum.PROJECT: |
351 | | - custom_field_data = self._project_custom_field_cache.get(team_id) |
| 361 | + custom_field_data = self._project_custom_field_cache.get(context["team_id"]) |
352 | 362 | else: |
353 | 363 | if parent == CustomFieldEntityEnum.TEAM: |
354 | | - custom_field_data = self._team_user_custom_field_cache.get(team_id) |
| 364 | + custom_field_data = self._team_user_custom_field_cache.get( |
| 365 | + context["team_id"] |
| 366 | + ) |
355 | 367 | else: |
356 | | - custom_field_data = self._project_user_custom_field_cache.get(team_id) |
| 368 | + custom_field_data = self._project_user_custom_field_cache.get( |
| 369 | + context["project_id"] |
| 370 | + ) |
357 | 371 | if field_id in custom_field_data["custom_fields_id_name_map"]: |
358 | 372 | return custom_field_data["custom_fields_id_name_map"][field_id] |
359 | 373 | raise AppException("Invalid custom field ID provided.") |
360 | 374 |
|
361 | 375 | def get_custom_field_component_id( |
362 | 376 | self, |
363 | | - team_id: int, |
| 377 | + context: EntityContext, |
364 | 378 | field_id: int, |
365 | 379 | entity: CustomFieldEntityEnum, |
366 | 380 | parent: CustomFieldEntityEnum, |
367 | 381 | ) -> str: |
368 | 382 | if entity == CustomFieldEntityEnum.PROJECT: |
369 | | - custom_field_data = self._project_custom_field_cache.get(team_id) |
| 383 | + custom_field_data = self._project_custom_field_cache.get(context["team_id"]) |
370 | 384 | else: |
371 | 385 | if parent == CustomFieldEntityEnum.TEAM: |
372 | | - custom_field_data = self._team_user_custom_field_cache.get(team_id) |
| 386 | + custom_field_data = self._team_user_custom_field_cache.get( |
| 387 | + context["team_id"] |
| 388 | + ) |
373 | 389 | else: |
374 | | - custom_field_data = self._project_user_custom_field_cache.get(team_id) |
| 390 | + custom_field_data = self._project_user_custom_field_cache.get( |
| 391 | + context["project_id"] |
| 392 | + ) |
375 | 393 | if field_id in custom_field_data["custom_fields_id_component_id_map"]: |
376 | 394 | return custom_field_data["custom_fields_id_component_id_map"][field_id] |
377 | 395 | raise AppException("Invalid custom field ID provided.") |
378 | 396 |
|
379 | 397 | def list_custom_field_names( |
380 | | - self, pk: int, entity: CustomFieldEntityEnum, parent: CustomFieldEntityEnum |
| 398 | + self, |
| 399 | + context: EntityContext, |
| 400 | + entity: CustomFieldEntityEnum, |
| 401 | + parent: CustomFieldEntityEnum, |
381 | 402 | ) -> list: |
382 | 403 | if entity == CustomFieldEntityEnum.PROJECT: |
383 | | - custom_field_data = self._project_custom_field_cache.get(pk) |
| 404 | + custom_field_data = self._project_custom_field_cache.get(context["team_id"]) |
384 | 405 | else: |
385 | 406 | if parent == CustomFieldEntityEnum.TEAM: |
386 | | - custom_field_data = self._team_user_custom_field_cache.get(pk) |
| 407 | + custom_field_data = self._team_user_custom_field_cache.get( |
| 408 | + context["team_id"] |
| 409 | + ) |
387 | 410 | else: |
388 | | - custom_field_data = self._project_user_custom_field_cache.get(pk) |
| 411 | + custom_field_data = self._project_user_custom_field_cache.get( |
| 412 | + context["project_id"] |
| 413 | + ) |
389 | 414 | return list(custom_field_data["custom_fields_name_id_map"].keys()) |
390 | 415 |
|
391 | 416 | def list_templates( |
392 | | - self, pk: int, entity: CustomFieldEntityEnum, parent: CustomFieldEntityEnum |
| 417 | + self, |
| 418 | + context: EntityContext, |
| 419 | + entity: CustomFieldEntityEnum, |
| 420 | + parent: CustomFieldEntityEnum, |
393 | 421 | ): |
394 | 422 | if entity == CustomFieldEntityEnum.PROJECT: |
395 | | - return self._project_custom_field_cache.get(pk)["templates"] |
| 423 | + return self._project_custom_field_cache.get(context["team_id"])["templates"] |
396 | 424 | elif entity == CustomFieldEntityEnum.CONTRIBUTOR: |
397 | 425 | if parent == CustomFieldEntityEnum.TEAM: |
398 | | - return self._team_user_custom_field_cache.get(pk)["templates"] |
| 426 | + return self._team_user_custom_field_cache.get(context["team_id"])[ |
| 427 | + "templates" |
| 428 | + ] |
399 | 429 | else: |
400 | | - return self._project_user_custom_field_cache.get(pk)["templates"] |
| 430 | + return self._project_user_custom_field_cache.get(context["project_id"])[ |
| 431 | + "templates" |
| 432 | + ] |
401 | 433 | raise AppException("Invalid entity provided.") |
0 commit comments