~nataliabidart/ubuntu-sso-client/stable-3-0-update-2.99.90

« back to all changes in this revision

Viewing changes to ubuntu_sso/qt/setup_account_page.py

  • Committer: Natalia B. Bidart
  • Date: 2012-03-06 14:53:38 UTC
  • mfrom: (812.1.89 ubuntu-sso-client)
  • Revision ID: natalia.bidart@canonical.com-20120306145338-x28na428035jkqwh
- Updating from trunk up to revno 901.

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
# pylint: enable=F0401
30
30
 
31
31
from PyQt4 import QtGui, QtCore
32
 
from twisted.internet import defer
33
32
 
34
33
from ubuntu_sso import NO_OP
35
 
from ubuntu_sso.qt import build_general_error_message
36
 
from ubuntu_sso.qt import common
37
 
from ubuntu_sso.qt.gui import SSOWizardEnhancedEditPage
38
 
from ubuntu_sso.qt import enhanced_check_box
39
 
from ubuntu_sso.utils.ui import SET_UP_ACCOUNT_BUTTON
 
34
from ubuntu_sso.logger import setup_gui_logging
 
35
from ubuntu_sso.qt import (
 
36
    LINK_STYLE,
 
37
    build_general_error_message,
 
38
    common,
 
39
    enhanced_check_box,
 
40
    ERROR_STYLE,
 
41
)
 
42
from ubuntu_sso.qt.sso_wizard_page import SSOWizardEnhancedEditPage
 
43
from ubuntu_sso.qt.ui.setup_account_ui import Ui_SetUpAccountPage
40
44
from ubuntu_sso.utils.ui import (
41
45
    AGREE_TO_PRIVACY_POLICY,
42
46
    AGREE_TO_TERMS,
43
47
    AGREE_TO_TERMS_AND_PRIVACY_POLICY,
44
48
    CAPTCHA_LOAD_ERROR,
 
49
    CAPTCHA_RELOAD_MESSAGE,
 
50
    CAPTCHA_RELOAD_TEXT,
45
51
    CAPTCHA_REQUIRED_ERROR,
46
52
    CAPTCHA_SOLUTION_ENTRY,
47
53
    EMAIL,
66
72
    PRIVACY_POLICY_TEXT,
67
73
    RETYPE_EMAIL,
68
74
    RETYPE_PASSWORD,
 
75
    SET_UP_ACCOUNT_BUTTON,
69
76
    TERMS_TEXT,
70
 
    TITLE,
 
77
    REGISTER_TITLE,
71
78
)
72
 
from ubuntu_sso.logger import setup_logging
73
 
 
74
 
 
75
 
logger = setup_logging('ubuntu_sso.setup_account_page')
76
 
 
77
 
# pylint: disable=C0103
78
 
ERROR = u'<font color="#df2d1f"><b> %s </b></font>'
79
 
TITLE_STYLE = "<span style=\"font-size:24px\">%s</span>"
 
79
 
 
80
 
 
81
logger = setup_gui_logging('ubuntu_sso.setup_account_page')
80
82
 
81
83
ERROR_EMAIL = 'email'
82
 
TERMS_LINK = ("<a href='{toc_link}'>"
83
 
              "<span style='color:#df2d1f;'>{terms_text}</span></a>")
84
 
PRIVACY_POLICY_LINK = ("<a href='{policy_link}'>"
85
 
                        "<span style='color:#df2d1f;'>{privacy_policy_text}"
86
 
                        "</span></a></font>")
87
84
 
88
85
 
89
86
class SetupAccountPage(SSOWizardEnhancedEditPage):
90
87
    """Customized Setup Account page for SSO."""
91
88
 
92
 
    userRegistered = QtCore.pyqtSignal('QString', 'QString')
 
89
    ui_class = Ui_SetUpAccountPage
 
90
    userRegistered = QtCore.pyqtSignal(unicode)
93
91
 
94
 
    def __init__(self, ui, subtitle, toc_link, policy_link, *args, **kwargs):
95
 
        super(SetupAccountPage, self).__init__(ui, *args, **kwargs)
96
 
        self._subtitle = subtitle
 
92
    def __init__(self, *args, **kwargs):
 
93
        self.captcha_file = None
97
94
        self.captcha_id = None
98
 
        self.captcha_file = None
99
 
        self.ui.captcha_view.setPixmap(QtGui.QPixmap())
100
 
        self.ui.password_edit.textEdited.connect(
101
 
            lambda: common.password_assistance(self.ui.password_edit,
102
 
                                                 self.ui.password_assistance,
103
 
                                                 common.NORMAL))
104
 
 
105
 
        if toc_link:
106
 
            terms_links = TERMS_LINK.format(toc_link=toc_link,
107
 
                                            terms_text=TERMS_TEXT)
