Projet

Général

Profil

Télécharger (9,12 ko) Statistiques
| Branche: | Tag: | Révision:

root / extra / modules / strongbox_ui.ptl @ e8e6dedb

1
import time
2

    
3
from quixote import get_request, get_response, get_session, redirect
4
from quixote.directory import Directory, AccessControlled
5

    
6
import wcs
7
import wcs.admin.root
8
from wcs.backoffice.menu import *
9

    
10
from qommon import errors, misc
11
from qommon.form import *
12
from qommon.strftime import strftime
13

    
14
from strongbox import StrongboxType, StrongboxItem
15

    
16

    
17

    
18
class StrongboxTypeDirectory(Directory):
19
    _q_exports = ['', 'edit', 'delete']
20

    
21
    def __init__(self, strongboxtype):
22
        self.strongboxtype = strongboxtype
23

    
24
    def _q_index [html] (self):
25
        html_top('strongbox', title = _('Item Type: %s') % self.strongboxtype.label)
26
        '<h2>%s</h2>' % _('Item Type: %s') % self.strongboxtype.label
27
        get_response().filter['sidebar'] = self.get_sidebar()
28
        get_session().display_message()
29

    
30
        if self.strongboxtype.validation_months:
31
            '<div class="bo-block">'
32
            '<ul>'
33
            '<li>'
34
            _('Number of months of validity:')
35
            ' '
36
            self.strongboxtype.validation_months
37
            '</li>'
38
            '</ul>'
39
            '</div>'
40

    
41
    def get_sidebar [html] (self):
42
        '<ul>'
43
        '<li><a href="edit">%s</a></li>' % _('Edit')
44
        '<li><a href="delete">%s</a></li>' % _('Delete')
45
        '</ul>'
46

    
47
    def edit [html] (self):
48
        form = self.form()
49
        if form.get_submit() == 'cancel':
50
            return redirect('.')
51

    
52
        if form.is_submitted() and not form.has_errors():
53
            self.submit(form)
54
            return redirect('..')
55

    
56
        html_top('strongbox', title = _('Edit Item Type: %s') % self.strongboxtype.label)
57
        '<h2>%s</h2>' % _('Edit Item Type: %s') % self.strongboxtype.label
58
        form.render()
59

    
60

    
61
    def form(self):
62
        form = Form(enctype='multipart/form-data')
63
        form.add(StringWidget, 'label', title = _('Label'), required = True,
64
                value = self.strongboxtype.label)
65
        form.add(IntWidget, 'validation_months', title=_('Number of months of validity'),
66
                value=self.strongboxtype.validation_months,
67
                hint=_('Use 0 if there is no expiration'))
68
        form.add_submit('submit', _('Submit'))
69
        form.add_submit('cancel', _('Cancel'))
70
        return form
71

    
72
    def submit(self, form):
73
        for k in ('label', 'validation_months'):
74
            widget = form.get_widget(k)
75
            if widget:
76
                setattr(self.strongboxtype, k, widget.parse())
77
        self.strongboxtype.store()
78

    
79
    def delete [html] (self):
80
        form = Form(enctype='multipart/form-data')
81
        form.widgets.append(HtmlWidget('<p>%s</p>' % _(
82
                        'You are about to irrevocably delete this item type.')))
83
        form.add_submit('submit', _('Submit'))
84
        form.add_submit('cancel', _('Cancel'))
85
        if form.get_submit() == 'cancel':
86
            return redirect('..')
87
        if not form.is_submitted() or form.has_errors():
88
            get_response().breadcrumb.append(('delete', _('Delete')))
89
            html_top('strongbox', title = _('Delete Item Type'))
90
            '<h2>%s</h2>' % _('Deleting Item Type: %s') % self.strongboxtype.label
91
            form.render()
92
        else:
93
            self.strongboxtype.remove_self()
94
            return redirect('..')
95

    
96

    
97
class StrongboxTypesDirectory(Directory):
98
    _q_exports = ['', 'new']
99

    
100
    def _q_traverse(self, path):
101
        get_response().breadcrumb.append(('types/', _('Item Types')))
102
        return Directory._q_traverse(self, path)
103

    
104
    def _q_index [html] (self):
105
        return redirect('..')
106

    
107
    def new [html] (self):
108
        type_ui = StrongboxTypeDirectory(StrongboxType())
109

    
110
        form = type_ui.form()
111
        if form.get_submit() == 'cancel':
112
            return redirect('.')
113

    
114
        if form.is_submitted() and not form.has_errors():
115
            type_ui.submit(form)
116
            return redirect('%s/' % type_ui.strongboxtype.id)
117

    
118
        get_response().breadcrumb.append(('new', _('New Item Type')))
119
        html_top('strongbox', title = _('New Item Type'))
120
        '<h2>%s</h2>' % _('New Item Type')
121
        form.render()
122

    
123
    def _q_lookup(self, component):
124
        try:
125
            strongboxtype = StrongboxType.get(component)
126
        except KeyError:
127
            raise errors.TraversalError()
128
        get_response().breadcrumb.append((str(strongboxtype.id), strongboxtype.label))
129
        return StrongboxTypeDirectory(strongboxtype)
130

    
131

    
132
class StrongboxDirectory(AccessControlled, Directory):
133
    _q_exports = ['', 'types', 'add', 'add_to']
