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

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

Replace the term 'WAeUP' by SIRP which is a WAeUP product.

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