108
 
        if policy_link:
109
 
            privacy_policy_link = PRIVACY_POLICY_LINK.format(
110
 
                                    policy_link=policy_link,
111
 
                                    privacy_policy_text=PRIVACY_POLICY_TEXT)
112
 
 
113
 
        terms = ''
114
 
        if toc_link and policy_link:
115
 
            terms = AGREE_TO_TERMS_AND_PRIVACY_POLICY.format(
116
 
                        app_name=self.app_name,
117
 
                        terms_and_conditions=terms_links,
118
 
                        privacy_policy=privacy_policy_link)
119
 
        elif toc_link:
120
 
            terms = AGREE_TO_TERMS.format(app_name=self.app_name,
121
 
                        terms_and_conditions=terms_links)
122
 
        elif policy_link:
123
 
            terms = AGREE_TO_PRIVACY_POLICY.format(app_name=self.app_name,
124
 
                        privacy_policy=privacy_policy_link)
125
 
 
126
 
        self.terms_checkbox = enhanced_check_box.EnhancedCheckBox(terms)
127
 
        self.ui.hlayout_check.addWidget(self.terms_checkbox)
128
 
        self.terms_checkbox.setVisible(bool(toc_link or policy_link))
129
 
 
 
95
        self.captcha_received = False
130
96
        self.set_up_button = None
131
 
        self.captcha_received = False
