|
12 | 12 | from django.db import IntegrityError, transaction |
13 | 13 | from drf_spectacular.utils import extend_schema_field |
14 | 14 | from fsm.serializer_fields import FSMStateField |
| 15 | +from fsm.state_manager import get_state_manager |
| 16 | +from fsm.utils import is_fsm_enabled |
15 | 17 | from label_studio_sdk.label_interface import LabelInterface |
16 | 18 | from projects.models import Project |
17 | 19 | from rest_flex_fields import FlexFieldsModelSerializer |
@@ -689,8 +691,26 @@ def add_tasks(self, task_annotations, task_predictions, validated_tasks): |
689 | 691 |
|
690 | 692 | logging.info(f'Tasks serialization success, len = {len(self.db_tasks)}') |
691 | 693 |
|
| 694 | + # Backfill FSM states for bulk-created tasks |
| 695 | + # bulk_create() bypasses save() so FSM transitions don't fire automatically |
| 696 | + self._backfill_fsm_states(self.db_tasks) |
| 697 | + |
692 | 698 | return db_tasks |
693 | 699 |
|
| 700 | + def _backfill_fsm_states(self, tasks): |
| 701 | + """ |
| 702 | + Backfill FSM states for tasks created via bulk_create(). |
| 703 | +
|
| 704 | + bulk_create() bypasses the model's save() method, so FSM transitions |
| 705 | + don't fire automatically. This sets initial CREATED state for newly imported tasks. |
| 706 | + """ |
| 707 | + if not tasks or not is_fsm_enabled(user=None): |
| 708 | + return |
| 709 | + |
| 710 | + StateManager = get_state_manager() |
| 711 | + for task in tasks: |
| 712 | + StateManager.execute_transition(entity=task, transition_name='task_created', user=None) |
| 713 | + |
694 | 714 | @staticmethod |
695 | 715 | def post_process_annotations(user, db_annotations, action): |
696 | 716 | pass |
|
0 commit comments