[12089] | 1 | ## $Id: contracts.py 12289 2014-12-21 22:17:06Z 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 | """ |
---|
[12097] | 19 | Customer contract components. |
---|
[12089] | 20 | """ |
---|
| 21 | import os |
---|
| 22 | import grok |
---|
| 23 | from zope.component import queryUtility, getUtility |
---|
| 24 | from zope.component.interfaces import IFactory |
---|
| 25 | from zope.interface import implementedBy |
---|
[12144] | 26 | from zope.schema import getFields |
---|
[12089] | 27 | from hurry.workflow.interfaces import IWorkflowInfo, IWorkflowState |
---|
| 28 | from waeup.ikoba.interfaces import MessageFactory as _ |
---|
[12258] | 29 | from waeup.ikoba.interfaces import ( |
---|
| 30 | IIkobaUtils, IObjectHistory, VERIFIED, IIDSource) |
---|
[12089] | 31 | from waeup.ikoba.customers.interfaces import ( |
---|
[12097] | 32 | IContractsContainer, ICustomerNavigation, |
---|
| 33 | IContract, IContractEdit, ICustomersUtils, |
---|
[12103] | 34 | ISampleContract, ISampleContractEdit) |
---|
[12097] | 35 | from waeup.ikoba.customers.utils import generate_contract_id |
---|
[12089] | 36 | from waeup.ikoba.utils.helpers import attrs_to_fields |
---|
| 37 | |
---|
[12097] | 38 | class ContractsContainer(grok.Container): |
---|
| 39 | """This is a container for customer contracts. |
---|
[12089] | 40 | """ |
---|
[12097] | 41 | grok.implements(IContractsContainer, ICustomerNavigation) |
---|
| 42 | grok.provides(IContractsContainer) |
---|
[12089] | 43 | |
---|
[12097] | 44 | def addContract(self, contract): |
---|
| 45 | if not IContract.providedBy(contract): |
---|
[12089] | 46 | raise TypeError( |
---|
[12097] | 47 | 'ContractsContainers contain only IContract instances') |
---|
| 48 | self[contract.contract_id] = contract |
---|
[12089] | 49 | return |
---|
| 50 | |
---|
| 51 | @property |
---|
| 52 | def customer(self): |
---|
| 53 | return self.__parent__ |
---|
| 54 | |
---|
| 55 | def writeLogMessage(self, view, message): |
---|
| 56 | return self.__parent__.writeLogMessage(view, message) |
---|
| 57 | |
---|
[12097] | 58 | ContractsContainer = attrs_to_fields(ContractsContainer) |
---|
[12089] | 59 | |
---|
[12097] | 60 | class ContractBase(grok.Container): |
---|
| 61 | """This is a customer contract baseclass. |
---|
[12089] | 62 | """ |
---|
[12097] | 63 | grok.implements(IContract, IContractEdit, ICustomerNavigation) |
---|
| 64 | grok.provides(IContract) |
---|
[12089] | 65 | grok.baseclass() |
---|
| 66 | |
---|
[12144] | 67 | check_docs_interface = None |
---|
| 68 | |
---|
[12097] | 69 | contract_category = None |
---|
[12092] | 70 | |
---|
[12103] | 71 | form_fields_interface = None |
---|
| 72 | |
---|
| 73 | edit_form_fields_interface = None |
---|
| 74 | |
---|
[12089] | 75 | def __init__(self): |
---|
[12097] | 76 | super(ContractBase, self).__init__() |
---|
[12089] | 77 | # The site doesn't exist in unit tests |
---|
[12258] | 78 | source = getUtility(IIDSource) |
---|
| 79 | self.contract_id = unicode(source.get_hex_uuid()) |
---|
[12094] | 80 | self.last_product_id = None |
---|
[12089] | 81 | return |
---|
| 82 | |
---|
| 83 | @property |
---|
| 84 | def history(self): |
---|
| 85 | history = IObjectHistory(self) |
---|
| 86 | return history |
---|
| 87 | |
---|
| 88 | @property |
---|
| 89 | def state(self): |
---|
| 90 | return IWorkflowState(self).getState() |
---|
| 91 | |
---|
| 92 | @property |
---|
| 93 | def translated_state(self): |
---|
| 94 | try: |
---|
| 95 | TRANSLATED_STATES = getUtility( |
---|
[12097] | 96 | ICustomersUtils).TRANSLATED_CONTRACT_STATES |
---|
[12089] | 97 | return TRANSLATED_STATES[self.state] |
---|
| 98 | except KeyError: |
---|
| 99 | return |
---|
| 100 | |
---|
| 101 | @property |
---|
| 102 | def class_name(self): |
---|
| 103 | return self.__class__.__name__ |
---|
| 104 | |
---|
| 105 | @property |
---|
| 106 | def formatted_transition_date(self): |
---|
| 107 | try: |
---|
[12210] | 108 | return self.history.messages[-1].split(' - ')[0] |
---|
| 109 | except IndexError: |
---|
[12089] | 110 | return |
---|
| 111 | |
---|
| 112 | @property |
---|
| 113 | def customer(self): |
---|
| 114 | try: |
---|
| 115 | return self.__parent__.__parent__ |
---|
| 116 | except AttributeError: |
---|
| 117 | return None |
---|
| 118 | |
---|
[12289] | 119 | @property |
---|
| 120 | def user_id(self): |
---|
| 121 | if self.customer is not None: |
---|
| 122 | return self.customer.customer_id |
---|
| 123 | return |
---|
| 124 | |
---|
[12089] | 125 | def writeLogMessage(self, view, message): |
---|
| 126 | return self.__parent__.__parent__.writeLogMessage(view, message) |
---|
| 127 | |
---|
| 128 | @property |
---|
[12167] | 129 | def is_editable_by_customer(self): |
---|
[12089] | 130 | try: |
---|
| 131 | # Customer must be approved |
---|
| 132 | cond1 = self.customer.state in getUtility( |
---|
[12099] | 133 | ICustomersUtils).CONMANAGE_CUSTOMER_STATES |
---|
[12097] | 134 | # Contract must be in state created |
---|
[12089] | 135 | cond2 = self.state in getUtility( |
---|
[12099] | 136 | ICustomersUtils).CONMANAGE_CONTRACT_STATES |
---|
[12089] | 137 | if not (cond1 and cond2): |
---|
| 138 | return False |
---|
| 139 | except AttributeError: |
---|
| 140 | pass |
---|
| 141 | return True |
---|
| 142 | |
---|
| 143 | @property |
---|
[12144] | 144 | def is_approvable(self): |
---|
| 145 | for key, field in getFields(self.check_docs_interface).items(): |
---|
| 146 | if key.endswith('_object'): |
---|
| 147 | obj = getattr(self, key, None) |
---|
| 148 | state = getattr(obj, 'state', None) |
---|
| 149 | if state and state != VERIFIED: |
---|
[12168] | 150 | return False, _("Attached documents must be verified first.") |
---|
| 151 | return True, None |
---|
[12144] | 152 | |
---|
| 153 | @property |
---|
[12089] | 154 | def translated_class_name(self): |
---|
| 155 | try: |
---|
[12099] | 156 | CONTYPES_DICT = getUtility(ICustomersUtils).CONTYPES_DICT |
---|
| 157 | return CONTYPES_DICT[self.class_name] |
---|
[12089] | 158 | except KeyError: |
---|
| 159 | return |
---|
| 160 | |
---|
| 161 | |
---|
[12097] | 162 | class SampleContract(ContractBase): |
---|
| 163 | """This is a sample contract. |
---|
[12089] | 164 | """ |
---|
| 165 | |
---|
[12103] | 166 | grok.implements(ISampleContract, ISampleContractEdit, ICustomerNavigation) |
---|
| 167 | |
---|
[12097] | 168 | contract_category = 'sample' |
---|
[12092] | 169 | |
---|
[12103] | 170 | form_fields_interface = ISampleContract |
---|
| 171 | |
---|
| 172 | edit_form_fields_interface = ISampleContractEdit |
---|
| 173 | |
---|
[12144] | 174 | check_docs_interface = ISampleContract |
---|
| 175 | |
---|
[12097] | 176 | SampleContract = attrs_to_fields(SampleContract) |
---|
[12089] | 177 | |
---|
| 178 | |
---|
[12097] | 179 | # Contracts must be importable. So we might need a factory. |
---|
| 180 | class SampleContractFactory(grok.GlobalUtility): |
---|
| 181 | """A factory for contracts. |
---|
[12089] | 182 | """ |
---|
| 183 | grok.implements(IFactory) |
---|
[12097] | 184 | grok.name(u'waeup.SampleContract') |
---|
| 185 | title = u"Create a new contract.", |
---|
| 186 | description = u"This factory instantiates new sample contract instances." |
---|
[12089] | 187 | |
---|
| 188 | def __call__(self, *args, **kw): |
---|
[12097] | 189 | return SampleContract(*args, **kw) |
---|
[12089] | 190 | |
---|
| 191 | def getInterfaces(self): |
---|
[12097] | 192 | return implementedBy(SampleContract) |
---|
[12089] | 193 | |
---|
[12097] | 194 | @grok.subscribe(IContract, grok.IObjectAddedEvent) |
---|
| 195 | def handle_contract_added(contract, event): |
---|
| 196 | """If an contract is added the transition create is fired. |
---|
[12090] | 197 | The latter produces a logging message. |
---|
| 198 | """ |
---|
[12097] | 199 | if IWorkflowState(contract).getState() is None: |
---|
| 200 | IWorkflowInfo(contract).fireTransition('create') |
---|
[12090] | 201 | return |
---|