[12092] | 1 | ## $Id: vocabularies.py 12127 2014-12-03 18:09:28Z henrik $ |
---|
[11958] | 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 | """Vocabularies and sources for the customer section. |
---|
| 19 | """ |
---|
[12092] | 20 | import grok |
---|
[11958] | 21 | from zope.component import getUtility, queryUtility |
---|
| 22 | from zope.catalog.interfaces import ICatalog |
---|
| 23 | from zope.interface import implements, directlyProvides |
---|
| 24 | from zope.schema.interfaces import ISource, IContextSourceBinder |
---|
| 25 | from zope.schema.interfaces import ValidationError |
---|
| 26 | from zc.sourcefactory.basic import BasicSourceFactory |
---|
| 27 | from zc.sourcefactory.contextual import BasicContextualSourceFactory |
---|
[12127] | 28 | from waeup.ikoba.sourcefactory import SmartBasicContextualSourceFactory |
---|
[12100] | 29 | from waeup.ikoba.interfaces import SimpleIkobaVocabulary, SUBMITTED, VERIFIED |
---|
[11958] | 30 | from waeup.ikoba.interfaces import MessageFactory as _ |
---|
| 31 | from waeup.ikoba.utils.helpers import get_sorted_preferred |
---|
| 32 | from waeup.ikoba.utils.countries import COUNTRIES |
---|
| 33 | |
---|
| 34 | #: a tuple of tuples (<COUNTRY-NAME>, <ISO-CODE>) with Nigeria first. |
---|
| 35 | COUNTRIES = get_sorted_preferred(COUNTRIES, ['NG']) |
---|
| 36 | nats_vocab = SimpleIkobaVocabulary(*COUNTRIES) |
---|
| 37 | |
---|
| 38 | |
---|
| 39 | class GenderSource(BasicSourceFactory): |
---|
| 40 | """A gender source delivers basically a mapping |
---|
| 41 | ``{'m': 'Male', 'f': 'Female'}`` |
---|
| 42 | |
---|
| 43 | Using a source, we make sure that the tokens (which are |
---|
| 44 | stored/expected for instance from CSV files) are something one |
---|
| 45 | can expect and not cryptic IntIDs. |
---|
| 46 | """ |
---|
| 47 | def getValues(self): |
---|
| 48 | return ['m', 'f'] |
---|
| 49 | |
---|
| 50 | def getToken(self, value): |
---|
| 51 | return value[0].lower() |
---|
| 52 | |
---|
| 53 | def getTitle(self, value): |
---|
| 54 | if value == 'm': |
---|
| 55 | return _('male') |
---|
| 56 | if value == 'f': |
---|
| 57 | return _('female') |
---|
| 58 | |
---|
[11985] | 59 | |
---|
[11958] | 60 | class RegNumNotInSource(ValidationError): |
---|
| 61 | """Registration number exists already |
---|
| 62 | """ |
---|
| 63 | # The docstring of ValidationErrors is used as error description |
---|
| 64 | # by zope.formlib. |
---|
| 65 | pass |
---|
| 66 | |
---|
[11985] | 67 | |
---|
[11958] | 68 | class RegNumberSource(object): |
---|
| 69 | """A source that accepts any entry for a certain field if not used |
---|
| 70 | already. |
---|
| 71 | |
---|
| 72 | Using this kind of source means a way of setting an invariant. |
---|
| 73 | |
---|
| 74 | We accept a value iff: |
---|
| 75 | - the value cannot be found in catalog or |
---|
| 76 | - the value can be found as part of some item but the bound item |
---|
| 77 | is the context object itself. |
---|
| 78 | """ |
---|
| 79 | implements(ISource) |
---|
| 80 | cat_name = 'customers_catalog' |
---|
| 81 | field_name = 'reg_number' |
---|
| 82 | validation_error = RegNumNotInSource |
---|
| 83 | comp_field = 'customer_id' |
---|
[11985] | 84 | |
---|
[11958] | 85 | def __init__(self, context): |
---|
| 86 | self.context = context |
---|
| 87 | return |
---|
| 88 | |
---|
| 89 | def __contains__(self, value): |
---|
| 90 | """We accept all values not already given to other customers. |
---|
| 91 | """ |
---|
| 92 | cat = queryUtility(ICatalog, self.cat_name) |
---|
| 93 | if cat is None: |
---|
| 94 | return True |
---|
| 95 | kw = {self.field_name: (value, value)} |
---|
| 96 | results = cat.searchResults(**kw) |
---|
| 97 | for entry in results: |
---|
| 98 | if not hasattr(self.context, self.comp_field): |
---|
| 99 | # we have no context with comp_field (most probably |
---|
| 100 | # while adding a new object, where the container is |
---|
| 101 | # the context) which means that the value was given |
---|
| 102 | # already to another object (as _something_ was found in |
---|
| 103 | # the catalog with that value). Fail on first round. |
---|
| 104 | raise self.validation_error(value) |
---|
| 105 | if getattr(entry, self.comp_field) != getattr( |
---|
| 106 | self.context, self.comp_field): |
---|
| 107 | # An entry already given to another customer is not in our |
---|
| 108 | # range of acceptable values. |
---|
| 109 | raise self.validation_error(value) |
---|
| 110 | #return False |
---|
| 111 | return True |
---|
| 112 | |
---|
[11985] | 113 | |
---|
[11958] | 114 | def contextual_reg_num_source(context): |
---|
| 115 | source = RegNumberSource(context) |
---|
| 116 | return source |
---|
[11985] | 117 | |
---|
[11958] | 118 | directlyProvides(contextual_reg_num_source, IContextSourceBinder) |
---|
[12092] | 119 | |
---|
| 120 | |
---|
[12100] | 121 | class ConCatProductSource(BasicContextualSourceFactory): |
---|
| 122 | """A contract category product source delivers all products |
---|
[12097] | 123 | which belong to a certain contract_category. |
---|
[12092] | 124 | """ |
---|
| 125 | |
---|
| 126 | def getValues(self, context): |
---|
[12098] | 127 | concat = getattr(context, 'contract_category', None) |
---|
[12092] | 128 | products = grok.getSite()['products'].values() |
---|
[12098] | 129 | if not concat: |
---|
[12093] | 130 | return products |
---|
[12092] | 131 | resultlist = [ |
---|
[12098] | 132 | value for value in products if value.contract_category == concat] |
---|
[12092] | 133 | return resultlist |
---|
| 134 | |
---|
| 135 | def getToken(self, context, value): |
---|
| 136 | return value.product_id |
---|
| 137 | |
---|
| 138 | def getTitle(self, context, value): |
---|
| 139 | return "%s - %s" % (value.product_id, value.title) |
---|
[12100] | 140 | |
---|
| 141 | |
---|
| 142 | class CustomerDocumentSource(BasicContextualSourceFactory): |
---|
[12101] | 143 | """A customer document source delivers all submitted and verified documents |
---|
[12100] | 144 | of the context customer. |
---|
| 145 | """ |
---|
| 146 | |
---|
| 147 | def getValues(self, context): |
---|
[12127] | 148 | # When checking conversion during import, contracts do not belong to |
---|
| 149 | # customers. Thus all portal documents must be returned. |
---|
| 150 | user_id = getattr(getattr(context, 'customer', None), 'user_id', None) |
---|
| 151 | catalog = getUtility(ICatalog, name='documents_catalog') |
---|
| 152 | results = catalog.searchResults(user_id=(user_id, user_id)) |
---|
[12100] | 153 | resultlist = [ |
---|
[12127] | 154 | value for value in results if value.state in (SUBMITTED, VERIFIED)] |
---|
[12100] | 155 | return resultlist |
---|
| 156 | |
---|
| 157 | def getToken(self, context, value): |
---|
| 158 | return value.document_id |
---|
| 159 | |
---|
| 160 | def getTitle(self, context, value): |
---|
| 161 | return "%s - %s" % (value.document_id, value.title) |
---|