Project

General

Profile

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

root / extra / modules / myspace.ptl @ abd8c1fb

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
from announces import AnnounceSubscription
23

    
24
class MyspaceDirectory(Directory):
25
    _q_exports = ['', 'profile', 'new', 'password', 'remove', 'announces']
26

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

    
34

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

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

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

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

    
60
            TextsDirectory.get_html_text('aq-top-of-profile')
61

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

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

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

    
81
        try:
82
            x = PasswordAccount.get_on_index(get_request().user.id, str('user_id'))
83
            '<p class="command"><a href="remove" rel="popup">%s</a></p>' % _('Remove My Account')
84
        except KeyError:
85
            pass
86

    
87
        options = get_cfg('misc', {}).get('announce_themes')
88
        if options:
89
            try:
90
                subscription = AnnounceSubscription.get_on_index(
91
                        get_request().user.id, str('user_id'))
92
            except KeyError:
93
                pass
94
            else:
95
                '<p class="command"><a href="announces">%s</a></p>' % _(
96
                        'Edit my Subscription to Announces')
97

    
98
        if user:
99
            user_forms = []
100
            formdefs = FormDef.select(lambda x: not x.disabled, order_by = 'name')
101
            user_forms = []
102
            for formdef in formdefs:
103
                user_forms.extend(formdef.data_class().get_with_indexed_value(
104
                            'user_id', user.id))
105
                #user_forms.extend(formdef.data_class().select(
106
                #            lambda x: x.user_id == user.id))
107
            user_forms.sort(lambda x,y: cmp(x.receipt_time, y.receipt_time))
108

    
109
            root.FormsRootDirectory().user_forms(user_forms)
110

    
111

    
112
    def profile [html] (self):
113
        user = get_request().user
114
        if not user or user.anonymous:
115
            raise errors.AccessUnauthorizedError()
116

    
117
        form = Form(enctype = 'multipart/form-data')
118
        formdef = user.get_formdef()
119
        formdef.add_fields_to_form(form, form_data = user.form_data)
120

    
121
        form.add_submit('submit', _('Apply Changes'))
122
        form.add_submit('cancel', _('Cancel'))
123

    
124
        if form.get_submit() == 'cancel':
125
            return redirect('.')
126

    
127
        if form.is_submitted() and not form.has_errors():
128
            self.profile_submit(form, formdef)
129
            return redirect('.')
130

    
131
        template.html_top(_('Edit Profile'))
132
        form.render()
133

    
134
    def profile_submit(self, form, formdef):
135
        user = get_request().user
136
        data = formdef.get_data(form)
137

    
138
        user.set_attributes_from_formdata(data)
139
        user.form_data = data
140

    
141
        user.store()
142

    
143
    def password [html] (self):
144
        ident_method = get_cfg('identification', {}).get('methods', ['idp'])[0]
145
        if ident_method != 'password':
146
            raise errors.TraversalError()
147

    
148
        user = get_request().user
149
        if not user or user.anonymous:
150
            raise errors.AccessUnauthorizedError()
151

    
152
        form = Form(enctype = 'multipart/form-data')
153
        form.add(PasswordWidget, 'new_password', title = _('New Password'),
154
                required=True)
155
        form.add(PasswordWidget, 'new2_password', title = _('New Password (confirm)'),
156
                required=True) 
157

    
158
        form.add_submit('submit', _('Change Password'))
159
        form.add_submit('cancel', _('Cancel'))
160

    
161
        if form.get_submit() == 'cancel':
162
            return redirect('.')
163

    
164
        if form.is_submitted() and not form.has_errors():
165
            qommon.ident.password.check_password(form, 'new_password')
166
            new_password = form.get_widget('new_password').parse()
167
            new2_password = form.get_widget('new2_password').parse()
168
            if new_password != new2_password:
169
                form.set_error('new2_password', _('Passwords do not match'))
170

    
171
        if form.is_submitted() and not form.has_errors():
