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.interfaces import ( |
---|
13 | IAccessCodeBatchContainer, IAccessCodeBatch, |
---|
14 | ) |
---|
15 | |
---|
16 | grok.context(IWAeUPObject) |
---|
17 | |
---|
18 | class BatchContainerPage(WAeUPPage): |
---|
19 | grok.name('index') |
---|
20 | grok.context(IAccessCodeBatchContainer) |
---|
21 | grok.template('batchcontainer') |
---|
22 | grok.require('waeup.manageACBatches') |
---|
23 | |
---|
24 | title = 'Access Code Batches' |
---|
25 | pnav = 0 |
---|
26 | |
---|
27 | def update(self, batches=None, archive=None, delete=None): |
---|
28 | if batches is None: |
---|
29 | return |
---|
30 | if archive is None and delete is None: |
---|
31 | return |
---|
32 | if isinstance(batches, basestring): |
---|
33 | batches = [batches] |
---|
34 | for name in batches: |
---|
35 | batch = self.context[name] |
---|
36 | csv_file = batch.archive() |
---|
37 | self.flash('Archived %s (%s)' % (name, csv_file)) |
---|
38 | if delete is None: |
---|
39 | continue |
---|
40 | del self.context[name] |
---|
41 | self.flash('Deleted batch %s' % name) |
---|
42 | |
---|
43 | class AddBatchPage(WAeUPAddFormPage): |
---|
44 | grok.name('add') |
---|
45 | grok.context(IAccessCodeBatchContainer) |
---|
46 | grok.require('waeup.manageACBatches') |
---|
47 | |
---|
48 | title = label = 'Create a WAeUP Scratch Card Batch' |
---|
49 | pnav = 0 |
---|
50 | |
---|
51 | form_fields = grok.AutoFields(IAccessCodeBatch).select( |
---|
52 | 'batch_prefix', 'entry_num', 'cost') |
---|
53 | |
---|
54 | @grok.action('Cancel') |
---|
55 | def cancel(self, *args, **kw): |
---|
56 | self.flash('Batch creation cancelled.') |
---|
57 | self.redirect(self.url(self.context)) |
---|
58 | |
---|
59 | @grok.action('Create batch') |
---|
60 | def createBatch(self, **data): |
---|
61 | creator = self.request.principal.id |
---|
62 | creation_date = datetime.now() |
---|
63 | data.update(creation_date=creation_date, creator=creator) |
---|
64 | batch = AccessCodeBatch(**data) |
---|
65 | self.context.addBatch(batch) |
---|
66 | csv_file = batch.createCSVLogFile() |
---|
67 | self.context._p_changed = True |
---|
68 | self.flash('Batch created (%s entries)' % data['entry_num']) |
---|
69 | self.flash('Data written to %s' % csv_file) |
---|
70 | self.redirect(self.url(self.context)) |
---|
71 | |
---|
72 | class BatchContainerBreadcrumb(Breadcrumb): |
---|
73 | """A breadcrumb for ac batch containers. |
---|
74 | """ |
---|
75 | grok.require('waeup.manageACBatches') |
---|
76 | grok.context(IAccessCodeBatchContainer) |
---|
77 | title = u'Access Code Batches' |
---|
78 | parent_viewname = 'administration' |
---|
79 | |
---|
80 | class AdminTaskManageACBatches(AdminTask): |
---|
81 | """Entry on administration page that links to batch container. |
---|
82 | """ |
---|
83 | grok.order(5) |
---|
84 | grok.require('waeup.manageACBatches') |
---|
85 | grok.template('admintaskacbatches') |
---|
86 | |
---|
87 | link_title = 'Manage access-code batches' |
---|
88 | target_viewname = 'accesscodes' |
---|
89 | |
---|
90 | class CreateBatchButton(AddActionButton): |
---|
91 | """Action button on batch container page which links to batch creation. |
---|
92 | """ |
---|
93 | grok.context(IAccessCodeBatchContainer) |
---|
94 | grok.view(BatchContainerPage) |
---|
95 | grok.require('waeup.manageACBatches') |
---|
96 | text = 'Add Scratch Card Batch' |
---|
97 | |
---|
98 | class ManageAccessCodes(grok.Viewlet): |
---|
99 | """Link on front page to access code management. |
---|
100 | """ |
---|
101 | grok.viewletmanager(LeftSidebar) |
---|
102 | grok.context(IUniversity) |
---|
103 | grok.view(UniversityPage) |
---|
104 | grok.order(6) |
---|
105 | grok.require('waeup.manageACBatches') |
---|
106 | |
---|
107 | def render(self): |
---|
108 | return u'<div class="portlet"><a href="accesscodes">Manage access-codes</a></div>' |
---|