Project

General

Profile

Download (7.78 KB) Statistics
| Branch: | Tag: | Revision:

root / extra / modules / myspace.ptl @ 9e6caf99

1
try:
2
    import lasso
3
except ImportError:
4
    pass
5

    
6
from quixote import get_publisher, get_request, redirect, get_response, get_session_manager
7
from quixote.directory import AccessControlled, Directory
8

    
9
from qommon import template
10
from qommon.form import *
11
from qommon import get_cfg, get_logger
12
from qommon import errors
13

    
14
import qommon.ident.password
15
from qommon.ident.password_accounts import PasswordAccount
16

    
17
from qommon.admin.texts import TextsDirectory
18

    
19
from wcs.formdef import FormDef
20
import root
21

    
22
class MyspaceDirectory(Directory):
23
    _q_exports = ['', 'profile', 'new', 'password', 'remove']
24

    
25
    def _q_traverse(self, path):
26
        if path != ['new'] and (not get_request().user or get_request().user.anonymous):
27
            raise errors.AccessUnauthorizedError()
28
        get_response().filter['bigdiv'] = 'profile'
29
        get_response().breadcrumb.append(('myspace/', _('My Space')))
30
        return Directory._q_traverse(self, path)
31

    
32

    
33
    def _q_index [html] (self):
34
        user = get_request().user
35
        if not user:
36
            raise errors.AccessUnauthorizedError()
37
        template.html_top(_('My Space'))
38
        if user.anonymous:
39
            return redirect('new')
40

    
41
        if user.is_admin or user.roles:
42
            root_url = get_publisher().get_root_url()
43
            '<p id="profile-links">'
44
            '<a href="%sbackoffice/">%s</a>' % (root_url, _('back office'))
45
            if user.is_admin:
46
                ' - <a href="%sadmin/">%s</a>' % (root_url, _('admin'))
47
            '</p>'
48

    
49
        ident_method = get_cfg('identification', {}).get('methods', ['idp'])[0]
50
        passwords_cfg = get_cfg('passwords', {})
51
        if get_session().lasso_session_dump:
52
            ident_method = 'idp'
53

    
54
        formdef = user.get_formdef()
55
        if formdef:
56
            '<h3>%s</h3>' % _('My Profile')
57

    
58
            TextsDirectory.get_html_text('aq-top-of-profile')
59

    
60
            if user.form_data:
61
                '<ul>'
62
                for field in formdef.fields:
63
                    value = user.form_data.get(field.id)
64
                    '<li>'
65
                    field.label
66
                    ' : '
67
                    value
68
                    '</li>'
69
                '</ul>'
70
            else:
71
                '<p>%s</p>' % _('Empty profile')
72

    
73
            if ident_method != 'idp':
74
                '<p class="command"><a href="profile" rel="popup">%s</a></p>' % _('Edit My Profile')
75

    
76
        if ident_method == 'password' and passwords_cfg.get('can_change', False):
77
            '<p class="command"><a href="password" rel="popup">%s</a></p>' % _('Change My Password')
78

    
79
        '<p class="command"><a href="remove" rel="popup">%s</a></p>' % _('Remove My Account')
80

    
81
        if user:
82
            user_forms = []
83
            formdefs = FormDef.select(lambda x: not x.disabled, order_by = 'name')
84
            user_forms = []
85
            for formdef in formdefs:
86
                user_forms.extend(formdef.data_class().get_with_indexed_value(
87
                            'user_id', user.id))
88
                #user_forms.extend(formdef.data_class().select(
89
                #            lambda x: x.user_id == user.id))
90
            user_forms.sort(lambda x,y: cmp(x.receipt_time, y.receipt_time))
91

    
92
            root.FormsRootDirectory().user_forms(user_forms)
93

    
94

    
95
    def profile [html] (self):
96
        user = get_request().user
97
        if not user or user.anonymous:
98
            raise errors.AccessUnauthorizedError()
99

    
100
        form = Form(enctype = 'multipart/form-data')
101
        formdef = user.get_formdef()
102
        formdef.add_fields_to_form(form, form_data = user.form_data)
103

    
104
        form.add_submit('submit', _('Apply Changes'))
105
        form.add_submit('cancel', _('Cancel'))
106

    
107
        if form.get_submit() == 'cancel':
108
            return redirect('.')