172
            passwords_cfg = get_cfg('passwords', {})
173
            account = PasswordAccount.get(get_session().username)
174
            account.hashing_algo = passwords_cfg.get('hashing_algo')
175
            account.set_password(new_password)
176
            account.store()
177
            return redirect('.')
178

    
179
        template.html_top(_('Change Password'))
180
        form.render()
181

    
182

    
183
    def new [html] (self):
184
        if not get_request().user or not get_request().user.anonymous:
185
            raise errors.AccessUnauthorizedError()
186

    
187
        form = Form(enctype = 'multipart/form-data')
188
        formdef = get_publisher().user_class.get_formdef()
189
        if formdef:
190
            formdef.add_fields_to_form(form)
191
        else:
192
            get_logger().error('missing user formdef (in myspace/new)')
193

    
194
        form.add_submit('submit', _('Register'))
195

    
196
        if form.is_submitted() and not form.has_errors():
197
            user = get_publisher().user_class()
198
            data = formdef.get_data(form)
199
            user.set_attributes_from_formdata(data)
200
            user.name_identifiers = get_request().user.name_identifiers
201
            user.lasso_dump = get_request().user.lasso_dump
202
            user.set_attributes_from_formdata(data)
203
            user.form_data = data
204
            user.store()
205
            get_session().set_user(user.id)
206
            root_url = get_publisher().get_root_url()
207
            return redirect('%smyspace' % root_url)
208

    
209
        template.html_top(_('Welcome'))
210
        form.render()
211

    
212

    
213
    def remove [html] (self):
214
        user = get_request().user
215
        if not user or user.anonymous:
216
            raise errors.AccessUnauthorizedError()
217

    
218
        form = Form(enctype = 'multipart/form-data')
219
        form.widgets.append(HtmlWidget('<p>%s</p>' % _(
220
                        'Are you really sure you want to remove your account?')))
221
        form.add_submit('submit', _('Remove my account'))
222
        form.add_submit('cancel', _('Cancel'))
223

    
224
        if form.get_submit() == 'cancel':
225
            return redirect('.')
226

    
227
        if form.is_submitted() and not form.has_errors():
228
            user = get_request().user
229
            account = PasswordAccount.get_on_index(user.id, str('user_id'))
230
            get_session_manager().expire_session() 
231
            account.remove_self()
232
            return redirect(get_publisher().get_root_url())
233

    
234
        template.html_top(_('Removing Account'))
235
        form.render()
236

    
237
    def announces [html] (self):
238
        options = get_cfg('misc', {}).get('announce_themes')
239
        if not options:
240
            raise errors.TraversalError()
241
        subscription = AnnounceSubscription.get_on_index(get_request().user.id, str('user_id'))
242
        if not subscription:
243
            raise errors.TraversalError()
244

    
245
        if subscription.enabled_themes is None:
246
            enabled_themes = options
247
        else:
248
            enabled_themes = subscription.enabled_themes
249

    
250
        form = Form(enctype = 'multipart/form-data')
251
        form.add(CheckboxesWidget, 'themes', title=_('Announce Themes'),
252
                value=enabled_themes, elements=options,
253
                inline=False, required=False)
254

    
255
        form.add_submit('submit', _('Apply Changes'))
256
        form.add_submit('cancel', _('Cancel'))
257

    
258
        if form.get_submit() == 'cancel':
259
            return redirect('.')
260

    
261
        if form.is_submitted() and not form.has_errors():
262
            chosen_themes = form.get_widget('themes').parse()
263
            if chosen_themes == options:
264
                chosen_themes = None
265
            subscription.enabled_themes = chosen_themes
266
            subscription.store()
267
            return redirect('.')
268

    
269
        template.html_top()
270
        get_response().breadcrumb.append(('announces', _('Announce Subscription')))
271
        form.render()
272

    
273

    
274
TextsDirectory.register('aq-top-of-profile',
275
        N_('Text on top of the profile page'))
276

    
(13-13/16)