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

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

Internationalize accessodes package.

  • Property svn:keywords set to Id
File size: 8.7 KB
Line 
1## $Id: browser.py 7719 2012-02-28 20:52:18Z 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 (%s entries)' % data['entry_num'])
87        self.flash('Data written to %s' % csv_file)
88        self.redirect(self.url(self.context))
89
90    @action(_('Cancel'), validator=NullValidator)
91    def cancel(self, *args, **kw):
92        self.flash(_('Batch creation cancelled.'))
93        self.redirect(self.url(self.context))
94
95class ReimportBatchPage(SIRPPage):
96    """Screen for reimporting AC batches.
97    """
98    grok.name('reimport')
99    grok.context(IAccessCodeBatchContainer)
100    grok.template('reimportbatchpage')
101    grok.require('waeup.manageACBatches')
102    reimport_button = _('Reimport')
103    cancel_button = _('Cancel')
104
105    label = _('Reimport Access Code Batches')
106    pnav = 0
107
108    def update(self, filenames=None, reimport=None, cancel=None):
109        if cancel is not None:
110            self.flash(_('Reimport cancelled.'))
111            self.redirect(self.url(self.context))
112            return
113        if reimport is None:
114            return
115        if not filenames:
116            self.flash(_('No file chosen. Action cancelled.'))
117            self.redirect(self.url(self.context))
118            return
119        if isinstance(filenames, basestring):
120            filenames = [filenames]
121        userid = self.request.principal.id
122        for filename in filenames:
123            try:
124                self.context.reimport(filename, userid)
125            except KeyError:
126                self.flash(_('This batch already exists: ${a}',
127                    mapping = {'a':filename}))
128                continue
129            self.flash(_('Successfully reimported: ${a}',
130                mapping = {'a':filename}))
131        self.redirect(self.url(self.context))
132
133class BatchContainerSearchPage(SIRPPage):
134    grok.name('search')
135    grok.context(IAccessCodeBatchContainer)
136    grok.template('searchpage')
137    grok.require('waeup.manageACBatches')
138    pnav = 0
139    label = _('Search and Manage Access Codes')
140    search_button = _('Search')
141    disable_button = _('Disable ACs')
142    enable_button = _('Enable ACs')
143
144    def update(self, *args, **kw):
145        datatable.need()
146        form = self.request.form
147        self.hitlist = []
148        if 'searchterm' in form and form['searchterm']:
149            self.searchterm = form['searchterm']
150            self.searchtype = form['searchtype']
151        elif 'old_searchterm' in form:
152            self.searchterm = form['old_searchterm']
153            self.searchtype = form['old_searchtype']
154        else:
155            return
156        if not 'entries' in form:
157            self.hitlist = search(query=self.searchterm,
158                searchtype=self.searchtype, view=self)
159            return
160        entries = form['entries']
161        if isinstance(entries, basestring):
162            entries = [entries]
163        for entry in entries:
164            if 'disable' in form:
165                try:
166                    comment = _(u"AC disabled")
167                    self.context.disable(entry, comment)
168                    self.flash(_('${a} disabled.', mapping = {'a':entry}))
169                except InvalidTransitionError:
170                    self.flash(_('${a}: Disable transition not allowed.',
171                        mapping = {'a':entry}))
172            elif 'enable' in form:
173                try:
174                    comment = _(u"AC re-enabled")
175                    self.context.enable(entry, comment)
176                    self.flash(_('${a} (re-)enabled.', mapping = {'a':entry}))
177                except InvalidTransitionError:
178                    self.flash(_('${a}: Re-enable transition not allowed.', mapping = {'a':entry}))
179        self.hitlist = search(query=self.searchterm,
180            searchtype=self.searchtype, view=self)
181        return
182
183class BatchContainerBreadcrumb(Breadcrumb):
184    """A breadcrumb for ac batch containers.
185    """
186    grok.require('waeup.manageACBatches')
187    grok.context(IAccessCodeBatchContainer)
188    title = _(u'Access Code Batches')
189    parent_viewname = 'administration'
190
191#class BatchContainerSearchBreadcrumb(Breadcrumb):
192#    """A breadcrumb for ac batch containers search page.
193#    """
194#    grok.require('waeup.manageACBatches')
195#    grok.context(IAccessCodeBatchContainer)
196#    grok.name('search')
197#    title = u'Search Access Codes'
198#    viewname = 'search'
199#    parent_viewname = 'index'
200
201class AdminTaskManageACBatches(AdminTask):
202    """Entry on administration page that links to batch container.
203    """
204    grok.order(5)
205    grok.require('waeup.manageACBatches')
206    grok.template('admintaskacbatches')
207
208    link_title = _('Access Code Batches')
209    target_viewname = 'accesscodes'
210
211class CreateBatchButton(AddActionButton):
212    """Action button on batch container page which links to batch creation.
213    """
214    grok.context(IAccessCodeBatchContainer)
215    grok.view(BatchContainerPage)
216    grok.require('waeup.manageACBatches')
217    text = _('Add Access Code Batch')
218
219class ReimportBatchButton(BatchOpButton):
220    """Action button on batch container page which links to batch reimport.
221    """
222    grok.context(IAccessCodeBatchContainer)
223    grok.view(BatchContainerPage)
224    grok.require('waeup.manageACBatches')
225    target = 'reimport'
226    text = _('Reimport Access Code Batch')
227
228class SearchAccessCodeButton(SearchActionButton):
229    """Action button on batch container page which links to search.
230    """
231    grok.context(IAccessCodeBatchContainer)
232    grok.view(BatchContainerPage)
233    grok.require('waeup.manageACBatches')
234    text = _('Search Access Codes')
235
236class ManageAccessCodes(ManageLink):
237    """Link in upper left box to access code management.
238    """
239    grok.order(6)
240    grok.require('waeup.manageACBatches')
241
242    link = u'accesscodes'
243    text = _(u'Access Codes')
Note: See TracBrowser for help on using the repository browser.