[7195] | 1 | ## $Id: browser.py 7447 2012-01-10 21:38:44Z 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 |
---|
[7447] | 34 | from waeup.sirp.browser.layout import action |
---|
[5082] | 35 | |
---|
[7321] | 36 | grok.context(ISIRPObject) |
---|
[5082] | 37 | |
---|
[7321] | 38 | class BatchContainerPage(SIRPPage): |
---|
[5082] | 39 | grok.name('index') |
---|
| 40 | grok.context(IAccessCodeBatchContainer) |
---|
| 41 | grok.template('batchcontainer') |
---|
[5104] | 42 | grok.require('waeup.manageACBatches') |
---|
[5082] | 43 | |
---|
| 44 | title = 'Access Code Batches' |
---|
| 45 | pnav = 0 |
---|
| 46 | |
---|
[5117] | 47 | def update(self, batches=None, archive=None, delete=None): |
---|
| 48 | if archive is None and delete is None: |
---|
| 49 | return |
---|
[5124] | 50 | if not batches: |
---|
| 51 | self.flash('No batch selected.') |
---|
| 52 | return |
---|
[5117] | 53 | if isinstance(batches, basestring): |
---|
| 54 | batches = [batches] |
---|
| 55 | for name in batches: |
---|
| 56 | batch = self.context[name] |
---|
| 57 | csv_file = batch.archive() |
---|
| 58 | self.flash('Archived %s (%s)' % (name, csv_file)) |
---|
| 59 | if delete is None: |
---|
| 60 | continue |
---|
| 61 | del self.context[name] |
---|
| 62 | self.flash('Deleted batch %s' % name) |
---|
[5104] | 63 | |
---|
[7321] | 64 | class AddBatchPage(SIRPAddFormPage): |
---|
[5084] | 65 | grok.name('add') |
---|
| 66 | grok.context(IAccessCodeBatchContainer) |
---|
[5104] | 67 | grok.require('waeup.manageACBatches') |
---|
[5084] | 68 | |
---|
[7447] | 69 | label = 'Create Access Code Batch' |
---|
[5084] | 70 | pnav = 0 |
---|
| 71 | |
---|
[5088] | 72 | form_fields = grok.AutoFields(IAccessCodeBatch).select( |
---|
[6417] | 73 | 'prefix', 'entry_num', 'cost') |
---|
[5088] | 74 | |
---|
[7447] | 75 | @action('Cancel') |
---|
[5104] | 76 | def cancel(self, *args, **kw): |
---|
[5088] | 77 | self.flash('Batch creation cancelled.') |
---|
| 78 | self.redirect(self.url(self.context)) |
---|
| 79 | |
---|
[7447] | 80 | @action('Create batch') |
---|
[5084] | 81 | def createBatch(self, **data): |
---|
[5088] | 82 | creator = self.request.principal.id |
---|
| 83 | creation_date = datetime.now() |
---|
| 84 | data.update(creation_date=creation_date, creator=creator) |
---|
[5126] | 85 | batch = self.context.createBatch(**data) |
---|
[5113] | 86 | csv_file = batch.createCSVLogFile() |
---|
[5088] | 87 | self.context._p_changed = True |
---|
| 88 | self.flash('Batch created (%s entries)' % data['entry_num']) |
---|
[5113] | 89 | self.flash('Data written to %s' % csv_file) |
---|
[5084] | 90 | self.redirect(self.url(self.context)) |
---|
| 91 | |
---|
[7321] | 92 | class ReimportBatchPage(SIRPPage): |
---|
[5132] | 93 | """Screen for reimporting AC batches. |
---|
| 94 | """ |
---|
[5130] | 95 | grok.name('reimport') |
---|
| 96 | grok.context(IAccessCodeBatchContainer) |
---|
| 97 | grok.template('reimportbatchpage') |
---|
| 98 | grok.require('waeup.manageACBatches') |
---|
| 99 | |
---|
| 100 | title = 'Reimport Access Code Batches' |
---|
| 101 | pnav = 0 |
---|
| 102 | |
---|
[5132] | 103 | def update(self, filenames=None, reimport=None, cancel=None): |
---|
| 104 | if cancel is not None: |
---|
| 105 | self.flash('Reimport cancelled.') |
---|
| 106 | self.redirect(self.url(self.context)) |
---|
| 107 | return |
---|
| 108 | if reimport is None: |
---|
| 109 | return |
---|
| 110 | if not filenames: |
---|
| 111 | self.flash('No file chosen. Action cancelled.') |
---|
| 112 | self.redirect(self.url(self.context)) |
---|
| 113 | return |
---|
| 114 | if isinstance(filenames, basestring): |
---|
| 115 | filenames = [filenames] |
---|
| 116 | userid = self.request.principal.id |
---|
| 117 | for filename in filenames: |
---|
| 118 | try: |
---|
| 119 | self.context.reimport(filename, userid) |
---|
| 120 | except KeyError: |
---|
| 121 | self.flash('This batch already exists: %s' % filename) |
---|
| 122 | continue |
---|
| 123 | self.flash('Successfully reimported: %s' % filename) |
---|
| 124 | self.redirect(self.url(self.context)) |
---|
[6388] | 125 | |
---|
[7321] | 126 | class BatchContainerSearchPage(SIRPPage): |
---|
[5155] | 127 | grok.name('search') |
---|
| 128 | grok.context(IAccessCodeBatchContainer) |
---|
| 129 | grok.template('searchpage') |
---|
| 130 | grok.require('waeup.manageACBatches') |
---|
| 131 | |
---|
[6450] | 132 | title = 'Access Codes' |
---|
[5155] | 133 | pnav = 0 |
---|
[6450] | 134 | label = 'Search and manage access codes' |
---|
[5155] | 135 | |
---|
[6450] | 136 | def update(self, *args, **kw): |
---|
[6455] | 137 | datatable.need() |
---|
[6450] | 138 | form = self.request.form |
---|
| 139 | self.hitlist = [] |
---|
| 140 | if 'searchterm' in form and form['searchterm']: |
---|
| 141 | self.searchterm = form['searchterm'] |
---|
| 142 | self.searchtype = form['searchtype'] |
---|
| 143 | elif 'old_searchterm' in form: |
---|
| 144 | self.searchterm = form['old_searchterm'] |
---|
| 145 | self.searchtype = form['old_searchtype'] |
---|
| 146 | else: |
---|
[5155] | 147 | return |
---|
[6450] | 148 | #import pdb; pdb.set_trace() |
---|
| 149 | if not 'entries' in form: |
---|
| 150 | self.hitlist = search(query=self.searchterm, |
---|
| 151 | searchtype=self.searchtype, view=self) |
---|
| 152 | return |
---|
| 153 | entries = form['entries'] |
---|
[5155] | 154 | if isinstance(entries, basestring): |
---|
| 155 | entries = [entries] |
---|
| 156 | for entry in entries: |
---|
[6450] | 157 | if 'disable' in form: |
---|
| 158 | try: |
---|
[6471] | 159 | comment = u"AC disabled" |
---|
[6459] | 160 | self.context.disable(entry, comment) |
---|
[6450] | 161 | self.flash('%s disabled.' % entry) |
---|
| 162 | except InvalidTransitionError: |
---|
[6451] | 163 | self.flash('%s: Disable transition not allowed.' % entry) |
---|
[6450] | 164 | elif 'enable' in form: |
---|
| 165 | try: |
---|
[6471] | 166 | comment = u"AC re-enabled" |
---|
[6459] | 167 | self.context.enable(entry, comment) |
---|
[6450] | 168 | self.flash('%s (re-)enabled.' % entry) |
---|
| 169 | except InvalidTransitionError: |
---|
[6451] | 170 | self.flash('%s: Re-enable transition not allowed.' % entry) |
---|
[6450] | 171 | self.hitlist = search(query=self.searchterm, |
---|
| 172 | searchtype=self.searchtype, view=self) |
---|
[5155] | 173 | return |
---|
| 174 | |
---|
[5082] | 175 | class BatchContainerBreadcrumb(Breadcrumb): |
---|
| 176 | """A breadcrumb for ac batch containers. |
---|
[5104] | 177 | """ |
---|
| 178 | grok.require('waeup.manageACBatches') |
---|
[5082] | 179 | grok.context(IAccessCodeBatchContainer) |
---|
| 180 | title = u'Access Code Batches' |
---|
| 181 | parent_viewname = 'administration' |
---|
| 182 | |
---|
[5155] | 183 | class BatchContainerSearchBreadcrumb(Breadcrumb): |
---|
| 184 | """A breadcrumb for ac batch containers search page. |
---|
| 185 | """ |
---|
| 186 | grok.require('waeup.manageACBatches') |
---|
| 187 | grok.context(IAccessCodeBatchContainer) |
---|
| 188 | grok.name('search') |
---|
[6450] | 189 | title = u'Search Access Codes' |
---|
[5155] | 190 | viewname = 'search' |
---|
| 191 | parent_viewname = 'index' |
---|
| 192 | |
---|
[5082] | 193 | class AdminTaskManageACBatches(AdminTask): |
---|
| 194 | """Entry on administration page that links to batch container. |
---|
| 195 | """ |
---|
| 196 | grok.order(5) |
---|
[5104] | 197 | grok.require('waeup.manageACBatches') |
---|
[5082] | 198 | grok.template('admintaskacbatches') |
---|
| 199 | |
---|
[5412] | 200 | link_title = 'Access Code Batches' |
---|
[5082] | 201 | target_viewname = 'accesscodes' |
---|
[5084] | 202 | |
---|
| 203 | class CreateBatchButton(AddActionButton): |
---|
| 204 | """Action button on batch container page which links to batch creation. |
---|
| 205 | """ |
---|
| 206 | grok.context(IAccessCodeBatchContainer) |
---|
| 207 | grok.view(BatchContainerPage) |
---|
[5104] | 208 | grok.require('waeup.manageACBatches') |
---|
[6450] | 209 | text = 'Add Access Code Batch' |
---|
[5104] | 210 | |
---|
[5129] | 211 | class ReimportBatchButton(BatchOpButton): |
---|
| 212 | """Action button on batch container page which links to batch reimport. |
---|
| 213 | """ |
---|
| 214 | grok.context(IAccessCodeBatchContainer) |
---|
| 215 | grok.view(BatchContainerPage) |
---|
| 216 | grok.require('waeup.manageACBatches') |
---|
| 217 | target = 'reimport' |
---|
[6450] | 218 | text = 'Reimport Access Code Batch' |
---|
[5129] | 219 | |
---|
[5155] | 220 | class SearchAccessCodeButton(SearchActionButton): |
---|
| 221 | """Action button on batch container page which links to search. |
---|
| 222 | """ |
---|
| 223 | grok.context(IAccessCodeBatchContainer) |
---|
| 224 | grok.view(BatchContainerPage) |
---|
| 225 | grok.require('waeup.manageACBatches') |
---|
[6450] | 226 | text = 'Search Access Codes' |
---|
[6388] | 227 | |
---|
[5381] | 228 | class ManageAccessCodes(ManageLink): |
---|
| 229 | """Link in upper left box to access code management. |
---|
[5104] | 230 | """ |
---|
| 231 | grok.order(6) |
---|
| 232 | grok.require('waeup.manageACBatches') |
---|
| 233 | |
---|
[5381] | 234 | link = u'accesscodes' |
---|
[5412] | 235 | text = u'Access Codes' |
---|