132
 
        self._signals = {
 
97
        self.terms_checkbox = None
 
98
        super(SetupAccountPage, self).__init__(*args, **kwargs)
 
99
 
 
100
    @property
 
101
    def _signals(self):
 
102
        """The signals to connect to the backend."""
 
103
        result = {
133
104
            'CaptchaGenerated':
134
105
             self._filter_by_app_name(self.on_captcha_generated),
135
106
            'CaptchaGenerationError':
139
110
            'UserRegistrationError':
140
111
             self._filter_by_app_name(self.on_user_registration_error),
141
112
        }
142
 
        self.setup_page()
143
 
 
144
 
    @defer.inlineCallbacks
145
 
    def setup_page(self):
146
 
        """Setup the widget components."""
147
 
        # request the backend to be used with the ui
148
 
        self.backend = yield self.get_backend()
149
 
        # set the callbacks for the captcha generation
150
 
        self._setup_signals()
151
 
        self._connect_ui_elements()
152
 
        self._refresh_captcha()
153
 
        self._set_translated_strings()
154
 
        self._set_line_edits_validations()
155
 
        self._register_fields()
 
113
        return result
156
114
 
157
115
    # Invalid name "initializePage"
158
116
    # pylint: disable=C0103
159
117
 
160
118
    def initializePage(self):
161
119
        """Setup UI details."""
162
 
        # We need to override some texts from SSO
163
 
        # to match our spec
164
 
        title_page = TITLE_STYLE % TITLE.format(app_name=self.app_name)
165
 
        self.setTitle(title_page)
166
 
        self.setSubTitle(self._subtitle)
167
120
        # Set Setup Account button
168
121
        self.wizard().setOption(QtGui.QWizard.HaveCustomButton3, True)
169
122
        try:
172
125
            pass
173
126
        self.setButtonText(QtGui.QWizard.CustomButton3, SET_UP_ACCOUNT_BUTTON)
174
127
        self.set_up_button = self.wizard().button(QtGui.QWizard.CustomButton3)
175
 
        self.set_up_button.clicked.connect(self.overlay.show)
176
128
        self.set_up_button.clicked.connect(self.set_next_validation)
177
129
        self.set_up_button.setEnabled(False)
178
130
 
181
133
        self.ui.confirm_email_label.setText(RETYPE_EMAIL)
182
134
        self.ui.password_label.setText(PASSWORD)
183
135
        self.ui.confirm_password_label.setText(RETYPE_PASSWORD)
184
 
        self.ui.password_info_label.hide()
185
136
 
186
137
        # Button setup
187
138
        self.wizard().setButtonLayout([
196
147
        self.ui.confirm_email_assistance.setVisible(False)
197
148
        self.ui.password_assistance.setVisible(False)
198
149
        self.ui.refresh_label.setVisible(True)
199
 
    #pylint: enable=C0103
 
150
 
 
151
    # pylint: enable=C0103
200
152
 
201
153
    def _set_translated_strings(self):
202
154
        """Set the strings."""
203
155
        logger.debug('SetUpAccountPage._set_translated_strings')
204
156
        # set the translated string
 
157
        title_page = REGISTER_TITLE.format(app_name=self.app_name)
 
158
        self.setTitle(title_page)
 
159
        self.setSubTitle(self.help_text)
 
160
 
205
161
        self.ui.name_label.setText(NAME_ENTRY)
206
162
        self.ui.email_label.setText(EMAIL1_ENTRY)
207
163
        self.ui.confirm_email_label.setText(EMAIL2_ENTRY)
208
164
        self.ui.password_label.setText(PASSWORD1_ENTRY)
209
165
        self.ui.confirm_password_label.setText(PASSWORD2_ENTRY)
210
 
        self.ui.password_info_label.setText(PASSWORD_HELP)
 
166
        self.ui.password_edit.setToolTip(PASSWORD_HELP)
211
167
        self.ui.captcha_solution_edit.setPlaceholderText(
212
168
                                                      CAPTCHA_SOLUTION_ENTRY)
 
169
        link = LINK_STYLE.format(link_url='#', link_text=CAPTCHA_RELOAD_TEXT)
 
170
        self.ui.refresh_label.setText(CAPTCHA_RELOAD_MESSAGE %
 
171
                                      {'reload_link': link})
 
172
 
 
173
        if self.tc_url:
 
174
            terms_links = LINK_STYLE.format(link_url=self.tc_url,
 
175
                                            link_text=TERMS_TEXT)
 
176
        if self.policy_url:
 
177
            privacy_policy_link = LINK_STYLE.format(link_url=self.policy_url,
 
178
                                                 link_text=PRIVACY_POLICY_TEXT)
 
179
 
 
180
        terms = ''
 
181
        if self.tc_url and self.policy_url:
 
182
            terms = AGREE_TO_TERMS_AND_PRIVACY_POLICY.format(
 
183
                        app_name=self.app_name,
 
184
                        terms_and_conditions=terms_links,
 
185
                        privacy_policy=privacy_policy_link)
 
186
        elif self.tc_url:
 
187
            terms = AGREE_TO_TERMS.format(app_name=self.app_name,
 
188
                        terms_and_conditions=terms_links)
 
189
        elif self.policy_url:
 
190
            terms = AGREE_TO_PRIVACY_POLICY.format(app_name=self.app_name,
 
191
                        privacy_policy=privacy_policy_link)
 
192
 
 
193
        self.terms_checkbox = enhanced_check_box.EnhancedCheckBox(terms)
 
194
        self.ui.hlayout_check.addWidget(self.terms_checkbox)
 
195
        self.terms_checkbox.setVisible(bool(self.tc_url or self.policy_url))
 
196
 
 
197
        self._register_fields()
213
198
 
214
199
    def _set_line_edits_validations(self):
215
200
        """Set the validations to be performed on the edits."""
233
218
        self.ui.password_edit.textChanged.connect(
234
219
            self.ui.confirm_password_edit.textChanged.emit)
235
220
 
236
 
    def _connect_ui_elements(self):
 
221
    def _connect_ui(self):
237
222
        """Set the connection of signals."""
238
 
        logger.debug('SetUpAccountPage._connect_ui_elements')
 
223
        logger.debug('SetUpAccountPage._connect_ui')
 
224
        self._set_line_edits_validations()
 
225
 
 
226
        self.ui.captcha_view.setPixmap(QtGui.QPixmap())
 
227
        self.ui.password_edit.textEdited.connect(
 
228
            lambda: common.password_assistance(self.ui.password_edit,
 
229
                                                 self.ui.password_assistance,
 
230
                                                 common.NORMAL))
 
231
 
239
232
        self.ui.refresh_label.linkActivated.connect(lambda url: \
240
233
                                          self._refresh_captcha())
241
234
        # We need to check if we enable the button on many signals
250
243
            self._enable_setup_button)
251
244
        self.terms_checkbox.stateChanged.connect(self._enable_setup_button)
252
245
 
 
246
        self._refresh_captcha()
 
247
 
253
248
    def _enable_setup_button(self):
254
249
        """Only enable the setup button if the form is valid."""
255
250
        name = unicode(self.ui.name_edit.text()).strip()
272
267
          email == confirm_email and len(name) > 0
273
268
 
274
269
        self.set_up_button.setEnabled(enabled)
275
 
        self.set_up_button.setProperty("DisabledState", not enabled)
276
 
        self.set_up_button.style().unpolish(self.set_up_button)
277
 
        self.set_up_button.style().polish(self.set_up_button)
278
270
 
279
271
    def _refresh_captcha(self):
280
272
        """Refresh the captcha image shown in the ui."""
281
273
        logger.debug('SetUpAccountPage._refresh_captcha')
 
274
        self.hide_error()
282
275
        # lets clean behind us, do we have the old file arround?
283
276
        if self.captcha_file and os.path.exists(self.captcha_file):
284
277
            os.unlink(self.captcha_file)
330
323
    def on_captcha_generation_error(self, error, *args, **kwargs):
331
324
        """An error ocurred."""
332
325
        logger.debug('SetUpAccountPage.on_captcha_generation_error')
333
 
        self.message_box.critical(self, self.app_name, CAPTCHA_LOAD_ERROR)
 
326
        self.show_error(self.app_name, CAPTCHA_LOAD_ERROR)
334
327
        self.on_captcha_refresh_complete()
335
328
 
336
329
    def on_user_registration_error(self, app_name, error):
342
335
            self.set_error_message(self.ui.email_assistance, msg)
343
336
        error_msg = build_general_error_message(error)
344
337
        if error_msg:
345
 
            self.message_box.critical(self, self.app_name, error_msg)
 
338
            self.show_error(self.app_name, error_msg)
346
339
        self._refresh_captcha()
347
340
 
348
 
    def on_user_registered(self, app_name, result):
 
341
    def on_user_registered(self, app_name, email):
349
342
        """Execute when the user did register."""
 
343
        self.hide_overlay()
350
344
        logger.debug('SetUpAccountPage.on_user_registered')
351
345
        email = unicode(self.ui.email_edit.text())
352
 
        password = unicode(self.ui.password_edit.text())
353
 
        self.userRegistered.emit(email, password)
 
346
        self.userRegistered.emit(email)
354
347
 
355
348
    def validate_form(self):
356
349
        """Validate the info of the form and return an error."""
383
376
            messages.append(CAPTCHA_REQUIRED_ERROR)
384
377
        if len(messages) > 0:
385
378
            condition = False
386
 
            self.message_box.critical(self, self.app_name, '\n'.join(messages))
 
379
            self.show_error(self.app_name, '\n'.join(messages))
387
380
        return condition
388
381
 
389
 
    # pylint: disable=W0212
390
382
    def set_next_validation(self):
391
383
        """Set the validation as the next page."""
392
384
        logger.debug('SetUpAccountPage.set_next_validation')
398
390
        # validate the current info of the form, try to perform the action
399
391
        # to register the user, and then move foward
400
392
        if self.validate_form():
 
393
            self.show_overlay()
 
394
            self.hide_error()
401
395
            args = (self.app_name, email, password, name, captcha_id,
402
396
                captcha_solution)
403
397
            f = self.backend.register_user
404
398
            error_handler = partial(self._handle_error, f,
405
399
                self.on_user_registration_error)
406
400
            f(*args, reply_handler=NO_OP, error_handler=error_handler)
407
 
            # Update validation page's title, which contains the email
408
 
            self.userRegistered.emit(email, password)
409
 
    # pylint: enable=W0212
410
401
 
411
402
    def is_correct_email(self, email_address):
412
403
        """Return if the email is correct."""
474
465
 
475
466
    def set_error_message(self, label, msg):
476
467
        """Set the message to the proper label applying the error style."""
477
 
        label.setText(ERROR % msg)
 
468
        label.setText(ERROR_STYLE % msg)
478
469
        label.setVisible(True)
479
470
 
480
 
    #pylint: disable=C0103
 
471
    # pylint: disable=C0103
 
472
 
481
473
    def showEvent(self, event):
482
474
        """Set set_up_button as default button when the page is shown."""
483
475
        # This method should stays here because if we move it to initializePage
485
477
        if self.set_up_button is not None:
486
478
            self.set_up_button.setVisible(True)
487
479
            self.set_up_button.setDefault(True)
488
 
            if not self.set_up_button.isEnabled():
489
 
                self.set_up_button.setProperty("DisabledState", True)
490
 
                self.set_up_button.style().unpolish(self.set_up_button)
491
 
                self.set_up_button.style().polish(self.set_up_button)
492
480
        self.connect(QtGui.QApplication.instance(),
493
481
            QtCore.SIGNAL("focusChanged(QWidget*, QWidget*)"),
494
482
            self.focus_changed)
495
483
        super(SetupAccountPage, self).showEvent(event)
496
484
        if not self.captcha_received:
497
 
            self.overlay.show()
 
485
            self.show_overlay()
498
486
 
499
487
    def hideEvent(self, event):
500
488
        """Disconnect the focusChanged signal when the page change."""
507
495
        except TypeError:
508
496
            pass
509
497
        super(SetupAccountPage, self).hideEvent(event)
510
 
    #pylint: enable=C0103
 
498
 
 
499
    # pylint: enable=C0103
511
500
 
512
501
    def on_captcha_refreshing(self):
513
502
        """Show overlay when captcha is refreshing."""
514
503
        if self.isVisible():
515
 
            self.overlay.show()
 
504
            self.show_overlay()
516
505
        self.captcha_received = False
517
506
 
518
507
    def on_captcha_refresh_complete(self):
519
508
        """Hide overlay when captcha finished refreshing."""
520
 
        self.overlay.hide()
 
509
        self.hide_overlay()
521
510
        self.captcha_received = True
522
511
 
523
512
    def get_captcha_image(self):