source: main/waeup.sirp/trunk/src/waeup/sirp/accesscodes/browser.py @ 7734

Last change on this file since 7734 was 7720, checked in by Henrik Bettermann, 13 years ago

Add missing translations.

  • Property svn:keywords set to Id
File size: 8.7 KB
Line 
1## $Id: browser.py 7720 2012-02-28 21:03:16Z 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 hurry.workflow.interfaces import InvalidTransitionError
23from waeup.sirp.browser.resources import datatable
24from waeup.sirp.browser import SIRPPage, SIRPAddFormPage, NullValidator
25from waeup.sirp.browser.breadcrumbs import Breadcrumb
26from waeup.sirp.browser.viewlets import (
27    AdminTask, AddActionButton, SearchActionButton, BatchOpButton, ManageLink)
28from waeup.sirp.interfaces import ISIRPObject
29from waeup.sirp.interfaces import MessageFactory as _
30from waeup.sirp.accesscodes.interfaces import (
31    IAccessCodeBatchContainer, IAccessCodeBatch,
32    )
33from waeup.sirp.accesscodes.catalog import search
34from waeup.sirp.browser.layout import action
35
36grok.context(ISIRPObject)
37
38class BatchContainerPage(SIRPPage):
39    grok.name('index')
40    grok.context(IAccessCodeBatchContainer)
41    grok.template('batchcontainer')
42    grok.require('waeup.manageACBatches')
43    archive_button = _('Archive')
44    delete_button = _('Archive and delete')
45
46    label = _('Access Code Batches')
47    pnav = 0
48
49    def update(self, batches=None, archive=None, delete=None):
50        if archive is None and delete is None:
51            return
52        if not batches:
53            self.flash(_('No batch selected.'))
54            return
55        if isinstance(batches, basestring):
56            batches = [batches]
57        for name in batches:
58            batch = self.context[name]
59            csv_file = batch.archive()
60            self.flash(_('Archived ${a} (${b})',
61                mapping = {'a':name, 'b':csv_file}))
62            if delete is None:
63                continue
64            del self.context[name]
65            self.flash(_('Deleted batch ${a}', mapping = {'a':name}))
66
67class AddBatchPage(SIRPAddFormPage):
68    grok.name('add')
69    grok.context(IAccessCodeBatchContainer)
70    grok.require('waeup.manageACBatches')
71
72    label = _('Create Access Code Batch')
73    pnav = 0
74
75    form_fields = grok.AutoFields(IAccessCodeBatch).select(
76        'prefix', 'entry_num', 'cost')
77
78    @action(_('Create batch'), style='primary')
79    def createBatch(self, **data):
80        creator = self.request.principal.id
81        creation_date = datetime.now()
82        data.update(creation_date=creation_date, creator=creator)
83        batch = self.context.createBatch(**data)
84        csv_file = batch.createCSVLogFile()
85        self.context._p_changed = True
86        self.flash(_('Batch created (${a} entries)',
87            mapping = {'a':data['entry_num']}))
88        self.flash(_('Data written to ${a}', mapping = {'a':csv_file}))
89        self.redirect(self.url(self.context))
90
91    @action(_('Cancel'), validator=NullValidator)
92    def cancel(self, *args, **kw):
93        self.flash(_('Batch creation cancelled.'))
94        self.redirect(self.url(self.context))
95
96class ReimportBatchPage(SIRPPage):
97    """Screen for reimporting AC batches.
98    """
99    grok.name('reimport')
100    grok.context(IAccessCodeBatchContainer)
101    grok.template('reimportbatchpage')
102    grok.require('waeup.manageACBatches')
103    reimport_button = _('Reimport')
104    cancel_button = _('Cancel')
105
106    label = _('Reimport Access Code Batches')
107    pnav = 0
108
109    def update(self, filenames=None, reimport=None, cancel=None):
110        if cancel is not None:
111            self.flash(_('Reimport cancelled.'))
112            self.redirect(self.url(self.context))
113            return
114        if reimport is None:
115            return
116        if not filenames:
117            self.flash(_('No file chosen. Action cancelled.'))
118            self.redirect(self.url(self.context))
119            return
120        if isinstance(filenames, basestring):
121            filenames = [filenames]
122        userid = self.request.principal.id
123        for filename in filenames:
124            try:
125                self.context.reimport(filename, userid)
126            except KeyError:
127                self.flash(_('This batch already exists: ${a}',
128                    mapping = {'a':filename}))
129                continue
130            self.flash(_('Successfully reimported: ${a}',
131                mapping = {'a':filename}))
132        self.redirect(self.url(self.context))
133
134class BatchContainerSearchPage(SIRPPage):
135    grok.name('search')
136    grok.context(IAccessCodeBatchContainer)
137    grok.template('searchpage')
138    grok.require('waeup.manageACBatches')
139    pnav = 0
140    label = _('Search and Manage Access Codes')
141    search_button = _('Search')
142    disable_button = _('Disable ACs')
143    enable_button = _('Enable ACs')
144
145    def update(self, *args, **kw):
146        datatable.need()
147        form = self.request.form
148        self.hitlist = []
149        if 'searchterm' in form and form['searchterm']:
150            self.searchterm = form['searchterm']
151            self.searchtype = form['searchtype']
152        elif 'old_searchterm' in form:
153            self.searchterm = form['old_searchterm']
154            self.searchtype = form['old_searchtype']
155        else:
156            return
157        if not 'entries' in form:
158            self.hitlist = search(query=self.searchterm,
159                searchtype=self.searchtype, view=self)
160            return
161        entries = form['entries']
162        if isinstance(entries, basestring):
163            entries = [entries]
164        for entry in entries:
165            if 'disable' in form:
166                try:
167                    comment = _(u"AC disabled")
168                    self.context.disable(entry, comment)
169                    self.flash(_('${a} disabled.', mapping = {'a':entry}))
170                except InvalidTransitionError:
171                    self.flash(_('${a}: Disable transition not allowed.',
172                        mapping = {'a':entry}))
173            elif 'enable' in form:
174                try:
175                    comment = _(u"AC re-enabled")
176                    self.context.enable(entry, comment)
177                    self.flash(_('${a} (re-)enabled.', mapping = {'a':entry}))
178                except InvalidTransitionError:
179                    self.flash(_('${a}: Re-enable transition not allowed.', mapping = {'a':entry}))
180        self.hitlist = search(query=self.searchterm,
181            searchtype=self.searchtype, view=self)
182        return
183
184class BatchContainerBreadcrumb(Breadcrumb):
185    """A breadcrumb for ac batch containers.
186    """
187    grok.require('waeup.manageACBatches')
188    grok.context(IAccessCodeBatchContainer)
189    title = _(u'Access Code Batches')
190    parent_viewname = 'administration'
191
192#class BatchContainerSearchBreadcrumb(Breadcrumb):
193#    """A breadcrumb for ac batch containers search page.
194#    """
195#    grok.require('waeup.manageACBatches')
196#    grok.context(IAccessCodeBatchContainer)
197#    grok.name('search')
198#    title = u'Search Access Codes'
199#    viewname = 'search'
200#    parent_viewname = 'index'
201
202class AdminTaskManageACBatches(AdminTask):
203    """Entry on administration page that links to batch container.
204    """
205    grok.order(5)
206    grok.require('waeup.manageACBatches')
207    grok.template('admintaskacbatches')
208
209    link_title = _('Access Code Batches')
210    target_viewname = 'accesscodes'
211
212class CreateBatchButton(AddActionButton):
213    """Action button on batch container page which links to batch creation.
214    """
215    grok.context(IAccessCodeBatchContainer)
216    grok.view(BatchContainerPage)
217    grok.require('waeup.manageACBatches')
218    text = _('Add Access Code Batch')
219
220class ReimportBatchButton(BatchOpButton):
221    """Action button on batch container page which links to batch reimport.
222    """
223    grok.context(IAccessCodeBatchContainer)
224    grok.view(BatchContainerPage)
225    grok.require('waeup.manageACBatches')
226    target = 'reimport'
227    text = _('Reimport Access Code Batch')
228
229class SearchAccessCodeButton(SearchActionButton):
230    """Action button on batch container page which links to search.
231    """
232    grok.context(IAccessCodeBatchContainer)
233    grok.view(BatchContainerPage)
234    grok.require('waeup.manageACBatches')
235    text = _('Search Access Codes')
236
237class ManageAccessCodes(ManageLink):
238    """Link in upper left box to access code management.
239    """
240    grok.order(6)
241    grok.require('waeup.manageACBatches')
242
243    link = u'accesscodes'
244    text = _(u'Access Codes')
Note: See TracBrowser for help on using the repository browser.