~ubuntuone-control-tower/ubuntu-sso-client/trunk

« back to all changes in this revision

Viewing changes to ubuntu_sso/qt/email_verification_page.py

- Refactor the pages and controller in sso (LP: #929686).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
#
 
3
# Copyright 2012 Canonical Ltd.
 
4
#
 
5
# This program is free software: you can redistribute it and/or modify it
 
6
# under the terms of the GNU General Public License version 3, as published
 
7
# by the Free Software Foundation.
 
8
#
 
9
# This program is distributed in the hope that it will be useful, but
 
10
# WITHOUT ANY WARRANTY; without even the implied warranties of
 
11
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
12
# PURPOSE.  See the GNU General Public License for more details.
 
13
#
 
14
# You should have received a copy of the GNU General Public License along
 
15
# with this program.  If not, see <http://www.gnu.org/licenses/>.
 
16
 
 
17
"""Email Verification page UI."""
 
18
 
 
19
from twisted.internet import defer
 
20
 
 
21
from ubuntu_sso.qt import build_general_error_message
 
22
from ubuntu_sso.qt.gui import SSOWizardPage
 
23
from ubuntu_sso.logger import setup_logging
 
24
from ubuntu_sso.utils.ui import (
 
25
    VERIFY_EMAIL_TITLE,
 
26
    VERIFY_EMAIL_CONTENT,
 
27
)
 
28
from ubuntu_sso.utils.ui import ERROR_EMAIL_TOKEN
 
29
 
 
30
 
 
31
logger = setup_logging('ubuntu_sso.email_verification_page')
 
32
 
 
33
 
 
34
class EmailVerificationPage(SSOWizardPage):
 
35
    """Widget used to input the email verification code."""
 
36
 
 
37
    def __init__(self, ui, *args, **kwargs):
 
38
        super(EmailVerificationPage, self).__init__(ui, *args, **kwargs)
 
39
        self.email = ''
 
40
        self._signals = {
 
41
            'EmailValidated':
 
42
             self._filter_by_app_name(self.on_email_validated),
 
43
            'EmailValidationError':
 
44
             self._filter_by_app_name(self.on_email_validation_error),
 
45
        }
 
46
        self.setup_page()
 
47
 
 
48
    @defer.inlineCallbacks
 
49
    def setup_page(self):
 
50
        """Setup the ui components."""
 
51
        self.backend = yield self.get_backend()
 
52
        self._setup_signals()
 
53
        self._connect_ui_elements()
 
54
 
 
55
    @property
 
56
    def verification_code(self):
 
57
        """Return the content of the verification code edit."""
 
58
        return str(self.ui.verification_code_edit.text())
 
59
 
 
60
    @property
 
61
    def next_button(self):
 
62
        """Return the button that move to the next stage."""
 
63
        return self.ui.next_button
 
64
 
 
65
    def _connect_ui_elements(self):
 
66
        """Set the connection of signals."""
 
67
        logger.debug('EmailVerificationController._connect_ui_elements')
 
68
        self.ui.verification_code_edit.textChanged.connect(
 
69
            self.validate_form)
 
70
        self.next_button.clicked.connect(self.validate_email)
 
71
 
 
72
    def validate_form(self):
 
73
        """Check the state of the form."""
 
74
        code = self.verification_code.strip()
 
75
        enabled = len(code) > 0
 
76
        self.next_button.setEnabled(enabled)
 
77
        self.next_button.setProperty('DisabledState',
 
78
            not self.next_button.isEnabled())
 
79
        self.next_button.style().unpolish(
 
80
            self.next_button)
 
81
        self.next_button.style().polish(
 
82
            self.next_button)
 
83
 
 
84
    def _set_titles(self):
 
85
        """Set the different titles."""
 
86
        logger.debug('EmailVerificationController._set_titles')
 
87
        self.header.set_title(VERIFY_EMAIL_TITLE)
 
88
        self.header.set_subtitle(VERIFY_EMAIL_CONTENT % {
 
89
            "app_name": self.app_name,
 
90
            "email": self.email,
 
91
        })
 
92
 
 
93
    def set_titles(self, email):
 
94
        """This class needs to have a public set_titles.
 
95
 
 
96
        Since the subtitle contains data that is only known after SetupAccount
 
97
        and _set_titles is only called on initialization.
 
98
        """
 
99
        self.email = email
 
100
        self._set_titles()
 
101
 
 
102
    def validate_email(self):
 
103
        """Call the next action."""
 
104
        logger.debug('EmailVerificationController.validate_email')
 
105
        email = unicode(self.wizard().field('email_address').toString())
 
106
        password = unicode(self.wizard().field('password').toString())
 
107
        code = unicode(self.ui.verification_code_edit.text())
 
108
        self.backend.validate_email(self.app_name, email,
 
109
                                    password, code)
 
110
 
 
111
    def on_email_validated(self, app_name, *args, **kwargs):
 
112
        """Signal thrown after the email is validated."""
 
113
        logger.info('EmailVerificationController.on_email_validated')
 
114
        email = self.wizard().field('email_address').toString()
 
115
        self.wizard().registrationSuccess.emit(app_name, email)
 
116
 
 
117
    def on_email_validation_error(self, app_name, error):
 
118
        """Signal thrown when there's a problem validating the email."""
 
119
        msg = error.pop(ERROR_EMAIL_TOKEN, '')
 
120
        msg += build_general_error_message(error)
 
121
        self.message_box.critical(self, self.app_name, msg)
 
122
 
 
123
    #pylint: disable=C0103
 
124
    def initializePage(self):
 
125
        """Called to prepare the page just before it is shown."""
 
126
        self.next_button.setDefault(True)
 
127
        self.next_button.setEnabled(False)
 
128
        self.next_button.setProperty('DisabledState',
 
129
            not self.next_button.isEnabled())
 
130
        self.next_button.style().unpolish(self.next_button)
 
131
        self.next_button.style().polish(self.next_button)
 
132
    #pylint: enable=C0103