I have actions.py file in which i have so many action related classes. Each action class has certain logic of connecting to DB. While connecting to DB, getting connection objection is common across all actions.
Is there a way to maintain such common logic at common place?? Every action need to refer to common place and get the connection object. Could some one please educate me on this?
class ActionDepositStatus(Action): def name(self): return ‘action_deposit_status’
def run(self, dispatcher, tracker, domain):
import cx_Oracle
# Connect as user "hr" with password "welcome" to the "oraclepdb" service running on this computer.
conn_str = u'INSTALLER/install@dc123dfarevs9-scan.es.ad.adp.com:1521/dit1.DC1234DFAVf9DB.ad.COM'
connection = cx_Oracle.connect(conn_str)
cursor = connection.cursor()
agency_id = tracker.get_slot('Jurisdiction')
deposit_Status = tracker.get_slot('depositStatus')
agencyids = {'federal': 9, 'flsui': 9}
depositStatuses = {'pending': "0", 'failed': "F",'held':"1"}
#cursor.execute(u'SELECT * FROM mt_control')
cursor.execute(u"select status, amount as amount from mt_it_history where A_I = :aid and STATUS = :status",{"agencyid":agencyids[agency_id],"status":depositStatuses[deposit_Status]})
row = cursor.fetchone()
depositstatus = row[0]
response = """It is currently $$ {} in {} status for {}. """.format(row[1],deposit_Status, agency_id )
dispatcher.utter_message(response)
return [SlotSet('depositStatus',deposit_Status),SlotSet('Jurisdiction',agency_id)]
class UtilMetods(Action):
def areThereAnyErrors(self,procid):
import cx_Oracle
conn_str = u'INSTALLER/install@dc34dfavs9wew-scan.es.ad.adp.com:1521/dit.DC1DFAVSdfdfdDB.AP.COM'
connection = cx_Oracle.connect(conn_str)
errormessagecursor = connection.cursor()
errormessagecursor.execute(u"select count(*) from MT_PROSS_MESGE where poc_id = :procid and msg_Tye = :meype",{"procid": procid, "messageType": 'E'})
counts = errormessagecursor.fetchone()
errorCount = counts[0]
print("error count is {}".format(errorCount))
if errorCount == 0:
return False
else:
return True