From 55802e46a78c0632f25259cb6f8a50a4a4ec3e50 Mon Sep 17 00:00:00 2001 From: Alex Iribarren Date: Sun, 17 Nov 2024 18:07:28 +0100 Subject: [PATCH] Reduce recursivity --- outlook/indico_outlook/plugin.py | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/outlook/indico_outlook/plugin.py b/outlook/indico_outlook/plugin.py index e5d1193a..60c95cc4 100644 --- a/outlook/indico_outlook/plugin.py +++ b/outlook/indico_outlook/plugin.py @@ -73,7 +73,7 @@ class OutlookUserPreferences(ExtraUserPreferences): 'outlook_favorite_categories': BooleanField(_('Sync favorite categories with Outlook'), [HiddenUnless('extra_outlook_active', preserve_data=True)], widget=SwitchWidget(), - description=_('Add all events in categories (and all subcategories) I mark as ' + description=_('Add all events in categories (and their first-level subcategories) I mark as ' 'favorite to my Outlook calendar')), 'outlook_status': SelectField(_('Outlook entry status'), [HiddenUnless('extra_outlook_active', preserve_data=True)], @@ -240,11 +240,13 @@ def event_updated(self, event, changes, **kwargs): if self._user_tracks_favorite_events(user): users_to_update.add(user) - # Now look for users that have marked as favorite that event's category (or any of its parents) - for category in event.category.chain_query.all(): - for user in category.favorite_of: - if self._user_tracks_favorite_categories(user) and event.can_access(user): - users_to_update.add(user) + # Now look for users that have marked as favorite that event's category (or its parent) + category_favorites = event.category.favorite_of + if event.category.parent: + category_favorites |= event.category.parent.favorite_of + for user in category_favorites: + if self._user_tracks_favorite_categories(user) and event.can_access(user): + users_to_update.add(user) for user in users_to_update: self.logger.info('Event data change: updating %s in %r', user, event) @@ -271,10 +273,12 @@ def event_deleted(self, event, **kwargs): for user in event.favorite_of: if self._user_tracks_favorite_events(user): users_to_update.add(user) - for category in event.category.chain_query.all(): - for user in category.favorite_of: - if self._user_tracks_favorite_categories(user) and event.can_access(user): - users_to_update.add(user) + category_favorites = event.category.favorite_of + if event.category.parent: + category_favorites |= event.category.parent.favorite_of + for user in category_favorites: + if self._user_tracks_favorite_categories(user) and event.can_access(user): + users_to_update.add(user) for user in users_to_update: self.logger.info('Event deletion: removing %s in %r', user, event) @@ -293,9 +297,11 @@ def _record_change(self, event, user, action): return if user in event.favorite_of and self._user_tracks_favorite_events(user): return - for category in event.category.chain_query.all(): - if user in category.favorite_of \ - and self._user_tracks_favorite_categories(user) \ + category_favorites = event.category.favorite_of + if event.category.parent: + category_favorites |= event.category.parent.favorite_of + for user in category_favorites: + if self._user_tracks_favorite_categories(user) \ and event.can_access(user): return