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

Last change on this file since 6454 was 6451, checked in by Henrik Bettermann, 14 years ago

Be more verbose.

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