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