89 lines
2.7 KiB
Python
89 lines
2.7 KiB
Python
# Generated by Django 5.2.4 on 2026-08-05 05:23
|
|
|
|
from django.conf import settings
|
|
from django.db import migrations, models
|
|
|
|
|
|
def populate_task_participants(apps, schema_editor):
|
|
Task = apps.get_model("tasks", "Task")
|
|
TaskActivity = apps.get_model("tasks", "TaskActivity")
|
|
TaskAttachment = apps.get_model("tasks", "TaskAttachment")
|
|
TaskChecklistItem = apps.get_model("tasks", "TaskChecklistItem")
|
|
TaskExternalLink = apps.get_model("tasks", "TaskExternalLink")
|
|
TaskLinkedObject = apps.get_model("tasks", "TaskLinkedObject")
|
|
through = Task.participants.through
|
|
task_field = next(
|
|
field
|
|
for field in through._meta.fields
|
|
if field.is_relation and field.related_model == Task
|
|
)
|
|
employee_field = next(
|
|
field
|
|
for field in through._meta.fields
|
|
if field.is_relation and field.related_model != Task
|
|
)
|
|
|
|
pairs = set()
|
|
for task_id, created_by_id, assignee_id in Task.objects.values_list(
|
|
"id",
|
|
"created_by_id",
|
|
"assignee_id",
|
|
):
|
|
pairs.add((task_id, created_by_id))
|
|
if assignee_id:
|
|
pairs.add((task_id, assignee_id))
|
|
|
|
contributor_fields = [
|
|
(TaskActivity, "actor_id"),
|
|
(TaskAttachment, "uploaded_by_id"),
|
|
(TaskChecklistItem, "created_by_id"),
|
|
(TaskChecklistItem, "completed_by_id"),
|
|
(TaskExternalLink, "created_by_id"),
|
|
(TaskLinkedObject, "created_by_id"),
|
|
]
|
|
for model, employee_id_field in contributor_fields:
|
|
pairs.update(
|
|
(task_id, employee_id)
|
|
for task_id, employee_id in model.objects.exclude(
|
|
**{f"{employee_id_field}__isnull": True}
|
|
).values_list("task_id", employee_id_field)
|
|
)
|
|
|
|
through.objects.bulk_create(
|
|
[
|
|
through(
|
|
**{
|
|
task_field.attname: task_id,
|
|
employee_field.attname: employee_id,
|
|
}
|
|
)
|
|
for task_id, employee_id in pairs
|
|
if task_id and employee_id
|
|
],
|
|
ignore_conflicts=True,
|
|
)
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
dependencies = [
|
|
("tasks", "0018_task_claim_activity"),
|
|
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
|
]
|
|
|
|
operations = [
|
|
migrations.AddField(
|
|
model_name="task",
|
|
name="participants",
|
|
field=models.ManyToManyField(
|
|
blank=True,
|
|
related_name="participating_tasks",
|
|
to=settings.AUTH_USER_MODEL,
|
|
verbose_name="Участники",
|
|
),
|
|
),
|
|
migrations.RunPython(
|
|
populate_task_participants,
|
|
migrations.RunPython.noop,
|
|
),
|
|
]
|