~diegosarmentero/ubuntu-sso-client/err-dict

« back to all changes in this revision

Viewing changes to ubuntu_sso/qt/gui.py

Added the way to reset the password.

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
from ubuntu_sso.qt.setup_account_ui import Ui_SetUpAccountPage
39
39
from ubuntu_sso.qt.terms_and_conditions_ui import Ui_TosPage
40
40
from ubuntu_sso.qt.success_message_ui import Ui_SuccessPage
 
41
from ubuntu_sso.qt.forgotten_password_ui import Ui_ForgottenPasswordPage
 
42
from ubuntu_sso.qt.reset_password_ui import Ui_ResetPasswordPage
41
43
# pylint: enable=F0401,E0611
42
44
from ubuntu_sso.qt.controllers import (
43
45
    ChooseSignInController,
44
46
    CurrentUserController,
45
47
    EmailVerificationController,
46
48
    ErrorController,
 
49
    ForgottenPasswordController,
 
50
    ResetPasswordController,
47
51
    SetUpAccountController,
48
52
    SuccessController,
49
53
    TosController,
76
80
    # pylint: enable=C0103
77
81
 
78
82
 
 
83
class EnhancedLineEdit(object):
 
84
    """Represents and enhanced lineedit.
 
85
 
 
86
    This class works on an already added lineedit to the widget so
 
87
    that we are just adding extra items to it.
 
88
    """
 
89
 
 
90
    def __init__(self, line_edit, valid_cb=lambda x: False):
 
91
        """Create an instance."""
 
92
        self._line_edit = line_edit
 
93
        layout = QHBoxLayout(self._line_edit)
 
94
        layout.setMargin(0)
 
95
        self._line_edit.setLayout(layout)
 
96
        self.valid_cb = valid_cb
 
97
        layout.addStretch()
 
98
        self.clear_button = QPushButton(self._line_edit)
 
99
        layout.addWidget(self.clear_button)
 
100
        self.clear_button.setMinimumSize(16, 16)
 
101
        self.clear_button.setVisible(False)
 
102
        self.clear_button.setFlat(True)
 
103
        self.clear_button.setCursor(QCursor(0))
 
104
        self.clear_button.setIcon(QApplication.style().standardIcon(
 
105
                                                QStyle.SP_MessageBoxWarning))
 
106
        # connect the change of text to the cation that will check if the
 
107
        # text is valid and if the icon should be shown.
 
108
        self._line_edit.textChanged.connect(self.show_button)
 
109
 
 
110
    def show_button(self, string):
 
111
        """Decide if we show the button or not."""
 
112
        if not self.valid_cb(string):
 
113
            self.clear_button.setVisible(True)
 
114
        else:
 
115
            self.clear_button.setVisible(False)
 
116
 
 
117
 
 
118
class SSOWizardEnhancedEditPage(SSOWizardPage):
 
119
    """Page that contains enhanced line edits."""
 
120
 
 
121
    def __init__(self, ui, controller, parent=None):
 
122
        """Create a new instance."""
 
123
        SSOWizardPage.__init__(self, ui, controller, parent)
 
124
        self._enhanced_edits = {}
 
125
 
 
126
    def set_line_edit_validation_rule(self, edit, cb):
 
127
        """Set a new enhanced edit so that we can show an icon."""
 
128
        if edit in self._enhanced_edits:
 
129
            self._enhanced_edits[edit].valid_cb = cb
 
130
        else:
 
131
            # create a new enhanced edit
 
132
            enhanced_edit = EnhancedLineEdit(edit, cb)
 
133
            self._enhanced_edits[edit] = enhanced_edit
 
134
 
 
135
 
79
136
class ChooseSignInPage(SSOWizardPage):
80
137
    """Widget that allows the user to choose how to sign in."""
81
138
 
170
227
        return self.ui.error_message_label
171
228
 
172
229
 
173
 
class ForgottenPasswordPage(SSOWizardPage):
 
230
class ForgottenPasswordPage(SSOWizardEnhancedEditPage):
174
231
    """Widget used to deal with users that forgot the password."""
175
232
 
176
233
    def __init__(self, ui, controller, parent=None):
177
234
        """Create a new instance."""
178
 
        SSOWizard.__init__(self, ui, controller, parent)
179
 
 
180
 
        
 
235
        SSOWizardEnhancedEditPage.__init__(self, ui, controller, parent)
 
236
 
 
237
    @property
 
238
    def email_widget(self):
 
239
        """Return the widget used to show the email information."""
 
240
        return self.ui.email_widget
 
241
 
 
242
    @property
 
243
    def forgotted_password_intro_label(self):
 
244
        """Return the intro label that lets the user know the issue."""
 
245
        return self.ui.forgotted_password_intro_label
 
246
 
 
247
    @property
 
248
    def error_label(self):
 
249
        """Return the label used to show error."""
 
250
        return self.ui.error_label
 
251
 
 
252
    @property
 
253
    def email_address_label(self):
 
254
        """Return the lable used to state the use of the line edit."""
 
255
        return self.ui.email_address_label
 
256
 
 
257
    @property
 
258
    def email_address(self):
 
259
        """Return the email address provided by the user."""
 
260
        return str(self.ui.email_line_edit.text())
 
261
 
 
262
    @property
 
263
    def email_address_line_edit(self):
 
264
        """Return the line edit with the content."""
 
265
        return self.ui.email_line_edit
 
266
 
 
267
    @property
 
268
    def send_button(self):
 
269
        """Return the button used to request the new password."""
 
270
        return self.ui.send_button
 
271
 
 
272
    @property
 
273
    def try_again_widget(self):
 
274
        """Return the widget used to display the try again button."""
 
275
        return self.ui.try_again_widget
 
276
 
 
277
    @property
 
278
    def try_again_button(self):
 
279
        """Return the button used to try again the reset password."""
 
280
        return self.ui.try_again_button
 
281
 
 
282
 
 
283
class ResetPasswordPage(SSOWizardEnhancedEditPage):
 
284
    """Widget used to allow the user change his password."""
 
285
 
 
286
    def __init__(self, ui, controller, parent=None):
 
287
        """Create a new instance."""
 
288
        SSOWizardEnhancedEditPage.__init__(self, ui, controller, parent)
 
289
 
 
290
    @property
 
291
    def reset_code_line_edit(self):
 
292
        """Return the  line edit where the reset code is given."""
 
293
        return self.ui.reset_code_line_edit
 
294
 
 
295
    @property
 
296
    def reset_code(self):
 
297
        """Return the reset code typed by the user."""
 
298
        return str(self.ui.reset_code_line_edit.text())
 
299
 
 
300
    @property
 
301
    def password_line_edit(self):
 
302
        """Return the line edit where the password is typed."""
 
303
        return self.ui.password_line_edit
 
304
 
 
305
    @property
 
306
    def password(self):
 
307
        """Return the passsword typed by the user."""
 
308
        return str(self.ui.password_line_edit.text())
 
309
 
 
310
    @property
 
311
    def confirm_password_line_edit(self):
 
312
        """Return the line edit used to confirm the password."""
 
313
        return self.ui.confirm_password_line_edit
 
314
 
 
315
    @property
 
316
    def retyped_password(self):
 
317
        """Return the password confirmation given by the user."""
 
318
        return str(self.ui.confirm_password_line_edit.text())
 
319
 
 
320
    @property
 
321
    def reset_password_button(self):
 
322
        """Return the button used to reset the password."""
 
323
        return self.ui.reset_password_button
 
324
 
 
325
 
181
326
class TosPage(SSOWizardPage):
182
327
    """Widget used to show the tos."""
183
328
 
191
336
        return self.ui.terms_webkit
192
337
 
193
338
 
194
 
class EnhancedLineEdit(object):
195
 
    """Represents and enhanced lineedit.
196
 
 
197
 
    This class works on an already added lineedit to the widget so
198
 
    that we are just adding extra items to it.
199
 
    """
200
 
 
201
 
    def __init__(self, line_edit, valid_cb=lambda x: False):
202
 
        """Create an instance."""
203
 
        self._line_edit = line_edit
204
 
        layout = QHBoxLayout(self._line_edit)
205
 
        layout.setMargin(0)
206
 
        self._line_edit.setLayout(layout)
207
 
        self.valid_cb = valid_cb
208
 
        layout.addStretch()
209
 
        self.clear_button = QPushButton(self._line_edit)
210
 
        layout.addWidget(self.clear_button)
211
 
        self.clear_button.setMinimumSize(16, 16)
212
 
        self.clear_button.setVisible(False)
213
 
        self.clear_button.setFlat(True)
214
 
        self.clear_button.setCursor(QCursor(0))
215
 
        self.clear_button.setIcon(QApplication.style().standardIcon(
216
 
                                                QStyle.SP_MessageBoxWarning))
217
 
        # connect the change of text to the cation that will check if the
218
 
        # text is valid and if the icon should be shown.
219
 
        self._line_edit.textChanged.connect(self.show_button)
220
 
 
221
 
    def show_button(self, string):
222
 
        """Decide if we show the button or not."""
223
 
        if not self.valid_cb(string):
224
 
            self.clear_button.setVisible(True)
225
 
        else:
226
 
            self.clear_button.setVisible(False)
227
 
 
228
 
 
229
 
class SetupAccountPage(SSOWizardPage):
 
339
class SetupAccountPage(SSOWizardEnhancedEditPage):
230
340
    """Widget used to create a new account."""
231
341
 
232
342
    def __init__(self, ui, controller, parent=None):
275
385
        frame.setPalette(palette)
276
386
        frame.setAutoFillBackground(True)
277
387
 
278
 
 
279
 
    def set_line_edit_validation_rule(self, edit, cb):
280
 
        """Set a new enhanced edit so that we can show an icon."""
281
 
        if edit in self._enhanced_edits:
282
 
            self._enhanced_edits[edit].valid_cb = cb
283
 
        else:
284
 
            # create a new enhanced edit
285
 
            enhanced_edit = EnhancedLineEdit(edit, cb)
286
 
            self._enhanced_edits[edit] = enhanced_edit
287
 
 
288
388
    @property
289
389
    def name(self):
290
390
        """Return the name input."""
422
522
                 help_text=''):
