Skip to content

Commit

Permalink
Enable creation of new events in update_events
Browse files Browse the repository at this point in the history
  • Loading branch information
dhomeier committed Jun 25, 2024
1 parent 80c0936 commit 720acea
Showing 1 changed file with 32 additions and 32 deletions.
64 changes: 32 additions & 32 deletions spond/spond.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,28 +313,26 @@ async def get_event(self, uid: str) -> dict:
@_SpondBase.require_authentication
async def update_event(self, uid: str, updates: dict):
"""
Updates an existing event.
Updates an existing event or creates a new one.
Parameters
----------
uid : str
UID of the event.
UID of the event to be updated. If no event of that UID exists,
a new one will be created with default settings.
updates : dict
The changes. e.g. if you want to change the description -> {'description': "New Description with changes"}
The changes to existing event or default template for new events.
e.g. if you want to change the description ->
{'description': "New Description with changes"}
For a new event this should at a minimum include entries for
(list of) 'owners', 'recipients' (a dict of {"group": {"id": GID}}),
'heading', 'startTimestamp', 'endTimestamp' (in datetime.isoformat).
Returns
-------
json results of post command
"""
if not self.events:
await self.get_events()
for event in self.events:
if event["id"] == uid:
break

url = f"{self.api_url}sponds/{uid}"

base_event: dict = {
"heading": None,
"description": None,
Expand All @@ -358,37 +356,39 @@ async def update_event(self, uid: str, updates: dict):
"autoAccept": False,
"payment": {},
"attachments": [],
"tasks": {
"openTasks": [],
"assignedTasks": [
{
"name": None,
"description": "",
"type": "ASSIGNED",
"id": None,
"adultsOnly": True,
"assignments": {"memberIds": [], "profiles": [], "remove": []},
}
],
},
"recipients": {"group": {"id": None}},
"tasks": {"openTasks": [], "assignedTasks": []},
}
data = dict(base_event)

if not self.events:
await self.get_events()
for event in self.events:
if event["id"] == uid:
base_event.update(event)
url = f"{self.API_BASE_URL}sponds/{uid}"
data.update(event)
url = f"{self.api_url}sponds/{uid}"
break
else:
errmsg = f"No event with id='{uid}' existing"
raise ValueError(errmsg)

for key in base_event:
# No event of that id, create a new one (id to be set by Spond)
if (
len(updates.get("owners", [])) < 1
or updates["owners"][0].get("id") is None
):
errmsg = '"owners" need to have a valid user id'
raise ValueError(errmsg)
if (
"recipients" not in updates
or updates["recipients"].get("group").get("id") is None
):
errmsg = '"recipients" need to contain a "group" with valid id'
raise ValueError(errmsg)
updates.pop("id", None)
url = f"{self.api_url}sponds"

for key in data:
if updates.get(key) is not None:
base_event[key] = updates[key]
data[key] = updates[key]

data = dict(base_event)
async with self.clientsession.post(
url, json=data, headers=self.auth_headers
) as r:
Expand Down

0 comments on commit 720acea

Please sign in to comment.