"""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, BatchOpButton) from waeup.sirp.interfaces import IWAeUPObject, IUniversity from waeup.sirp.accesscodes.accesscodes import AccessCodeBatch 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 archive is None and delete is None: return if not batches: self.flash('No batch selected.') 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 = self.context.createBatch(**data) 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 ReimportBatchPage(WAeUPPage): """Screen for reimporting AC batches. """ grok.name('reimport') grok.context(IAccessCodeBatchContainer) grok.template('reimportbatchpage') grok.require('waeup.manageACBatches') title = 'Reimport Access Code Batches' pnav = 0 def update(self, filenames=None, reimport=None, cancel=None): if cancel is not None: self.flash('Reimport cancelled.') self.redirect(self.url(self.context)) return if reimport is None: return if not filenames: self.flash('No file chosen. Action cancelled.') self.redirect(self.url(self.context)) return if isinstance(filenames, basestring): filenames = [filenames] userid = self.request.principal.id for filename in filenames: try: self.context.reimport(filename, userid) except KeyError: self.flash('This batch already exists: %s' % filename) continue self.flash('Successfully reimported: %s' % filename) 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 ReimportBatchButton(BatchOpButton): """Action button on batch container page which links to batch reimport. """ grok.context(IAccessCodeBatchContainer) grok.view(BatchContainerPage) grok.require('waeup.manageACBatches') target = 'reimport' text = 'Reimport 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
'