1 | """UI components for accesscodes. |
---|
2 | """ |
---|
3 | import grok |
---|
4 | from datetime import datetime |
---|
5 | from waeup.sirp.browser import WAeUPPage, WAeUPAddFormPage |
---|
6 | from waeup.sirp.browser.breadcrumbs import Breadcrumb |
---|
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 |
---|
10 | |
---|
11 | from waeup.sirp.accesscodes.accesscodes import AccessCodeBatch |
---|
12 | from waeup.sirp.accesscodes.catalog import invalidated_num |
---|
13 | from waeup.sirp.accesscodes.interfaces import ( |
---|
14 | IAccessCodeBatchContainer, IAccessCodeBatch, |
---|
15 | ) |
---|
16 | |
---|
17 | grok.context(IWAeUPObject) |
---|
18 | |
---|
19 | class BatchContainerPage(WAeUPPage): |
---|
20 | grok.name('index') |
---|
21 | grok.context(IAccessCodeBatchContainer) |
---|
22 | grok.template('batchcontainer') |
---|
23 | grok.require('waeup.manageACBatches') |
---|
24 | |
---|
25 | title = 'Access Code Batches' |
---|
26 | pnav = 0 |
---|
27 | |
---|
28 | def invalidated(self, batch): |
---|
29 | return invalidated_num(batch.prefix, batch.num) |
---|
30 | |
---|
31 | |
---|
32 | class AddBatchPage(WAeUPAddFormPage): |
---|
33 | grok.name('add') |
---|
34 | grok.context(IAccessCodeBatchContainer) |
---|
35 | grok.require('waeup.manageACBatches') |
---|
36 | |
---|
37 | title = label = 'Create a WAeUP Scratch Card Batch' |
---|
38 | pnav = 0 |
---|
39 | |
---|
40 | form_fields = grok.AutoFields(IAccessCodeBatch).select( |
---|
41 | 'batch_prefix', 'entry_num', 'cost') |
---|
42 | |
---|
43 | @grok.action('Cancel') |
---|
44 | def cancel(self, *args, **kw): |
---|
45 | self.flash('Batch creation cancelled.') |
---|
46 | self.redirect(self.url(self.context)) |
---|
47 | |
---|
48 | @grok.action('Create batch') |
---|
49 | def createBatch(self, **data): |
---|
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']) |
---|
57 | self.redirect(self.url(self.context)) |
---|
58 | |
---|
59 | class BatchContainerBreadcrumb(Breadcrumb): |
---|
60 | """A breadcrumb for ac batch containers. |
---|
61 | """ |
---|
62 | grok.require('waeup.manageACBatches') |
---|
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) |
---|
71 | grok.require('waeup.manageACBatches') |
---|
72 | grok.template('admintaskacbatches') |
---|
73 | |
---|
74 | link_title = 'Manage access-code batches' |
---|
75 | target_viewname = 'accesscodes' |
---|
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) |
---|
82 | grok.require('waeup.manageACBatches') |
---|
83 | text = 'Add Scratch Card Batch' |
---|
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>' |
---|