From fd349ba590ad85ed8f8f4dadafb6e027cc49805b Mon Sep 17 00:00:00 2001 From: Adrian Moennich Date: Tue, 9 Jun 2015 11:49:12 +0200 Subject: [PATCH] Chat: Adapt to form_header/form_footer changes Also use a proper WTForm to attach chatrooms to events --- chat/indico_chat/controllers/management.py | 34 ++++++++++++------- chat/indico_chat/forms.py | 5 +++ chat/indico_chat/templates/manage_event.html | 17 +++------- .../templates/manage_event_edit.html | 6 ++-- 4 files changed, 34 insertions(+), 28 deletions(-) diff --git a/chat/indico_chat/controllers/management.py b/chat/indico_chat/controllers/management.py index b16dc85..7bcb6a5 100644 --- a/chat/indico_chat/controllers/management.py +++ b/chat/indico_chat/controllers/management.py @@ -16,7 +16,7 @@ from __future__ import unicode_literals -from flask import session, request, flash, redirect, jsonify +from flask import session, flash, redirect, jsonify from flask_pluginengine import current_plugin from indico.core.db import db @@ -29,25 +29,34 @@ from indico.web.forms.base import FormDefaults from indico_chat import _ from indico_chat.controllers.base import RHChatManageEventBase, RHEventChatroomMixin -from indico_chat.forms import AddChatroomForm, EditChatroomForm +from indico_chat.forms import AddChatroomForm, EditChatroomForm, AttachChatroomForm from indico_chat.models.chatrooms import ChatroomEventAssociation, Chatroom from indico_chat.notifications import notify_created, notify_attached, notify_modified, notify_deleted from indico_chat.views import WPChatEventMgmt from indico_chat.xmpp import create_room, update_room, get_room_config, room_exists -class RHChatManageEvent(RHChatManageEventBase): +class AttachChatroomMixin: + def _get_attach_form(self): + form = AttachChatroomForm() + form.chatroom.query = Chatroom.find(Chatroom.created_by_user == session.user, + ~Chatroom.events.any(ChatroomEventAssociation.event_id == self.event_id)) + return form + + +class RHChatManageEvent(AttachChatroomMixin, RHChatManageEventBase): """Lists the chatrooms of an event""" def _process(self): chatrooms = ChatroomEventAssociation.find_for_event(self.event, include_hidden=True, _eager='chatroom.events').all() - chatroom_filter = (~Chatroom.id.in_(x.chatroom_id for x in chatrooms)) if chatrooms else True - available_chatrooms = Chatroom.find_all(Chatroom.created_by_user == session.user, chatroom_filter) logs_enabled = current_plugin.settings.get('log_url') + attach_form = self._get_attach_form() + if not attach_form.chatroom._get_object_list(): + attach_form = None return WPChatEventMgmt.render_template('manage_event.html', self._conf, event_chatrooms=chatrooms, event=self.event, chat_links=current_plugin.settings.get('chat_links'), - available_chatrooms=available_chatrooms, logs_enabled=logs_enabled) + logs_enabled=logs_enabled, attach_form=attach_form) class RHChatManageEventModify(RHEventChatroomMixin, RHChatManageEventBase): @@ -122,18 +131,19 @@ class RHChatManageEventCreate(RHChatManageEventBase): return WPChatEventMgmt.render_template('manage_event_edit.html', self._conf, form=form, event=self.event) -class RHChatManageEventAttach(RHChatManageEventBase): +class RHChatManageEventAttach(AttachChatroomMixin, RHChatManageEventBase): """Attaches an existing chatroom to an event""" def _checkParams(self, params): RHChatManageEventBase._checkParams(self, params) - self.chatroom = Chatroom.find_one(id=request.form['chatroom_id']) def _process(self): - event_chatroom = ChatroomEventAssociation(event_id=self.event_id, chatroom=self.chatroom) - db.session.add(event_chatroom) - notify_attached(self.chatroom, self.event, session.user) - flash(_('Chatroom added'), 'success') + form = self._get_attach_form() + if form.validate_on_submit(): + event_chatroom = ChatroomEventAssociation(event_id=self.event_id, chatroom=form.chatroom.data) + db.session.add(event_chatroom) + notify_attached(form.chatroom.data, self.event, session.user) + flash(_('Chatroom added'), 'success') return redirect(url_for_plugin('.manage_rooms', self.event)) diff --git a/chat/indico_chat/forms.py b/chat/indico_chat/forms.py index 8eb9070..b6bde1b 100644 --- a/chat/indico_chat/forms.py +++ b/chat/indico_chat/forms.py @@ -17,6 +17,7 @@ from __future__ import unicode_literals from flask_pluginengine import current_plugin +from wtforms.ext.sqlalchemy.fields import QuerySelectField from wtforms.fields.core import BooleanField from wtforms.fields.simple import StringField, TextAreaField from wtforms.validators import DataRequired, ValidationError @@ -29,6 +30,10 @@ from indico_chat.models.chatrooms import Chatroom from indico_chat.xmpp import generate_jid, room_exists +class AttachChatroomForm(IndicoForm): + chatroom = QuerySelectField(_('Available chatrooms'), [DataRequired()], get_label='name') + + class EditChatroomForm(IndicoForm): event_specific_fields = {'hidden', 'show_password'} diff --git a/chat/indico_chat/templates/manage_event.html b/chat/indico_chat/templates/manage_event.html index 01ea3c6..b952ebe 100644 --- a/chat/indico_chat/templates/manage_event.html +++ b/chat/indico_chat/templates/manage_event.html @@ -91,22 +91,13 @@ {% trans %}Create new room{% endtrans %} - {% if available_chatrooms %} + {% if attach_form %}

{% trans %}Add existing chat room{% endtrans %}

- {{ form_header(action=url_for_plugin('.manage_rooms_attach', event), method='post') }} -
-
{% trans %}Available chatrooms{% endtrans %}
-
- -
-
- {% call form_footer() %} + {{ form_header(attach_form, action=url_for_plugin('.manage_rooms_attach', event)) }} + {{ form_row(attach_form.chatroom) }} + {% call form_footer(attach_form) %} {% endcall %}
diff --git a/chat/indico_chat/templates/manage_event_edit.html b/chat/indico_chat/templates/manage_event_edit.html index 5c72f75..e96948d 100644 --- a/chat/indico_chat/templates/manage_event_edit.html +++ b/chat/indico_chat/templates/manage_event_edit.html @@ -13,7 +13,7 @@ {% endblock %} {% block content %} - {{ form_header(id='chatroom-form') }} + {{ form_header(form, id='chatroom-form') }} {% call form_fieldset(legend='Chatroom settings') %} {{ form_rows(form, skip=form.event_specific_fields) }} @@ -23,8 +23,8 @@ {{ form_rows(form, fields=form.event_specific_fields) }} {% endcall %} - {% call form_footer() %} - + {% call form_footer(form) %} + {% trans %}Cancel{% endtrans %} {% endcall %}