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.viewlets import ( |
---|
8 | AdminTask, AddActionButton, SearchActionButton, BatchOpButton, ManageLink) |
---|
9 | from waeup.sirp.interfaces import IWAeUPObject |
---|
10 | |
---|
11 | from waeup.sirp.accesscodes.interfaces import ( |
---|
12 | IAccessCodeBatchContainer, IAccessCodeBatch, |
---|
13 | ) |
---|
14 | |
---|
15 | grok.context(IWAeUPObject) |
---|
16 | |
---|
17 | class BatchContainerPage(WAeUPPage): |
---|
18 | grok.name('index') |
---|
19 | grok.context(IAccessCodeBatchContainer) |
---|
20 | grok.template('batchcontainer') |
---|
21 | grok.require('waeup.manageACBatches') |
---|
22 | |
---|
23 | title = 'Access Code Batches' |
---|
24 | pnav = 0 |
---|
25 | |
---|
26 | def update(self, batches=None, archive=None, delete=None): |
---|
27 | if archive is None and delete is None: |
---|
28 | return |
---|
29 | if not batches: |
---|
30 | self.flash('No batch selected.') |
---|
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 = 'Create Scratch Card Batch' |
---|
49 | pnav = 0 |
---|
50 | |
---|
51 | form_fields = grok.AutoFields(IAccessCodeBatch).select( |
---|
52 | '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 = self.context.createBatch(**data) |
---|
65 | csv_file = batch.createCSVLogFile() |
---|
66 | self.context._p_changed = True |
---|
67 | self.flash('Batch created (%s entries)' % data['entry_num']) |
---|
68 | self.flash('Data written to %s' % csv_file) |
---|
69 | self.redirect(self.url(self.context)) |
---|
70 | |
---|
71 | class ReimportBatchPage(WAeUPPage): |
---|
72 | """Screen for reimporting AC batches. |
---|
73 | """ |
---|
74 | grok.name('reimport') |
---|
75 | grok.context(IAccessCodeBatchContainer) |
---|
76 | grok.template('reimportbatchpage') |
---|
77 | grok.require('waeup.manageACBatches') |
---|
78 | |
---|
79 | title = 'Reimport Access Code Batches' |
---|
80 | pnav = 0 |
---|
81 | |
---|
82 | def update(self, filenames=None, reimport=None, cancel=None): |
---|
83 | if cancel is not None: |
---|
84 | self.flash('Reimport cancelled.') |
---|
85 | self.redirect(self.url(self.context)) |
---|
86 | return |
---|
87 | if reimport is None: |
---|
88 | return |
---|
89 | if not filenames: |
---|
90 | self.flash('No file chosen. Action cancelled.') |
---|
91 | self.redirect(self.url(self.context)) |
---|
92 | return |
---|
93 | if isinstance(filenames, basestring): |
---|
94 | filenames = [filenames] |
---|
95 | userid = self.request.principal.id |
---|
96 | for filename in filenames: |
---|
97 | try: |
---|
98 | self.context.reimport(filename, userid) |
---|
99 | except KeyError: |
---|
100 | self.flash('This batch already exists: %s' % filename) |
---|
101 | continue |
---|
102 | self.flash('Successfully reimported: %s' % filename) |
---|
103 | self.redirect(self.url(self.context)) |
---|
104 | |
---|
105 | class BatchContainerSearchPage(WAeUPPage): |
---|
106 | grok.name('search') |
---|
107 | grok.context(IAccessCodeBatchContainer) |
---|
108 | grok.template('searchpage') |
---|
109 | grok.require('waeup.manageACBatches') |
---|
110 | |
---|
111 | title = 'Search Scratch Cards' |
---|
112 | pnav = 0 |
---|
113 | |
---|
114 | def update(self, search=None, searchterm=None, searchtype=None, |
---|
115 | entries=None, disable=None, enable=None): |
---|
116 | self.searchresults = None |
---|
117 | if search is not None: |
---|
118 | searchresults = self.context.search(searchterm, searchtype) |
---|
119 | if len(searchresults) == 0: |
---|
120 | return |
---|
121 | self.searchresults = [] |
---|
122 | for result in searchresults: |
---|
123 | status = u'unused' |
---|
124 | if result.disabled is True: |
---|
125 | status = u'disabled by %s on %s' % ( |
---|
126 | result.student_id, |
---|
127 | result.invalidation_date.strftime('%c') |
---|
128 | ) |
---|
129 | elif result.invalidation_date is not None: |
---|
130 | status = u'invalidated by %s on %s' % ( |
---|
131 | result.student_id, |
---|
132 | result.invalidation_date.strftime('%c') |
---|
133 | ) |
---|
134 | entry = dict( |
---|
135 | serial = result.batch_serial, |
---|
136 | code = result.representation, |
---|
137 | status = status) |
---|
138 | self.searchresults.append(entry) |
---|
139 | if entries is None: |
---|
140 | return |
---|
141 | if isinstance(entries, basestring): |
---|
142 | entries = [entries] |
---|
143 | for entry in entries: |
---|
144 | if disable is not None: |
---|
145 | self.context.disable(entry, self.request.principal.id) |
---|
146 | self.flash('disabled %s' % entry) |
---|
147 | elif enable is not None: |
---|
148 | self.context.enable(entry) |
---|
149 | self.flash('(re-)enabled %s' % entry) |
---|
150 | return |
---|
151 | |
---|
152 | class BatchContainerBreadcrumb(Breadcrumb): |
---|
153 | """A breadcrumb for ac batch containers. |
---|
154 | """ |
---|
155 | grok.require('waeup.manageACBatches') |
---|
156 | grok.context(IAccessCodeBatchContainer) |
---|
157 | title = u'Access Code Batches' |
---|
158 | parent_viewname = 'administration' |
---|
159 | |
---|
160 | class BatchContainerSearchBreadcrumb(Breadcrumb): |
---|
161 | """A breadcrumb for ac batch containers search page. |
---|
162 | """ |
---|
163 | grok.require('waeup.manageACBatches') |
---|
164 | grok.context(IAccessCodeBatchContainer) |
---|
165 | grok.name('search') |
---|
166 | title = u'Search Scratch Cards' |
---|
167 | viewname = 'search' |
---|
168 | parent_viewname = 'index' |
---|
169 | |
---|
170 | class AdminTaskManageACBatches(AdminTask): |
---|
171 | """Entry on administration page that links to batch container. |
---|
172 | """ |
---|
173 | grok.order(5) |
---|
174 | grok.require('waeup.manageACBatches') |
---|
175 | grok.template('admintaskacbatches') |
---|
176 | |
---|
177 | link_title = 'Access Code Batches' |
---|
178 | target_viewname = 'accesscodes' |
---|
179 | |
---|
180 | class CreateBatchButton(AddActionButton): |
---|
181 | """Action button on batch container page which links to batch creation. |
---|
182 | """ |
---|
183 | grok.context(IAccessCodeBatchContainer) |
---|
184 | grok.view(BatchContainerPage) |
---|
185 | grok.require('waeup.manageACBatches') |
---|
186 | text = 'Add Scratch Card Batch' |
---|
187 | |
---|
188 | class ReimportBatchButton(BatchOpButton): |
---|
189 | """Action button on batch container page which links to batch reimport. |
---|
190 | """ |
---|
191 | grok.context(IAccessCodeBatchContainer) |
---|
192 | grok.view(BatchContainerPage) |
---|
193 | grok.require('waeup.manageACBatches') |
---|
194 | target = 'reimport' |
---|
195 | text = 'Reimport Scratch Card Batch' |
---|
196 | |
---|
197 | class SearchAccessCodeButton(SearchActionButton): |
---|
198 | """Action button on batch container page which links to search. |
---|
199 | """ |
---|
200 | grok.context(IAccessCodeBatchContainer) |
---|
201 | grok.view(BatchContainerPage) |
---|
202 | grok.require('waeup.manageACBatches') |
---|
203 | text = 'Search Scratch Cards' |
---|
204 | |
---|
205 | class ManageAccessCodes(ManageLink): |
---|
206 | """Link in upper left box to access code management. |
---|
207 | """ |
---|
208 | grok.order(6) |
---|
209 | grok.require('waeup.manageACBatches') |
---|
210 | |
---|
211 | link = u'accesscodes' |
---|
212 | text = u'Access Codes' |
---|