Projet

Général

Profil

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

root / auquotidien / modules / strongbox_ui.py @ 8b02623d

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 _
11
from qommon import errors, misc
12
from qommon.form import *
13
from qommon.strftime import strftime
14
from qommon.backoffice.menu import html_top
15
from qommon import get_cfg
16

    
17
from strongbox import StrongboxType, StrongboxItem
18

    
19

    
20

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

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

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

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

    
45
        return r.getvalue()
46

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

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

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

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

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

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

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

    
107

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

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

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

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

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

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

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

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

    
144

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

    
149
    types = StrongboxTypesDirectory()
150

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

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

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

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

    
167

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

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

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

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

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

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

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

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

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

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

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

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