[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 |
---|
[5129] | 7 | from waeup.sirp.browser.viewlets import ( |
---|
[5381] | 8 | AdminTask, AddActionButton, SearchActionButton, BatchOpButton, ManageLink) |
---|
[6388] | 9 | from waeup.sirp.interfaces import IWAeUPObject |
---|
[5082] | 10 | |
---|
[5088] | 11 | from waeup.sirp.accesscodes.interfaces import ( |
---|
| 12 | IAccessCodeBatchContainer, IAccessCodeBatch, |
---|
| 13 | ) |
---|
[5082] | 14 | |
---|
| 15 | grok.context(IWAeUPObject) |
---|
| 16 | |
---|
| 17 | class BatchContainerPage(WAeUPPage): |
---|
| 18 | grok.name('index') |
---|
| 19 | grok.context(IAccessCodeBatchContainer) |
---|
| 20 | grok.template('batchcontainer') |
---|
[5104] | 21 | grok.require('waeup.manageACBatches') |
---|
[5082] | 22 | |
---|
| 23 | title = 'Access Code Batches' |
---|
| 24 | pnav = 0 |
---|
| 25 | |
---|
[5117] | 26 | def update(self, batches=None, archive=None, delete=None): |
---|
| 27 | if archive is None and delete is None: |
---|
| 28 | return |
---|
[5124] | 29 | if not batches: |
---|
| 30 | self.flash('No batch selected.') |
---|
| 31 | return |
---|
[5117] | 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) |
---|
[5104] | 42 | |
---|
[5084] | 43 | class AddBatchPage(WAeUPAddFormPage): |
---|
| 44 | grok.name('add') |
---|
| 45 | grok.context(IAccessCodeBatchContainer) |
---|
[5104] | 46 | grok.require('waeup.manageACBatches') |
---|
[5084] | 47 | |
---|
[5918] | 48 | title = 'Create Scratch Card Batch' |
---|
[5084] | 49 | pnav = 0 |
---|
| 50 | |
---|
[5088] | 51 | form_fields = grok.AutoFields(IAccessCodeBatch).select( |
---|
[6417] | 52 | 'prefix', 'entry_num', 'cost') |
---|
[5088] | 53 | |
---|
| 54 | @grok.action('Cancel') |
---|
[5104] | 55 | def cancel(self, *args, **kw): |
---|
[5088] | 56 | self.flash('Batch creation cancelled.') |
---|
| 57 | self.redirect(self.url(self.context)) |
---|
| 58 | |
---|
[5084] | 59 | @grok.action('Create batch') |
---|
| 60 | def createBatch(self, **data): |
---|
[5088] | 61 | creator = self.request.principal.id |
---|
| 62 | creation_date = datetime.now() |
---|
| 63 | data.update(creation_date=creation_date, creator=creator) |
---|
[5126] | 64 | batch = self.context.createBatch(**data) |
---|
[5113] | 65 | csv_file = batch.createCSVLogFile() |
---|
[5088] | 66 | self.context._p_changed = True |
---|
| 67 | self.flash('Batch created (%s entries)' % data['entry_num']) |
---|
[5113] | 68 | self.flash('Data written to %s' % csv_file) |
---|
[5084] | 69 | self.redirect(self.url(self.context)) |
---|
| 70 | |
---|
[5130] | 71 | class ReimportBatchPage(WAeUPPage): |
---|
[5132] | 72 | """Screen for reimporting AC batches. |
---|
| 73 | """ |
---|
[5130] | 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 | |
---|
[5132] | 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)) |
---|
[6388] | 104 | |
---|
[5155] | 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 | |
---|
[5082] | 152 | class BatchContainerBreadcrumb(Breadcrumb): |
---|
| 153 | """A breadcrumb for ac batch containers. |
---|
[5104] | 154 | """ |
---|
| 155 | grok.require('waeup.manageACBatches') |
---|
[5082] | 156 | grok.context(IAccessCodeBatchContainer) |
---|
| 157 | title = u'Access Code Batches' |
---|
| 158 | parent_viewname = 'administration' |
---|
| 159 | |
---|
[5155] | 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 | |
---|
[5082] | 170 | class AdminTaskManageACBatches(AdminTask): |
---|
| 171 | """Entry on administration page that links to batch container. |
---|
| 172 | """ |
---|
| 173 | grok.order(5) |
---|
[5104] | 174 | grok.require('waeup.manageACBatches') |
---|
[5082] | 175 | grok.template('admintaskacbatches') |
---|
| 176 | |
---|
[5412] | 177 | link_title = 'Access Code Batches' |
---|
[5082] | 178 | target_viewname = 'accesscodes' |
---|
[5084] | 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) |
---|
[5104] | 185 | grok.require('waeup.manageACBatches') |
---|
[5084] | 186 | text = 'Add Scratch Card Batch' |
---|
[5104] | 187 | |
---|
[5129] | 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 | |
---|
[5155] | 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') |
---|
[6388] | 203 | text = 'Search Scratch Cards' |
---|
| 204 | |
---|
[5381] | 205 | class ManageAccessCodes(ManageLink): |
---|
| 206 | """Link in upper left box to access code management. |
---|
[5104] | 207 | """ |
---|
| 208 | grok.order(6) |
---|
| 209 | grok.require('waeup.manageACBatches') |
---|
| 210 | |
---|
[5381] | 211 | link = u'accesscodes' |
---|
[5412] | 212 | text = u'Access Codes' |
---|