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

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

Add logger for accesscodes.

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