Skip to content

Commit

Permalink
[SDESK-7470] - Create new async Agenda resource, service & REST API (#…
Browse files Browse the repository at this point in the history
…2172)

* Create resource model for agenda resource

* Create new async resource service for agendas

* Create resource config for agenda with REST API

* Register the resource config with the planning module

* Disable web endpoints in old agenda resource

* Suggested fixes

* Suggested fixes

* Black reformat
  • Loading branch information
BrianMwangi21 authored Jan 9, 2025
1 parent 45668fb commit f7d2619
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 1 deletion.
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"]
51 changes: 51 additions & 0 deletions server/planning/agendas_async/agendas_async_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from typing import Any
from planning.core.service import BasePlanningAsyncService
from planning.types import AgendasResourceModel
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 get_planning_by_agenda_id is added to utils
# 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_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 get_planning_by_agenda_id is added to utils
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"
)

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"]),
)
10 changes: 9 additions & 1 deletion server/planning/core/service.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Generic, TypeVar

from apps.archive.common import get_user
from superdesk.core.resources.service import AsyncResourceService

from planning.types import BasePlanningModel
Expand All @@ -9,4 +10,11 @@


class BasePlanningAsyncService(AsyncResourceService[Generic[PlanningResourceModelType]]):
pass
async def on_create(self, docs: list[PlanningResourceModelType]) -> None:
await super().on_create(docs)

current_user = get_user()
if current_user:
for doc in docs:
doc.original_creator = current_user.id
doc.version_creator = current_user.id
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
10 changes: 10 additions & 0 deletions server/planning/types/agendas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
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 = True

0 comments on commit f7d2619

Please sign in to comment.