423
523
        """Create a new wizard."""
424
524
        QWizard.__init__(self, parent)
 
525
        # store common useful data provided by the app
 
526
        self.app_name = app_name
 
527
        self.tos_url = tos_url
 
528
        self.help_text = help_text
 
529
 
425
530
        # set the diff pages of the QWizard
426
531
        self.sign_in_controller = ChooseSignInController(title='Sign In')
427
532
        self.sign_in_page = ChooseSignInPage(Ui_ChooseSignInPage(),
428
533
                                             self.sign_in_controller,
429
534
                                             parent=self)
430
 
        self.setup_controller = SetUpAccountController(app_name=app_name,
431
 
                                                       help_message='')
 
535
        self.setup_controller = SetUpAccountController()
432
536
        self.setup_account = SetupAccountPage(Ui_SetUpAccountPage(),
433
537
                                              self.setup_controller,
434
538
                                              parent=self)
435
 
        self.tos = TosPage(Ui_TosPage(), TosController())
 
539
        self.tos = TosPage(Ui_TosPage(), TosController(tos_url=tos_url),
 
540
                           parent=self)
436
541
        self.email_verification = EmailVerificationPage(
437
542
                                                Ui_EmailVerificationPage(),
438
543
                                                EmailVerificationController())
439
 
        self.current_user_controller = CurrentUserController(title='Sign in',
440
 
                                                            app_name=app_name)
 
