~ubuntu-branches/ubuntu/oneiric/ubuntu-sso-client/oneiric

« back to all changes in this revision

Viewing changes to ubuntu_sso/utils/ui.py

  • Committer: Sebastien Bacher
  • Date: 2011-07-25 19:22:29 UTC
  • mfrom: (32.2.3 ubuntu-sso-client)
  • Revision ID: seb128@ubuntu.com-20110725192229-r2kxv3bo1mvk5fj7
* New upstream release.
  - Support new CreateCollection API as well (LP: #805244)
* cleanup leftover from the dh_python2 transition, 
  thanks to Stefano Rivera

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
#
 
3
# Author: Natalia Bidart <natalia.bidart@canonical.com>
 
4
# Author: Manuel de la Pena <manuel@canonical.com>
 
5
#
 
6
# Copyright 2011 Canonical Ltd.
 
7
#
 
8
# This program is free software: you can redistribute it and/or modify it
 
9
# under the terms of the GNU General Public License version 3, as published
 
10
# by the Free Software Foundation.
 
11
#
 
12
# This program is distributed in the hope that it will be useful, but
 
13
# WITHOUT ANY WARRANTY; without even the implied warranties of
 
14
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
15
# PURPOSE.  See the GNU General Public License for more details.
 
16
#
 
17
# You should have received a copy of the GNU General Public License along
 
18
# with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
"""Utils to be used by the UI modules."""
 
20
 
 
21
import os
 
22
import re
 
23
import xdg
 
24
import gettext
 
25
 
 
26
from ubuntu_sso.logger import setup_logging
 
27
 
 
28
logger = setup_logging('ubuntu_sso.utils.ui')
 
29
 
 
30
 
 
31
gettext.textdomain('ubuntu-sso-client')
 
32
_ = gettext.gettext
 
33
 
 
34
# all the text that is used in the gui
 
35
CAPTCHA_SOLUTION_ENTRY = _('Type the characters above')
 
36
CAPTCHA_LOAD_ERROR = _('There was a problem getting the captcha, '
 
37
                       'reloading...')
 
38
CONNECT_HELP_LABEL = _('To connect this computer to %(app_name)s ' \
 
39
                       'enter your details below.')
 
40
EMAIL1_ENTRY = _('Email address')
 
41
EMAIL2_ENTRY = _('Re-type Email address')
 
42
EMAIL_LABEL = EMAIL1_ENTRY + ':'
 
43
EMAIL_MISMATCH = _('The email addresses don\'t match, please double check '
 
44
                       'and try entering them again.')
 
45
EMAIL_INVALID = _('The email must be a valid email address.')
 
46
EMAIL_TOKEN_ENTRY = _('Enter code verification here')
 
47
ERROR = _('The process did not finish successfully.')
 
48
EXISTING_ACCOUNT_CHOICE_BUTTON = _('Sign me in with my existing account')
 
49
FIELD_REQUIRED = _('This field is required.')
 
50
FORGOTTEN_PASSWORD_BUTTON = _('I\'ve forgotten my password')
 
51
JOIN_HEADER_LABEL = _('Create %(app_name)s account')
 
52
LOADING = _('Loading...')
 
53
LOGIN_BUTTON_LABEL = _('Already have an account? Click here to sign in')
 
54
LOGIN_EMAIL_ENTRY = _('Email address')
 
55
LOGIN_HEADER_LABEL = _('Connect to %(app_name)s')
 
56
LOGIN_PASSWORD_ENTRY = _('Password')
 
57
LOGIN_PASSWORD_LABEL = LOGIN_PASSWORD_ENTRY + ':'
 
58
NAME_ENTRY = _('Name')
 
59
NEXT = _('Next')
 
60
ONE_MOMENT_PLEASE = _('One moment please...')
 
61
PASSWORD_CHANGED = _('Your password was successfully changed.')
 
62
PASSWORD1_ENTRY = RESET_PASSWORD1_ENTRY = _('Password')
 
63
PASSWORD2_ENTRY = RESET_PASSWORD2_ENTRY = _('Re-type Password')
 
64
PASSWORD_HELP = _('The password must have a minimum of 8 characters and ' \
 
65
                  'include one uppercase character and one number.')
 
66
PASSWORD_MISMATCH = _('The passwords don\'t match, please double check ' \
 
67
                      'and try entering them again.')
 
68
PASSWORD_TOO_WEAK = _('The password is too weak.')
 
69
REQUEST_PASSWORD_TOKEN_LABEL = _('To reset your %(app_name)s password,'
 
70
                                 ' enter your email address below:')
 
71
REQUEST_PASSWORD_TOKEN_WRONG_EMAIL = _('Sorry we did not recognize the email'
 
72
                                       ' address.')
 
73
REQUEST_PASSWORD_TOKEN_TECH_ERROR = _('We are very Sorry! The service that'
 
74
                                      ' signs you on is not responding right'
 
75
                                      ' now\nPlease try again or come back in'
 
76
                                      ' a few minutes.')
 
77
RESET_PASSWORD = _('Reset password')
 
78
RESET_CODE_ENTRY = _('Reset code')
 
79
RESET_EMAIL_ENTRY = _('Email address')
 
80
SET_NEW_PASSWORD_LABEL = _('A password reset code has been sent to ' \
 
81
                           '%(email)s.\nPlease enter the code below ' \
 
82
                           'along with your new password.')
 
83
SET_UP_ACCOUNT_CHOICE_BUTTON = _('I don\'t have an account yet - sign me up')
 
84
SET_UP_ACCOUNT_BUTTON = _('Set up Account')
 
85
SIGN_IN_BUTTON = _('Sign In')
 
86
SUCCESS = _('The process finished successfully. Congratulations!')
 
87
SURNAME_ENTRY = _('Surname')
 
88
TC_BUTTON = _('Show Terms & Conditions')
 
89
TC_NOT_ACCEPTED = _('Agreeing to the Ubuntu One Terms & Conditions is ' \
 
90
                        'required to subscribe.')
 
91
TRY_AGAIN_BUTTON = _('Try again')
 
92
UNKNOWN_ERROR = _('There was an error when trying to complete the ' \
 
93
                      'process. Please check the information and try again.')
 
94
VERIFY_EMAIL_TITLE = _('Enter verification code')
 
95
VERIFY_EMAIL_CONTENT = _('Check %(email)s for an email from'
 
96
                         ' Ubuntu Single Sign On.'
 
97
                         ' This message contains a verification code.'
 
98
                         ' Enter the code in the field below and click OK'
 
99
                         ' to complete creating your %(app_name)s account')
 
100
VERIFY_EMAIL_LABEL = ('<b>%s</b>\n\n' % VERIFY_EMAIL_TITLE +
 
101
                      VERIFY_EMAIL_CONTENT)
 
102
TOS_LABEL = _("You can also find these terms at <a href='%(url)s'>%(url)s</a>")
 
103
YES_TO_TC = _('I agree with the %(app_name)s terms and conditions')
 
104
YES_TO_UPDATES = _('Yes! Email me %(app_name)s tips and updates.')
 
105
CAPTCHA_RELOAD_TOOLTIP = _('Reload')
 
106
 
 
107
 
 
108
def get_data_dir():
 
109
    """Build absolute path to  the 'data' directory."""
 
110
    module = os.path.dirname(__file__)
 
111
    result = os.path.abspath(os.path.join(module, os.pardir,
 
112
                                          os.pardir, 'data'))
 
113
    logger.debug('get_data_file: trying to load from %r (exists? %s)',
 
114
                 result, os.path.exists(result))
 
115
    if os.path.exists(result):
 
116
        logger.info('get_data_file: returning data dir located at %r.', result)
 
117
        return result
 
118
 
 
119
    # no local data dir, looking within system data dirs
 
120
    data_dirs = xdg.BaseDirectory.xdg_data_dirs
 
121
    for path in data_dirs:
 
122
        result = os.path.join(path, 'ubuntu-sso-client', 'data')
 
123
        result = os.path.abspath(result)
 
124
        logger.debug('get_data_file: trying to load from %r (exists? %s)',
 
125
                     result, os.path.exists(result))
 
126
        if os.path.exists(result):
 
127
            logger.info('get_data_file: data dir located at %r.', result)
 
128
            return result
 
129
    else:
 
130
        msg = 'get_data_file: can not build a valid data path. Giving up.' \
 
131
              '__file__ is %r, data_dirs are %r'
 
132
        logger.error(msg, __file__, data_dirs)
 
133
 
 
134
 
 
135
def get_data_file(*args):
 
136
    """Build absolute path to the path within the 'data' directory."""
 
137
    return os.path.join(get_data_dir(), *args)
 
138
 
 
139
 
 
140
def get_password_strength(password):
 
141
    """Return the strength of the password.
 
142
 
 
143
    This function returns the strength of the password so that ui elements
 
144
    can show the user how good his password is. The logic used is the
 
145
    following:
 
146
 
 
147
    * 1 extra point for 4 chars passwords
 
148
    * 1 extra point for 8 chars passwords
 
149
    * 1 extra point for more than 11 chars passwords.
 
150
    * 1 extra point for passwords with at least one number.
 
151
    * 1 extra point for passwords for lower and capital chars.
 
152
    * 1 extra point for passwords with a special char.
 
153
 
 
154
    A passwords starts with 0 and the extra points are added accordingly.
 
155
    """
 
156
    score = 0
 
157
    if len(password) < 1:
 
158
        return 0
 
159
    if len(password) < 4:
 
160
        score = 1
 
161
    if len(password) >= 8:
 
162
        score += 1
 
163
    if len(password) >= 11:
 
164
        score += 1
 
165
    if re.search('\d+', password):
 
166
        score += 1
 
167
    if re.search('[a-z]', password) and re.search('[A-Z]', password):
 
168
        score += 1
 
169
    if re.search('.[!,@,#,$,%,^,&,*,?,_,~,-,£,(,)]', password):
 
170
        score += 1
 
171
    return score
 
172
 
 
173
 
 
174
def is_min_required_password(password):
 
175
    """Return if the password meets the minimum requirements."""
 
176
    if (len(password) < 8 or
 
177
        re.search('[A-Z]', password) is None or
 
178
        re.search('\d+', password) is None):
 
179
        return False
 
180
    return True
 
181
 
 
182
 
 
183
def is_correct_email(email_address):
 
184
    """Return if the email is correct."""
 
185
    return '@' in email_address