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

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

Produce more user friendly and shorter object history messages.

File size: 7.3 KB
RevLine 
[5082]1"""UI components for accesscodes.
2"""
3import grok
[5088]4from datetime import datetime
[6450]5from hurry.workflow.interfaces import InvalidTransitionError
[6455]6from waeup.sirp.browser.resources import datatable
[5084]7from waeup.sirp.browser import WAeUPPage, WAeUPAddFormPage
[5082]8from waeup.sirp.browser.breadcrumbs import Breadcrumb
[5129]9from waeup.sirp.browser.viewlets import (
[5381]10    AdminTask, AddActionButton, SearchActionButton, BatchOpButton, ManageLink)
[6388]11from waeup.sirp.interfaces import IWAeUPObject
[5082]12
[5088]13from waeup.sirp.accesscodes.interfaces import (
14    IAccessCodeBatchContainer, IAccessCodeBatch,
15    )
[6450]16from waeup.sirp.accesscodes.catalog import search
[5082]17
18grok.context(IWAeUPObject)
19
20class BatchContainerPage(WAeUPPage):
21    grok.name('index')
22    grok.context(IAccessCodeBatchContainer)
23    grok.template('batchcontainer')
[5104]24    grok.require('waeup.manageACBatches')
[5082]25
26    title = 'Access Code Batches'
27    pnav = 0
28
[5117]29    def update(self, batches=None, archive=None, delete=None):
30        if archive is None and delete is None:
31            return
[5124]32        if not batches:
33            self.flash('No batch selected.')
34            return
[5117]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)
[5104]45
[5084]46class AddBatchPage(WAeUPAddFormPage):
47    grok.name('add')
48    grok.context(IAccessCodeBatchContainer)
[5104]49    grok.require('waeup.manageACBatches')
[5084]50
[6450]51    title = 'Create Access Code Batch'
[5084]52    pnav = 0
53
[5088]54    form_fields = grok.AutoFields(IAccessCodeBatch).select(
[6417]55        'prefix', 'entry_num', 'cost')
[5088]56
57    @grok.action('Cancel')
[5104]58    def cancel(self, *args, **kw):
[5088]59        self.flash('Batch creation cancelled.')
60        self.redirect(self.url(self.context))
61
[5084]62    @grok.action('Create batch')
63    def createBatch(self, **data):
[5088]64        creator = self.request.principal.id
65        creation_date = datetime.now()
66        data.update(creation_date=creation_date, creator=creator)
[5126]67        batch = self.context.createBatch(**data)
[5113]68        csv_file = batch.createCSVLogFile()
[5088]69        self.context._p_changed = True
70        self.flash('Batch created (%s entries)' % data['entry_num'])
[5113]71        self.flash('Data written to %s' % csv_file)
[5084]72        self.redirect(self.url(self.context))
73
[5130]74class ReimportBatchPage(WAeUPPage):
[5132]75    """Screen for reimporting AC batches.
76    """
[5130]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
[5132]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))
[6388]107
[5155]108class BatchContainerSearchPage(WAeUPPage):
109    grok.name('search')
110    grok.context(IAccessCodeBatchContainer)
111    grok.template('searchpage')
112    grok.require('waeup.manageACBatches')
113
[6450]114    title = 'Access Codes'
[5155]115    pnav = 0
[6450]116    label = 'Search and manage access codes'
[5155]117
[6450]118    def update(self, *args, **kw):
[6455]119        datatable.need()
[6450]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:
[5155]129            return
[6450]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']
[5155]136        if isinstance(entries, basestring):
137            entries = [entries]
138        for entry in entries:
[6450]139            if 'disable' in form:
140                try:
[6471]141                    comment = u"AC disabled"
[6459]142                    self.context.disable(entry, comment)
[6450]143                    self.flash('%s disabled.' % entry)
144                except InvalidTransitionError:
[6451]145                    self.flash('%s: Disable transition not allowed.' % entry)
[6450]146            elif 'enable' in form:
147                try:
[6471]148                    comment = u"AC re-enabled"
[6459]149                    self.context.enable(entry, comment)
[6450]150                    self.flash('%s (re-)enabled.' % entry)
151                except InvalidTransitionError:
[6451]152                    self.flash('%s: Re-enable transition not allowed.' % entry)
[6450]153        self.hitlist = search(query=self.searchterm,
154            searchtype=self.searchtype, view=self)
[5155]155        return
156
[5082]157class BatchContainerBreadcrumb(Breadcrumb):
158    """A breadcrumb for ac batch containers.
[5104]159    """
160    grok.require('waeup.manageACBatches')
[5082]161    grok.context(IAccessCodeBatchContainer)
162    title = u'Access Code Batches'
163    parent_viewname = 'administration'
164
[5155]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')
[6450]171    title = u'Search Access Codes'
[5155]172    viewname = 'search'
173    parent_viewname = 'index'
174
[5082]175class AdminTaskManageACBatches(AdminTask):
176    """Entry on administration page that links to batch container.
177    """
178    grok.order(5)
[5104]179    grok.require('waeup.manageACBatches')
[5082]180    grok.template('admintaskacbatches')
181
[5412]182    link_title = 'Access Code Batches'
[5082]183    target_viewname = 'accesscodes'
[5084]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)
[5104]190    grok.require('waeup.manageACBatches')
[6450]191    text = 'Add Access Code Batch'
[5104]192
[5129]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'
[6450]200    text = 'Reimport Access Code Batch'
[5129]201
[5155]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')
[6450]208    text = 'Search Access Codes'
[6388]209
[5381]210class ManageAccessCodes(ManageLink):
211    """Link in upper left box to access code management.
[5104]212    """
213    grok.order(6)
214    grok.require('waeup.manageACBatches')
215
[5381]216    link = u'accesscodes'
[5412]217    text = u'Access Codes'
Note: See TracBrowser for help on using the repository browser.