146 lines
6.8 KiB
Python
146 lines
6.8 KiB
Python
from typing import AsyncGenerator
|
|
import strawberry
|
|
from channels.db import database_sync_to_async
|
|
from strawberry.types import Info
|
|
from core.graphql.pubsub import pubsub
|
|
from core.graphql.types.project_scope_template import (
|
|
ProjectScopeTemplateType,
|
|
ProjectAreaTemplateType,
|
|
ProjectTaskTemplateType,
|
|
)
|
|
from core.graphql.utils import _extract_id
|
|
from core.models.project_scope_template import (
|
|
ProjectScopeTemplate,
|
|
ProjectAreaTemplate,
|
|
ProjectTaskTemplate,
|
|
)
|
|
|
|
|
|
@strawberry.type
|
|
class Subscription:
|
|
# Template
|
|
@strawberry.subscription(description="Subscribe to project scope template creation events")
|
|
async def project_scope_template_created(self, info: Info) -> AsyncGenerator[ProjectScopeTemplateType, None]:
|
|
user = info.context.user
|
|
if not user or not user.is_authenticated:
|
|
raise PermissionError("Authentication required")
|
|
|
|
async with pubsub.subscribe("project_scope_template_created") as subscriber:
|
|
async for payload in subscriber:
|
|
entity_id = await _extract_id(payload)
|
|
try:
|
|
instance = await database_sync_to_async(ProjectScopeTemplate.objects.get)(pk=entity_id)
|
|
except ProjectScopeTemplate.DoesNotExist:
|
|
continue
|
|
yield instance
|
|
|
|
@strawberry.subscription(description="Subscribe to project scope template updates")
|
|
async def project_scope_template_updated(self, info: Info) -> AsyncGenerator[ProjectScopeTemplateType, None]:
|
|
user = info.context.user
|
|
if not user or not user.is_authenticated:
|
|
raise PermissionError("Authentication required")
|
|
|
|
async with pubsub.subscribe("project_scope_template_updated") as subscriber:
|
|
async for payload in subscriber:
|
|
entity_id = await _extract_id(payload)
|
|
try:
|
|
instance = await database_sync_to_async(ProjectScopeTemplate.objects.get)(pk=entity_id)
|
|
except ProjectScopeTemplate.DoesNotExist:
|
|
continue
|
|
yield instance
|
|
|
|
@strawberry.subscription(description="Subscribe to project scope template deletion events")
|
|
async def project_scope_template_deleted(self, info: Info) -> AsyncGenerator[strawberry.ID, None]:
|
|
user = info.context.user
|
|
if not user or not user.is_authenticated:
|
|
raise PermissionError("Authentication required")
|
|
|
|
async with pubsub.subscribe("project_scope_template_deleted") as subscriber:
|
|
async for payload in subscriber:
|
|
entity_id = await _extract_id(payload)
|
|
yield strawberry.ID(entity_id)
|
|
|
|
# Area template
|
|
@strawberry.subscription(description="Subscribe to project area template creation events")
|
|
async def project_area_template_created(self, info: Info) -> AsyncGenerator[ProjectAreaTemplateType, None]:
|
|
user = info.context.user
|
|
if not user or not user.is_authenticated:
|
|
raise PermissionError("Authentication required")
|
|
|
|
async with pubsub.subscribe("project_area_template_created") as subscriber:
|
|
async for payload in subscriber:
|
|
entity_id = await _extract_id(payload)
|
|
try:
|
|
instance = await database_sync_to_async(ProjectAreaTemplate.objects.get)(pk=entity_id)
|
|
except ProjectAreaTemplate.DoesNotExist:
|
|
continue
|
|
yield instance
|
|
|
|
@strawberry.subscription(description="Subscribe to project area template updates")
|
|
async def project_area_template_updated(self, info: Info) -> AsyncGenerator[ProjectAreaTemplateType, None]:
|
|
user = info.context.user
|
|
if not user or not user.is_authenticated:
|
|
raise PermissionError("Authentication required")
|
|
|
|
async with pubsub.subscribe("project_area_template_updated") as subscriber:
|
|
async for payload in subscriber:
|
|
entity_id = await _extract_id(payload)
|
|
try:
|
|
instance = await database_sync_to_async(ProjectAreaTemplate.objects.get)(pk=entity_id)
|
|
except ProjectAreaTemplate.DoesNotExist:
|
|
continue
|
|
yield instance
|
|
|
|
@strawberry.subscription(description="Subscribe to project area template deletion events")
|
|
async def project_area_template_deleted(self, info: Info) -> AsyncGenerator[strawberry.ID, None]:
|
|
user = info.context.user
|
|
if not user or not user.is_authenticated:
|
|
raise PermissionError("Authentication required")
|
|
|
|
async with pubsub.subscribe("project_area_template_deleted") as subscriber:
|
|
async for payload in subscriber:
|
|
entity_id = await _extract_id(payload)
|
|
yield strawberry.ID(entity_id)
|
|
|
|
# Task template
|
|
@strawberry.subscription(description="Subscribe to project task template creation events")
|
|
async def project_task_template_created(self, info: Info) -> AsyncGenerator[ProjectTaskTemplateType, None]:
|
|
user = info.context.user
|
|
if not user or not user.is_authenticated:
|
|
raise PermissionError("Authentication required")
|
|
|
|
async with pubsub.subscribe("project_task_template_created") as subscriber:
|
|
async for payload in subscriber:
|
|
entity_id = await _extract_id(payload)
|
|
try:
|
|
instance = await database_sync_to_async(ProjectTaskTemplate.objects.get)(pk=entity_id)
|
|
except ProjectTaskTemplate.DoesNotExist:
|
|
continue
|
|
yield instance
|
|
|
|
@strawberry.subscription(description="Subscribe to project task template updates")
|
|
async def project_task_template_updated(self, info: Info) -> AsyncGenerator[ProjectTaskTemplateType, None]:
|
|
user = info.context.user
|
|
if not user or not user.is_authenticated:
|
|
raise PermissionError("Authentication required")
|
|
|
|
async with pubsub.subscribe("project_task_template_updated") as subscriber:
|
|
async for payload in subscriber:
|
|
entity_id = await _extract_id(payload)
|
|
try:
|
|
instance = await database_sync_to_async(ProjectTaskTemplate.objects.get)(pk=entity_id)
|
|
except ProjectTaskTemplate.DoesNotExist:
|
|
continue
|
|
yield instance
|
|
|
|
@strawberry.subscription(description="Subscribe to project task template deletion events")
|
|
async def project_task_template_deleted(self, info: Info) -> AsyncGenerator[strawberry.ID, None]:
|
|
user = info.context.user
|
|
if not user or not user.is_authenticated:
|
|
raise PermissionError("Authentication required")
|
|
|
|
async with pubsub.subscribe("project_task_template_deleted") as subscriber:
|
|
async for payload in subscriber:
|
|
entity_id = await _extract_id(payload)
|
|
yield strawberry.ID(entity_id)
|