Skip to content

Commit 720acea

Browse files
committed
Enable creation of new events in update_events
1 parent 80c0936 commit 720acea

File tree

1 file changed

+32
-32
lines changed

1 file changed

+32
-32
lines changed

spond/spond.py

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -313,28 +313,26 @@ async def get_event(self, uid: str) -> dict:
313313
@_SpondBase.require_authentication
314314
async def update_event(self, uid: str, updates: dict):
315315
"""
316-
Updates an existing event.
316+
Updates an existing event or creates a new one.
317317
318318
Parameters
319319
----------
320320
uid : str
321-
UID of the event.
321+
UID of the event to be updated. If no event of that UID exists,
322+
a new one will be created with default settings.
322323
updates : dict
323-
The changes. e.g. if you want to change the description -> {'description': "New Description with changes"}
324+
The changes to existing event or default template for new events.
325+
e.g. if you want to change the description ->
326+
{'description': "New Description with changes"}
327+
For a new event this should at a minimum include entries for
328+
(list of) 'owners', 'recipients' (a dict of {"group": {"id": GID}}),
329+
'heading', 'startTimestamp', 'endTimestamp' (in datetime.isoformat).
324330
325331
Returns
326332
-------
327333
json results of post command
328334
329335
"""
330-
if not self.events:
331-
await self.get_events()
332-
for event in self.events:
333-
if event["id"] == uid:
334-
break
335-
336-
url = f"{self.api_url}sponds/{uid}"
337-
338336
base_event: dict = {
339337
"heading": None,
340338
"description": None,
@@ -358,37 +356,39 @@ async def update_event(self, uid: str, updates: dict):
358356
"autoAccept": False,
359357
"payment": {},
360358
"attachments": [],
361-
"tasks": {
362-
"openTasks": [],
363-
"assignedTasks": [
364-
{
365-
"name": None,
366-
"description": "",
367-
"type": "ASSIGNED",
368-
"id": None,
369-
"adultsOnly": True,
370-
"assignments": {"memberIds": [], "profiles": [], "remove": []},
371-
}
372-
],
373-
},
359+
"recipients": {"group": {"id": None}},
360+
"tasks": {"openTasks": [], "assignedTasks": []},
374361
}
362+
data = dict(base_event)
375363

376364
if not self.events:
377365
await self.get_events()
378366
for event in self.events:
379367
if event["id"] == uid:
380-
base_event.update(event)
381-
url = f"{self.API_BASE_URL}sponds/{uid}"
368+
data.update(event)
369+
url = f"{self.api_url}sponds/{uid}"
382370
break
383371
else:
384-
errmsg = f"No event with id='{uid}' existing"
385-
raise ValueError(errmsg)
386-
387-
for key in base_event:
372+
# No event of that id, create a new one (id to be set by Spond)
373+
if (
374+
len(updates.get("owners", [])) < 1
375+
or updates["owners"][0].get("id") is None
376+
):
377+
errmsg = '"owners" need to have a valid user id'
378+
raise ValueError(errmsg)
379+
if (
380+
"recipients" not in updates
381+
or updates["recipients"].get("group").get("id") is None
382+
):
383+
errmsg = '"recipients" need to contain a "group" with valid id'
384+
raise ValueError(errmsg)
385+
updates.pop("id", None)
386+
url = f"{self.api_url}sponds"
387+
388+
for key in data:
388389
if updates.get(key) is not None:
389-
base_event[key] = updates[key]
390+
data[key] = updates[key]
390391

391-
data = dict(base_event)
392392
async with self.clientsession.post(
393393
url, json=data, headers=self.auth_headers
394394
) as r:

0 commit comments

Comments
 (0)