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

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

Produce more user friendly and shorter object history messages.

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