source: main/waeup.ikoba/branches/uli-payments/src/waeup/ikoba/customers/contracts.py @ 12135

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

Add sample contract interfaces with additional document field. Use these interfaces in form pages.

  • Property svn:keywords set to Id
File size: 5.5 KB
Line 
1## $Id: contracts.py 12103 2014-12-01 09:59:14Z 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    ISampleContract, ISampleContractEdit)
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    form_fields_interface = None
68
69    edit_form_fields_interface = None
70
71    def __init__(self):
72        super(ContractBase, self).__init__()
73        # The site doesn't exist in unit tests
74        try:
75            self.contract_id = generate_contract_id()
76        except AttributeError:
77            self.contract_id = u'a123'
78        self.last_product_id = None
79        return
80
81    @property
82    def history(self):
83        history = IObjectHistory(self)
84        return history
85
86    @property
87    def state(self):
88        return IWorkflowState(self).getState()
89
90    @property
91    def translated_state(self):
92        try:
93            TRANSLATED_STATES = getUtility(
94                ICustomersUtils).TRANSLATED_CONTRACT_STATES
95            return TRANSLATED_STATES[self.state]
96        except KeyError:
97            return
98
99    @property
100    def class_name(self):
101        return self.__class__.__name__
102
103    @property
104    def formatted_transition_date(self):
105        try:
106            return self.last_transition_date.strftime('%Y-%m-%d %H:%M:%S')
107        except AttributeError:
108            return
109
110    @property
111    def customer(self):
112        try:
113            return self.__parent__.__parent__
114        except AttributeError:
115            return None
116
117    def writeLogMessage(self, view, message):
118        return self.__parent__.__parent__.writeLogMessage(view, message)
119
120    @property
121    def is_editable(self):
122        try:
123            # Customer must be approved
124            cond1 = self.customer.state in getUtility(
125                ICustomersUtils).CONMANAGE_CUSTOMER_STATES
126            # Contract must be in state created
127            cond2 = self.state in getUtility(
128                ICustomersUtils).CONMANAGE_CONTRACT_STATES
129            if not (cond1 and cond2):
130                return False
131        except AttributeError:
132            pass
133        return True
134
135    @property
136    def translated_class_name(self):
137        try:
138            CONTYPES_DICT = getUtility(ICustomersUtils).CONTYPES_DICT
139            return CONTYPES_DICT[self.class_name]
140        except KeyError:
141            return
142
143
144class SampleContract(ContractBase):
145    """This is a sample contract.
146    """
147
148    grok.implements(ISampleContract, ISampleContractEdit, ICustomerNavigation)
149
150    contract_category = 'sample'
151
152    form_fields_interface = ISampleContract
153
154    edit_form_fields_interface = ISampleContractEdit
155
156SampleContract = attrs_to_fields(SampleContract)
157
158
159# Contracts must be importable. So we might need a factory.
160class SampleContractFactory(grok.GlobalUtility):
161    """A factory for contracts.
162    """
163    grok.implements(IFactory)
164    grok.name(u'waeup.SampleContract')
165    title = u"Create a new contract.",
166    description = u"This factory instantiates new sample contract instances."
167
168    def __call__(self, *args, **kw):
169        return SampleContract(*args, **kw)
170
171    def getInterfaces(self):
172        return implementedBy(SampleContract)
173
174@grok.subscribe(IContract, grok.IObjectAddedEvent)
175def handle_contract_added(contract, event):
176    """If an contract is added the transition create is fired.
177    The latter produces a logging message.
178    """
179    if IWorkflowState(contract).getState() is None:
180        IWorkflowInfo(contract).fireTransition('create')
181    return
Note: See TracBrowser for help on using the repository browser.