mirror of
https://github.com/lucaspalomodevelop/indico-plugins.git
synced 2026-03-13 07:29:39 +00:00
Use a better default name/JID
name is the event title, jid is the name plus the event start date
This commit is contained in:
parent
065924e7fa
commit
f3ce183709
@ -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)
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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 '<Chatroom({}, {}, {}, {})>'.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'
|
||||
|
||||
@ -23,7 +23,7 @@
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
<form method="post" action="">
|
||||
<form method="post" action="" id="chatroom-form">
|
||||
<table>
|
||||
{% for field in form.visible_fields if field.short_name not in form.event_specific_fields %}
|
||||
<tr>
|
||||
@ -37,6 +37,11 @@
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</table>
|
||||
<input class="i-button" type="submit" value="{% trans %}Save{% endtrans %}">
|
||||
<input class="i-button" id="save-chatroom" type="submit" value="{% trans %}Save{% endtrans %}">
|
||||
</form>
|
||||
<script>
|
||||
$('#chatroom-form').on('submit', function() {
|
||||
$('#save-chatroom').prop('disabled', true).blur();
|
||||
});
|
||||
</script>
|
||||
</div>
|
||||
|
||||
@ -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')
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user