~diegosarmentero/ubuntu-sso-client/translation-problem

841.3.3 by Diego Sarmentero
Refactoring
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
"""Forgotten Password page UI."""
18
871.1.3 by Diego Sarmentero
Several fixes regarding the tests
19
from functools import partial
20
853.1.2 by Diego Sarmentero
Creating signals for the different pages, and let the wizard decided what to do in each event.
21
from PyQt4 import QtCore
841.3.3 by Diego Sarmentero
Refactoring
22
871.1.3 by Diego Sarmentero
Several fixes regarding the tests
23
from ubuntu_sso import NO_OP
897.3.6 by Natalia B. Bidart
Lost of UI cleanups.
24
from ubuntu_sso.logger import setup_gui_logging, log_call
884.1.1 by Natalia B. Bidart
- Move the 'choose sign in page' to client code (U1 control panel
25
from ubuntu_sso.qt.sso_wizard_page import SSOWizardEnhancedEditPage
26
from ubuntu_sso.qt.ui.forgotten_password_ui import Ui_ForgottenPasswordPage
841.3.3 by Diego Sarmentero
Refactoring
27
from ubuntu_sso.utils.ui import (
28
    EMAIL_LABEL,
897.3.2 by Natalia B. Bidart
- Add proper titles and subtitles for the Login and Forgot password pages
29
    FORGOTTEN_PASSWORD_TITLE,
30
    FORGOTTEN_PASSWORD_SUBTITLE,
841.3.3 by Diego Sarmentero
Refactoring
31
    is_correct_email,
32
    RESET_PASSWORD,
33
    REQUEST_PASSWORD_TOKEN_WRONG_EMAIL,
34
)
35
36
897.3.6 by Natalia B. Bidart
Lost of UI cleanups.
37
logger = setup_gui_logging('ubuntu_sso.forgotten_password_page')
900.1.1 by Diego Sarmentero
Improving the logging operations
38
39
841.3.3 by Diego Sarmentero
Refactoring
40
class ForgottenPasswordPage(SSOWizardEnhancedEditPage):
41
    """Widget used to deal with users that forgot the password."""
42
884.1.1 by Natalia B. Bidart
- Move the 'choose sign in page' to client code (U1 control panel
43
    ui_class = Ui_ForgottenPasswordPage
897.3.1 by Natalia B. Bidart
Improves to tests.
44
    passwordResetTokenSent = QtCore.pyqtSignal(unicode)
853.1.2 by Diego Sarmentero
Creating signals for the different pages, and let the wizard decided what to do in each event.
45
884.1.1 by Natalia B. Bidart
- Move the 'choose sign in page' to client code (U1 control panel
46
    @property
47
    def _signals(self):
48
        """The signals to connect to the backend."""
49
        result = {
841.3.6 by Diego Sarmentero
Refactor the pages and controller in sso.
50
            'PasswordResetTokenSent':
51
             self._filter_by_app_name(self.on_password_reset_token_sent),
52
            'PasswordResetError':
53
             self._filter_by_app_name(self.on_password_reset_error),
54
        }
884.1.1 by Natalia B. Bidart
- Move the 'choose sign in page' to client code (U1 control panel
55
        return result
841.3.3 by Diego Sarmentero
Refactoring
56
57
    @property
58
    def email_address(self):
59
        """Return the email address provided by the user."""
897.3.4 by Natalia B. Bidart
All tests green.
60
        return unicode(self.ui.email_line_edit.text())
841.3.3 by Diego Sarmentero
Refactoring
61
62
    #pylint: disable=C0103
897.3.4 by Natalia B. Bidart
All tests green.
63
841.3.3 by Diego Sarmentero
Refactoring
64
    def initializePage(self):
65
        """Set the initial state of ForgottenPassword page."""
900.1.5 by Diego Sarmentero
more improves in the logs.
66
        logger.debug('initializePage - About to show ForgottenPasswordPage')
897.3.4 by Natalia B. Bidart
All tests green.
67
        self.ui.send_button.setDefault(True)
841.3.3 by Diego Sarmentero
Refactoring
68
        enabled = not self.ui.email_line_edit.text().isEmpty()
897.3.4 by Natalia B. Bidart
All tests green.
69
        self.ui.send_button.setEnabled(enabled)
70
841.3.3 by Diego Sarmentero
Refactoring
71
    #pylint: enable=C0103
72
73
    def _register_fields(self):
74
        """Register the fields of the wizard page."""
75
        self.registerField('email_address',
897.3.4 by Natalia B. Bidart
All tests green.
76
                           self.ui.email_line_edit)
841.3.3 by Diego Sarmentero
Refactoring
77
78
    def _set_translated_strings(self):
79
        """Set the translated strings in the view."""
897.3.2 by Natalia B. Bidart
- Add proper titles and subtitles for the Login and Forgot password pages
80
        self.setTitle(FORGOTTEN_PASSWORD_TITLE)
81
        subtitle = FORGOTTEN_PASSWORD_SUBTITLE.format(app_name=self.app_name)
82
        self.setSubTitle(subtitle)
897.3.4 by Natalia B. Bidart
All tests green.
83
        self.ui.email_address_label.setText(EMAIL_LABEL)
84
        self.ui.send_button.setText(RESET_PASSWORD)
841.3.3 by Diego Sarmentero
Refactoring
85
86
    def _set_enhanced_line_edit(self):
87
        """Set the extra logic to the line edits."""
897.3.4 by Natalia B. Bidart
All tests green.
88
        self.set_line_edit_validation_rule(self.ui.email_line_edit,
841.3.3 by Diego Sarmentero
Refactoring
89
                                           is_correct_email)
90
91
    def _connect_ui(self):
92
        """Connect the diff signals from the Ui."""
897.3.4 by Natalia B. Bidart
All tests green.
93
        self.ui.email_line_edit.textChanged.connect(self._validate)
94
        self.ui.send_button.clicked.connect(self.request_new_password)
884.1.1 by Natalia B. Bidart
- Move the 'choose sign in page' to client code (U1 control panel
95
        self._set_enhanced_line_edit()
96
        self._register_fields()
97
874.3.2 by Diego Sarmentero
Styling SSO UI
98
    def request_new_password(self):
99
        """Send the request password operation."""
886.1.14 by Diego Sarmentero
Fixing errors reset when the user restart request again some data.
100
        self.hide_error()
871.1.3 by Diego Sarmentero
Several fixes regarding the tests
101
        args = (self.app_name, self.email_address)
900.1.1 by Diego Sarmentero
Improving the logging operations
102
        logger.debug('Sending request new password for %s, email: %s', *args)
871.1.3 by Diego Sarmentero
Several fixes regarding the tests
103
        f = self.backend.request_password_reset_token
104
105
        error_handler = partial(self._handle_error, f,
106
            self.on_password_reset_error)
107
889.1.1 by Diego Sarmentero
Fix: The header in the pages is above the overlay
108
        self.show_overlay()
874.3.2 by Diego Sarmentero
Styling SSO UI
109
        f(*args, reply_handler=NO_OP, error_handler=error_handler)
841.3.3 by Diego Sarmentero
Refactoring
110
111
    def _validate(self):
112
        """Validate that we have an email."""
897.3.4 by Natalia B. Bidart
All tests green.
113
        email = unicode(self.ui.email_line_edit.text())
114
        self.ui.send_button.setEnabled(is_correct_email(email))
841.3.3 by Diego Sarmentero
Refactoring
115
897.3.1 by Natalia B. Bidart
Improves to tests.
116
    def on_password_reset_token_sent(self, app_name, email):
841.3.3 by Diego Sarmentero
Refactoring
117
        """Action taken when we managed to get the password reset done."""
900.1.4 by Diego Sarmentero
Some improves in the logger
118
        logger.info('ForgottenPasswordPage.on_password_reset_token_sent for '
119
                    '%s, email: %s', app_name, email)
841.3.3 by Diego Sarmentero
Refactoring
120
        # ignore the result and move to the reset page
889.1.1 by Diego Sarmentero
Fix: The header in the pages is above the overlay
121
        self.hide_overlay()
897.3.1 by Natalia B. Bidart
Improves to tests.
122
        self.passwordResetTokenSent.emit(email)
841.3.3 by Diego Sarmentero
Refactoring
123
897.3.6 by Natalia B. Bidart
Lost of UI cleanups.
124
    @log_call(logger.error)
841.3.3 by Diego Sarmentero
Refactoring
125
    def on_password_reset_error(self, app_name, error):
126
        """Action taken when there was an error requesting the reset."""
127
        # set the error message
889.1.1 by Diego Sarmentero
Fix: The header in the pages is above the overlay
128
        self.hide_overlay()
897.3.6 by Natalia B. Bidart
Lost of UI cleanups.
129
        msg = REQUEST_PASSWORD_TOKEN_WRONG_EMAIL
904.1.10 by Natalia B. Bidart
Fixed tests.
130
        self.show_error(msg)