[5082] | 1 | """UI components for accesscodes. |
---|
| 2 | """ |
---|
| 3 | import grok |
---|
[5088] | 4 | from datetime import datetime |
---|
[6450] | 5 | from hurry.workflow.interfaces import InvalidTransitionError |
---|
[5084] | 6 | from waeup.sirp.browser import WAeUPPage, WAeUPAddFormPage |
---|
[5082] | 7 | from waeup.sirp.browser.breadcrumbs import Breadcrumb |
---|
[5129] | 8 | from waeup.sirp.browser.viewlets import ( |
---|
[5381] | 9 | AdminTask, AddActionButton, SearchActionButton, BatchOpButton, ManageLink) |
---|
[6388] | 10 | from waeup.sirp.interfaces import IWAeUPObject |
---|
[5082] | 11 | |
---|
[5088] | 12 | from waeup.sirp.accesscodes.interfaces import ( |
---|
| 13 | IAccessCodeBatchContainer, IAccessCodeBatch, |
---|
| 14 | ) |
---|
[6450] | 15 | from waeup.sirp.accesscodes.catalog import search |
---|
[5082] | 16 | |
---|
| 17 | grok.context(IWAeUPObject) |
---|
| 18 | |
---|
| 19 | class BatchContainerPage(WAeUPPage): |
---|
| 20 | grok.name('index') |
---|
| 21 | grok.context(IAccessCodeBatchContainer) |
---|
| 22 | grok.template('batchcontainer') |
---|
[5104] | 23 | grok.require('waeup.manageACBatches') |
---|
[5082] | 24 | |
---|
| 25 | title = 'Access Code Batches' |
---|
| 26 | pnav = 0 |
---|
| 27 | |
---|
[5117] | 28 | def update(self, batches=None, archive=None, delete=None): |
---|
| 29 | if archive is None and delete is None: |
---|
| 30 | return |
---|
[5124] | 31 | if not batches: |
---|
| 32 | self.flash('No batch selected.') |
---|
| 33 | return |
---|
[5117] | 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) |
---|
[5104] | 44 | |
---|
[5084] | 45 | class AddBatchPage(WAeUPAddFormPage): |
---|
| 46 | grok.name('add') |
---|
| 47 | grok.context(IAccessCodeBatchContainer) |
---|
[5104] | 48 | grok.require('waeup.manageACBatches') |
---|
[5084] | 49 | |
---|
[6450] | 50 | title = 'Create Access Code Batch' |
---|
[5084] | 51 | pnav = 0 |
---|
| 52 | |
---|
[5088] | 53 | form_fields = grok.AutoFields(IAccessCodeBatch).select( |
---|
[6417] | 54 | 'prefix', 'entry_num', 'cost') |
---|
[5088] | 55 | |
---|
| 56 | @grok.action('Cancel') |
---|
[5104] | 57 | def cancel(self, *args, **kw): |
---|
[5088] | 58 | self.flash('Batch creation cancelled.') |
---|
| 59 | self.redirect(self.url(self.context)) |
---|
| 60 | |
---|
[5084] | 61 | @grok.action('Create batch') |
---|
| 62 | def createBatch(self, **data): |
---|
[5088] | 63 | creator = self.request.principal.id |
---|
| 64 | creation_date = datetime.now() |
---|
| 65 | data.update(creation_date=creation_date, creator=creator) |
---|
[5126] | 66 | batch = self.context.createBatch(**data) |
---|
[5113] | 67 | csv_file = batch.createCSVLogFile() |
---|
[5088] | 68 | self.context._p_changed = True |
---|
| 69 | self.flash('Batch created (%s entries)' % data['entry_num']) |
---|
[5113] | 70 | self.flash('Data written to %s' % csv_file) |
---|
[5084] | 71 | self.redirect(self.url(self.context)) |
---|
| 72 | |
---|
[5130] | 73 | class ReimportBatchPage(WAeUPPage): |
---|
[5132] | 74 | """Screen for reimporting AC batches. |
---|
| 75 | """ |
---|
[5130] | 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 | |
---|
[5132] | 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)) |
---|
[6388] | 106 | |
---|
[5155] | 107 | class BatchContainerSearchPage(WAeUPPage): |
---|
| 108 | grok.name('search') |
---|
| 109 | grok.context(IAccessCodeBatchContainer) |
---|
| 110 | grok.template('searchpage') |
---|
| 111 | grok.require('waeup.manageACBatches') |
---|
| 112 | |
---|
[6450] | 113 | title = 'Access Codes' |
---|
[5155] | 114 | pnav = 0 |
---|
[6450] | 115 | label = 'Search and manage access codes' |
---|
[5155] | 116 | |
---|
[6450] | 117 | def update(self, *args, **kw): |
---|
| 118 | form = self.request.form |
---|
| 119 | self.hitlist = [] |
---|
| 120 | if 'searchterm' in form and form['searchterm']: |
---|
| 121 | self.searchterm = form['searchterm'] |
---|
| 122 | self.searchtype = form['searchtype'] |
---|
| 123 | elif 'old_searchterm' in form: |
---|
| 124 | self.searchterm = form['old_searchterm'] |
---|
| 125 | self.searchtype = form['old_searchtype'] |
---|
| 126 | else: |
---|
[5155] | 127 | return |
---|
[6450] | 128 | #import pdb; pdb.set_trace() |
---|
| 129 | if not 'entries' in form: |
---|
| 130 | self.hitlist = search(query=self.searchterm, |
---|
| 131 | searchtype=self.searchtype, view=self) |
---|
| 132 | return |
---|
| 133 | entries = form['entries'] |
---|
[5155] | 134 | if isinstance(entries, basestring): |
---|
| 135 | entries = [entries] |
---|
| 136 | for entry in entries: |
---|
[6450] | 137 | if 'disable' in form: |
---|
| 138 | try: |
---|
| 139 | self.context.disable(entry, self.request.principal.id) |
---|
| 140 | self.flash('%s disabled.' % entry) |
---|
| 141 | except InvalidTransitionError: |
---|
[6451] | 142 | self.flash('%s: Disable transition not allowed.' % entry) |
---|
[6450] | 143 | elif 'enable' in form: |
---|
| 144 | try: |
---|
| 145 | self.context.enable(entry, self.request.principal.id) |
---|
| 146 | self.flash('%s (re-)enabled.' % entry) |
---|
| 147 | except InvalidTransitionError: |
---|
[6451] | 148 | self.flash('%s: Re-enable transition not allowed.' % entry) |
---|
[6450] | 149 | self.hitlist = search(query=self.searchterm, |
---|
| 150 | searchtype=self.searchtype, view=self) |
---|
[5155] | 151 | return |
---|
| 152 | |
---|
[5082] | 153 | class BatchContainerBreadcrumb(Breadcrumb): |
---|
| 154 | """A breadcrumb for ac batch containers. |
---|
[5104] | 155 | """ |
---|
| 156 | grok.require('waeup.manageACBatches') |
---|
[5082] | 157 | grok.context(IAccessCodeBatchContainer) |
---|
| 158 | title = u'Access Code Batches' |
---|
| 159 | parent_viewname = 'administration' |
---|
| 160 | |
---|
[5155] | 161 | class BatchContainerSearchBreadcrumb(Breadcrumb): |
---|
| 162 | """A breadcrumb for ac batch containers search page. |
---|
| 163 | """ |
---|
| 164 | grok.require('waeup.manageACBatches') |
---|
| 165 | grok.context(IAccessCodeBatchContainer) |
---|
| 166 | grok.name('search') |
---|
[6450] | 167 | title = u'Search Access Codes' |
---|
[5155] | 168 | viewname = 'search' |
---|
| 169 | parent_viewname = 'index' |
---|
| 170 | |
---|
[5082] | 171 | class AdminTaskManageACBatches(AdminTask): |
---|
| 172 | """Entry on administration page that links to batch container. |
---|
| 173 | """ |
---|
| 174 | grok.order(5) |
---|
[5104] | 175 | grok.require('waeup.manageACBatches') |
---|
[5082] | 176 | grok.template('admintaskacbatches') |
---|
| 177 | |
---|
[5412] | 178 | link_title = 'Access Code Batches' |
---|
[5082] | 179 | target_viewname = 'accesscodes' |
---|
[5084] | 180 | |
---|
| 181 | class CreateBatchButton(AddActionButton): |
---|
| 182 | """Action button on batch container page which links to batch creation. |
---|
| 183 | """ |
---|
| 184 | grok.context(IAccessCodeBatchContainer) |
---|
| 185 | grok.view(BatchContainerPage) |
---|
[5104] | 186 | grok.require('waeup.manageACBatches') |
---|
[6450] | 187 | text = 'Add Access Code Batch' |
---|
[5104] | 188 | |
---|
[5129] | 189 | class ReimportBatchButton(BatchOpButton): |
---|
| 190 | """Action button on batch container page which links to batch reimport. |
---|
| 191 | """ |
---|
| 192 | grok.context(IAccessCodeBatchContainer) |
---|
| 193 | grok.view(BatchContainerPage) |
---|
| 194 | grok.require('waeup.manageACBatches') |
---|
| 195 | target = 'reimport' |
---|
[6450] | 196 | text = 'Reimport Access Code Batch' |
---|
[5129] | 197 | |
---|
[5155] | 198 | class SearchAccessCodeButton(SearchActionButton): |
---|
| 199 | """Action button on batch container page which links to search. |
---|
| 200 | """ |
---|
| 201 | grok.context(IAccessCodeBatchContainer) |
---|
| 202 | grok.view(BatchContainerPage) |
---|
| 203 | grok.require('waeup.manageACBatches') |
---|
[6450] | 204 | text = 'Search Access Codes' |
---|
[6388] | 205 | |
---|
[5381] | 206 | class ManageAccessCodes(ManageLink): |
---|
| 207 | """Link in upper left box to access code management. |
---|
[5104] | 208 | """ |
---|
| 209 | grok.order(6) |
---|
| 210 | grok.require('waeup.manageACBatches') |
---|
| 211 | |
---|
[5381] | 212 | link = u'accesscodes' |
---|
[5412] | 213 | text = u'Access Codes' |
---|