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

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

Some button adjustments.

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