comparison src/browser/sat_browser/register.py @ 467:97c72fe4a5f2

browser_side: import fixes: - moved browser modules in a sat_browser packages, to avoid import conflicts with std lib (e.g. logging), and let pyjsbuild work normaly - refactored bad import practices: classes are most of time not imported directly, module is imported instead.
author Goffi <goffi@goffi.org>
date Mon, 09 Jun 2014 22:15:26 +0200
parents src/browser/register.py@bea9788f3170
children bbdc5357dc00
comparison
equal deleted inserted replaced
466:01880aa8ea2d 467:97c72fe4a5f2
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Libervia: a Salut à Toi frontend
5 # Copyright (C) 2011, 2012, 2013, 2014 Jérôme Poisson <goffi@goffi.org>
6 # Copyright (C) 2011, 2012 Adrien Vigneron <adrienvigneron@mailoo.org>
7
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU Affero General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU Affero General Public License for more details.
17
18 # You should have received a copy of the GNU Affero General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21 #This page manage subscription and new account creation
22
23 import pyjd # this is dummy in pyjs
24 from sat.core.i18n import _
25
26 from pyjamas.ui.SimplePanel import SimplePanel
27 from pyjamas.ui.VerticalPanel import VerticalPanel
28 from pyjamas.ui.HorizontalPanel import HorizontalPanel
29 from pyjamas.ui.TabPanel import TabPanel
30 from pyjamas.ui.TabBar import TabBar
31 from pyjamas.ui.PasswordTextBox import PasswordTextBox
32 from pyjamas.ui.TextBox import TextBox
33 from pyjamas.ui.FormPanel import FormPanel
34 from pyjamas.ui.Button import Button
35 from pyjamas.ui.Label import Label
36 from pyjamas.ui.HTML import HTML
37 from pyjamas.ui.PopupPanel import PopupPanel
38 from pyjamas.ui.Image import Image
39 from pyjamas.ui.Hidden import Hidden
40 from pyjamas import Window
41 from pyjamas.ui.KeyboardListener import KEY_ENTER
42 from pyjamas.Timer import Timer
43
44 import re
45
46 from constants import Const as C
47
48
49 class RegisterPanel(FormPanel):
50
51 def __init__(self, callback):
52 """
53 @param callback: method to call if login successful
54 """
55 FormPanel.__init__(self)
56 self.setSize('600px', '350px')
57 self.callback = callback
58 self.setMethod(FormPanel.METHOD_POST)
59 main_panel = HorizontalPanel()
60 main_panel.setStyleName('registerPanel_main')
61 left_side = Image("media/libervia/register_left.png")
62 main_panel.add(left_side)
63
64 ##TabPanel##
65 tab_bar = TabBar()
66 tab_bar.setStyleName('registerPanel_tabs')
67 self.right_side = TabPanel(tab_bar)
68 self.right_side.setStyleName('registerPanel_right_side')
69 main_panel.add(self.right_side)
70 main_panel.setCellWidth(self.right_side, '100%')
71
72 ##Login tab##
73 login_tab = SimplePanel()
74 login_tab.setStyleName('registerPanel_content')
75 login_vpanel = VerticalPanel()
76 login_tab.setWidget(login_vpanel)
77
78 self.login_warning_msg = Label('')
79 self.login_warning_msg.setVisible(False)
80 self.login_warning_msg.setStyleName('formWarning')
81 login_vpanel.add(self.login_warning_msg)
82
83 login_label = Label('Login:')
84 self.login_box = TextBox()
85 self.login_box.setName("login")
86 self.login_box.addKeyboardListener(self)
87 login_pass_label = Label('Password:')
88 self.login_pass_box = PasswordTextBox()
89 self.login_pass_box.setName("login_password")
90 self.login_pass_box.addKeyboardListener(self)
91
92 login_vpanel.add(login_label)
93 login_vpanel.add(self.login_box)
94 login_vpanel.add(login_pass_label)
95 login_vpanel.add(self.login_pass_box)
96 login_but = Button("Log in", getattr(self, "onLogin"))
97 login_but.setStyleName('button')
98 login_but.addStyleName('red')
99 login_vpanel.add(login_but)
100
101 #The hidden submit_type field
102 self.submit_type = Hidden('submit_type')
103 login_vpanel.add(self.submit_type)
104
105 ##Register tab##
106 register_tab = SimplePanel()
107 register_tab.setStyleName('registerPanel_content')
108 register_vpanel = VerticalPanel()
109 register_tab.setWidget(register_vpanel)
110
111 self.register_warning_msg = HTML('')
112 self.register_warning_msg.setVisible(False)
113 self.register_warning_msg.setStyleName('formWarning')
114 register_vpanel.add(self.register_warning_msg)
115
116 register_login_label = Label('Login:')
117 self.register_login_box = TextBox()
118 self.register_login_box.setName("register_login")
119 self.register_login_box.addKeyboardListener(self)
120 email_label = Label('E-mail:')
121 self.email_box = TextBox()
122 self.email_box.setName("email")
123 self.email_box.addKeyboardListener(self)
124 register_pass_label = Label('Password:')
125 self.register_pass_box = PasswordTextBox()
126 self.register_pass_box.setName("register_password")
127 self.register_pass_box.addKeyboardListener(self)
128 register_vpanel.add(register_login_label)
129 register_vpanel.add(self.register_login_box)
130 register_vpanel.add(email_label)
131 register_vpanel.add(self.email_box)
132 register_vpanel.add(register_pass_label)
133 register_vpanel.add(self.register_pass_box)
134
135 register_but = Button("Register", getattr(self, "onRegister"))
136 register_but.setStyleName('button')
137 register_but.addStyleName('red')
138 register_vpanel.add(register_but)
139
140 self.right_side.add(login_tab, 'Login')
141 self.right_side.add(register_tab, 'Register')
142 self.right_side.addTabListener(self)
143 self.right_side.selectTab(1)
144 login_tab.setWidth(None)
145 register_tab.setWidth(None)
146
147 self.add(main_panel)
148 self.addFormHandler(self)
149 self.setAction('register_api/login')
150
151 def onBeforeTabSelected(self, sender, tabIndex):
152 return True
153
154 def onTabSelected(self, sender, tabIndex):
155 if tabIndex == 0:
156 self.login_box.setFocus(True)
157 elif tabIndex == 1:
158 self.register_login_box.setFocus(True)
159
160 def onKeyPress(self, sender, keycode, modifiers):
161 if keycode == KEY_ENTER:
162 # Browsers offer an auto-completion feature to any
163 # text box, but the selected value is not set when
164 # the widget looses the focus. Using a timer with
165 # any delay value > 0 would do the trick.
166 if sender == self.login_box:
167 Timer(5, lambda timer: self.login_pass_box.setFocus(True))
168 elif sender == self.login_pass_box:
169 self.onLogin(None)
170 elif sender == self.register_login_box:
171 Timer(5, lambda timer: self.email_box.setFocus(True))
172 elif sender == self.email_box:
173 Timer(5, lambda timer: self.register_pass_box.setFocus(True))
174 elif sender == self.register_pass_box:
175 self.onRegister(None)
176
177 def onKeyUp(self, sender, keycode, modifiers):
178 pass
179
180 def onKeyDown(self, sender, keycode, modifiers):
181 pass
182
183 def onLogin(self, button):
184 if not re.match(r'^[a-z0-9_-]+$', self.login_box.getText(), re.IGNORECASE):
185 self.login_warning_msg.setText('Invalid login, valid characters are a-z A-Z 0-9 _ -')
186 self.login_warning_msg.setVisible(True)
187 else:
188 self.submit_type.setValue('login')
189 self.submit()
190
191 def onRegister(self, button):
192 if not re.match(r'^[a-z0-9_-]+$', self.register_login_box.getText(), re.IGNORECASE):
193 self.register_warning_msg.setHTML(_('Invalid login, valid characters<br>are a-z A-Z 0-9 _ -'))
194 self.register_warning_msg.setVisible(True)
195 elif not re.match(r'^.+@.+\..+', self.email_box.getText(), re.IGNORECASE):
196 self.register_warning_msg.setHTML(_('Invalid email address'))
197 self.register_warning_msg.setVisible(True)
198 elif len(self.register_pass_box.getText()) < C.PASSWORD_MIN_LENGTH:
199 self.register_warning_msg.setHTML(_('Your password must contain<br>at least %d characters') % C.PASSWORD_MIN_LENGTH)
200 self.register_warning_msg.setVisible(True)
201 else:
202 self.register_warning_msg.setVisible(False)
203 self.submit_type.setValue('register')
204 self.submit()
205
206 def onSubmit(self, event):
207 pass
208
209 def onSubmitComplete(self, event):
210 result = event.getResults()
211 if result == "PROFILE AUTH ERROR":
212 Window.alert(_('Your login and/or password is incorrect. Please try again'))
213 elif result == "XMPP AUTH ERROR":
214 # TODO: call stdui action CHANGE_XMPP_PASSWD_ID as it's done in primitivus
215 Window.alert(_(u'Your SàT profile has been authenticated but the associated XMPP account failed to connect. Please use another SàT frontend to set another XMPP password.'))
216 elif result == "LOGGED":
217 self.callback()
218 elif result == "SESSION_ACTIVE":
219 Window.alert(_('Session already active, this should not happen, please contact the author to fix it'))
220 elif result == "ALREADY EXISTS":
221 self.register_warning_msg.setHTML(_('This login already exists,<br>please choose another one'))
222 self.register_warning_msg.setVisible(True)
223 elif result == "INTERNAL":
224 self.register_warning_msg.setHTML(_('SERVER ERROR: something went wrong during registration process, please contact the server administrator'))
225 self.register_warning_msg.setVisible(True)
226 elif result == "REGISTRATION":
227 self.login_warning_msg.setVisible(False)
228 self.register_warning_msg.setVisible(False)
229 self.login_box.setText(self.register_login_box.getText())
230 self.login_pass_box.setText('')
231 self.register_login_box.setText('')
232 self.register_pass_box.setText('')
233 self.email_box.setText('')
234 self.right_side.selectTab(0)
235 self.login_pass_box.setFocus(True)
236 Window.alert(_('An email has been sent to you with your login informations\nPlease remember that this is ONLY A TECHNICAL DEMO'))
237 else:
238 Window.alert(_('Submit error: %s' % result))
239
240
241 class RegisterBox(PopupPanel):
242
243 def __init__(self, callback, *args, **kwargs):
244 PopupPanel.__init__(self, *args, **kwargs)
245 self._form = RegisterPanel(callback)
246 self.setWidget(self._form)
247
248 def onWindowResized(self, width, height):
249 super(RegisterBox, self).onWindowResized(width, height)
250 self.centerBox()
251
252 def show(self):
253 super(RegisterBox, self).show()
254 self.centerBox()
255 self._form.login_box.setFocus(True)