From f3ce183709f0358084245572cb4c58bfc9548235 Mon Sep 17 00:00:00 2001 From: Adrian Moennich Date: Wed, 8 Oct 2014 11:00:07 +0200 Subject: [PATCH] Use a better default name/JID name is the event title, jid is the name plus the event start date --- chat/indico_chat/controllers.py | 5 +++-- chat/indico_chat/forms.py | 19 ++++++++++++++++--- chat/indico_chat/models/chatrooms.py | 8 ++------ .../templates/manage_event_edit.html | 9 +++++++-- chat/indico_chat/xmpp.py | 16 +++++++++++++--- 5 files changed, 41 insertions(+), 16 deletions(-) diff --git a/chat/indico_chat/controllers.py b/chat/indico_chat/controllers.py index f8b54f1..c19eab4 100644 --- a/chat/indico_chat/controllers.py +++ b/chat/indico_chat/controllers.py @@ -135,13 +135,14 @@ class RHChatManageEventCreate(RHChatManageEventBase): """Creates a new chatroom for an event""" def _process(self): - form = AddChatroomForm() + form = AddChatroomForm(obj=FormDefaults(name=unicode(self.event.title, 'utf-8')), + date=self.event.getAdjustedStartDate()) if form.validate_on_submit(): chatroom = Chatroom(created_by_user=session.user) event_chatroom = ChatroomEventAssociation(event_id=self.event_id, chatroom=chatroom) form.populate_obj(event_chatroom, fields=form.event_specific_fields) form.populate_obj(chatroom, skip=form.event_specific_fields) - chatroom.generate_jid() + chatroom.jid_node = form.jid_node.data db.session.add_all((chatroom, event_chatroom)) db.session.flush() create_room(chatroom) diff --git a/chat/indico_chat/forms.py b/chat/indico_chat/forms.py index 3766434..d8d8958 100644 --- a/chat/indico_chat/forms.py +++ b/chat/indico_chat/forms.py @@ -20,11 +20,11 @@ from wtforms.fields.core import BooleanField from wtforms.fields.simple import TextField, TextAreaField from wtforms.validators import DataRequired, ValidationError -from indico.web.forms.base import IndicoForm +from indico.web.forms.base import IndicoForm, generated_data from indico.util.string import strip_whitespace from indico_chat.models.chatrooms import Chatroom -from indico_chat.xmpp import generate_jid +from indico_chat.xmpp import generate_jid, room_exists class EditChatroomForm(IndicoForm): @@ -44,11 +44,24 @@ class AddChatroomForm(EditChatroomForm): custom_server = TextField('Server', filters=[strip_whitespace, lambda x: x.lower() if x else x], description='External Jabber server. Should be left empty in most cases.') + def __init__(self, *args, **kwargs): + self._date = kwargs.pop('date') + super(AddChatroomForm, self).__init__(*args, **kwargs) + def validate_name(self, field): - jid = generate_jid(field.data) + jid = generate_jid(field.data, self._date) if not jid: # This error is not very helpful to a user, but it is extremely unlikely - only if he uses a name # which does not contain a single char usable in a JID raise ValidationError('Could not convert name to a jabber ID') if Chatroom.find_first(jid_node=jid, custom_server=self.custom_server.data): raise ValidationError('A room with this name already exists') + tmp_room = Chatroom(jid_node=jid, custom_server=self.custom_server.data) + if room_exists(tmp_room.jid): + raise ValidationError('A room with this name/JID already exists on the Jabber server ({})'.format( + tmp_room.jid + )) + + @generated_data + def jid_node(self): + return generate_jid(self.name.data, self._date) diff --git a/chat/indico_chat/models/chatrooms.py b/chat/indico_chat/models/chatrooms.py index 7fd8cde..0f23f1d 100644 --- a/chat/indico_chat/models/chatrooms.py +++ b/chat/indico_chat/models/chatrooms.py @@ -23,7 +23,8 @@ from indico.util.date_time import now_utc from indico.util.string import return_ascii from MaKaC.user import AvatarHolder from MaKaC.conference import ConferenceHolder -from indico_chat.xmpp import generate_jid, delete_room + +from indico_chat.xmpp import delete_room class Chatroom(db.Model): @@ -118,11 +119,6 @@ class Chatroom(db.Model): server = '!' + server return ''.format(self.id, self.name, self.jid_node, server) - def generate_jid(self): - """Generates the JID based on the room name""" - assert self.jid_node is None - self.jid_node = generate_jid(self.name) - class ChatroomEventAssociation(db.Model): __tablename__ = 'chatroom_events' diff --git a/chat/indico_chat/templates/manage_event_edit.html b/chat/indico_chat/templates/manage_event_edit.html index 3818d05..3cb1a1e 100644 --- a/chat/indico_chat/templates/manage_event_edit.html +++ b/chat/indico_chat/templates/manage_event_edit.html @@ -23,7 +23,7 @@ {% endif %} -
+ {% for field in form.visible_fields if field.short_name not in form.event_specific_fields %} @@ -37,6 +37,11 @@ {% endfor %}
- +
+ diff --git a/chat/indico_chat/xmpp.py b/chat/indico_chat/xmpp.py index e6c67e5..012b6a5 100644 --- a/chat/indico_chat/xmpp.py +++ b/chat/indico_chat/xmpp.py @@ -102,14 +102,24 @@ def room_exists(jid): return _execute_xmpp(_room_exists) -def generate_jid(name): - """Generates a valid JID node identifier from a name""" - jid = unicode_to_ascii(name).lower() +def sanitize_jid(s): + """Generates a valid JID node identifier from a string""" + jid = unicode_to_ascii(s).lower() jid = WHITESPACE.sub('-', jid) jid = INVALID_JID_CHARS.sub('', jid) return jid.strip()[:256] +def generate_jid(name, append_date=None): + """Generates a v alid JID based on the room name. + + :param append_date: appends the given date to the JID + """ + if not append_date: + return sanitize_jid(name) + return '{}-{}'.format(sanitize_jid(name), append_date.strftime('%Y-%m-%d')) + + def _make_form(xmpp, room): """Creates an XMPP room config form""" form = xmpp.plugin['xep_0004'].make_form(ftype='submit')