1 | ## $Id: applications.py 12094 2014-11-30 08:12:27Z 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 | """ |
---|
19 | Customer application components. |
---|
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 |
---|
26 | from hurry.workflow.interfaces import IWorkflowInfo, IWorkflowState |
---|
27 | from waeup.ikoba.interfaces import MessageFactory as _ |
---|
28 | from waeup.ikoba.interfaces import IIkobaUtils, IObjectHistory |
---|
29 | from waeup.ikoba.customers.interfaces import ( |
---|
30 | IApplicationsContainer, ICustomerNavigation, |
---|
31 | IApplication, ICustomersUtils, |
---|
32 | ) |
---|
33 | from waeup.ikoba.customers.utils import generate_application_id |
---|
34 | from waeup.ikoba.utils.helpers import attrs_to_fields |
---|
35 | |
---|
36 | class 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 | |
---|
56 | ApplicationsContainer = attrs_to_fields(ApplicationsContainer) |
---|
57 | |
---|
58 | class 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 | 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_APPLICATION_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).APPMANAGE_CUSTOMER_STATES |
---|
122 | # Application must be in state created |
---|
123 | cond2 = self.state in getUtility( |
---|
124 | ICustomersUtils).APPMANAGE_APPLICATION_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 | APPTYPES_DICT = getUtility(ICustomersUtils).APPTYPES_DICT |
---|
135 | return APPTYPES_DICT[self.class_name] |
---|
136 | except KeyError: |
---|
137 | return |
---|
138 | |
---|
139 | |
---|
140 | class SampleApplication(ApplicationBase): |
---|
141 | """This is a sample application. |
---|
142 | """ |
---|
143 | |
---|
144 | application_category = 'sample' |
---|
145 | |
---|
146 | SampleApplication = attrs_to_fields(SampleApplication) |
---|
147 | |
---|
148 | |
---|
149 | # Applications must be importable. So we might need a factory. |
---|
150 | class SampleApplicationFactory(grok.GlobalUtility): |
---|
151 | """A factory for applications. |
---|
152 | """ |
---|
153 | grok.implements(IFactory) |
---|
154 | grok.name(u'waeup.SampleApplication') |
---|
155 | title = u"Create a new application.", |
---|
156 | description = u"This factory instantiates new sample application instances." |
---|
157 | |
---|
158 | def __call__(self, *args, **kw): |
---|
159 | return SampleApplication(*args, **kw) |
---|
160 | |
---|
161 | def getInterfaces(self): |
---|
162 | return implementedBy(SampleApplication) |
---|
163 | |
---|
164 | @grok.subscribe(IApplication, grok.IObjectAddedEvent) |
---|
165 | def handle_document_added(application, event): |
---|
166 | """If an application is added the transition create is fired. |
---|
167 | The latter produces a logging message. |
---|
168 | """ |
---|
169 | if IWorkflowState(application).getState() is None: |
---|
170 | IWorkflowInfo(application).fireTransition('create') |
---|
171 | return |
---|