source: main/waeup.ikoba/trunk/src/waeup/ikoba/customers/applications.py @ 12093

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

Add product field with source AppCatProductSource? for filtering products.

  • work in progress -
  • Property svn:keywords set to Id
File size: 5.3 KB
Line 
1## $Id: applications.py 12092 2014-11-29 17:37:58Z 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 application 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    IApplicationsContainer, ICustomerNavigation,
31    IApplication, ICustomersUtils,
32    )
33from waeup.ikoba.customers.utils import generate_application_id
34from waeup.ikoba.utils.helpers import attrs_to_fields
35
36class ApplicationsContainer(grok.Container):
37    """This is a container for customer applications.
38    """
39    grok.implements(IApplicationsContainer, ICustomerNavigation)
40    grok.provides(IApplicationsContainer)
41
42    def addApplication(self, application):
43        if not IApplication.providedBy(application):
44            raise TypeError(
45                'ApplicationsContainers contain only IApplication instances')
46        self[application.application_id] = application
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
56ApplicationsContainer = attrs_to_fields(ApplicationsContainer)
57
58class ApplicationBase(grok.Container):
59    """This is a customer application baseclass.
60    """
61    grok.implements(IApplication, ICustomerNavigation)
62    grok.provides(IApplication)
63    grok.baseclass()
64
65    application_category = None
66
67    def __init__(self):
68        super(ApplicationBase, self).__init__()
69        # The site doesn't exist in unit tests
70        try:
71            self.application_id = generate_application_id()
72        except AttributeError:
73            self.application_id = u'a123'
74        return
75
76    @property
77    def history(self):
78        history = IObjectHistory(self)
79        return history
80
81    @property
82    def state(self):
83        return IWorkflowState(self).getState()
84
85    @property
86    def translated_state(self):
87        try:
88            TRANSLATED_STATES = getUtility(
89                ICustomersUtils).TRANSLATED_APPLICATION_STATES
90            return TRANSLATED_STATES[self.state]
91        except KeyError:
92            return
93
94    @property
95    def class_name(self):
96        return self.__class__.__name__
97
98    @property
99    def formatted_transition_date(self):
100        try:
101            return self.last_transition_date.strftime('%Y-%m-%d %H:%M:%S')
102        except AttributeError:
103            return
104
105    @property
106    def customer(self):
107        try:
108            return self.__parent__.__parent__
109        except AttributeError:
110            return None
111
112    def writeLogMessage(self, view, message):
113        return self.__parent__.__parent__.writeLogMessage(view, message)
114
115    @property
116    def is_editable(self):
117        try:
118            # Customer must be approved
119            cond1 = self.customer.state in getUtility(
120                ICustomersUtils).APPMANAGE_CUSTOMER_STATES
121            # Application must be in state created
122            cond2 = self.state in getUtility(
123                ICustomersUtils).APPMANAGE_APPLICATION_STATES
124            if not (cond1 and cond2):
125                return False
126        except AttributeError:
127            pass
128        return True
129
130    @property
131    def translated_class_name(self):
132        try:
133            APPTYPES_DICT = getUtility(ICustomersUtils).APPTYPES_DICT
134            return APPTYPES_DICT[self.class_name]
135        except KeyError:
136            return
137
138
139class SampleApplication(ApplicationBase):
140    """This is a sample application.
141    """
142
143    application_category = 'sample'
144
145SampleApplication = attrs_to_fields(SampleApplication)
146
147
148# Applications must be importable. So we might need a factory.
149class SampleApplicationFactory(grok.GlobalUtility):
150    """A factory for applications.
151    """
152    grok.implements(IFactory)
153    grok.name(u'waeup.SampleApplication')
154    title = u"Create a new application.",
155    description = u"This factory instantiates new sample application instances."
156
157    def __call__(self, *args, **kw):
158        return SampleApplication(*args, **kw)
159
160    def getInterfaces(self):
161        return implementedBy(SampleApplication)
162
163@grok.subscribe(IApplication, grok.IObjectAddedEvent)
164def handle_document_added(application, event):
165    """If an application is added the transition create is fired.
166    The latter produces a logging message.
167    """
168    if IWorkflowState(application).getState() is None:
169        IWorkflowInfo(application).fireTransition('create')
170    return
Note: See TracBrowser for help on using the repository browser.