Projet

Général

Profil

Télécharger (10,4 ko) Statistiques
| Branche: | Tag: | Révision:

root / extra / modules / strongbox_ui.py @ c182b1ab

1
import time
2

    
3
from quixote import get_request, get_response, get_session, redirect
4
from quixote.directory import Directory, AccessControlled
5
from quixote.html import TemplateIO, htmltext
6

    
7
import wcs
8
import wcs.admin.root
9

    
10
from qommon import errors, misc
11
from qommon.form import *
12
from qommon.strftime import strftime
13
from qommon.backoffice.menu import html_top
14
from qommon import get_cfg
15

    
16
from strongbox import StrongboxType, StrongboxItem
17

    
18

    
19

    
20
class StrongboxTypeDirectory(Directory):
21
    _q_exports = ['', 'edit', 'delete']
22

    
23
    def __init__(self, strongboxtype):
24
        self.strongboxtype = strongboxtype
25

    
26
    def _q_index(self):
27
        html_top('strongbox', title = _('Item Type: %s') % self.strongboxtype.label)
28
        r = TemplateIO(html=True)
29
        r += htmltext('<h2>%s</h2>') % _('Item Type: %s') % self.strongboxtype.label
30
        get_response().filter['sidebar'] = self.get_sidebar()
31
        r += get_session().display_message()
32

    
33
        if self.strongboxtype.validation_months:
34
            r += htmltext('<div class="bo-block">')
35
            r += htmltext('<ul>')
36
            r += htmltext('<li>')
37
            r += _('Number of months of validity:')
38
            r += ' '
39
            r += self.strongboxtype.validation_months
40
            r += htmltext('</li>')
41
            r += htmltext('</ul>')
42
            r += htmltext('</div>')
43

    
44
        return r.getvalue()
45

    
46
    def get_sidebar(self):
47
        r = TemplateIO(html=True)
48
        r += htmltext('<ul>')
49
        r += htmltext('<li><a href="edit">%s</a></li>') % _('Edit')
50
        r += htmltext('<li><a href="delete">%s</a></li>') % _('Delete')
51
        r += htmltext('</ul>')
52
        return r.getvalue()
53

    
54
    def edit(self):
55
        form = self.form()
56
        if form.get_submit() == 'cancel':
57
            return redirect('.')
58

    
59
        if form.is_submitted() and not form.has_errors():
60
            self.submit(form)
61
            return redirect('..')
62

    
63
        html_top('strongbox', title = _('Edit Item Type: %s') % self.strongboxtype.label)
64
        r = TemplateIO(html=True)
65
        r += htmltext('<h2>%s</h2>') % _('Edit Item Type: %s') % self.strongboxtype.label
66
        r += form.render()
67
        return r.getvalue()
68

    
69
    def form(self):
70
        form = Form(enctype='multipart/form-data')
71
        form.add(StringWidget, 'label', title = _('Label'), required = True,
72
                value = self.strongboxtype.label)
73
        form.add(IntWidget, 'validation_months', title=_('Number of months of validity'),
74
                value=self.strongboxtype.validation_months,
75
                hint=_('Use 0 if there is no expiration'))
76
        form.add_submit('submit', _('Submit'))
77
        form.add_submit('cancel', _('Cancel'))
78
        return form
79

    
80
    def submit(self, form):
81
        for k in ('label', 'validation_months'):
82
            widget = form.get_widget(k)
83
            if widget:
84
                setattr(self.strongboxtype, k, widget.parse())
85
        self.strongboxtype.store()
86

    
87
    def delete(self):
88
        form = Form(enctype='multipart/form-data')
89
        form.widgets.append(HtmlWidget('<p>%s</p>' % _(
90
                        'You are about to irrevocably delete this item type.')))
91
        form.add_submit('submit', _('Submit'))
92
        form.add_submit('cancel', _('Cancel'))
93
        if form.get_submit() == 'cancel':
94
            return redirect('..')
95
        if not form.is_submitted() or form.has_errors():
