I need advice regarding a code snippet for python.
@firestore_fn.on_document_created(document=r"UserDeletes/{id}")
async def delete_user(event: firestore_fn.Event[firestore_fn.DocumentSnapshot | None]) -> None:
adminpass = firebase_admin.auth
database_updates_before_deletion = firestore.client()
#delete STORAGE items of user
databasephotos.bucket(f'profileImage/${event.data.get('userid')}').delete(force=True)
databasephotos.bucket(f'bannerImage/${event.data.get('userid')}').delete(force=True)
databasephotos.bucket(f'Audios/${event.data.get('textid')}/${event.data.get('userid')}').delete(force=True)
databasephotos.bucket(f'AudiosSamples/${event.data.get('textid')}/${event.data.get('userid')}').delete(force=True)
#delete all DOCUMENT audios from user
audios_to_delete = database_updates_before_deletion.collection('Audios').where(filter=FieldFilter('userid', '==', event.data.get('userid') )).get()
for doc in audios_to_delete:
await database_updates_before_deletion.collection('Audios').document(f'{doc.to_dict().get('id')}').delete()
audios_to_delete = database_updates_before_deletion.collection('SampleAudios').where(filter=FieldFilter('userid', '==', event.data.get('userid') )).get()
for doc in audios_to_delete:
await database_updates_before_deletion.collection('SampleAudios').document(f'{doc.to_dict().get('id')}').delete()
#get all texts to update, where the user deleted was the lastest narrator.
texts_to_update = await database_updates_before_deletion.collection('Texts').where(filter=FieldFilter('latestnarratorid', '==', event.data.get('userid'))).get()
if(len(texts_to_update) != 0):
#for every texts found
for doc in texts_to_update:
#get a different audio from the collection of narrations that the text has available
audio_replace = await database_updates_before_deletion.collection('Audios').where(FieldFilter('textid', '==', event.data.get('textid'))).limit(1).get()
#if one is found, get values needed. Else, the user deleted was the only narrator, so no document of audio was found
if(len(audio_replace) != 0):
audio_replace_id = audio_replace[0].to_dict().get('id')
audio_replace_name = audio_replace[0].to_dict().get('nameOfnarrator')
audio_replace_url = audio_replace[0].to_dict().get('urlfromstorage')
audio_replace_narratorid = audio_replace[0].to_dict().get('userid')
audio_text_count = audio_replace[0].to_dict().get('audiosCount')
else:
#case where user had the last audio narration of a text, update text with empty values.
audio_replace_id = ''
audio_replace_name = ''
audio_replace_url = ''
audio_replace_narratorid = ''
audio_text_count = 0
#update the document
database_updates_before_deletion.collection('Texts').document(f'${doc.to_dict().get('id')}').update({
'id': audio_replace_id,
'nameOfnarrator': audio_replace_name,
'urlfromstorage': audio_replace_url,
'userid':audio_replace_narratorid,
'audiosCount' : audio_text_count - 1,
})
#finally, delete user
the_user_id_to_delete = event.data.get('userid')
adminpass.delete_user(the_user_id_to_delete)
return
The goal is to delete all audios and storage that the user has, once the user decides to delete its profile. The method is run by firebase functions services.
I need a little advice on how to handle the asynchronous operation for the method.
My application lets users narrates parts of sermons written by preachers. I have it so that when a user uploads a audio, and it is approved, it will become the latest narration available. So when another user opens the text, that audio will be the first one available and ready to play.
When users decide to delete their account, I need to replace the latest narration with any other audio available for that text. If the user that decided to delete the account, was the only narrator, then the text will have 0 audios available, so a different User Interface for the text will be displayed when that happens.
I have little experience with python so I would appreciate a little feedback regarding how to handle the async await operations. Thank you.