"""UI components for accesscodes. """ import grok from datetime import datetime from waeup.sirp.browser import WAeUPPage, WAeUPAddFormPage from waeup.sirp.browser.breadcrumbs import Breadcrumb from waeup.sirp.browser.pages import UniversityPage from waeup.sirp.browser.viewlets import AdminTask, AddActionButton, LeftSidebar from waeup.sirp.interfaces import IWAeUPObject, IUniversity from waeup.sirp.accesscodes.accesscodes import AccessCodeBatch from waeup.sirp.accesscodes.catalog import invalidated_num from waeup.sirp.accesscodes.interfaces import ( IAccessCodeBatchContainer, IAccessCodeBatch, ) grok.context(IWAeUPObject) class BatchContainerPage(WAeUPPage): grok.name('index') grok.context(IAccessCodeBatchContainer) grok.template('batchcontainer') grok.require('waeup.manageACBatches') title = 'Access Code Batches' pnav = 0 def update(self, batches=None, archive=None, delete=None): if batches is None: return if archive is None and delete is None: return if isinstance(batches, basestring): batches = [batches] for name in batches: batch = self.context[name] csv_file = batch.archive() self.flash('Archived %s (%s)' % (name, csv_file)) if delete is None: continue del self.context[name] self.flash('Deleted batch %s' % name) class AddBatchPage(WAeUPAddFormPage): grok.name('add') grok.context(IAccessCodeBatchContainer) grok.require('waeup.manageACBatches') title = label = 'Create a WAeUP Scratch Card Batch' pnav = 0 form_fields = grok.AutoFields(IAccessCodeBatch).select( 'batch_prefix', 'entry_num', 'cost') @grok.action('Cancel') def cancel(self, *args, **kw): self.flash('Batch creation cancelled.') self.redirect(self.url(self.context)) @grok.action('Create batch') def createBatch(self, **data): creator = self.request.principal.id creation_date = datetime.now() data.update(creation_date=creation_date, creator=creator) batch = AccessCodeBatch(**data) self.context.addBatch(batch) csv_file = batch.createCSVLogFile() self.context._p_changed = True self.flash('Batch created (%s entries)' % data['entry_num']) self.flash('Data written to %s' % csv_file) self.redirect(self.url(self.context)) class BatchContainerBreadcrumb(Breadcrumb): """A breadcrumb for ac batch containers. """ grok.require('waeup.manageACBatches') grok.context(IAccessCodeBatchContainer) title = u'Access Code Batches' parent_viewname = 'administration' class AdminTaskManageACBatches(AdminTask): """Entry on administration page that links to batch container. """ grok.order(5) grok.require('waeup.manageACBatches') grok.template('admintaskacbatches') link_title = 'Manage access-code batches' target_viewname = 'accesscodes' class CreateBatchButton(AddActionButton): """Action button on batch container page which links to batch creation. """ grok.context(IAccessCodeBatchContainer) grok.view(BatchContainerPage) grok.require('waeup.manageACBatches') text = 'Add Scratch Card Batch' class ManageAccessCodes(grok.Viewlet): """Link on front page to access code management. """ grok.viewletmanager(LeftSidebar) grok.context(IUniversity) grok.view(UniversityPage) grok.order(6) grok.require('waeup.manageACBatches') def render(self): return u'
Manage access-codes
'