130
137
def _set_next_existing(self, view):
131
138
"""Set the next id and fire signal."""
132
139
logger.debug('ChooseSignInController._set_next_existing')
133
view.next = self._existing_account_id
140
view.next = view.wizard().current_user_page_id
134
141
view.wizard().next()
136
143
def _set_next_new(self, view):
137
144
"""Set the next id and fire signal."""
138
145
logger.debug('ChooseSignInController._set_next_new')
139
view.next = self._new_account_id
146
view.next = view.wizard().setup_account_page_id
140
147
view.wizard().next()
143
150
class CurrentUserController(BackendController):
144
151
"""Controller used in the view that is used to allow the signin."""
146
def __init__(self, backend=None, title='', app_name='', message_box=None):
153
def __init__(self, backend=None, title='', message_box=None):
147
154
"""Create a new instance."""
148
155
super(CurrentUserController, self).__init__()
149
156
if message_box is None:
150
157
message_box = QMessageBox
151
158
self.message_box = message_box
152
159
self._title = title
153
self._app_name = app_name
155
161
def _set_translated_strings(self, view):
156
162
"""Set the translated strings."""
157
163
logger.debug('CurrentUserController._set_translated_strings')
158
164
view.email_edit.setPlaceholderText(EMAIL1_ENTRY)
159
165
view.password_edit.setPlaceholderText(PASSWORD1_ENTRY)
160
view.forgot_password_label.setText(FORGOTTEN_PASSWORD_BUTTON)
166
view.forgot_password_label.setText(
167
FAKE_URL % FORGOTTEN_PASSWORD_BUTTON)
161
168
view.sign_in_button.setText(SIGN_IN_BUTTON)
163
def _connect_buttons(self, view, backend):
170
def _connect_ui(self, view, backend):
164
171
"""Connect the buttons to perform actions."""
165
172
logger.debug('CurrentUserController._connect_buttons')
166
173
view.sign_in_button.clicked.connect(lambda: self.login(view, backend))
167
174
# lets add call backs to be execute for the calls we are interested
168
175
backend.on_login_error_cb = lambda app, error:\
169
self.on_login_error(view, app, error)
176
self.on_login_error(view, error)
170
177
backend.on_logged_in_cb = lambda app, result:\
171
178
self.on_logged_in(view, app, result)
179
view.forgot_password_label.linkActivated.connect(lambda link:\
180
self.on_forgotten_password(view))
173
182
def login(self, view, backend):
174
183
"""Perform the login using the backend."""
175
184
logger.debug('CurrentUserController.login')
176
185
# grab the data from the view and call the backend
177
d = backend.login(self._app_name, view.email, view.password)
178
d.addErrback(lambda e: self.on_login_error(view, self._app_name, e))
186
d = backend.login(view.wizard().app_name, view.email, view.password)
187
d.addErrback(lambda e: self.on_login_error(view, e))
180
def on_login_error(self, view, app_name, error):
189
def on_login_error(self, view, error):
181
190
"""There was an error when login in."""
182
191
# let the user know
183
logger.error('Got error when login %s, error: %s', app_name, error)
184
self.message_box.critical(view, app_name, str(error))
192
logger.error('Got error when login %s, error: %s',
193
view.wizard().app_name, error)
194
self.message_box.critical(view, view.wizard().app_name, str(error))
187
197
def on_logged_in(self, view, app_name, result):
198
214
backend = yield self.get_backend()
199
215
view.setTitle(self._title)
200
216
self._set_translated_strings(view)
201
self._connect_buttons(view, backend)
217
self._connect_ui(view, backend)
202
218
#pylint: enable=C0103
205
221
class SetUpAccountController(BackendController):
206
222
"""Conroller for the setup account view."""
208
def __init__(self, tos_id=0, validation_id=1, app_name='',
209
help_message='', message_box=None):
224
def __init__(self, message_box=None):
210
225
"""Create a new instance."""
211
226
super(SetUpAccountController, self).__init__()
212
227
if message_box is None:
213
228
message_box = QMessageBox
214
229
self.message_box = message_box
215
self._tos_id = tos_id
216
self._validation_id = validation_id
217
self._app_name = app_name
218
self._help_message = help_message
220
231
def _set_translated_strings(self, view):
221
232
"""Set the different gettext translated strings."""
341
350
def set_next_tos(self, view):
342
351
"""Set the tos page as the next one."""
343
352
logger.debug('SetUpAccountController.set_next_tos')
344
view.next = self._tos_id
353
view.next = view.wizard().tos_page_id
345
354
view.wizard().next()
347
356
def validate_form(self, view):
348
357
"""Validate the info of the form and return an error."""
349
358
logger.debug('SetUpAccountController.validate_form')
350
if not self.is_correct_email(view.email):
351
self.message_box.critical(view, self._app_name, EMAIL_INVALID)
359
if not is_correct_email(view.email):
360
self.message_box.critical(view, view.wizard().app_name,
352
362
if view.email_edit.text() != view.confirm_email_edit.text():
353
self.message_box.critical(view, self._app_name, EMAIL_MISMATCH)
363
self.message_box.critical(view, view.wizard().app_name,
355
366
if not is_min_required_password(str(view.password_edit.text())):
356
self.message_box.critical(view, self._app_name, PASSWORD_TOO_WEAK)
367
self.message_box.critical(view, view.wizard().app_name,
358
370
if view.password_edit.text() != view.confirm_password_edit.text():
359
self.message_box.critical(view, self._app_name, PASSWORD_MISMATCH)
371
self.message_box.critical(view, view.wizard().app_name,
474
488
#pylint: enable=C0103
491
class ForgottenPasswordController(BackendController):
492
"""Controller used to deal with the forgotten pwd page."""
495
"""Create a new instance."""
496
super(ForgottenPasswordController, self).__init__()
498
def _register_fields(self, view):
499
"""Register the fields of the wizard page."""
500
view.registerField('email_address', view.email_address_line_edit)
502
def _set_translated_strings(self, view):
503
"""Set the translated strings in the view."""
504
view.forgotted_password_intro_label.setText(
505
REQUEST_PASSWORD_TOKEN_LABEL % {'app_name':
506
view.wizard().app_name})
507
view.email_address_label.setText(EMAIL_LABEL)
508
view.send_button.setText(RESET_PASSWORD)
509
view.try_again_button.setText(TRY_AGAIN_BUTTON)
511
def _set_enhanced_line_edit(self, view):
512
"""Set the extra logic to the line edits."""
513
view.set_line_edit_validation_rule(view.email_address_line_edit,
516
def _connect_ui(self, view, backend):
517
"""Connect the diff signals from the Ui."""
518
view.send_button.clicked.connect(
519
lambda: backend.request_password_reset_token(
520
view.wizard().app_name,
522
view.try_again_button.clicked.connect(lambda: self.on_try_again(view))
523
# set the backend callbacks to be used
524
backend.on_password_reset_token_sent_cb = lambda app, result:\
525
self.on_password_reset_token_sent(view)
526
backend.on_password_reset_error_cb = lambda app_name, error:\
527
self.on_password_reset_error(app_name,
531
def on_try_again(self, view):
532
"""Set back the widget to the initial state."""
533
view.error_label.setVisible(False)
534
view.try_again_widget.setVisible(False)
535
view.email_widget.setVisible(True)
537
def on_password_reset_token_sent(self, view):
538
"""Action taken when we managed to get the password reset done."""
539
# ignore the result and move to the reset page
540
view.next = view.wizard().reset_password_page_id
543
def on_password_reset_error(self, app_name, error, view):
544
"""Action taken when there was an error requesting the reset."""
545
if error['errtype'] == 'ResetPasswordTokenError':
546
# the account provided is wrong, lets tell the user to try
548
view.error_label.setText(REQUEST_PASSWORD_TOKEN_WRONG_EMAIL)
549
view.error_label.setVisible(True)
551
# ouch, I dont know what went wrong, tell the user to try later
552
view.email_widget.setVisible(False)
553
view.forgotted_password_intro_label.setVisible(False)
554
view.try_again_wisget.setVisible(True)
555
# set the error message
556
view.error_label.setText(REQUEST_PASSWORD_TOKEN_TECH_ERROR)
558
#pylint: disable=C0103
560
def setupUi(self, view):
561
"""Setup the view."""
562
backend = yield self.get_backend()
563
# hide the error label
564
view.error_label.setVisible(False)
565
view.try_again_widget.setVisible(False)
566
self._set_translated_strings(view)
567
self._connect_ui(view, backend)
568
self._set_enhanced_line_edit(view)
569
self._register_fields(view)
570
#pylint: enable=C0103
573
class ResetPasswordController(BackendController):
574
"""Controller used to deal with reseintg the password."""
577
"""Create a new instance."""
578
super(ResetPasswordController, self).__init__()
580
def _set_translated_strings(self, view):
581
"""Translate the diff strings used in the app."""
582
view.reset_code_line_edit.setPlaceholderText(RESET_CODE_ENTRY)
583
view.password_line_edit.setPlaceholderText(PASSWORD1_ENTRY)
584
view.confirm_password_line_edit.setPlaceholderText(PASSWORD2_ENTRY)
585
view.reset_password_button.setText(RESET_PASSWORD)
586
view.setSubTitle(PASSWORD_HELP)
588
def _connect_ui(self, view, backend):
589
"""Connect the different ui signals."""
590
view.reset_password_button.clicked.connect(
591
lambda: self.set_new_password(view, backend))
592
backend.on_password_changed_cb = lambda app, result:\
593
self.on_password_changed(app,
596
backend.on_password_change_error_cb = lambda app, error:\
597
self.on_password_change_error(app,
601
def _add_line_edits_validations(self, view):
602
"""Add the validations to be use by the line edits."""
603
view.set_line_edit_validation_rule(view.password_line_edit,
604
is_min_required_password)
605
view.set_line_edit_validation_rule(view.confirm_password_line_edit,
606
lambda x: self.is_correct_password_confirmation(x, view))
607
# same as the above case, lets connect a signal to a signal
608
view.password_line_edit.textChanged.connect(
609
view.confirm_password_line_edit.textChanged.emit)
611
def on_password_changed(self, app_name, result, view):
612
"""Let user know that the password was changed."""
614
def on_password_change_error(self, app_name, error, view):
615
"""Let the user know that there was an error."""
617
def set_new_password(self, view, backend):
618
"""Request a new password to be set."""
619
app_name = view.wizard().app_name
620
email = str(view.wizard().field('email_address').toString())
621
code = view.reset_code
622
logger.info('Settig new password for %s and email %s with code %s',
623
app_name, email, code)
624
backend.set_new_password(app_name, email, code, view.password)
626
def is_correct_password_confirmation(self, password, view):
627
"""Return if the password is correct."""
628
return view.password_line_edit.text() == password
630
#pylint: disable=C0103
632
def setupUi(self, view):
633
"""Setup the view."""
634
backend = yield self.get_backend()
635
self._set_translated_strings(view)
636
self._connect_ui(view, backend)
637
self._add_line_edits_validations(view)
638
#pylint: enable=C0103
477
641
class SuccessController(object):
478
642
"""Controller used for the success page."""