Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SDESK-7470] - Create new async Agenda resource, service & REST API #2172

Merged
merged 9 commits into from
Jan 9, 2025
1 change: 1 addition & 0 deletions server/planning/agendas.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class AgendasResource(Resource):
"PATCH": "planning_agenda_management",
"DELETE": "planning_agenda_management",
}
internal_resource = True


class AgendasService(Service):
Expand Down
4 changes: 4 additions & 0 deletions server/planning/agendas_async/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .agendas_async_service import AgendasAsyncService
from .module import agendas_resource_config

__all__ = ["AgendasAsyncService", "agendas_resource_config"]
57 changes: 57 additions & 0 deletions server/planning/agendas_async/agendas_async_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from typing import Any
from planning.core.service import BasePlanningAsyncService
from planning.types import AgendasResourceModel
from planning.common import set_original_creator
from apps.auth import get_user_id
from superdesk import get_resource_service
from superdesk.errors import SuperdeskApiError
from superdesk.notification import push_notification


class AgendasAsyncService(BasePlanningAsyncService[AgendasResourceModel]):
# TODO-ASYNC: on_fetched mechanism to be added to the base REST API class
# async def _generate_planning_info(self, docs):
# # TODO-ASYNC: Change this when async planning service is done
# planning_service = get_resource_service("planning")
# for doc in docs:
# doc["plannings"] = planning_service.get_planning_by_agenda_id(doc.get(ID_FIELD)).docs
#
# async def on_fetched(self, docs):
# await self._generate_planning_info(docs.get(ITEMS))
#
# async def on_fetched_item(self, doc):
# await self._generate_planning_info([doc])

async def on_create(self, docs: list[AgendasResourceModel]) -> None:
for doc in docs:
doc_dict = doc.to_dict()
set_original_creator(doc_dict)
BrianMwangi21 marked this conversation as resolved.
Show resolved Hide resolved

async def on_created(self, docs: list[AgendasResourceModel]) -> None:
for doc in docs:
push_notification(
"agenda:created", item=str(doc.id), user=str(doc.original_creator) if doc.original_creator else None
)

async def on_update(self, updates: dict[str, Any], original: AgendasResourceModel) -> None:
user_id = get_user_id()
if user_id:
updates["version_creator"] = get_user_id()

async def on_updated(self, updates: dict[str, Any], original: AgendasResourceModel) -> None:
# await self._generate_planning_info([updates])
push_notification(
"agenda:updated",
item=str(original.id),
user=str(updates.get("version_creator", "")),
)

async def on_delete(self, doc: AgendasResourceModel):
# TODO-ASYNC: Change this when async planning service is done
if get_resource_service("planning").get_planning_by_agenda_id(doc.id).count() > 0:
raise SuperdeskApiError.badRequestError(
message="Agenda is referenced by Planning items. " "Cannot delete Agenda"
)
BrianMwangi21 marked this conversation as resolved.
Show resolved Hide resolved

async def on_deleted(self, doc: AgendasResourceModel):
push_notification("agenda:deleted", item=str(doc.id))
14 changes: 14 additions & 0 deletions server/planning/agendas_async/module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from superdesk.core.resources import (
ResourceConfig,
RestEndpointConfig,
)

from .agendas_async_service import AgendasAsyncService
from planning.types import AgendasResourceModel

agendas_resource_config = ResourceConfig(
name="agenda",
data_class=AgendasResourceModel,
service=AgendasAsyncService,
rest_endpoints=RestEndpointConfig(resource_methods=["GET", "POST"], item_methods=["GET", "PATCH", "DELETE"]),
)
2 changes: 2 additions & 0 deletions server/planning/module.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from superdesk.core.module import Module
from planning.agendas_async import agendas_resource_config
from planning.events import events_resource_config, events_history_resource_config
from planning.planning import planning_resource_config, planning_history_resource_config
from planning.assignments import assignments_resource_config, delivery_resource_config
Expand All @@ -17,5 +18,6 @@
planning_types_resource_config,
events_history_resource_config,
planning_history_resource_config,
agendas_resource_config,
],
)
2 changes: 2 additions & 0 deletions server/planning/types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from .assignment import AssignmentResourceModel
from .published import PublishedPlanningModel
from .enums import PostStates, UpdateMethods, WorkflowState
from .agendas import AgendasResourceModel
from .planning_types import PlanningTypesResourceModel


Expand All @@ -40,6 +41,7 @@
"PostStates",
"UpdateMethods",
"WorkflowState",
"AgendasResourceModel",
]


Expand Down
11 changes: 11 additions & 0 deletions server/planning/types/agendas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from pydantic import Field
from typing import Annotated

from .base import BasePlanningModel
from superdesk.core.resources import fields
from superdesk.core.resources.validators import validate_iunique_value_async


class AgendasResourceModel(BasePlanningModel):
name: Annotated[fields.Keyword, validate_iunique_value_async("agendas", "name")]
is_enabled: bool = Field(default=True)
BrianMwangi21 marked this conversation as resolved.
Show resolved Hide resolved
Loading