[7195] | 1 | ## $Id: browser.py 7719 2012-02-28 20:52:18Z 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 |
---|
| 86 | self.flash('Batch created (%s entries)' % data['entry_num']) |
---|
[5113] | 87 | self.flash('Data written to %s' % csv_file) |
---|
[5084] | 88 | self.redirect(self.url(self.context)) |
---|
| 89 | |
---|
[7719] | 90 | @action(_('Cancel'), validator=NullValidator) |
---|
[7488] | 91 | def cancel(self, *args, **kw): |
---|
[7719] | 92 | self.flash(_('Batch creation cancelled.')) |
---|
[7488] | 93 | self.redirect(self.url(self.context)) |
---|
| 94 | |
---|
[7321] | 95 | class ReimportBatchPage(SIRPPage): |
---|
[5132] | 96 | """Screen for reimporting AC batches. |
---|
| 97 | """ |
---|
[5130] | 98 | grok.name('reimport') |
---|
| 99 | grok.context(IAccessCodeBatchContainer) |
---|
| 100 | grok.template('reimportbatchpage') |
---|
| 101 | grok.require('waeup.manageACBatches') |
---|
[7719] | 102 | reimport_button = _('Reimport') |
---|
| 103 | cancel_button = _('Cancel') |
---|
[5130] | 104 | |
---|
[7719] | 105 | label = _('Reimport Access Code Batches') |
---|
[5130] | 106 | pnav = 0 |
---|
| 107 | |
---|
[5132] | 108 | def update(self, filenames=None, reimport=None, cancel=None): |
---|
| 109 | if cancel is not None: |
---|
[7719] | 110 | self.flash(_('Reimport cancelled.')) |
---|
[5132] | 111 | self.redirect(self.url(self.context)) |
---|
| 112 | return |
---|
| 113 | if reimport is None: |
---|
| 114 | return |
---|
| 115 | if not filenames: |
---|
[7719] | 116 | self.flash(_('No file chosen. Action cancelled.')) |
---|
[5132] | 117 | self.redirect(self.url(self.context)) |
---|
| 118 | return |
---|
| 119 | if isinstance(filenames, basestring): |
---|
| 120 | filenames = [filenames] |
---|
| 121 | userid = self.request.principal.id |
---|
| 122 | for filename in filenames: |
---|
| 123 | try: |
---|
| 124 | self.context.reimport(filename, userid) |
---|
| 125 | except KeyError: |
---|
[7719] | 126 | self.flash(_('This batch already exists: ${a}', |
---|
| 127 | mapping = {'a':filename})) |
---|
[5132] | 128 | continue |
---|
[7719] | 129 | self.flash(_('Successfully reimported: ${a}', |
---|
| 130 | mapping = {'a':filename})) |
---|
[5132] | 131 | self.redirect(self.url(self.context)) |
---|
[6388] | 132 | |
---|
[7321] | 133 | class BatchContainerSearchPage(SIRPPage): |
---|
[5155] | 134 | grok.name('search') |
---|
| 135 | grok.context(IAccessCodeBatchContainer) |
---|
| 136 | grok.template('searchpage') |
---|
| 137 | grok.require('waeup.manageACBatches') |
---|
| 138 | pnav = 0 |
---|
[7719] | 139 | label = _('Search and Manage Access Codes') |
---|
| 140 | search_button = _('Search') |
---|
| 141 | disable_button = _('Disable ACs') |
---|
| 142 | enable_button = _('Enable ACs') |
---|
[5155] | 143 | |
---|
[6450] | 144 | def update(self, *args, **kw): |
---|
[6455] | 145 | datatable.need() |
---|
[6450] | 146 | form = self.request.form |
---|
| 147 | self.hitlist = [] |
---|
| 148 | if 'searchterm' in form and form['searchterm']: |
---|
| 149 | self.searchterm = form['searchterm'] |
---|
| 150 | self.searchtype = form['searchtype'] |
---|
| 151 | elif 'old_searchterm' in form: |
---|
| 152 | self.searchterm = form['old_searchterm'] |
---|
| 153 | self.searchtype = form['old_searchtype'] |
---|
| 154 | else: |
---|
[5155] | 155 | return |
---|
[6450] | 156 | if not 'entries' in form: |
---|
| 157 | self.hitlist = search(query=self.searchterm, |
---|
| 158 | searchtype=self.searchtype, view=self) |
---|
| 159 | return |
---|
| 160 | entries = form['entries'] |
---|
[5155] | 161 | if isinstance(entries, basestring): |
---|
| 162 | entries = [entries] |
---|
| 163 | for entry in entries: |
---|
[6450] | 164 | if 'disable' in form: |
---|
| 165 | try: |
---|
[7719] | 166 | comment = _(u"AC disabled") |
---|
[6459] | 167 | self.context.disable(entry, comment) |
---|
[7719] | 168 | self.flash(_('${a} disabled.', mapping = {'a':entry})) |
---|
[6450] | 169 | except InvalidTransitionError: |
---|
[7719] | 170 | self.flash(_('${a}: Disable transition not allowed.', |
---|
| 171 | mapping = {'a':entry})) |
---|
[6450] | 172 | elif 'enable' in form: |
---|
| 173 | try: |
---|
[7719] | 174 | comment = _(u"AC re-enabled") |
---|
[6459] | 175 | self.context.enable(entry, comment) |
---|
[7719] | 176 | self.flash(_('${a} (re-)enabled.', mapping = {'a':entry})) |
---|
[6450] | 177 | except InvalidTransitionError: |
---|
[7719] | 178 | self.flash(_('${a}: Re-enable transition not allowed.', mapping = {'a':entry})) |
---|
[6450] | 179 | self.hitlist = search(query=self.searchterm, |
---|
| 180 | searchtype=self.searchtype, view=self) |
---|
[5155] | 181 | return |
---|
| 182 | |
---|
[5082] | 183 | class BatchContainerBreadcrumb(Breadcrumb): |
---|
| 184 | """A breadcrumb for ac batch containers. |
---|
[5104] | 185 | """ |
---|
| 186 | grok.require('waeup.manageACBatches') |
---|
[5082] | 187 | grok.context(IAccessCodeBatchContainer) |
---|
[7719] | 188 | title = _(u'Access Code Batches') |
---|
[5082] | 189 | parent_viewname = 'administration' |
---|
| 190 | |
---|
[7488] | 191 | #class BatchContainerSearchBreadcrumb(Breadcrumb): |
---|
| 192 | # """A breadcrumb for ac batch containers search page. |
---|
| 193 | # """ |
---|
| 194 | # grok.require('waeup.manageACBatches') |
---|
| 195 | # grok.context(IAccessCodeBatchContainer) |
---|
| 196 | # grok.name('search') |
---|
| 197 | # title = u'Search Access Codes' |
---|
| 198 | # viewname = 'search' |
---|
| 199 | # parent_viewname = 'index' |
---|
[5155] | 200 | |
---|
[5082] | 201 | class AdminTaskManageACBatches(AdminTask): |
---|
| 202 | """Entry on administration page that links to batch container. |
---|
| 203 | """ |
---|
| 204 | grok.order(5) |
---|
[5104] | 205 | grok.require('waeup.manageACBatches') |
---|
[5082] | 206 | grok.template('admintaskacbatches') |
---|
| 207 | |
---|
[7719] | 208 | link_title = _('Access Code Batches') |
---|
[5082] | 209 | target_viewname = 'accesscodes' |
---|
[5084] | 210 | |
---|
| 211 | class CreateBatchButton(AddActionButton): |
---|
| 212 | """Action button on batch container page which links to batch creation. |
---|
| 213 | """ |
---|
| 214 | grok.context(IAccessCodeBatchContainer) |
---|
| 215 | grok.view(BatchContainerPage) |
---|
[5104] | 216 | grok.require('waeup.manageACBatches') |
---|
[7719] | 217 | text = _('Add Access Code Batch') |
---|
[5104] | 218 | |
---|
[5129] | 219 | class ReimportBatchButton(BatchOpButton): |
---|
| 220 | """Action button on batch container page which links to batch reimport. |
---|
| 221 | """ |
---|
| 222 | grok.context(IAccessCodeBatchContainer) |
---|
| 223 | grok.view(BatchContainerPage) |
---|
| 224 | grok.require('waeup.manageACBatches') |
---|
| 225 | target = 'reimport' |
---|
[7719] | 226 | text = _('Reimport Access Code Batch') |
---|
[5129] | 227 | |
---|
[5155] | 228 | class SearchAccessCodeButton(SearchActionButton): |
---|
| 229 | """Action button on batch container page which links to search. |
---|
| 230 | """ |
---|
| 231 | grok.context(IAccessCodeBatchContainer) |
---|
| 232 | grok.view(BatchContainerPage) |
---|
| 233 | grok.require('waeup.manageACBatches') |
---|
[7719] | 234 | text = _('Search Access Codes') |
---|
[6388] | 235 | |
---|
[5381] | 236 | class ManageAccessCodes(ManageLink): |
---|
| 237 | """Link in upper left box to access code management. |
---|
[5104] | 238 | """ |
---|
| 239 | grok.order(6) |
---|
| 240 | grok.require('waeup.manageACBatches') |
---|
| 241 | |
---|
[5381] | 242 | link = u'accesscodes' |
---|
[7719] | 243 | text = _(u'Access Codes') |
---|