Skip to content

Commit

Permalink
Replaced Any usage for better type checking
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianMwangi21 committed Dec 19, 2024
1 parent b8f2958 commit 87bde0d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
4 changes: 2 additions & 2 deletions server/planning/events/events_history_async_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@


class EventsHistoryAsyncService(HistoryAsyncService[EventsHistoryResourceModel]):
async def on_item_created(self, items: list[EventResourceModel | Any], operation: str | None = None):
async def on_item_created(self, items: list[dict[str, Any]], operation: str | None = None):
created_from_planning = []
regular_events = []
for item in items:
Expand Down Expand Up @@ -59,7 +59,7 @@ async def on_item_updated(self, updates: dict[str, Any], original: dict[str, Any

await self._save_history(item, diff, operation)

async def _save_history(self, item, update: dict[str, Any], operation: str | None = None):
async def _save_history(self, item: dict[str, Any], update: dict[str, Any], operation: str | None = None):
history = {
"event_id": item[ID_FIELD],
"user_id": self.get_user_id(),
Expand Down
16 changes: 8 additions & 8 deletions server/planning/history_async_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
class HistoryAsyncService(AsyncResourceService[Generic[HistoryResourceModelType]]):
"""Provide common async methods for tracking history of Creation, Updates and Spiking to collections"""

async def on_item_created(self, items: list[Any], operation: str | None = None):
async def on_item_created(self, items: list[dict[str, Any]], operation: str | None = None):
for item in items:
if not item.get("duplicate_from"):
await self._save_history(
Expand All @@ -56,7 +56,7 @@ async def on_item_created(self, items: list[Any], operation: str | None = None):
operation or "create",
)

async def on_item_updated(self, updates: dict[str, Any], original: Any, operation: str | None = None):
async def on_item_updated(self, updates: dict[str, Any], original: dict[str, Any], operation: str | None = None):
item = deepcopy(original)
if list(item.keys()) == ["_id"]:
diff = updates
Expand All @@ -67,24 +67,24 @@ async def on_item_updated(self, updates: dict[str, Any], original: Any, operatio

await self._save_history(item, diff, operation or "edited")

async def on_spike(self, updates: dict[str, Any], original: Any):
async def on_spike(self, updates: dict[str, Any], original: dict[str, Any]):
await self.on_item_updated(updates, original, "spiked")

async def on_unspike(self, updates: dict[str, Any], original: Any):
async def on_unspike(self, updates: dict[str, Any], original: dict[str, Any]):
await self.on_item_updated(updates, original, "unspiked")

async def on_cancel(self, updates: dict[str, Any], original: Any):
async def on_cancel(self, updates: dict[str, Any], original: dict[str, Any]):
operation = "events_cancel" if original.get(ITEM_TYPE) == "event" else "planning_cancel"
await self.on_item_updated(updates, original, operation)

async def on_reschedule(self, updates: dict[str, Any], original: Any):
async def on_reschedule(self, updates: dict[str, Any], original: dict[str, Any]):
await self.on_item_updated(updates, original, "reschedule")

async def on_reschedule_from(self, item: Any):
async def on_reschedule_from(self, item: dict[str, Any]):
new_item = deepcopy(item)
await self._save_history({ID_FIELD: str(item[ID_FIELD])}, new_item, "reschedule_from")

async def on_postpone(self, updates: dict[str, Any], original: Any):
async def on_postpone(self, updates: dict[str, Any], original: dict[str, Any]):
await self.on_item_updated(updates, original, "postpone")

async def get_user_id(self):
Expand Down
19 changes: 9 additions & 10 deletions server/planning/planning/planning_history_async_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from typing import Any


from planning.core.service import PlanningResourceModelType
from planning.types import PlanningHistoryResourceModel
from superdesk.flask import request
from superdesk.resource_fields import ID_FIELD
Expand All @@ -35,7 +36,7 @@


class PlanningHistoryAsyncService(HistoryAsyncService[PlanningHistoryResourceModel]):
async def on_item_created(self, items: list[PlanningResourceModel], operation=None):
async def on_item_created(self, items: list[dict[str, Any]], operation=None):
add_to_planning = False
if request and hasattr(request, "args"):
add_to_planning = strtobool(request.args.get("add_to_planning", "false"))
Expand Down Expand Up @@ -63,14 +64,12 @@ async def _save_history(self, item, update: dict[str, Any], operation: str | Non

await self.create([history])

async def on_item_updated(
self, updates: dict[str, Any], original: PlanningResourceModel, operation: str | None = None
):
item = deepcopy(original.to_dict())
async def on_item_updated(self, updates: dict[str, Any], original: dict[str, Any], operation: str | None = None):
item = deepcopy(original)
if list(item.keys()) == ["_id"]:
diff = self._remove_unwanted_fields(updates)
else:
diff = await self._changes(original.to_dict(), updates)
diff = await self._changes(original, updates)
diff.pop("coverages", None)
if updates:
item.update(updates)
Expand All @@ -87,9 +86,9 @@ async def on_item_updated(

await self._save_history(item, diff, operation)

await self._save_coverage_history(updates, original.to_dict())
await self._save_coverage_history(updates, original)

async def on_cancel(self, updates: dict[str, Any], original):
async def on_cancel(self, updates: dict[str, Any], original: dict[str, Any]):
await self.on_item_updated(
updates,
original,
Expand Down Expand Up @@ -181,7 +180,7 @@ async def _save_coverage_history(self, updates: dict[str, Any], original: dict[s
for cov in deleted:
await self._save_history(item, {"coverage_id": cov.get("coverage_id")}, "coverage_deleted")

async def on_spike(self, updates: dict[str, Any], original: PlanningResourceModel):
async def on_spike(self, updates: dict[str, Any], original: dict[str, Any]):
"""Spike event
On spike of a planning item the history of any agendas that the item belongs to will have an entry added to
Expand All @@ -192,7 +191,7 @@ async def on_spike(self, updates: dict[str, Any], original: PlanningResourceMode
"""
await super().on_spike(updates, original)

async def on_unspike(self, updates: dict[str, Any], original: PlanningResourceModel):
async def on_unspike(self, updates: dict[str, Any], original: dict[str, Any]):
await super().on_unspike(updates, original)

async def on_duplicate(self, parent: dict[str, Any], duplicate: dict[str, Any]):
Expand Down

0 comments on commit 87bde0d

Please sign in to comment.