From b495b7633aa2688284c65b32b0989711cea2d17c Mon Sep 17 00:00:00 2001 From: Benjamin Bertrand <benjamin.bertrand@esss.se> Date: Tue, 2 Jan 2018 21:08:27 +0100 Subject: [PATCH] Fix temporary ics_id generation - The query to retrieve used ICS ids was run at each iteration of the loop... - Use a set to quickly test the existence of used ICS ids --- app/models.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/app/models.py b/app/models.py index 6c333c9..4a2690e 100644 --- a/app/models.py +++ b/app/models.py @@ -49,17 +49,18 @@ def temporary_ics_ids(): def used_temporary_ics_ids(): - """Generator that returns the list of temporary ICS ids used""" + """Return a set with the temporary ICS ids used""" temporary_items = Item.query.filter( Item.ics_id.startswith( current_app.config['TEMPORARY_ICS_ID'])).all() - return (item.ics_id for item in temporary_items) + return {item.ics_id for item in temporary_items} def get_temporary_ics_id(): """Return a temporary ICS id that is available""" + used_temp_ics_ids = used_temporary_ics_ids() for ics_id in temporary_ics_ids(): - if ics_id not in used_temporary_ics_ids(): + if ics_id not in used_temp_ics_ids: return ics_id else: raise ValueError('No temporary ICS id available') -- GitLab