[5082] | 1 | """UI components for accesscodes. |
---|
| 2 | """ |
---|
| 3 | import grok |
---|
[5088] | 4 | from datetime import datetime |
---|
[5084] | 5 | from waeup.sirp.browser import WAeUPPage, WAeUPAddFormPage |
---|
[5082] | 6 | from waeup.sirp.browser.breadcrumbs import Breadcrumb |
---|
[5104] | 7 | from waeup.sirp.browser.pages import UniversityPage |
---|
| 8 | from waeup.sirp.browser.viewlets import AdminTask, AddActionButton, LeftSidebar |
---|
| 9 | from waeup.sirp.interfaces import IWAeUPObject, IUniversity |
---|
[5082] | 10 | |
---|
[5088] | 11 | from waeup.sirp.accesscodes.accesscodes import AccessCodeBatch |
---|
[5104] | 12 | from waeup.sirp.accesscodes.catalog import invalidated_num |
---|
[5088] | 13 | from waeup.sirp.accesscodes.interfaces import ( |
---|
| 14 | IAccessCodeBatchContainer, IAccessCodeBatch, |
---|
| 15 | ) |
---|
[5082] | 16 | |
---|
| 17 | grok.context(IWAeUPObject) |
---|
| 18 | |
---|
| 19 | class BatchContainerPage(WAeUPPage): |
---|
| 20 | grok.name('index') |
---|
| 21 | grok.context(IAccessCodeBatchContainer) |
---|
| 22 | grok.template('batchcontainer') |
---|
[5104] | 23 | grok.require('waeup.manageACBatches') |
---|
[5082] | 24 | |
---|
| 25 | title = 'Access Code Batches' |
---|
| 26 | pnav = 0 |
---|
| 27 | |
---|
[5104] | 28 | def invalidated(self, batch): |
---|
| 29 | return invalidated_num(batch.prefix, batch.num) |
---|
| 30 | |
---|
| 31 | |
---|
[5084] | 32 | class AddBatchPage(WAeUPAddFormPage): |
---|
| 33 | grok.name('add') |
---|
| 34 | grok.context(IAccessCodeBatchContainer) |
---|
[5104] | 35 | grok.require('waeup.manageACBatches') |
---|
[5084] | 36 | |
---|
| 37 | title = label = 'Create a WAeUP Scratch Card Batch' |
---|
| 38 | pnav = 0 |
---|
| 39 | |
---|
[5088] | 40 | form_fields = grok.AutoFields(IAccessCodeBatch).select( |
---|
| 41 | 'batch_prefix', 'entry_num', 'cost') |
---|
| 42 | |
---|
| 43 | @grok.action('Cancel') |
---|
[5104] | 44 | def cancel(self, *args, **kw): |
---|
[5088] | 45 | self.flash('Batch creation cancelled.') |
---|
| 46 | self.redirect(self.url(self.context)) |
---|
| 47 | |
---|
[5084] | 48 | @grok.action('Create batch') |
---|
| 49 | def createBatch(self, **data): |
---|
[5088] | 50 | creator = self.request.principal.id |
---|
| 51 | creation_date = datetime.now() |
---|
| 52 | data.update(creation_date=creation_date, creator=creator) |
---|
| 53 | batch = AccessCodeBatch(**data) |
---|
| 54 | self.context.addBatch(batch) |
---|
| 55 | self.context._p_changed = True |
---|
| 56 | self.flash('Batch created (%s entries)' % data['entry_num']) |
---|
[5084] | 57 | self.redirect(self.url(self.context)) |
---|
| 58 | |
---|
[5082] | 59 | class BatchContainerBreadcrumb(Breadcrumb): |
---|
| 60 | """A breadcrumb for ac batch containers. |
---|
[5104] | 61 | """ |
---|
| 62 | grok.require('waeup.manageACBatches') |
---|
[5082] | 63 | grok.context(IAccessCodeBatchContainer) |
---|
| 64 | title = u'Access Code Batches' |
---|
| 65 | parent_viewname = 'administration' |
---|
| 66 | |
---|
| 67 | class AdminTaskManageACBatches(AdminTask): |
---|
| 68 | """Entry on administration page that links to batch container. |
---|
| 69 | """ |
---|
| 70 | grok.order(5) |
---|
[5104] | 71 | grok.require('waeup.manageACBatches') |
---|
[5082] | 72 | grok.template('admintaskacbatches') |
---|
| 73 | |
---|
| 74 | link_title = 'Manage access-code batches' |
---|
| 75 | target_viewname = 'accesscodes' |
---|
[5084] | 76 | |
---|
| 77 | class CreateBatchButton(AddActionButton): |
---|
| 78 | """Action button on batch container page which links to batch creation. |
---|
| 79 | """ |
---|
| 80 | grok.context(IAccessCodeBatchContainer) |
---|
| 81 | grok.view(BatchContainerPage) |
---|
[5104] | 82 | grok.require('waeup.manageACBatches') |
---|
[5084] | 83 | text = 'Add Scratch Card Batch' |
---|
[5104] | 84 | |
---|
| 85 | class ManageAccessCodes(grok.Viewlet): |
---|
| 86 | """Link on front page to access code management. |
---|
| 87 | """ |
---|
| 88 | grok.viewletmanager(LeftSidebar) |
---|
| 89 | grok.context(IUniversity) |
---|
| 90 | grok.view(UniversityPage) |
---|
| 91 | grok.order(6) |
---|
| 92 | grok.require('waeup.manageACBatches') |
---|
| 93 | |
---|
| 94 | def render(self): |
---|
| 95 | return u'<div class="portlet"><a href="accesscodes">Manage access-codes</a></div>' |
---|