109

    
110
        if form.is_submitted() and not form.has_errors():
111
            self.profile_submit(form, formdef)
112
            return redirect('.')
113

    
114
        template.html_top(_('Edit Profile'))
115
        form.render()
116

    
117
    def profile_submit(self, form, formdef):
118
        user = get_request().user
119
        data = formdef.get_data(form)
120

    
121
        user.set_attributes_from_formdata(data)
122
        user.form_data = data
123

    
124
        user.store()
125

    
126
    def password [html] (self):
127
        ident_method = get_cfg('identification', {}).get('methods', ['idp'])[0]
128
        if ident_method != 'password':
129
            raise errors.TraversalError()
130

    
131
        user = get_request().user
132
        if not user or user.anonymous:
133
            raise errors.AccessUnauthorizedError()
134

    
135
        form = Form(enctype = 'multipart/form-data')
136
        form.add(PasswordWidget, 'new_password', title = _('New Password'),
137
                required=True)
138
        form.add(PasswordWidget, 'new2_password', title = _('New Password (confirm)'),
139
                required=True) 
140

    
141
        form.add_submit('submit', _('Change Password'))
142
        form.add_submit('cancel', _('Cancel'))
143

    
144
        if form.get_submit() == 'cancel':
145
            return redirect('.')
146

    
147
        if form.is_submitted() and not form.has_errors():
148
            qommon.ident.password.check_password(form, 'new_password')
149
            new_password = form.get_widget('new_password').parse()
150
            new2_password = form.get_widget('new2_password').parse()
151
            if new_password != new2_password:
152
                form.set_error('new2_password', _('Passwords do not match'))
153

    
154
        if form.is_submitted() and not form.has_errors():
155
            passwords_cfg = get_cfg('passwords', {})
156
            account = PasswordAccount.get(get_session().username)
157
            account.hashing_algo = passwords_cfg.get('hashing_algo')
158
            account.set_password(new_password)
159
            account.store()
160
            return redirect('.')
161

    
162
        template.html_top(_('Change Password'))
163
        form.render()
164

    
165

    
166
    def new [html] (self):
167
        if not get_request().user or not get_request().user.anonymous:
168
            raise errors.AccessUnauthorizedError()
169

    
170
        form = Form(enctype = 'multipart/form-data')
171
        formdef = get_publisher().user_class.get_formdef()
172
        if formdef:
173
            formdef.add_fields_to_form(form)
174
        else:
175
            get_logger().error('missing user formdef (in myspace/new)')
176

    
177
        form.add_submit('submit', _('Register'))
178

    
179
        if form.is_submitted() and not form.has_errors():
180
            user = get_publisher().user_class()
181
            data = formdef.get_data(form)
182
            user.set_attributes_from_formdata(data)
183
            user.name_identifiers = get_request().user.name_identifiers
184
            user.lasso_dump = get_request().user.lasso_dump
185
            user.set_attributes_from_formdata(data)
186
            user.form_data = data
187
            user.store()
188
            get_session().set_user(user.id)
189
            root_url = get_publisher().get_root_url()
190
            return redirect('%smyspace' % root_url)
191

    
192
        template.html_top(_('Welcome'))
193
        form.render()
194

    
195

    
196
    def remove [html] (self):
197
        user = get_request().user
198
        if not user or user.anonymous:
199
            raise errors.AccessUnauthorizedError()
200

    
201
        form = Form(enctype = 'multipart/form-data')
202
        form.widgets.append(HtmlWidget('<p>%s</p>' % _(
203
                        'Are you really sure you want to remove your account?')))
204
        form.add_submit('submit', _('Remove my account'))
205
        form.add_submit('cancel', _('Cancel'))
206

    
207
        if form.get_submit() == 'cancel':
208
            return redirect('.')
209

    
210
        if form.is_submitted() and not form.has_errors():
211
            user = get_request().user
212
            account = PasswordAccount.get_on_index(user.id, str('user_id'))
213
            get_session_manager().expire_session() 
214
            account.remove_self()
215
            user.remove_self()
216
            return redirect(get_publisher().get_root_url())
217

    
218
        template.html_top(_('Removing Account'))
219
        form.render()
220

    
221
TextsDirectory.register('aq-top-of-profile',
222
        N_('Text on top of the profile page'))
223

    
(12-12/15)