Skip to content

Commit

Permalink
Complete migration of on_create and on_created methods
Browse files Browse the repository at this point in the history
SDESK-7456
  • Loading branch information
eos87 committed Jan 2, 2025
1 parent 4349b1a commit 31a172e
Show file tree
Hide file tree
Showing 8 changed files with 1,260 additions and 30 deletions.
15 changes: 12 additions & 3 deletions server/planning/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,10 +465,19 @@ def set_original_creator(doc):
doc["original_creator"] = user


def list_uniq_with_order(list):
def unique_items_in_order(input_list: list) -> list:
"""
Removes duplicates from a list while preserving the original order.
"""
seen = set()
seen_add = seen.add
return [x for x in list if not (x in seen or seen_add(x))]
unique_list = []

for item in input_list:
if item not in seen:
seen.add(item)
unique_list.append(item)

return unique_list


def set_ingested_event_state(updates, original):
Expand Down
20 changes: 16 additions & 4 deletions server/planning/core/service.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from typing import Generic, TypeVar

from typing import TypeVar, List
from apps.auth import get_user_id
from superdesk.core.resources.service import AsyncResourceService

from planning.types import BasePlanningModel
Expand All @@ -8,5 +8,17 @@
PlanningResourceModelType = TypeVar("PlanningResourceModelType", bound=BasePlanningModel)


class BasePlanningAsyncService(AsyncResourceService[Generic[PlanningResourceModelType]]):
pass
class BasePlanningAsyncService(AsyncResourceService[PlanningResourceModelType]):
"""Base async service to handle repetitive logic that is used throughout the planning project."""

async def on_create(self, docs: List[PlanningResourceModelType]) -> None:
"""
Sets `original_creator` and `version_creator` by default to avoid repeating this everywhere
"""
await super().on_create(docs)

user_id = get_user_id()
if user_id:
for doc in docs:
doc.original_creator = user_id
doc.version_creator = user_id
5 changes: 1 addition & 4 deletions server/planning/events/events_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ async def prepare_events_data(self, docs: list[EventResourceModel]) -> None:
if applicable, sets up planning schedules, and links events to planning items.
Args:
events (list[EventResourceModel]): A list of event models to prepare.
docs (list[EventResourceModel]): A list of event models to prepare.
Returns:
None: Modifies the input list in-place.
Expand All @@ -177,9 +177,6 @@ async def prepare_events_data(self, docs: list[EventResourceModel]) -> None:
if not event.language:
event.language = event.languages[0] if len(event.languages) > 0 else get_app_config("DEFAULT_LANGUAGE")

# TODO-ASYNC: consider moving this into base service later
event.original_creator = ObjectId(get_user_id()) or None

# overwrite expiry date if needed
self._overwrite_event_expiry_date(event)

Expand Down
4 changes: 2 additions & 2 deletions server/planning/planning/planning.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
update_post_item,
get_coverage_type_name,
set_original_creator,
list_uniq_with_order,
unique_items_in_order,
TEMP_ID_PREFIX,
DEFAULT_ASSIGNMENT_PRIORITY,
get_planning_allow_scheduled_updates,
Expand Down Expand Up @@ -337,7 +337,7 @@ def validate_planning(self, updates, original=None):

# Remove duplicate agendas
if len(updates.get("agendas", [])) > 0:
updates["agendas"] = list_uniq_with_order(updates["agendas"])
updates["agendas"] = unique_items_in_order(updates["agendas"])

# Validate scheduled updates
for coverage in updates.get("coverages") or []:
Expand Down
Loading

0 comments on commit 31a172e

Please sign in to comment.