~nataliabidart/ubuntu-sso-client/find-me-bin-dir

« back to all changes in this revision

Viewing changes to ubuntu_sso/qt/current_user_sign_in_page.py

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

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# -*- coding: utf-8 -*-
2
 
 
 
2
#
3
3
# Copyright 2012 Canonical Ltd.
4
4
#
5
5
# This program is free software: you can redistribute it and/or modify it
19
19
import gettext
20
20
 
21
21
from PyQt4 import QtGui
 
22
from twisted.internet import defer
22
23
 
 
24
from ubuntu_sso.qt import build_general_error_message
23
25
from ubuntu_sso.qt.gui import SSOWizardPage
 
26
from ubuntu_sso.logger import setup_logging
 
27
from ubuntu_sso.utils.ui import (
 
28
    EMAIL_LABEL,
 
29
    FORGOTTEN_PASSWORD_BUTTON,
 
30
    is_correct_email,
 
31
    LOGIN_PASSWORD_LABEL,
 
32
    SIGN_IN_BUTTON,
 
33
)
24
34
 
25
35
 
26
36
_ = gettext.gettext
 
37
logger = setup_logging('ubuntu_sso.current_user_sign_in_page')
27
38
 
28
39
 
29
40
class CurrentUserSignInPage(SSOWizardPage):
30
41
    """Wizard Page that lets a current user Sign into Ubuntu One."""
31
42
 
 
43
    def __init__(self, ui, *args, **kwargs):
 
44
        super(CurrentUserSignInPage, self).__init__(ui, *args, **kwargs)
 
45
 
 
46
        self._signals = {
 
47
            'LoggedIn':
 
48
             self._filter_by_app_name(self.on_logged_in),
 
49
            'LoginError':
 
50
             self._filter_by_app_name(self.on_login_error),
 
51
             'UserNotValidated':
 
52
             self._filter_by_app_name(self.on_user_not_validated),
 
53
         }
 
54
        self.setup_page()
 
55
 
 
56
    # pylint: disable=W0212
 
57
    def on_user_not_validated(self, *args):
 
58
        """Show the validate email page."""
 
59
        self.next = self.wizard()._pages[self.wizard().email_verification]
 
60
        email = unicode(self.ui.email_edit.text())
 
61
        self.wizard().page(self.next).set_titles(email)
 
62
        self.wizard().next()
 
63
    # pylint: enable=W0212
 
64
 
 
65
    @defer.inlineCallbacks
 
66
    def setup_page(self):
 
67
        """Setup the widget components."""
 
68
        self.backend = yield self.get_backend()
 
69
        self._set_translated_strings()
 
70
        # lets add call backs to be execute for the calls we are interested
 
71
        self._setup_signals()
 
72
        self._connect_ui()
 
73
 
32
74
    # Invalid names of Qt-inherited methods
33
75
    # pylint: disable=C0103
34
76
 
59
101
    def cleanupPage(self):
60
102
        """Reset the state of the wizard if Verification code was visited."""
61
103
        self.wizard()._next_id = None
 
104
 
 
105
    def _set_translated_strings(self):
 
106
        """Set the translated strings."""
 
107
        logger.debug('CurrentUserSignInPage._set_translated_strings')
 
108
        self.ui.email_label.setText(EMAIL_LABEL)
 
109
        self.ui.password_label.setText(LOGIN_PASSWORD_LABEL)
 
110
        self.ui.forgot_password_label.setText(FORGOTTEN_PASSWORD_BUTTON)
 
111
        self.ui.sign_in_button.setText(SIGN_IN_BUTTON)
 
112
 
 
113
    def _connect_ui(self):
 
114
        """Connect the buttons to perform actions."""
 
115
        logger.debug('CurrentUserSignInPage._connect_buttons')
 
116
        self.ui.forgot_password_label.linkActivated.connect(
 
117
                                                    self.on_forgotten_password)
 
118
        self.ui.email_edit.textChanged.connect(self._validate)
 
119
        self.ui.password_edit.textChanged.connect(self._validate)
 
120
        self.ui.sign_in_button.clicked.connect(self.login)
 
121
 
 
122
    def _validate(self):
 
123
        """Perform input validation."""
 
124
        valid = True
 
125
        correct_mail = is_correct_email(unicode(self.ui.email_edit.text()))
 
126
        password = unicode(self.ui.password_edit.text())
 
127
        if not correct_mail or not password:
 
128
            valid = False
 
129
        self.ui.sign_in_button.setEnabled(valid)
 
130
        self.ui.sign_in_button.setProperty("DisabledState",
 
131
            not self.ui.sign_in_button.isEnabled())
 
132
        self.ui.sign_in_button.style().unpolish(self.ui.sign_in_button)
 
133
        self.ui.sign_in_button.style().polish(self.ui.sign_in_button)
 
134
 
 
135
    def login(self):
 
136
        """Perform the login using the self.backend."""
 
137
        logger.debug('CurrentUserSignInPage.login')
 
138
        # grab the data from the view and call the backend
 
139
        email = unicode(self.ui.email_edit.text())
 
140
        password = unicode(self.ui.password_edit.text())
 
141
        self.backend.login(self.app_name, email, password)
 
142
 
 
143
    def on_login_error(self, app_name, error):
 
144
        """There was an error when login in."""
 
145
        # let the user know
 
146
        logger.error('Got error when login %s, error: %s',
 
147
            self.app_name, error)
 
148
        self.message_box.critical(self, self.app_name,
 
149
            build_general_error_message(error))
 
150
 
 
151
    # pylint: disable=W0212
 
152
    def on_logged_in(self, app_name, result):
 
153
        """We managed to log in."""
 
154
        logger.info('Logged in for %s', app_name)
 
155
        self.next = self.wizard()._pages[self.wizard().success]
 
156
        self.wizard().next()
 
157
        logger.debug('Wizard.loginSuccess emitted.')
 
158
 
 
159
    def on_forgotten_password(self):
 
160
        """Show the user the forgotten password page."""
 
161
        logger.info('Forgotten password')
 
162
        self.next = self.wizard()._pages[self.wizard().forgotten]
 
163
        self.wizard().next()
 
164
    # pylint: enable=W0212