# -*- coding: utf-8 -*-## Copyright (C) 2010-2012 Roberto Longobardi# # This file is part of the Test Manager plugin for Trac.# # The Test Manager plugin for Trac 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 3 of the License, or (at your option) any later # version.# # The Test Manager plugin for Trac 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 the Test Manager plugin for Trac. See the file LICENSE.txt. # If not, see <http://www.gnu.org/licenses/>.#importreimporttimefromdatetimeimportdate,datetimefromtrac.coreimport*fromtrac.dbimportTable,Column,Indexfromtrac.envimportIEnvironmentSetupParticipantfromtrac.resourceimportResource,ResourceNotFoundfromtrac.util.translationimport_,N_,gettextfromtracgenericclass.modelimportIConcreteClassProvider,AbstractVariableFieldsObject,need_db_create_for_realm,create_db_for_realm,need_db_upgrade_for_realm,upgrade_db_for_realmfromtracgenericclass.utilimport*classResourceWorkflowState(AbstractVariableFieldsObject):""" This object represents the current workflow state of the associated resource. """# Fields that must not be modified directly by the userprotected_fields=('id','res_realm','state')def__init__(self,env,id=None,res_realm=None,state='new',db=None):""" The resource workflow state is related to a resource, the 'id' and 'res_realm' arguments. The state can be any string. """self.values={}self.values['id']=idself.values['res_realm']=res_realmself.values['state']=statekey=self.build_key_object()AbstractVariableFieldsObject.__init__(self,env,'resourceworkflowstate',key,db)defget_key_prop_names(self):return['id','res_realm']defcreate_instance(self,key):returnResourceWorkflowState(self.env,key['id'],key['res_realm'])classGenericWorkflowModelProvider(Component):""" This class provides the data model for the generic workflow plugin. The actual data model on the db is created starting from the SCHEMA declaration below. For each table, we specify whether to create also a '_custom' and a '_change' table. This class also provides the specification of the available fields for each class, being them standard fields and the custom fields specified in the trac.ini file. The custom field specification follows the same syntax as for Tickets. Currently, only 'text' type of custom fields are supported. """implements(IConcreteClassProvider,IEnvironmentSetupParticipant)SCHEMA={'resourceworkflowstate':{'table':Table('resourceworkflowstate',key=('id','res_realm'))[Column('id'),Column('res_realm'),Column('state')],'has_custom':True,'has_change':True,'version':1}}FIELDS={'resourceworkflowstate':[{'name':'id','type':'text','label':N_('ID')},{'name':'res_realm','type':'text','label':N_('Resource realm')},{'name':'state','type':'text','label':N_('Workflow state')}]}METADATA={'resourceworkflowstate':{'label':"Workflow State",'searchable':False,'has_custom':True,'has_change':True},}# IConcreteClassProvider methodsdefget_realms(self):yield'resourceworkflowstate'defget_data_models(self):returnself.SCHEMAdefget_fields(self):returnself.FIELDSdefget_metadata(self):returnself.METADATAdefcreate_instance(self,realm,key=None):obj=Noneifrealm=='resourceworkflowstate':ifkeyisnotNone:obj=ResourceWorkflowState(self.env,key['id'],key['res_realm'])else:obj=ResourceWorkflowState(self.env)returnobjdefcheck_permission(self,req,realm,key_str=None,operation='set',name=None,value=None):pass# IEnvironmentSetupParticipant methodsdefenvironment_created(self):self.upgrade_environment(get_db(self.env))defenvironment_needs_upgrade(self,db):forrealminself.SCHEMA:realm_metadata=self.SCHEMA[realm]ifneed_db_create_for_realm(self.env,realm,realm_metadata,db)or \
need_db_upgrade_for_realm(self.env,realm,realm_metadata,db):returnTruereturnFalsedefupgrade_environment(self,db):# Create or update dbforrealminself.SCHEMA:realm_metadata=self.SCHEMA[realm]ifneed_db_create_for_realm(self.env,realm,realm_metadata,db):create_db_for_realm(self.env,realm,realm_metadata,db)elifneed_db_upgrade_for_realm(self.env,realm,realm_metadata,db):upgrade_db_for_realm(self.env,'tracgenericworkflow.upgrades',realm,realm_metadata,db)