## $Id: applications.py 12092 2014-11-29 17:37:58Z henrik $ ## ## Copyright (C) 2014 Uli Fouquet & Henrik Bettermann ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; if not, write to the Free Software ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ## """ Customer application components. """ import os import grok from zope.component import queryUtility, getUtility from zope.component.interfaces import IFactory from zope.interface import implementedBy from hurry.workflow.interfaces import IWorkflowInfo, IWorkflowState from waeup.ikoba.interfaces import MessageFactory as _ from waeup.ikoba.interfaces import IIkobaUtils, IObjectHistory from waeup.ikoba.customers.interfaces import ( IApplicationsContainer, ICustomerNavigation, IApplication, ICustomersUtils, ) from waeup.ikoba.customers.utils import generate_application_id from waeup.ikoba.utils.helpers import attrs_to_fields class ApplicationsContainer(grok.Container): """This is a container for customer applications. """ grok.implements(IApplicationsContainer, ICustomerNavigation) grok.provides(IApplicationsContainer) def addApplication(self, application): if not IApplication.providedBy(application): raise TypeError( 'ApplicationsContainers contain only IApplication instances') self[application.application_id] = application return @property def customer(self): return self.__parent__ def writeLogMessage(self, view, message): return self.__parent__.writeLogMessage(view, message) ApplicationsContainer = attrs_to_fields(ApplicationsContainer) class ApplicationBase(grok.Container): """This is a customer application baseclass. """ grok.implements(IApplication, ICustomerNavigation) grok.provides(IApplication) grok.baseclass() application_category = None def __init__(self): super(ApplicationBase, self).__init__() # The site doesn't exist in unit tests try: self.application_id = generate_application_id() except AttributeError: self.application_id = u'a123' return @property def history(self): history = IObjectHistory(self) return history @property def state(self): return IWorkflowState(self).getState() @property def translated_state(self): try: TRANSLATED_STATES = getUtility( ICustomersUtils).TRANSLATED_APPLICATION_STATES return TRANSLATED_STATES[self.state] except KeyError: return @property def class_name(self): return self.__class__.__name__ @property def formatted_transition_date(self): try: return self.last_transition_date.strftime('%Y-%m-%d %H:%M:%S') except AttributeError: return @property def customer(self): try: return self.__parent__.__parent__ except AttributeError: return None def writeLogMessage(self, view, message): return self.__parent__.__parent__.writeLogMessage(view, message) @property def is_editable(self): try: # Customer must be approved cond1 = self.customer.state in getUtility( ICustomersUtils).APPMANAGE_CUSTOMER_STATES # Application must be in state created cond2 = self.state in getUtility( ICustomersUtils).APPMANAGE_APPLICATION_STATES if not (cond1 and cond2): return False except AttributeError: pass return True @property def translated_class_name(self): try: APPTYPES_DICT = getUtility(ICustomersUtils).APPTYPES_DICT return APPTYPES_DICT[self.class_name] except KeyError: return class SampleApplication(ApplicationBase): """This is a sample application. """ application_category = 'sample' SampleApplication = attrs_to_fields(SampleApplication) # Applications must be importable. So we might need a factory. class SampleApplicationFactory(grok.GlobalUtility): """A factory for applications. """ grok.implements(IFactory) grok.name(u'waeup.SampleApplication') title = u"Create a new application.", description = u"This factory instantiates new sample application instances." def __call__(self, *args, **kw): return SampleApplication(*args, **kw) def getInterfaces(self): return implementedBy(SampleApplication) @grok.subscribe(IApplication, grok.IObjectAddedEvent) def handle_document_added(application, event): """If an application is added the transition create is fired. The latter produces a logging message. """ if IWorkflowState(application).getState() is None: IWorkflowInfo(application).fireTransition('create') return