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 ( |
---|
9 | AdminTask, AddActionButton, LeftSidebar, BatchOpButton) |
---|
10 | from waeup.sirp.interfaces import IWAeUPObject, IUniversity |
---|
11 | |
---|
12 | from waeup.sirp.accesscodes.accesscodes import AccessCodeBatch |
---|
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 update(self, batches=None, archive=None, delete=None): |
---|
29 | if archive is None and delete is None: |
---|
30 | return |
---|
31 | if not batches: |
---|
32 | self.flash('No batch selected.') |
---|
33 | return |
---|
34 | if isinstance(batches, basestring): |
---|
35 | batches = [batches] |
---|
36 | for name in batches: |
---|
37 | batch = self.context[name] |
---|
38 | csv_file = batch.archive() |
---|
39 | self.flash('Archived %s (%s)' % (name, csv_file)) |
---|
40 | if delete is None: |
---|
41 | continue |
---|
42 | del self.context[name] |
---|
43 | self.flash('Deleted batch %s' % name) |
---|
44 | |
---|
45 | class AddBatchPage(WAeUPAddFormPage): |
---|
46 | grok.name('add') |
---|
47 | grok.context(IAccessCodeBatchContainer) |
---|
48 | grok.require('waeup.manageACBatches') |
---|
49 | |
---|
50 | title = label = 'Create a WAeUP Scratch Card Batch' |
---|
51 | pnav = 0 |
---|
52 | |
---|
53 | form_fields = grok.AutoFields(IAccessCodeBatch).select( |
---|
54 | 'batch_prefix', 'entry_num', 'cost') |
---|
55 | |
---|
56 | @grok.action('Cancel') |
---|
57 | def cancel(self, *args, **kw): |
---|
58 | self.flash('Batch creation cancelled.') |
---|
59 | self.redirect(self.url(self.context)) |
---|
60 | |
---|
61 | @grok.action('Create batch') |
---|
62 | def createBatch(self, **data): |
---|
63 | creator = self.request.principal.id |
---|
64 | creation_date = datetime.now() |
---|
65 | data.update(creation_date=creation_date, creator=creator) |
---|
66 | batch = self.context.createBatch(**data) |
---|
67 | csv_file = batch.createCSVLogFile() |
---|
68 | self.context._p_changed = True |
---|
69 | self.flash('Batch created (%s entries)' % data['entry_num']) |
---|
70 | self.flash('Data written to %s' % csv_file) |
---|
71 | self.redirect(self.url(self.context)) |
---|
72 | |
---|
73 | class ReimportBatchPage(WAeUPPage): |
---|
74 | """Screen for reimporting AC batches. |
---|
75 | """ |
---|
76 | grok.name('reimport') |
---|
77 | grok.context(IAccessCodeBatchContainer) |
---|
78 | grok.template('reimportbatchpage') |
---|
79 | grok.require('waeup.manageACBatches') |
---|
80 | |
---|
81 | title = 'Reimport Access Code Batches' |
---|
82 | pnav = 0 |
---|
83 | |
---|
84 | def update(self, filenames=None, reimport=None, cancel=None): |
---|
85 | if cancel is not None: |
---|
86 | self.flash('Reimport cancelled.') |
---|
87 | self.redirect(self.url(self.context)) |
---|
88 | return |
---|
89 | if reimport is None: |
---|
90 | return |
---|
91 | if not filenames: |
---|
92 | self.flash('No file chosen. Action cancelled.') |
---|
93 | self.redirect(self.url(self.context)) |
---|
94 | return |
---|
95 | if isinstance(filenames, basestring): |
---|
96 | filenames = [filenames] |
---|
97 | userid = self.request.principal.id |
---|
98 | for filename in filenames: |
---|
99 | try: |
---|
100 | self.context.reimport(filename, userid) |
---|
101 | except KeyError: |
---|
102 | self.flash('This batch already exists: %s' % filename) |
---|
103 | continue |
---|
104 | self.flash('Successfully reimported: %s' % filename) |
---|
105 | self.redirect(self.url(self.context)) |
---|
106 | |
---|
107 | |
---|
108 | class BatchContainerBreadcrumb(Breadcrumb): |
---|
109 | """A breadcrumb for ac batch containers. |
---|
110 | """ |
---|
111 | grok.require('waeup.manageACBatches') |
---|
112 | grok.context(IAccessCodeBatchContainer) |
---|
113 | title = u'Access Code Batches' |
---|
114 | parent_viewname = 'administration' |
---|
115 | |
---|
116 | class AdminTaskManageACBatches(AdminTask): |
---|
117 | """Entry on administration page that links to batch container. |
---|
118 | """ |
---|
119 | grok.order(5) |
---|
120 | grok.require('waeup.manageACBatches') |
---|
121 | grok.template('admintaskacbatches') |
---|
122 | |
---|
123 | link_title = 'Manage access-code batches' |
---|
124 | target_viewname = 'accesscodes' |
---|
125 | |
---|
126 | class CreateBatchButton(AddActionButton): |
---|
127 | """Action button on batch container page which links to batch creation. |
---|
128 | """ |
---|
129 | grok.context(IAccessCodeBatchContainer) |
---|
130 | grok.view(BatchContainerPage) |
---|
131 | grok.require('waeup.manageACBatches') |
---|
132 | text = 'Add Scratch Card Batch' |
---|
133 | |
---|
134 | class ReimportBatchButton(BatchOpButton): |
---|
135 | """Action button on batch container page which links to batch reimport. |
---|
136 | """ |
---|
137 | grok.context(IAccessCodeBatchContainer) |
---|
138 | grok.view(BatchContainerPage) |
---|
139 | grok.require('waeup.manageACBatches') |
---|
140 | target = 'reimport' |
---|
141 | text = 'Reimport Scratch Card Batch' |
---|
142 | |
---|
143 | class ManageAccessCodes(grok.Viewlet): |
---|
144 | """Link on front page to access code management. |
---|
145 | """ |
---|
146 | grok.viewletmanager(LeftSidebar) |
---|
147 | grok.context(IUniversity) |
---|
148 | grok.view(UniversityPage) |
---|
149 | grok.order(6) |
---|
150 | grok.require('waeup.manageACBatches') |
---|
151 | |
---|
152 | def render(self): |
---|
153 | return u'<div class="portlet"><a href="accesscodes">Manage access-codes</a></div>' |
---|