Project

General

Profile

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

root / extra / modules / strongbox_ui.ptl @ 787465e8

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
        form = Form(enctype='multipart/form-data')
26
        form.add_submit('edit', _('Edit'))
27
        form.add_submit('delete', _('Delete'))
28
        form.add_submit('back', _('Back'))
29

    
30
        if form.get_submit() == 'edit':
31
            return redirect('edit')
32
        if form.get_submit() == 'delete':
33
            return redirect('delete')
34
        if form.get_submit() == 'back':
35
            return redirect('..')
36

    
37
        html_top('strongbox', title = _('Item Type: %s') % self.strongboxtype.label)
38
        '<h2>%s</h2>' % _('Item Type: %s') % self.strongboxtype.label
39

    
40
        get_session().display_message()
41

    
42
        form.render()
43

    
44
    def edit [html] (self):
45
        form = self.form()
46
        if form.get_submit() == 'cancel':
47
            return redirect('.')
48

    
49
        if form.is_submitted() and not form.has_errors():
50
            self.submit(form)
51
            return redirect('..')
52

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

    
57

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

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

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

    
93

    
94
class StrongboxTypesDirectory(Directory):
95
    _q_exports = ['', 'new']
96

    
97
    def _q_traverse(self, path):
98
        get_response().breadcrumb.append(('types/', _('Item Types')))
99
        return Directory._q_traverse(self, path)
100

    
101
    def _q_index [html] (self):
102
        return redirect('..')
103

    
104
    def new [html] (self):
105
        type_ui = StrongboxTypeDirectory(StrongboxType())
106

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

    
111
        if form.is_submitted() and not form.has_errors():
112
            type_ui.submit(form)
113
            return redirect('%s/' % type_ui.strongboxtype.id)
114

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

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

    
128

    
129
class StrongboxDirectory(AccessControlled, Directory):
130
    _q_exports = ['', 'types', 'add', 'add_to']
131
    label = N_('Strongbox')
132

    
133
    types = StrongboxTypesDirectory()
134

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

    
145
        get_response().breadcrumb.append(('strongbox/', _('Strongbox')))
146

    
147

    
148
    def _q_index [html] (self):
149
        html_top('strongbox', _('Strongbox'))
150

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

    
155
        get_session().display_message()
156

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

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

    
190
        '<ul class="biglist" id="strongbox-list">'
191
        for l in types:
192
            type_id = l.id
193
            '<li class="biglistitem" id="itemId_%s">' % type_id
194
            '<strong class="label"><a href="types/%s/">%s</a></strong>' % (type_id, l.label)
195
            '<p class="commands">'
196
            command_icon('types/%s/edit' % type_id, 'edit')
197
            command_icon('types/%s/delete' % type_id, 'remove')
198
            '</p></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

    
(25-25/26)