diff --git a/chat/indico_chat/controllers.py b/chat/indico_chat/controllers.py index 221d535..750bf09 100644 --- a/chat/indico_chat/controllers.py +++ b/chat/indico_chat/controllers.py @@ -39,11 +39,9 @@ class RHChatEventPage(RHConferenceBaseDisplay): def _process(self): try: - event_id = int(self._conf.id) + chatrooms = ChatroomEventAssociation.find_for_event(self._conf).all() except ValueError: raise IndicoError('This page is not available for legacy events.') - chatrooms = ChatroomEventAssociation.find_all(ChatroomEventAssociation.event_id == event_id, - ~ChatroomEventAssociation.hidden) cols = set() if any(c.chatroom.description for c in chatrooms): cols.add('description') @@ -71,8 +69,7 @@ class RHChatManageEvent(RHChatManageEventBase): """Lists the chatrooms of an event""" def _process(self): - chatrooms = ChatroomEventAssociation.find_all(ChatroomEventAssociation.event_id == self.event_id, - _eager='chatroom.events') + chatrooms = ChatroomEventAssociation.find_for_event(self.event, include_hidden=True, _eager='chatroom.events') chatroom_filter = (~Chatroom.id.in_(x.chatroom_id for x in chatrooms)) if chatrooms else True available_chatrooms = Chatroom.find_all(Chatroom.created_by_id == int(session.user.id), chatroom_filter) return WPChatEventMgmt.render_template('manage_event.html', self._conf, event_chatrooms=chatrooms, diff --git a/chat/indico_chat/models/chatrooms.py b/chat/indico_chat/models/chatrooms.py index c25f9f4..58c379c 100644 --- a/chat/indico_chat/models/chatrooms.py +++ b/chat/indico_chat/models/chatrooms.py @@ -172,3 +172,16 @@ class ChatroomEventAssociation(db.Model): @return_ascii def __repr__(self): return ''.format(self.event_id, self.chatroom) + + @classmethod + def find_for_event(cls, event, include_hidden=False, **kwargs): + """Returns a Query that retrieves the chatrooms for an event + + :param event: an indico event (with a numeric ID) + :param include_hidden: if hidden chatrooms should be included, too + :param kwargs: extra kwargs to pass to ``find()`` + """ + query = cls.find(event_id=int(event.id), **kwargs) + if not include_hidden: + query = query.filter(~cls.hidden) + return query diff --git a/chat/indico_chat/plugin.py b/chat/indico_chat/plugin.py index 1b3d8e0..861e600 100644 --- a/chat/indico_chat/plugin.py +++ b/chat/indico_chat/plugin.py @@ -98,8 +98,7 @@ class ChatPlugin(IndicoPlugin): self.register_js_bundle('chat_js', 'js/chat.js') def inject_event_header(self, event, **kwargs): - chatrooms = ChatroomEventAssociation.find_all(ChatroomEventAssociation.event_id == event.id, - ~ChatroomEventAssociation.hidden) + chatrooms = ChatroomEventAssociation.find_for_event(event).all() if not chatrooms: return '' how_to_connect = self.settings.get('how_to_connect') @@ -107,8 +106,7 @@ class ChatPlugin(IndicoPlugin): how_to_connect=how_to_connect, chat_links=self.settings.get('chat_links')) def _has_visible_chatrooms(self, event): - return bool(ChatroomEventAssociation.find(ChatroomEventAssociation.event_id == event.id, - ~ChatroomEventAssociation.hidden).count()) + return bool(ChatroomEventAssociation.find_for_event(event).count()) def extend_event_menu(self, sender, **kwargs): return EventMenuEntry('chat.event_page', 'Chat Rooms', name='chat-event-page', plugin=True,