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

Last change on this file since 12127 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
RevLine 
[12089]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"""
[12097]19Customer contract components.
[12089]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 (
[12097]30    IContractsContainer, ICustomerNavigation,
31    IContract, IContractEdit, ICustomersUtils,
[12103]32    ISampleContract, ISampleContractEdit)
[12097]33from waeup.ikoba.customers.utils import generate_contract_id
[12089]34from waeup.ikoba.utils.helpers import attrs_to_fields
35
[12097]36class ContractsContainer(grok.Container):
37    """This is a container for customer contracts.
[12089]38    """
[12097]39    grok.implements(IContractsContainer, ICustomerNavigation)
40    grok.provides(IContractsContainer)
[12089]41
[12097]42    def addContract(self, contract):
43        if not IContract.providedBy(contract):
[12089]44            raise TypeError(
[12097]45                'ContractsContainers contain only IContract instances')
46        self[contract.contract_id] = contract
[12089]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
[12097]56ContractsContainer = attrs_to_fields(ContractsContainer)
[12089]57
[12097]58class ContractBase(grok.Container):
59    """This is a customer contract baseclass.
[12089]60    """
[12097]61    grok.implements(IContract, IContractEdit, ICustomerNavigation)
62    grok.provides(IContract)
[12089]63    grok.baseclass()
64
[12097]65    contract_category = None
[12092]66
[12103]67    form_fields_interface = None
68
69    edit_form_fields_interface = None
70
[12089]71    def __init__(self):
[12097]72        super(ContractBase, self).__init__()
[12089]73        # The site doesn't exist in unit tests
74        try:
[12097]75            self.contract_id = generate_contract_id()
[12089]76        except AttributeError:
[12097]77            self.contract_id = u'a123'
[12094]78        self.last_product_id = None
[12089]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(
[12097]94                ICustomersUtils).TRANSLATED_CONTRACT_STATES
[12089]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(
[12099]125                ICustomersUtils).CONMANAGE_CUSTOMER_STATES
[12097]126            # Contract must be in state created
[12089]127            cond2 = self.state in getUtility(
[12099]128                ICustomersUtils).CONMANAGE_CONTRACT_STATES
[12089]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:
[12099]138            CONTYPES_DICT = getUtility(ICustomersUtils).CONTYPES_DICT
139            return CONTYPES_DICT[self.class_name]
[12089]140        except KeyError:
141            return
142
143
[12097]144class SampleContract(ContractBase):
145    """This is a sample contract.
[12089]146    """
147
[12103]148    grok.implements(ISampleContract, ISampleContractEdit, ICustomerNavigation)
149
[12097]150    contract_category = 'sample'
[12092]151
[12103]152    form_fields_interface = ISampleContract
153
154    edit_form_fields_interface = ISampleContractEdit
155
[12097]156SampleContract = attrs_to_fields(SampleContract)
[12089]157
158
[12097]159# Contracts must be importable. So we might need a factory.
160class SampleContractFactory(grok.GlobalUtility):
161    """A factory for contracts.
[12089]162    """
163    grok.implements(IFactory)
[12097]164    grok.name(u'waeup.SampleContract')
165    title = u"Create a new contract.",
166    description = u"This factory instantiates new sample contract instances."
[12089]167
168    def __call__(self, *args, **kw):
[12097]169        return SampleContract(*args, **kw)
[12089]170
171    def getInterfaces(self):
[12097]172        return implementedBy(SampleContract)
[12089]173
[12097]174@grok.subscribe(IContract, grok.IObjectAddedEvent)
175def handle_contract_added(contract, event):
176    """If an contract is added the transition create is fired.
[12090]177    The latter produces a logging message.
178    """
[12097]179    if IWorkflowState(contract).getState() is None:
180        IWorkflowInfo(contract).fireTransition('create')
[12090]181    return
Note: See TracBrowser for help on using the repository browser.