0
|
1 #!/usr/bin/python |
|
2 # -*- coding: utf-8 -*- |
|
3 |
|
4 """ |
|
5 Libervia: a Salut à Toi frontend |
|
6 Copyright (C) 2011 Jérôme Poisson (goffi@goffi.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 |
|
22 #This page manage subscription and new account creation |
|
23 import pyjd # this is dummy in pyjs |
|
24 |
|
25 from pyjamas.ui.VerticalPanel import VerticalPanel |
|
26 from pyjamas.ui.Grid import Grid |
|
27 from pyjamas.ui.PasswordTextBox import PasswordTextBox |
|
28 from pyjamas.ui.TextBox import TextBox |
|
29 from pyjamas.ui.FormPanel import FormPanel |
|
30 from pyjamas.ui.Button import Button |
46
|
31 from pyjamas.ui.Label import Label |
|
32 from pyjamas.ui.CheckBox import CheckBox |
0
|
33 from pyjamas.ui.DialogBox import DialogBox |
|
34 from pyjamas import Window |
|
35 from pyjamas.ui import HasAlignment |
46
|
36 import re |
0
|
37 |
|
38 |
|
39 |
|
40 |
|
41 class RegisterPanel(FormPanel): |
|
42 |
|
43 def __init__(self, callback): |
|
44 """ |
|
45 @param callback: method to call if login successful |
|
46 """ |
|
47 FormPanel.__init__(self) |
|
48 self.callback = callback |
|
49 self.setMethod(FormPanel.METHOD_POST) |
|
50 vPanel = VerticalPanel() |
|
51 vPanel.setHorizontalAlignment(HasAlignment.ALIGN_CENTER) |
46
|
52 self.warning_msg = Label() |
|
53 self.warning_msg.setVisible(False) |
|
54 self.warning_msg.setStyleName('formWarning') |
|
55 vPanel.add(self.warning_msg) |
|
56 |
|
57 self.login_box = TextBox() |
|
58 self.login_box.setName("login") |
|
59 self.pass_box = PasswordTextBox() |
|
60 self.pass_box.setName("password") |
|
61 login_grid = Grid(2, 2) |
|
62 login_grid.setText(0,0,"Login:") |
|
63 login_grid.setWidget(0,1, self.login_box) |
|
64 login_grid.setText(1,0, "Password:") |
|
65 login_grid.setWidget(1,1, self.pass_box) |
|
66 vPanel.add(login_grid) |
|
67 |
|
68 self.new_account = CheckBox('Register new account') |
|
69 self.new_account.setName('new_account') |
|
70 self.new_account.addClickListener(self.onNewAccountChecked) |
|
71 vPanel.add(self.new_account) |
|
72 |
|
73 |
|
74 self.email_box = TextBox() |
|
75 self.email_box.setName("email") |
|
76 self.register_grid = Grid(1, 2) |
|
77 self.register_grid.setText(0,0,"email:") |
|
78 self.register_grid.setWidget(0,1, self.email_box) |
|
79 self.register_grid.setVisible(False) |
|
80 vPanel.add(self.register_grid) |
|
81 self.info_register_lb = Label('Your password will be sent to the given email') |
|
82 self.info_register_lb.setVisible(False) |
|
83 vPanel.add(self.register_grid) |
|
84 vPanel.add(self.info_register_lb) |
|
85 |
|
86 self.login_but = Button("Login", getattr(self, "onLogin")) |
|
87 vPanel.add(self.login_but) |
|
88 self.register_but = Button("Register", getattr(self, "onRegister")) |
|
89 self.register_but.setVisible(False) |
|
90 vPanel.add(self.register_but) |
0
|
91 self.add(vPanel) |
|
92 self.addFormHandler(self) |
|
93 self.setAction('register_api/login') |
|
94 |
46
|
95 def changeMode(self, mode): |
|
96 """Change the configuration of the dialog |
|
97 @param mode: "login" or "register""" |
|
98 if mode == "register": |
|
99 self.pass_box.setEnabled(False) |
|
100 self.register_grid.setVisible(True) |
|
101 self.info_register_lb.setVisible(True) |
|
102 self.login_but.setVisible(False) |
|
103 self.register_but.setVisible(True) |
|
104 self.new_account.setChecked(True) |
|
105 else: |
|
106 self.pass_box.setEnabled(True) |
|
107 self.register_grid.setVisible(False) |
|
108 self.info_register_lb.setVisible(False) |
|
109 self.login_but.setVisible(True) |
|
110 self.register_but.setVisible(False) |
|
111 self.new_account.setChecked(False) |
|
112 |
|
113 |
|
114 def onNewAccountChecked(self, sender): |
|
115 if sender.isChecked(): |
|
116 self.changeMode("register") |
|
117 else: |
|
118 self.changeMode("login") |
|
119 |
0
|
120 def onLogin(self): |
|
121 self.submit() |
46
|
122 |
|
123 def onRegister(self): |
|
124 print self.login_box.getText() |
|
125 if not re.match(r'^[a-z0-9_-]+$',self.login_box.getText(), re.IGNORECASE): |
|
126 self.warning_msg.setText('Invaling login, valid characters are a-z A-Z 0-9 _ -') |
|
127 self.warning_msg.setVisible(True) |
|
128 elif not re.match(r'^.+@.+\..+', self.email_box.getText(), re.IGNORECASE): |
|
129 self.warning_msg.setText('Invaling email address') |
|
130 self.warning_msg.setVisible(True) |
|
131 else: |
|
132 self.warning_msg.setVisible(False) |
|
133 self.submit() |
|
134 |
0
|
135 |
|
136 def onSubmit(self, event): |
|
137 pass |
|
138 |
|
139 def onSubmitComplete(self, event): |
|
140 result = event.getResults() |
|
141 if result == "AUTH ERROR": |
|
142 Window.alert('You login and/or password is incorrect. Please try again') |
|
143 elif result == "LOGGED": |
|
144 self.callback() |
46
|
145 elif result == "ALREADY EXISTS": |
|
146 self.warning_msg.setText('This login already exists, please choose an other one') |
|
147 self.warning_msg.setVisible(True) |
|
148 elif result == "REGISTRATION": |
|
149 self.warning_msg.setVisible(False) |
|
150 self.changeMode('login') |
|
151 self.pass_box.setText('') |
|
152 Window.alert('An email has been sent to you with your login informations\nPlease remember that this is ONLY A TECHNICAL DEMO') |
0
|
153 else: |
|
154 Window.alert('Submit error: %s' % result) |
|
155 |
|
156 class RegisterBox(DialogBox): |
|
157 |
|
158 def __init__(self, callback, *args,**kwargs): |
|
159 DialogBox.__init__(self,*args,**kwargs) |
|
160 _form = RegisterPanel(callback) |
|
161 self.setHTML('<b>You are not identified, please log in</b>') |
|
162 self.setWidget(_form) |