544
        self.current_user_controller = CurrentUserController(title='Sign in')
441
545
        self.current_user = CurrentUserSignInPage(Ui_CurrentUserSignInPage(),
442
546
                                                  self.current_user_controller,
443
547
                                                  parent=self)
446
550
                                   parent=self)
447
551
        self.error_controller = ErrorController()
448
552
        self.error = ErrorPage(Ui_ErrorPage(), self.error_controller)
 
553
        self.forgotte_pwd_controller = ForgottenPasswordController()
 
554
        self.forgotten = ForgottenPasswordPage(Ui_ForgottenPasswordPage(),
 
555
                                               self.forgotte_pwd_controller,
 
556
                                               parent=self)
 
557
        self.reset_password_controller = ResetPasswordController()
 
558
        self.reset_password = ResetPasswordPage(Ui_ResetPasswordPage(),
 
559
                                                self.reset_password_controller,
 
560
                                                parent=self)
449
561
        # store the ids of the pages so that it is easier to access them later
450
562
        self._pages = {}
451
563
        for page in [self.sign_in_page, self.setup_account, self.tos,
452
564
                     self.email_verification, self.current_user, self.success,
453
 
                     self.error]:
 
565
                     self.error, self.forgotten, self.reset_password]:
454
566
            self._pages[page] = self.addPage(page)
455
567
 
456
568
        # set the buttons layout to only have cancel and back since the next
497
609
    @property
498
610
    def forgotten_password_page_id(self):
499
611
        """Return the id of the forgotten password page."""
 
612
        return self._pages[self.forgotten]
 
613
 
 
614
    @property
 
615
    def reset_password_page_id(self):
 
616
        """Return the id of the reset password page."""
 
617
        return self._pages[self.reset_password]
500
618
 
501
619
    @property
502
620
    def error_page_id(self):
507
625
class UbuntuSSOClientGUI(object):
508
626
    """Ubuntu single sign-on GUI."""
509
627
 
510
 
    def __init__(self, app_name, tc_url='', help_text='',
 
628
    def __init__(self, app_name, tc_url='http://one.ubuntu.com', help_text='',
511
629
                 window_id=0, login_only=False):
512
630
        """Create a new instance."""
513
631
        # create the controller and the ui, then set the cb and call the show