134
    label = N_('Strongbox')
135

    
136
    types = StrongboxTypesDirectory()
137

    
138
    def _q_access(self):
139
        user = get_request().user
140
        if not user:
141
            raise errors.AccessUnauthorizedError()
142
        admin_role = get_cfg('aq-permissions', {}).get('strongbox', None)
143
        if not (user.is_admin or admin_role in (user.roles or [])):
144
            raise errors.AccessForbiddenError(
145
                    public_msg = _('You are not allowed to access Strongbox Management'),
146
                    location_hint = 'backoffice')
147

    
148
        get_response().breadcrumb.append(('strongbox/', _('Strongbox')))
149

    
150

    
151
    def _q_index [html] (self):
152
        html_top('strongbox', _('Strongbox'))
153

    
154
        '<ul id="main-actions">'
155
        '  <li><a class="new-item" href="types/new">%s</a></li>' % _('New Item Type')
156
        '</ul>'
157

    
158
        get_session().display_message()
159

    
160
        '<div class="splitcontent-left">'
161
        '<div class="bo-block">'
162
        '<h2>%s</h2>' % _('Propose a file to a user')
163
        form = Form(enctype='multipart/form-data')
164
        form.add(StringWidget, 'q', title = _('User'), required=True)
165
        form.add_submit('search', _('Search'))
166
        form.render()
167
        if form.is_submitted() and not form.has_errors():
168
            q = form.get_widget('q').parse()
169
            users = self.search_for_users(q)
170
            if users:
171
                if len(users) == 1:
172
                    return redirect('add_to?user_id=%s' % users[0].id)
173
                if len(users) < 50:
174
                    _('(first 50 users only)')
175
                '<ul>'
176
                for u in users:
177
                    '<li><a href="add_to?user_id=%s">%s</a></li>' % (u.id, u.display_name)
178
                '</ul>'
179
            else:
180
                _('No user found.')
181
        '</div>'
182
        '</div>'
183

    
184
        '<div class="splitcontent-right">'
185
        '<div class="bo-block">'
186
        types = StrongboxType.select()
187
        '<h2>%s</h2>' % _('Item Types')
188
        if not types:
189
            '<p>'
190
            _('There is no item types defined at the moment.')
191
            '</p>'
192

    
193
        '<ul class="biglist" id="strongbox-list">'
194
        for l in types:
195
            type_id = l.id
196
            '<li class="biglistitem" id="itemId_%s">' % type_id
197
            '<strong class="label"><a href="types/%s/">%s</a></strong>' % (type_id, l.label)
198
            '</li>'
199
        '</ul>'
200
        '</div>'
201
        '</div>'
202

    
203
    def search_for_users(self, q):
204
        if hasattr(get_publisher().user_class, 'search'):
205
            return get_publisher().user_class.search(q)
206
        if q:
207
            users = [x for x in get_publisher().user_class.select()
208
                     if q in (x.name or '') or q in (x.email or '')]
209
            return users
210
        else:
211
            return []
212

    
213
    def get_form(self):
214
        types = [(x.id, x.label) for x in StrongboxType.select()]
215
        form = Form(action='add', enctype='multipart/form-data')
216
        form.add(StringWidget, 'description', title=_('Description'), size=60)
217
        form.add(FileWidget, 'file', title=_('File'), required=True)
218
        form.add(SingleSelectWidget, 'type_id', title=_('Document Type'),
219
                 options = [(None, _('Not specified'))] + types)
220
        form.add(DateWidget, 'date_time', title = _('Document Date'))
221
        form.add_submit('submit', _('Upload'))
222
        return form
223

    
224
    def add(self):
225
        form = self.get_form()
226
        form.add(StringWidget, 'user_id', title=_('User'))
227
        if not form.is_submitted():
228
           return redirect('.')
229

    
230
        sffile = StrongboxItem()
231
        sffile.user_id = form.get_widget('user_id').parse()
232
        sffile.description = form.get_widget('description').parse()
233
        sffile.proposed_time = time.localtime()
234
        sffile.proposed_id = get_request().user.id
235
        sffile.type_id = form.get_widget('type_id').parse()
236
        v = form.get_widget('date_time').parse()
237
        sffile.set_expiration_time_from_date(v)
238
        sffile.store()
239
        sffile.set_file(form.get_widget('file').parse())
240
        sffile.store()
241
        return redirect('.')
242

    
243
    def add_to [html] (self):
244
        form = Form(enctype='multipart/form-data', action='add_to')
245
        form.add(StringWidget, 'user_id', title = _('User'), required=True)
246
        try:
247
            user_id = form.get_widget('user_id').parse()
248
            user = get_publisher().user_class.get(user_id)
249
        except:
250
            return redirect('.')
251
        if not user:
252
            return redirect('.')
253
        get_request().form = {}
254
        get_request().environ['REQUEST_METHOD'] = 'GET'
255

    
256
        html_top('strongbox', _('Strongbox'))
257
        '<h2>%s %s</h2>' % (_('Propose a file to:'), user.display_name)
258
        form = self.get_form()
259
        form.add(HiddenWidget, 'user_id', title=_('User'), value=user.id)
260
        form.render()
261

    
(31-31/32)