1
# -*- coding: utf-8 -*-
3
# Copyright 2012 Canonical Ltd.
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.
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.
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/>.
17
"""Email Verification page UI."""
19
from twisted.internet import defer
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 (
28
from ubuntu_sso.utils.ui import ERROR_EMAIL_TOKEN
31
logger = setup_logging('ubuntu_sso.email_verification_page')
34
class EmailVerificationPage(SSOWizardPage):
35
"""Widget used to input the email verification code."""
37
def __init__(self, ui, *args, **kwargs):
38
super(EmailVerificationPage, self).__init__(ui, *args, **kwargs)
42
self._filter_by_app_name(self.on_email_validated),
43
'EmailValidationError':
44
self._filter_by_app_name(self.on_email_validation_error),
48
@defer.inlineCallbacks
50
"""Setup the ui components."""
51
self.backend = yield self.get_backend()
53
self._connect_ui_elements()
56
def verification_code(self):
57
"""Return the content of the verification code edit."""
58
return str(self.ui.verification_code_edit.text())
61
def next_button(self):
62
"""Return the button that move to the next stage."""
63
return self.ui.next_button
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(
70
self.next_button.clicked.connect(self.validate_email)
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(
81
self.next_button.style().polish(
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,
93
def set_titles(self, email):
94
"""This class needs to have a public set_titles.
96
Since the subtitle contains data that is only known after SetupAccount
97
and _set_titles is only called on initialization.
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,
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)
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)
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