91 lines
2.0 KiB
Python
91 lines
2.0 KiB
Python
from django.db import transaction
|
|
|
|
from tasks.activity import create_task_activity
|
|
from tasks.models import TaskActivityAction, TaskLinkedObject
|
|
from tasks.realtime import send_task_board_update
|
|
|
|
|
|
class TaskLinkNotFound(Exception):
|
|
"""Raised when a task link cannot be found in the requested resource kind."""
|
|
|
|
|
|
@transaction.atomic
|
|
def link_task_object(
|
|
*,
|
|
task,
|
|
actor,
|
|
kind,
|
|
content_type,
|
|
object_id,
|
|
object_label,
|
|
object_type,
|
|
realtime_type,
|
|
):
|
|
link, created = TaskLinkedObject.objects.get_or_create(
|
|
task=task,
|
|
kind=kind,
|
|
content_type=content_type,
|
|
object_id=object_id,
|
|
defaults={"created_by": actor},
|
|
)
|
|
if created:
|
|
create_task_activity(
|
|
task,
|
|
actor,
|
|
TaskActivityAction.LINKED,
|
|
object_kind=kind,
|
|
object_id=object_id,
|
|
metadata={
|
|
"object_label": object_label,
|
|
"object_type": object_type,
|
|
},
|
|
)
|
|
|
|
send_task_board_update(
|
|
task.board,
|
|
"linked",
|
|
realtime_type,
|
|
object_id,
|
|
extra={"task_id": task.id, "link_id": link.id},
|
|
)
|
|
return link, created
|
|
|
|
|
|
@transaction.atomic
|
|
def unlink_task_object(
|
|
*,
|
|
task,
|
|
actor,
|
|
kind,
|
|
link_id,
|
|
object_type,
|
|
realtime_type,
|
|
):
|
|
try:
|
|
link = TaskLinkedObject.objects.get(
|
|
task=task,
|
|
kind=kind,
|
|
id=link_id,
|
|
)
|
|
except TaskLinkedObject.DoesNotExist as exc:
|
|
raise TaskLinkNotFound from exc
|
|
|
|
object_id = link.object_id
|
|
persisted_link_id = link.id
|
|
link.delete()
|
|
create_task_activity(
|
|
task,
|
|
actor,
|
|
TaskActivityAction.UNLINKED,
|
|
object_kind=kind,
|
|
object_id=object_id,
|
|
metadata={"object_type": object_type},
|
|
)
|
|
send_task_board_update(
|
|
task.board,
|
|
"unlinked",
|
|
realtime_type,
|
|
object_id,
|
|
extra={"task_id": task.id, "link_id": persisted_link_id},
|
|
)
|