source: main/waeup.kofa/trunk/src/waeup/kofa/accesscodes/browser.py @ 10687

Last change on this file since 10687 was 9637, checked in by Henrik Bettermann, 12 years ago

Add viewlets for reports.py

Add permission to roles.

Set label attribbute of views.

  • Property svn:keywords set to Id
File size: 9.8 KB
Line 
1## $Id: browser.py 9637 2012-11-15 14:25:56Z henrik $
2##
3## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
4## This program is free software; you can redistribute it and/or modify
5## it under the terms of the GNU General Public License as published by
6## the Free Software Foundation; either version 2 of the License, or
7## (at your option) any later version.
8##
9## This program is distributed in the hope that it will be useful,
10## but WITHOUT ANY WARRANTY; without even the implied warranty of
11## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12## GNU General Public License for more details.
13##
14## You should have received a copy of the GNU General Public License
15## along with this program; if not, write to the Free Software
16## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17##
18"""UI components for accesscodes.
19"""
20import grok
21from datetime import datetime
22from zope.component import getUtility
23from hurry.workflow.interfaces import InvalidTransitionError
24from waeup.kofa.browser.resources import datatable
25from waeup.kofa.browser.layout import KofaPage, KofaAddFormPage, NullValidator
26from waeup.kofa.browser.breadcrumbs import Breadcrumb
27from waeup.kofa.browser.viewlets import (
28    AdminTask, AddActionButton, SearchActionButton, BatchOpButton, ManageLink)
29from waeup.kofa.interfaces import IKofaObject, IKofaUtils
30from waeup.kofa.interfaces import MessageFactory as _
31from waeup.kofa.accesscodes.interfaces import (
32    IAccessCodeBatchContainer, IAccessCodeBatch,
33    )
34from waeup.kofa.accesscodes.catalog import search
35from waeup.kofa.browser.layout import action
36
37grok.context(IKofaObject)
38
39class BatchContainerPage(KofaPage):
40    grok.name('index')
41    grok.context(IAccessCodeBatchContainer)
42    grok.template('batchcontainer')
43    grok.require('waeup.manageACBatches')
44    archive_button = _('Archive')
45    delete_button = _('Archive and delete')
46
47    label = _('Access Code Batches')
48    pnav = 0
49
50    def update(self, batches=None, archive=None, delete=None):
51        if archive is None and delete is None:
52            return
53        if not batches:
54            self.flash(_('No batch selected.'))
55            return
56        if isinstance(batches, basestring):
57            batches = [batches]
58        ob_class = self.__implemented__.__name__.replace('waeup.kofa.','')
59        for name in batches:
60            batch = self.context[name]
61            csv_file = batch.archive()
62            self.flash(_('Archived ${a} (${b})',
63                mapping = {'a':name, 'b':csv_file}))
64            message = 'archived: %s (%s)' % (name,csv_file)
65            self.context.logger_info(ob_class, message)
66            if delete is None:
67                continue
68            del self.context[name]
69            self.context._p_changed = True
70            self.flash(_('Deleted batch ${a}', mapping = {'a':name}))
71            message = 'deleted: %s' % name
72            self.context.logger_info(ob_class, message)
73
74class AddBatchPage(KofaAddFormPage):
75    grok.name('add')
76    grok.context(IAccessCodeBatchContainer)
77    grok.require('waeup.manageACBatches')
78
79    label = _('Create Access Code Batch')
80    pnav = 0
81
82    form_fields = grok.AutoFields(IAccessCodeBatch).select(
83        'prefix', 'entry_num', 'cost')
84
85    @action(_('Create batch'), style='primary')
86    def createBatch(self, **data):
87        creator = self.request.principal.id
88        creation_date = datetime.utcnow()
89        data.update(creation_date=creation_date, creator=creator)
90        batch = self.context.createBatch(**data)
91        csv_file = batch.createCSVLogFile()
92        self.context._p_changed = True
93        self.flash(_('Batch created (${a} entries)',
94            mapping = {'a':data['entry_num']}))
95        self.flash(_('Data written to ${a}', mapping = {'a':csv_file}))
96        ob_class = self.__implemented__.__name__.replace('waeup.kofa.','')
97        message = 'created: %s (%d, %f)' % (
98            csv_file, data['entry_num'], data['cost'])
99        self.context.logger_info(ob_class, message)
100        self.redirect(self.url(self.context))
101
102    @action(_('Cancel'), validator=NullValidator)
103    def cancel(self, *args, **kw):
104        self.flash(_('Batch creation cancelled.'))
105        self.redirect(self.url(self.context))
106
107class ReimportBatchPage(KofaPage):
108    """Screen for reimporting AC batches.
109    """
110    grok.name('reimport')
111    grok.context(IAccessCodeBatchContainer)
112    grok.template('reimportbatchpage')
113    grok.require('waeup.manageACBatches')
114    reimport_button = _('Reimport')
115    cancel_button = _('Cancel')
116
117    label = _('Reimport Access Code Batches')
118    pnav = 0
119
120    def update(self, filenames=None, reimport=None, cancel=None):
121        if cancel is not None:
122            self.flash(_('Reimport cancelled.'))
123            self.redirect(self.url(self.context))
124            return
125        if reimport is None:
126            return
127        if not filenames:
128            self.flash(_('No file chosen. Action cancelled.'))
129            self.redirect(self.url(self.context))
130            return
131        if isinstance(filenames, basestring):
132            filenames = [filenames]
133        userid = self.request.principal.id
134        for filename in filenames:
135            try:
136                self.context.reimport(filename, userid)
137            except KeyError:
138                self.flash(_('This batch already exists: ${a}',
139                    mapping = {'a':filename}))
140                continue
141            self.flash(_('Successfully reimported: ${a}',
142                mapping = {'a':filename}))
143            ob_class = self.__implemented__.__name__.replace('waeup.kofa.','')
144            message = 'reimported: %s' % filename
145            self.context.logger_info(ob_class, message)
146        self.redirect(self.url(self.context))
147
148class BatchContainerSearchPage(KofaPage):
149    grok.name('search')
150    grok.context(IAccessCodeBatchContainer)
151    grok.template('searchpage')
152    grok.require('waeup.manageACBatches')
153    pnav = 0
154    label = _('Search and Manage Access Codes')
155    search_button = _('Search')
156    disable_button = _('Disable ACs')
157    enable_button = _('Enable ACs')
158
159    def update(self, *args, **kw):
160        datatable.need()
161        form = self.request.form
162        self.hitlist = []
163        if 'searchterm' in form and form['searchterm']:
164            self.searchterm = form['searchterm']
165            self.searchtype = form['searchtype']
166        elif 'old_searchterm' in form:
167            self.searchterm = form['old_searchterm']
168            self.searchtype = form['old_searchtype']
169        else:
170            return
171        if not 'entries' in form:
172            self.hitlist = search(query=self.searchterm,
173                searchtype=self.searchtype, view=self)
174            return
175        entries = form['entries']
176        if isinstance(entries, basestring):
177            entries = [entries]
178        ob_class = self.__implemented__.__name__.replace('waeup.kofa.','')
179        for entry in entries:
180            if 'disable' in form:
181                try:
182                    comment = _(u"disabled")
183                    self.context.disable(entry, comment)
184                    self.flash(_('${a} disabled.', mapping = {'a':entry}))
185                    message = 'disabled: %s' % entry
186                    self.context.logger_info(ob_class, message)
187                except InvalidTransitionError:
188                    self.flash(_('${a}: Disable transition not allowed.',
189                        mapping = {'a':entry}))
190            elif 'enable' in form:
191                try:
192                    comment = _(u"re-enabled")
193                    self.context.enable(entry, comment)
194                    self.flash(_('${a} (re-)enabled.', mapping = {'a':entry}))
195                    message = '(re-)enabled: %s' % entry
196                    self.context.logger_info(ob_class, message)
197                except InvalidTransitionError:
198                    self.flash(_('${a}: Re-enable transition not allowed.', mapping = {'a':entry}))
199        self.hitlist = search(query=self.searchterm,
200            searchtype=self.searchtype, view=self)
201        return
202
203class BatchContainerBreadcrumb(Breadcrumb):
204    """A breadcrumb for ac batch containers.
205    """
206    grok.require('waeup.manageACBatches')
207    grok.context(IAccessCodeBatchContainer)
208    title = _(u'Access Code Batches')
209    parent_viewname = 'administration'
210
211#class BatchContainerSearchBreadcrumb(Breadcrumb):
212#    """A breadcrumb for ac batch containers search page.
213#    """
214#    grok.require('waeup.manageACBatches')
215#    grok.context(IAccessCodeBatchContainer)
216#    grok.name('search')
217#    title = u'Search Access Codes'
218#    viewname = 'search'
219#    parent_viewname = 'index'
220
221class AdminTaskManageACBatches(AdminTask):
222    """Entry on administration page that links to batch container.
223    """
224    grok.order(5)
225    grok.require('waeup.manageACBatches')
226    grok.template('admintaskacbatches')
227
228    link_title = _('Access Code Batches')
229    target_viewname = 'accesscodes'
230
231class CreateBatchButton(AddActionButton):
232    """Action button on batch container page which links to batch creation.
233    """
234    grok.context(IAccessCodeBatchContainer)
235    grok.view(BatchContainerPage)
236    grok.require('waeup.manageACBatches')
237    text = _('Add Access Code Batch')
238
239class ReimportBatchButton(BatchOpButton):
240    """Action button on batch container page which links to batch reimport.
241    """
242    grok.context(IAccessCodeBatchContainer)
243    grok.view(BatchContainerPage)
244    grok.require('waeup.manageACBatches')
245    target = 'reimport'
246    text = _('Reimport Access Code Batch')
247
248class SearchAccessCodeButton(SearchActionButton):
249    """Action button on batch container page which links to search.
250    """
251    grok.context(IAccessCodeBatchContainer)
252    grok.view(BatchContainerPage)
253    grok.require('waeup.manageACBatches')
254    text = _('Search Access Codes')
255
256class ManageAccessCodes(ManageLink):
257    """Link in upper left box to access code management.
258    """
259    grok.order(5)
260    grok.require('waeup.manageACBatches')
261
262    link = u'accesscodes'
263    text = _(u'Access Codes')
Note: See TracBrowser for help on using the repository browser.