[7195] | 1 | ## $Id: browser.py 7321 2011-12-10 06:15:17Z henrik $ |
---|
| 2 | ## |
---|
| 3 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
| 4 | ## This program is free software; you can redistribute it and/or modify |
---|
| 5 | ## it under the terms of the GNU General Public License as published by |
---|
| 6 | ## the Free Software Foundation; either version 2 of the License, or |
---|
| 7 | ## (at your option) any later version. |
---|
| 8 | ## |
---|
| 9 | ## This program is distributed in the hope that it will be useful, |
---|
| 10 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 11 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 12 | ## GNU General Public License for more details. |
---|
| 13 | ## |
---|
| 14 | ## You should have received a copy of the GNU General Public License |
---|
| 15 | ## along with this program; if not, write to the Free Software |
---|
| 16 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 17 | ## |
---|
[5082] | 18 | """UI components for accesscodes. |
---|
| 19 | """ |
---|
| 20 | import grok |
---|
[5088] | 21 | from datetime import datetime |
---|
[6450] | 22 | from hurry.workflow.interfaces import InvalidTransitionError |
---|
[6455] | 23 | from waeup.sirp.browser.resources import datatable |
---|
[7321] | 24 | from waeup.sirp.browser import SIRPPage, SIRPAddFormPage |
---|
[5082] | 25 | from waeup.sirp.browser.breadcrumbs import Breadcrumb |
---|
[5129] | 26 | from waeup.sirp.browser.viewlets import ( |
---|
[5381] | 27 | AdminTask, AddActionButton, SearchActionButton, BatchOpButton, ManageLink) |
---|
[7321] | 28 | from waeup.sirp.interfaces import ISIRPObject |
---|
[5082] | 29 | |
---|
[5088] | 30 | from waeup.sirp.accesscodes.interfaces import ( |
---|
| 31 | IAccessCodeBatchContainer, IAccessCodeBatch, |
---|
| 32 | ) |
---|
[6450] | 33 | from waeup.sirp.accesscodes.catalog import search |
---|
[5082] | 34 | |
---|
[7321] | 35 | grok.context(ISIRPObject) |
---|
[5082] | 36 | |
---|
[7321] | 37 | class BatchContainerPage(SIRPPage): |
---|
[5082] | 38 | grok.name('index') |
---|
| 39 | grok.context(IAccessCodeBatchContainer) |
---|
| 40 | grok.template('batchcontainer') |
---|
[5104] | 41 | grok.require('waeup.manageACBatches') |
---|
[5082] | 42 | |
---|
| 43 | title = 'Access Code Batches' |
---|
| 44 | pnav = 0 |
---|
| 45 | |
---|
[5117] | 46 | def update(self, batches=None, archive=None, delete=None): |
---|
| 47 | if archive is None and delete is None: |
---|
| 48 | return |
---|
[5124] | 49 | if not batches: |
---|
| 50 | self.flash('No batch selected.') |
---|
| 51 | return |
---|
[5117] | 52 | if isinstance(batches, basestring): |
---|
| 53 | batches = [batches] |
---|
| 54 | for name in batches: |
---|
| 55 | batch = self.context[name] |
---|
| 56 | csv_file = batch.archive() |
---|
| 57 | self.flash('Archived %s (%s)' % (name, csv_file)) |
---|
| 58 | if delete is None: |
---|
| 59 | continue |
---|
| 60 | del self.context[name] |
---|
| 61 | self.flash('Deleted batch %s' % name) |
---|
[5104] | 62 | |
---|
[7321] | 63 | class AddBatchPage(SIRPAddFormPage): |
---|
[5084] | 64 | grok.name('add') |
---|
| 65 | grok.context(IAccessCodeBatchContainer) |
---|
[5104] | 66 | grok.require('waeup.manageACBatches') |
---|
[5084] | 67 | |
---|
[6450] | 68 | title = 'Create Access Code Batch' |
---|
[5084] | 69 | pnav = 0 |
---|
| 70 | |
---|
[5088] | 71 | form_fields = grok.AutoFields(IAccessCodeBatch).select( |
---|
[6417] | 72 | 'prefix', 'entry_num', 'cost') |
---|
[5088] | 73 | |
---|
| 74 | @grok.action('Cancel') |
---|
[5104] | 75 | def cancel(self, *args, **kw): |
---|
[5088] | 76 | self.flash('Batch creation cancelled.') |
---|
| 77 | self.redirect(self.url(self.context)) |
---|
| 78 | |
---|
[5084] | 79 | @grok.action('Create batch') |
---|
| 80 | def createBatch(self, **data): |
---|
[5088] | 81 | creator = self.request.principal.id |
---|
| 82 | creation_date = datetime.now() |
---|
| 83 | data.update(creation_date=creation_date, creator=creator) |
---|
[5126] | 84 | batch = self.context.createBatch(**data) |
---|
[5113] | 85 | csv_file = batch.createCSVLogFile() |
---|
[5088] | 86 | self.context._p_changed = True |
---|
| 87 | self.flash('Batch created (%s entries)' % data['entry_num']) |
---|
[5113] | 88 | self.flash('Data written to %s' % csv_file) |
---|
[5084] | 89 | self.redirect(self.url(self.context)) |
---|
| 90 | |
---|
[7321] | 91 | class ReimportBatchPage(SIRPPage): |
---|
[5132] | 92 | """Screen for reimporting AC batches. |
---|
| 93 | """ |
---|
[5130] | 94 | grok.name('reimport') |
---|
| 95 | grok.context(IAccessCodeBatchContainer) |
---|
| 96 | grok.template('reimportbatchpage') |
---|
| 97 | grok.require('waeup.manageACBatches') |
---|
| 98 | |
---|
| 99 | title = 'Reimport Access Code Batches' |
---|
| 100 | pnav = 0 |
---|
| 101 | |
---|
[5132] | 102 | def update(self, filenames=None, reimport=None, cancel=None): |
---|
| 103 | if cancel is not None: |
---|
| 104 | self.flash('Reimport cancelled.') |
---|
| 105 | self.redirect(self.url(self.context)) |
---|
| 106 | return |
---|
| 107 | if reimport is None: |
---|
| 108 | return |
---|
| 109 | if not filenames: |
---|
| 110 | self.flash('No file chosen. Action cancelled.') |
---|
| 111 | self.redirect(self.url(self.context)) |
---|
| 112 | return |
---|
| 113 | if isinstance(filenames, basestring): |
---|
| 114 | filenames = [filenames] |
---|
| 115 | userid = self.request.principal.id |
---|
| 116 | for filename in filenames: |
---|
| 117 | try: |
---|
| 118 | self.context.reimport(filename, userid) |
---|
| 119 | except KeyError: |
---|
| 120 | self.flash('This batch already exists: %s' % filename) |
---|
| 121 | continue |
---|
| 122 | self.flash('Successfully reimported: %s' % filename) |
---|
| 123 | self.redirect(self.url(self.context)) |
---|
[6388] | 124 | |
---|
[7321] | 125 | class BatchContainerSearchPage(SIRPPage): |
---|
[5155] | 126 | grok.name('search') |
---|
| 127 | grok.context(IAccessCodeBatchContainer) |
---|
| 128 | grok.template('searchpage') |
---|
| 129 | grok.require('waeup.manageACBatches') |
---|
| 130 | |
---|
[6450] | 131 | title = 'Access Codes' |
---|
[5155] | 132 | pnav = 0 |
---|
[6450] | 133 | label = 'Search and manage access codes' |
---|
[5155] | 134 | |
---|
[6450] | 135 | def update(self, *args, **kw): |
---|
[6455] | 136 | datatable.need() |
---|
[6450] | 137 | form = self.request.form |
---|
| 138 | self.hitlist = [] |
---|
| 139 | if 'searchterm' in form and form['searchterm']: |
---|
| 140 | self.searchterm = form['searchterm'] |
---|
| 141 | self.searchtype = form['searchtype'] |
---|
| 142 | elif 'old_searchterm' in form: |
---|
| 143 | self.searchterm = form['old_searchterm'] |
---|
| 144 | self.searchtype = form['old_searchtype'] |
---|
| 145 | else: |
---|
[5155] | 146 | return |
---|
[6450] | 147 | #import pdb; pdb.set_trace() |
---|
| 148 | if not 'entries' in form: |
---|
| 149 | self.hitlist = search(query=self.searchterm, |
---|
| 150 | searchtype=self.searchtype, view=self) |
---|
| 151 | return |
---|
| 152 | entries = form['entries'] |
---|
[5155] | 153 | if isinstance(entries, basestring): |
---|
| 154 | entries = [entries] |
---|
| 155 | for entry in entries: |
---|
[6450] | 156 | if 'disable' in form: |
---|
| 157 | try: |
---|
[6471] | 158 | comment = u"AC disabled" |
---|
[6459] | 159 | self.context.disable(entry, comment) |
---|
[6450] | 160 | self.flash('%s disabled.' % entry) |
---|
| 161 | except InvalidTransitionError: |
---|
[6451] | 162 | self.flash('%s: Disable transition not allowed.' % entry) |
---|
[6450] | 163 | elif 'enable' in form: |
---|
| 164 | try: |
---|
[6471] | 165 | comment = u"AC re-enabled" |
---|
[6459] | 166 | self.context.enable(entry, comment) |
---|
[6450] | 167 | self.flash('%s (re-)enabled.' % entry) |
---|
| 168 | except InvalidTransitionError: |
---|
[6451] | 169 | self.flash('%s: Re-enable transition not allowed.' % entry) |
---|
[6450] | 170 | self.hitlist = search(query=self.searchterm, |
---|
| 171 | searchtype=self.searchtype, view=self) |
---|
[5155] | 172 | return |
---|
| 173 | |
---|
[5082] | 174 | class BatchContainerBreadcrumb(Breadcrumb): |
---|
| 175 | """A breadcrumb for ac batch containers. |
---|
[5104] | 176 | """ |
---|
| 177 | grok.require('waeup.manageACBatches') |
---|
[5082] | 178 | grok.context(IAccessCodeBatchContainer) |
---|
| 179 | title = u'Access Code Batches' |
---|
| 180 | parent_viewname = 'administration' |
---|
| 181 | |
---|
[5155] | 182 | class BatchContainerSearchBreadcrumb(Breadcrumb): |
---|
| 183 | """A breadcrumb for ac batch containers search page. |
---|
| 184 | """ |
---|
| 185 | grok.require('waeup.manageACBatches') |
---|
| 186 | grok.context(IAccessCodeBatchContainer) |
---|
| 187 | grok.name('search') |
---|
[6450] | 188 | title = u'Search Access Codes' |
---|
[5155] | 189 | viewname = 'search' |
---|
| 190 | parent_viewname = 'index' |
---|
| 191 | |
---|
[5082] | 192 | class AdminTaskManageACBatches(AdminTask): |
---|
| 193 | """Entry on administration page that links to batch container. |
---|
| 194 | """ |
---|
| 195 | grok.order(5) |
---|
[5104] | 196 | grok.require('waeup.manageACBatches') |
---|
[5082] | 197 | grok.template('admintaskacbatches') |
---|
| 198 | |
---|
[5412] | 199 | link_title = 'Access Code Batches' |
---|
[5082] | 200 | target_viewname = 'accesscodes' |
---|
[5084] | 201 | |
---|
| 202 | class CreateBatchButton(AddActionButton): |
---|
| 203 | """Action button on batch container page which links to batch creation. |
---|
| 204 | """ |
---|
| 205 | grok.context(IAccessCodeBatchContainer) |
---|
| 206 | grok.view(BatchContainerPage) |
---|
[5104] | 207 | grok.require('waeup.manageACBatches') |
---|
[6450] | 208 | text = 'Add Access Code Batch' |
---|
[5104] | 209 | |
---|
[5129] | 210 | class ReimportBatchButton(BatchOpButton): |
---|
| 211 | """Action button on batch container page which links to batch reimport. |
---|
| 212 | """ |
---|
| 213 | grok.context(IAccessCodeBatchContainer) |
---|
| 214 | grok.view(BatchContainerPage) |
---|
| 215 | grok.require('waeup.manageACBatches') |
---|
| 216 | target = 'reimport' |
---|
[6450] | 217 | text = 'Reimport Access Code Batch' |
---|
[5129] | 218 | |
---|
[5155] | 219 | class SearchAccessCodeButton(SearchActionButton): |
---|
| 220 | """Action button on batch container page which links to search. |
---|
| 221 | """ |
---|
| 222 | grok.context(IAccessCodeBatchContainer) |
---|
| 223 | grok.view(BatchContainerPage) |
---|
| 224 | grok.require('waeup.manageACBatches') |
---|
[6450] | 225 | text = 'Search Access Codes' |
---|
[6388] | 226 | |
---|
[5381] | 227 | class ManageAccessCodes(ManageLink): |
---|
| 228 | """Link in upper left box to access code management. |
---|
[5104] | 229 | """ |
---|
| 230 | grok.order(6) |
---|
| 231 | grok.require('waeup.manageACBatches') |
---|
| 232 | |
---|
[5381] | 233 | link = u'accesscodes' |
---|
[5412] | 234 | text = u'Access Codes' |
---|