@@ -313,28 +313,26 @@ async def get_event(self, uid: str) -> dict:
313
313
@_SpondBase .require_authentication
314
314
async def update_event (self , uid : str , updates : dict ):
315
315
"""
316
- Updates an existing event.
316
+ Updates an existing event or creates a new one .
317
317
318
318
Parameters
319
319
----------
320
320
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.
322
323
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).
324
330
325
331
Returns
326
332
-------
327
333
json results of post command
328
334
329
335
"""
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
-
338
336
base_event : dict = {
339
337
"heading" : None ,
340
338
"description" : None ,
@@ -358,37 +356,39 @@ async def update_event(self, uid: str, updates: dict):
358
356
"autoAccept" : False ,
359
357
"payment" : {},
360
358
"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" : []},
374
361
}
362
+ data = dict (base_event )
375
363
376
364
if not self .events :
377
365
await self .get_events ()
378
366
for event in self .events :
379
367
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 } "
382
370
break
383
371
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 :
388
389
if updates .get (key ) is not None :
389
- base_event [key ] = updates [key ]
390
+ data [key ] = updates [key ]
390
391
391
- data = dict (base_event )
392
392
async with self .clientsession .post (
393
393
url , json = data , headers = self .auth_headers
394
394
) as r :
0 commit comments