comparison src/browser/sat_browser/register.py @ 848:7dafa5ee809a

browser: replace re module usage by pure javascript
author souliane <souliane@mailoo.org>
date Fri, 15 Jan 2016 16:33:08 +0100
parents f8a7a046ff9c
children 77a494f13118
comparison
equal deleted inserted replaced
847:330db23d4a44 848:7dafa5ee809a
38 from pyjamas.ui.Hidden import Hidden 38 from pyjamas.ui.Hidden import Hidden
39 from pyjamas import Window 39 from pyjamas import Window
40 from pyjamas.ui.KeyboardListener import KEY_ENTER 40 from pyjamas.ui.KeyboardListener import KEY_ENTER
41 from pyjamas.Timer import Timer 41 from pyjamas.Timer import Timer
42 42
43 import re 43 from __pyjamas__ import JS
44 44
45 from constants import Const as C 45 from constants import Const as C
46 46
47 47
48 48
176 176
177 def onKeyDown(self, sender, keycode, modifiers): 177 def onKeyDown(self, sender, keycode, modifiers):
178 pass 178 pass
179 179
180 def onLogin(self, button): 180 def onLogin(self, button):
181 if not re.match(r'^[a-z0-9_-]+(@[a-z0-9_-]+\.[a-z0-9_-]+)?$', self.login_box.getText(), re.IGNORECASE): 181 if not self.checkJID(self.login_box.getText()):
182 self.login_warning_msg.setHTML('Invalid login, valid characters<br>are a-z A-Z 0-9 _ - or a bare JID') 182 self.login_warning_msg.setHTML('Invalid login, valid characters<br>are a-z A-Z 0-9 _ - or a bare JID')
183 else: 183 else:
184 self.submit_type.setValue('login') 184 self.submit_type.setValue('login')
185 self.submit(None) 185 self.submit(None)
186 186
187 def onRegister(self, button): 187 def onRegister(self, button):
188 # XXX: for now libervia forces the creation to lower case 188 # XXX: for now libervia forces the creation to lower case
189 self.register_login_box.setText(self.register_login_box.getText().lower()) 189 self.register_login_box.setText(self.register_login_box.getText().lower())
190 if not re.match(r'^[a-z0-9_-]+$', self.register_login_box.getText(), re.IGNORECASE): 190 if not self.checkLogin(self.register_login_box.getText()):
191 self.register_warning_msg.setHTML(_('Invalid login, valid characters<br>are a-z A-Z 0-9 _ -')) 191 self.register_warning_msg.setHTML(_('Invalid login, valid characters<br>are a-z A-Z 0-9 _ -'))
192 elif not re.match(r'^[a-z0-9_-]+@[a-z0-9_-]+\.[a-z0-9_-]+$', self.email_box.getText(), re.IGNORECASE): 192 elif not self.checkEmail(self.email_box.getText()):
193 self.register_warning_msg.setHTML(_('Invalid email address')) 193 self.register_warning_msg.setHTML(_('Invalid email address'))
194 elif len(self.register_pass_box.getText()) < C.PASSWORD_MIN_LENGTH: 194 elif len(self.register_pass_box.getText()) < C.PASSWORD_MIN_LENGTH:
195 self.register_warning_msg.setHTML(_('Your password must contain<br>at least %d characters.') % C.PASSWORD_MIN_LENGTH) 195 self.register_warning_msg.setHTML(_('Your password must contain<br>at least %d characters.') % C.PASSWORD_MIN_LENGTH)
196 else: 196 else:
197 self.register_warning_msg.setHTML("") 197 self.register_warning_msg.setHTML("")
233 self.login_pass_box.setFocus(True) 233 self.login_pass_box.setFocus(True)
234 Window.alert(_('An email has been sent to you with your login informations\nPlease remember that this is ONLY A TECHNICAL DEMO.')) 234 Window.alert(_('An email has been sent to you with your login informations\nPlease remember that this is ONLY A TECHNICAL DEMO.'))
235 else: 235 else:
236 Window.alert(_("An error occurred and we couldn't process your request. Please report the following error name to the administrators of your network: '%s'" % result)) 236 Window.alert(_("An error occurred and we couldn't process your request. Please report the following error name to the administrators of your network: '%s'" % result))
237 237
238 def checkLogin(self, text):
239 """Check if the given text is a valid login
240
241 @param text (unicode)
242 @return bool
243 """
244 # FIXME: Pyjamas re module is not stable so we use pure JS instead
245 JS("""return /^(\w|-)+$/.test(text);""")
246
247 def checkEmail(self, text):
248 """Check if the given text is a valid email address.
249
250 @param text (unicode)
251 @return bool
252 """
253 # FIXME: Pyjamas re module is not stable so we use pure JS instead
254 JS("""return /^(\w|-)+@(\w|-)+\.(\w|-)+$/.test(text);""")
255
256 def checkJID(self, text):
257 """Check if the given text is a valid JID.
258
259 @param text (unicode)
260 @return bool
261 """
262 # FIXME: Pyjamas re module is not stable so we use pure JS instead
263 JS("""return /^(\w|-)+(@(\w|-)+\.(\w|-)+)?$/.test(text);""")
264
238 265
239 class RegisterBox(PopupPanel): 266 class RegisterBox(PopupPanel):
240 267
241 def __init__(self, callback, *args, **kwargs): 268 def __init__(self, callback, *args, **kwargs):
242 PopupPanel.__init__(self, *args, **kwargs) 269 PopupPanel.__init__(self, *args, **kwargs)