96
            get_response().breadcrumb.append(('delete', _('Delete')))
97
            html_top('strongbox', title = _('Delete Item Type'))
98
            r = TemplateIO(html=True)
99
            r += htmltext('<h2>%s</h2>') % _('Deleting Item Type: %s') % self.strongboxtype.label
100
            r += form.render()
101
            return r.getvalue()
102
        else:
103
            self.strongboxtype.remove_self()
104
            return redirect('..')
105

    
106

    
107
class StrongboxTypesDirectory(Directory):
108
    _q_exports = ['', 'new']
109

    
110
    def _q_traverse(self, path):
111
        get_response().breadcrumb.append(('types/', _('Item Types')))
112
        return Directory._q_traverse(self, path)
113

    
114
    def _q_index(self):
115
        return redirect('..')
116

    
117
    def new(self):
118
        type_ui = StrongboxTypeDirectory(StrongboxType())
119

    
120
        form = type_ui.form()
121
        if form.get_submit() == 'cancel':
122
            return redirect('.')
123

    
124
        if form.is_submitted() and not form.has_errors():
125
            type_ui.submit(form)
126
            return redirect('%s/' % type_ui.strongboxtype.id)
127

    
128
        get_response().breadcrumb.append(('new', _('New Item Type')))
129
        html_top('strongbox', title = _('New Item Type'))
130
        r = TemplateIO(html=True)
131
        r += htmltext('<h2>%s</h2>') % _('New Item Type')
132
        r += form.render()
133
        return r.getvalue()
134

    
135
    def _q_lookup(self, component):
136
        try:
137
            strongboxtype = StrongboxType.get(component)
138
        except KeyError:
139
            raise errors.TraversalError()
140
        get_response().breadcrumb.append((str(strongboxtype.id), strongboxtype.label))
141
        return StrongboxTypeDirectory(strongboxtype)
142

    
143

    
144
class StrongboxDirectory(AccessControlled, Directory):
145
    _q_exports = ['', 'types', 'add', 'add_to']
146
    label = N_('Strongbox')
147

    
148
    types = StrongboxTypesDirectory()
149

    
150
    def is_accessible(self, user):
151
        from .backoffice import check_visibility
152
        return check_visibility('strongbox', user)
153

    
154
    def _q_access(self):
155
        user = get_request().user
156
        if not user:
157
            raise errors.AccessUnauthorizedError()
158

    
159
        if not self.is_accessible(user):
160
            raise errors.AccessForbiddenError(
161
                    public_msg = _('You are not allowed to access Strongbox Management'),
162
                    location_hint = 'backoffice')
163

    
164
        get_response().breadcrumb.append(('strongbox/', _('Strongbox')))
165

    
166

    
167
    def _q_index(self):
168
        html_top('strongbox', _('Strongbox'))
169
        r = TemplateIO(html=True)
170
        get_response().filter['sidebar'] = self.get_sidebar()
171

    
172
        r += get_session().display_message()
173

    
174
        r += htmltext('<div class="splitcontent-left">')
175
        r += htmltext('<div class="bo-block">')
176
        r += htmltext('<h2>%s</h2>') % _('Propose a file to a user')
177
        form = Form(enctype='multipart/form-data')
178
        form.add(StringWidget, 'q', title = _('User'), required=True)
179
        form.add_submit('search', _('Search'))
180
        r += form.render()
181
        if form.is_submitted() and not form.has_errors():
182
            q = form.get_widget('q').parse()
183
            users = self.search_for_users(q)
184
            if users:
185
                if len(users) == 1:
186
                    return redirect('add_to?user_id=%s' % users[0].id)
187
                if len(users) < 50:
188
                    r += _('(first 50 users only)')
189
                r += htmltext('<ul>')
190
                for u in users:
191
                    r += htmltext('<li><a href="add_to?user_id=%s">%s</a></li>') % (u.id, u.display_name)
192
                r += htmltext('</ul>')
193
            else:
194
                r += _('No user found.')
