Add ChatroomEventAssociation.find_for_event method

This commit is contained in:
Adrian Moennich 2014-10-07 13:33:52 +02:00
parent 0242a9790c
commit cf91ca9cec
3 changed files with 17 additions and 9 deletions

View File

@ -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,

View File

@ -172,3 +172,16 @@ class ChatroomEventAssociation(db.Model):
@return_ascii
def __repr__(self):
return '<ChatroomEventAssociation({}, {})>'.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

View File

@ -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,