source: main/waeup.ikoba/trunk/src/waeup/ikoba/customers/contracts.py @ 12099

Last change on this file since 12099 was 12099, checked in by Henrik Bettermann, 10 years ago

Renaming batch 3

  • Property svn:keywords set to Id
File size: 5.2 KB
Line 
1## $Id: contracts.py 12099 2014-11-30 21:08:42Z 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"""
19Customer contract components.
20"""
21import os
22import grok
23from zope.component import queryUtility, getUtility
24from zope.component.interfaces import IFactory
25from zope.interface import implementedBy
26from hurry.workflow.interfaces import IWorkflowInfo, IWorkflowState
27from waeup.ikoba.interfaces import MessageFactory as _
28from waeup.ikoba.interfaces import IIkobaUtils, IObjectHistory
29from waeup.ikoba.customers.interfaces import (
30    IContractsContainer, ICustomerNavigation,
31    IContract, IContractEdit, ICustomersUtils,
32    )
33from waeup.ikoba.customers.utils import generate_contract_id
34from waeup.ikoba.utils.helpers import attrs_to_fields
35
36class ContractsContainer(grok.Container):
37    """This is a container for customer contracts.
38    """
39    grok.implements(IContractsContainer, ICustomerNavigation)
40    grok.provides(IContractsContainer)
41
42    def addContract(self, contract):
43        if not IContract.providedBy(contract):
44            raise TypeError(
45                'ContractsContainers contain only IContract instances')
46        self[contract.contract_id] = contract
47        return
48
49    @property
50    def customer(self):
51        return self.__parent__
52
53    def writeLogMessage(self, view, message):
54        return self.__parent__.writeLogMessage(view, message)
55
56ContractsContainer = attrs_to_fields(ContractsContainer)
57
58class ContractBase(grok.Container):
59    """This is a customer contract baseclass.
60    """
61    grok.implements(IContract, IContractEdit, ICustomerNavigation)
62    grok.provides(IContract)
63    grok.baseclass()
64
65    contract_category = None
66
67    def __init__(self):
68        super(ContractBase, self).__init__()
69        # The site doesn't exist in unit tests
70        try:
71            self.contract_id = generate_contract_id()
72        except AttributeError:
73            self.contract_id = u'a123'
74        self.last_product_id = None
75        return
76
77    @property
78    def history(self):
79        history = IObjectHistory(self)
80        return history
81
82    @property
83    def state(self):
84        return IWorkflowState(self).getState()
85
86    @property
87    def translated_state(self):
88        try:
89            TRANSLATED_STATES = getUtility(
90                ICustomersUtils).TRANSLATED_CONTRACT_STATES
91            return TRANSLATED_STATES[self.state]
92        except KeyError:
93            return
94
95    @property
96    def class_name(self):
97        return self.__class__.__name__
98
99    @property
100    def formatted_transition_date(self):
101        try:
102            return self.last_transition_date.strftime('%Y-%m-%d %H:%M:%S')
103        except AttributeError:
104            return
105
106    @property
107    def customer(self):
108        try:
109            return self.__parent__.__parent__
110        except AttributeError:
111            return None
112
113    def writeLogMessage(self, view, message):
114        return self.__parent__.__parent__.writeLogMessage(view, message)
115
116    @property
117    def is_editable(self):
118        try:
119            # Customer must be approved
120            cond1 = self.customer.state in getUtility(
121                ICustomersUtils).CONMANAGE_CUSTOMER_STATES
122            # Contract must be in state created
123            cond2 = self.state in getUtility(
124                ICustomersUtils).CONMANAGE_CONTRACT_STATES
125            if not (cond1 and cond2):
126                return False
127        except AttributeError:
128            pass
129        return True
130
131    @property
132    def translated_class_name(self):
133        try:
134            CONTYPES_DICT = getUtility(ICustomersUtils).CONTYPES_DICT
135            return CONTYPES_DICT[self.class_name]
136        except KeyError:
137            return
138
139
140class SampleContract(ContractBase):
141    """This is a sample contract.
142    """
143
144    contract_category = 'sample'
145
146SampleContract = attrs_to_fields(SampleContract)
147
148
149# Contracts must be importable. So we might need a factory.
150class SampleContractFactory(grok.GlobalUtility):
151    """A factory for contracts.
152    """
153    grok.implements(IFactory)
154    grok.name(u'waeup.SampleContract')
155    title = u"Create a new contract.",
156    description = u"This factory instantiates new sample contract instances."
157
158    def __call__(self, *args, **kw):
159        return SampleContract(*args, **kw)
160
161    def getInterfaces(self):
162        return implementedBy(SampleContract)
163
164@grok.subscribe(IContract, grok.IObjectAddedEvent)
165def handle_contract_added(contract, event):
166    """If an contract is added the transition create is fired.
167    The latter produces a logging message.
168    """
169    if IWorkflowState(contract).getState() is None:
170        IWorkflowInfo(contract).fireTransition('create')
171    return
Note: See TracBrowser for help on using the repository browser.