195
        r += htmltext('</div>')
196
        r += htmltext('</div>')
197

    
198
        r += htmltext('<div class="splitcontent-right">')
199
        r += htmltext('<div class="bo-block">')
200
        types = StrongboxType.select()
201
        r += htmltext('<h2>%s</h2>') % _('Item Types')
202
        if not types:
203
            r += htmltext('<p>')
204
            r += _('There is no item types defined at the moment.')
205
            r += htmltext('</p>')
206

    
207
        r += htmltext('<ul class="biglist" id="strongbox-list">')
208
        for l in types:
209
            type_id = l.id
210
            r += htmltext('<li class="biglistitem" id="itemId_%s">') % type_id
211
            r += htmltext('<strong class="label"><a href="types/%s/">%s</a></strong>') % (type_id, l.label)
212
            r += htmltext('</li>')
213
        r += htmltext('</ul>')
214
        r += htmltext('</div>')
215
        r += htmltext('</div>')
216
        return r.getvalue()
217

    
218
    def get_sidebar(self):
219
        r = TemplateIO(html=True)
220
        r += htmltext('<ul id="sidebar-actions">')
221
        r += htmltext('  <li><a class="new-item" href="types/new">%s</a></li>') % _('New Item Type')
222
        r += htmltext('</ul>')
223
        return r.getvalue()
224

    
225
    def search_for_users(self, q):
226
        if hasattr(get_publisher().user_class, 'search'):
227
            return get_publisher().user_class.search(q)
228
        if q:
229
            users = [x for x in get_publisher().user_class.select()
230
                     if q in (x.name or '') or q in (x.email or '')]
231
            return users
232
        else:
233
            return []
234

    
235
    def get_form(self):
236
        types = [(x.id, x.label) for x in StrongboxType.select()]
237
        form = Form(action='add', enctype='multipart/form-data')
238
        form.add(StringWidget, 'description', title=_('Description'), size=60)
239
        form.add(FileWidget, 'file', title=_('File'), required=True)
240
        form.add(SingleSelectWidget, 'type_id', title=_('Document Type'),
241
                 options = [(None, _('Not specified'))] + types)
242
        form.add(DateWidget, 'date_time', title = _('Document Date'))
243
        form.add_submit('submit', _('Upload'))
244
        return form
245

    
246
    def add(self):
247
        form = self.get_form()
248
        form.add(StringWidget, 'user_id', title=_('User'))
249
        if not form.is_submitted():
250
           return redirect('.')
251

    
252
        sffile = StrongboxItem()
253
        sffile.user_id = form.get_widget('user_id').parse()
254
        sffile.description = form.get_widget('description').parse()
255
        sffile.proposed_time = time.localtime()
256
        sffile.proposed_id = get_request().user.id
257
        sffile.type_id = form.get_widget('type_id').parse()
258
        v = form.get_widget('date_time').parse()
259
        sffile.set_expiration_time_from_date(v)
260
        sffile.store()
261
        sffile.set_file(form.get_widget('file').parse())
262
        sffile.store()
263
        return redirect('.')
264

    
265
    def add_to(self):
266
        form = Form(enctype='multipart/form-data', action='add_to')
267
        form.add(StringWidget, 'user_id', title = _('User'), required=True)
268
        try:
269
            user_id = form.get_widget('user_id').parse()
270
            user = get_publisher().user_class.get(user_id)
271
        except:
272
            return redirect('.')
273
        if not user:
274
            return redirect('.')
275
        get_request().form = {}
276
        get_request().environ['REQUEST_METHOD'] = 'GET'
277

    
278
        html_top('strongbox', _('Strongbox'))
279
        r = TemplateIO(html=True)
280
        r += htmltext('<h2>%s %s</h2>') % (_('Propose a file to:'), user.display_name)
281
        form = self.get_form()
282
        form.add(HiddenWidget, 'user_id', title=_('User'), value=user.id)
283
        r += form.render()
284
        return r.getvalue()
(29-29/30)