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

Last change on this file since 15185 was 11254, checked in by uli, 11 years ago

Merge changes from uli-diazo-themed back into trunk. If this works, then a miracle happened.

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