[11958] | 1 | ## $Id: browser.py 11862 2014-10-21 07:07:04Z henrik $ |
---|
| 2 | ## |
---|
| 3 | ## Copyright (C) 2014 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 | ## |
---|
| 18 | """UI components for customers and related components. |
---|
| 19 | """ |
---|
| 20 | |
---|
| 21 | import grok |
---|
| 22 | from waeup.ikoba.interfaces import MessageFactory as _ |
---|
| 23 | from waeup.ikoba.browser.layout import ( |
---|
| 24 | IkobaPage, IkobaEditFormPage, IkobaAddFormPage, IkobaDisplayFormPage, |
---|
| 25 | IkobaForm, NullValidator) |
---|
| 26 | from waeup.ikoba.browser.breadcrumbs import Breadcrumb |
---|
| 27 | from waeup.ikoba.utils.helpers import get_current_principal, to_timezone, now |
---|
| 28 | from waeup.ikoba.customers.interfaces import ( |
---|
| 29 | ICustomersContainer |
---|
| 30 | ) |
---|
| 31 | from waeup.ikoba.customers.catalog import search |
---|
| 32 | |
---|
| 33 | class CustomersBreadcrumb(Breadcrumb): |
---|
| 34 | """A breadcrumb for the customers container. |
---|
| 35 | """ |
---|
| 36 | grok.context(ICustomersContainer) |
---|
| 37 | title = _('Customers') |
---|
| 38 | |
---|
| 39 | @property |
---|
| 40 | def target(self): |
---|
| 41 | user = get_current_principal() |
---|
| 42 | if getattr(user, 'user_type', None) == 'customer': |
---|
| 43 | return None |
---|
| 44 | return self.viewname |
---|
| 45 | |
---|
| 46 | class CustomersContainerPage(IkobaPage): |
---|
| 47 | """The standard view for customer containers. |
---|
| 48 | """ |
---|
| 49 | grok.context(ICustomersContainer) |
---|
| 50 | grok.name('index') |
---|
| 51 | grok.require('waeup.viewCustomersContainer') |
---|
| 52 | grok.template('containerpage') |
---|
| 53 | label = _('Find customers') |
---|
| 54 | search_button = _('Find customer(s)') |
---|
| 55 | pnav = 4 |
---|
| 56 | |
---|
| 57 | def update(self, *args, **kw): |
---|
| 58 | form = self.request.form |
---|
| 59 | self.hitlist = [] |
---|
| 60 | if form.get('searchtype', None) == 'suspended': |
---|
| 61 | self.searchtype = form['searchtype'] |
---|
| 62 | self.searchterm = None |
---|
| 63 | elif 'searchterm' in form and form['searchterm']: |
---|
| 64 | self.searchterm = form['searchterm'] |
---|
| 65 | self.searchtype = form['searchtype'] |
---|
| 66 | elif 'old_searchterm' in form: |
---|
| 67 | self.searchterm = form['old_searchterm'] |
---|
| 68 | self.searchtype = form['old_searchtype'] |
---|
| 69 | else: |
---|
| 70 | if 'search' in form: |
---|
| 71 | self.flash(_('Empty search string'), type="warning") |
---|
| 72 | return |
---|
| 73 | if self.searchtype == 'current_session': |
---|
| 74 | try: |
---|
| 75 | self.searchterm = int(self.searchterm) |
---|
| 76 | except ValueError: |
---|
| 77 | self.flash(_('Only year dates allowed (e.g. 2011).'), |
---|
| 78 | type="danger") |
---|
| 79 | return |
---|
| 80 | self.hitlist = search(query=self.searchterm, |
---|
| 81 | searchtype=self.searchtype, view=self) |
---|
| 82 | if not self.hitlist: |
---|
| 83 | self.flash(_('No customer found.'), type="warning") |
---|
| 84 | return |
---|
| 85 | |
---|
| 86 | class CustomersContainerManagePage(IkobaPage): |
---|
| 87 | """The manage page for customer containers. |
---|
| 88 | """ |
---|
| 89 | grok.context(ICustomersContainer) |
---|
| 90 | grok.name('manage') |
---|
| 91 | grok.require('waeup.manageCustomer') |
---|
| 92 | grok.template('containermanagepage') |
---|
| 93 | pnav = 4 |
---|
| 94 | label = _('Manage customer section') |
---|
| 95 | search_button = _('Find customer(s)') |
---|
| 96 | remove_button = _('Remove selected') |
---|
| 97 | |
---|
| 98 | def update(self, *args, **kw): |
---|
| 99 | form = self.request.form |
---|
| 100 | self.hitlist = [] |
---|
| 101 | if form.get('searchtype', None) == 'suspended': |
---|
| 102 | self.searchtype = form['searchtype'] |
---|
| 103 | self.searchterm = None |
---|
| 104 | elif 'searchterm' in form and form['searchterm']: |
---|
| 105 | self.searchterm = form['searchterm'] |
---|
| 106 | self.searchtype = form['searchtype'] |
---|
| 107 | elif 'old_searchterm' in form: |
---|
| 108 | self.searchterm = form['old_searchterm'] |
---|
| 109 | self.searchtype = form['old_searchtype'] |
---|
| 110 | else: |
---|
| 111 | if 'search' in form: |
---|
| 112 | self.flash(_('Empty search string'), type="warning") |
---|
| 113 | return |
---|
| 114 | if self.searchtype == 'current_session': |
---|
| 115 | try: |
---|
| 116 | self.searchterm = int(self.searchterm) |
---|
| 117 | except ValueError: |
---|
| 118 | self.flash(_('Only year dates allowed (e.g. 2011).'), |
---|
| 119 | type="danger") |
---|
| 120 | return |
---|
| 121 | if not 'entries' in form: |
---|
| 122 | self.hitlist = search(query=self.searchterm, |
---|
| 123 | searchtype=self.searchtype, view=self) |
---|
| 124 | if not self.hitlist: |
---|
| 125 | self.flash(_('No customer found.'), type="warning") |
---|
| 126 | if 'remove' in form: |
---|
| 127 | self.flash(_('No item selected.'), type="warning") |
---|
| 128 | return |
---|
| 129 | entries = form['entries'] |
---|
| 130 | if isinstance(entries, basestring): |
---|
| 131 | entries = [entries] |
---|
| 132 | deleted = [] |
---|
| 133 | for entry in entries: |
---|
| 134 | if 'remove' in form: |
---|
| 135 | del self.context[entry] |
---|
| 136 | deleted.append(entry) |
---|
| 137 | self.hitlist = search(query=self.searchterm, |
---|
| 138 | searchtype=self.searchtype, view=self) |
---|
| 139 | if len(deleted): |
---|
| 140 | self.flash(_('Successfully removed: ${a}', |
---|
| 141 | mapping = {'a':', '.join(deleted)})) |
---|
| 142 | return |
---|