Advertisement
Guest User

Untitled

a guest
Sep 26th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 13.87 KB | None | 0 0
  1. from Tkinter import *
  2. from selenium import webdriver
  3. from selenium.common.exceptions import TimeoutException
  4. from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
  5. from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
  6. import time
  7. from selenium.webdriver.common.keys import Keys # For CTRL clicking
  8. #import lxml.html # For parsing table data
  9. import re # For searching
  10. from selenium.webdriver.common.action_chains import ActionChains
  11. from selenium.common.exceptions import NoSuchElementException
  12. from selenium.common.exceptions import ElementNotVisibleException
  13. from ScrolledText import ScrolledText
  14.  
  15.  
  16. def login():
  17.     driver.get("https://pm.officeally.com/emr/Login.aspx?ReturnUrl=%2femr%2fPatientCharts%2fManagePatients.aspx%3fTab%3dC&Tab=C")
  18.     #window.update()
  19.     inputUsername = driver.find_element_by_name("Login1$UserName")
  20.     inputPassword = driver.find_element_by_name("Login1$Password$TextBox1")
  21.     inputSubmit = driver.find_element_by_id("Login1_LoginButton")
  22.     inputUsername.send_keys(username)
  23.     inputPassword.send_keys(password)
  24.     inputSubmit.click()
  25.     #window.update()
  26.  
  27.    
  28. def closePopup():
  29.     if len(driver.window_handles) > 1:
  30.         driver.implicitly_wait(10)
  31.         #print "Closing Popup"
  32.         driver.switch_to_window(driver.window_handles[1])
  33.         driver.find_element_by_id('Button3').click()
  34.         driver.switch_to_window(driver.window_handles[0])
  35.  
  36.  
  37. def closeNews():
  38.     try:
  39.         driver.find_element_by_id('btnAcknowledge').click()
  40.         print 'Closing News and Information message'
  41.     except (ElementNotVisibleException, NoSuchElementException):
  42.         return 1
  43.  
  44.  
  45. def closeAlerts():
  46.     driver.execute_script('closeAlerts()')
  47.  
  48.  
  49. def closeTab():
  50.     currentIndex = driver.window_handles.index(driver.current_window_handle)
  51.     newIndex = driver.window_handles[currentIndex - 1]
  52.     print "Closing tab"
  53.     driver.close()
  54.     driver.switch_to_window(newIndex)
  55.     window.update()
  56.  
  57.  
  58. def nextTab():
  59.     driver.implicitly_wait(10)
  60.     currentIndex = driver.window_handles.index(driver.current_window_handle)
  61.     driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.NUMPAD2) #MAC
  62.     #driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB)     #WINDOWS
  63.     print "Going to next tab"
  64.     driver.implicitly_wait(10)
  65.     driver.switch_to_window(driver.window_handles[currentIndex + 1])
  66.     window.update()
  67.    
  68.    
  69. def searchFor(name):
  70.     printOutput(("--------- " + name[0].title() + ' ' + name[len(name)-1].title() + " ---------"))
  71.     driver.switch_to_window(driver.window_handles[0])
  72.     driver.implicitly_wait(10)
  73.     inputSearchPatients = driver.find_element_by_id("ctl00_phFolderContent_ucSearch_txtSearch")
  74.     inputSearchPatients.clear()
  75.     inputSearchPatients.send_keys(Keys.CONTROL, "a")
  76.     try:
  77.         inputSearchPatients.send_keys(name[len(name)-1].title())
  78.     except NoSuchElementException:
  79.         printOutput('ERROR')
  80.     driver.implicitly_wait(10)
  81.     driver.find_element_by_id("ctl00_phFolderContent_ucSearch_btnSearch").send_keys(Keys.RETURN) # Clicks Search
  82.     driver.implicitly_wait(10)
  83.     try:
  84.         #grabs link from text and merges with chrome script
  85.         linkstr = driver.find_element_by_partial_link_text(name[0].title()).get_attribute("href")
  86.         theScript = str("window.open(\"" + linkstr + "\");")
  87.         driver.execute_script(theScript)
  88.         return 0
  89.     except NoSuchElementException:
  90.         printOutput('Can not find patient')
  91.         global errorCount
  92.         errorCount += 1
  93.         return 1
  94.  
  95.  
  96. def checkLastNoteDate():
  97.     driver.implicitly_wait(10)
  98.     WebDriverWait(driver, 6).until(EC.visibility_of(driver.find_element_by_id('divSummary')))
  99.     lastVisitDiv = driver.page_source.encode('utf-8')
  100.     try:
  101.         lastVisitDiv = driver.find_element_by_xpath('//*[@id="ctl00_phFolderContent_panelView_ctl02_grdSection"]/tbody/tr[2]/td[1]/p')
  102.         lastNoteDate = lastVisitDiv.text[:10].split('/')
  103.         printOutput(('Last encounter:' + ' ' + lastVisitDiv.text[:10]))
  104.         if lastNoteDate == currentDate:
  105.             return 0 # Already Exists
  106.         return 1 # Previous Day Exists
  107.     except (ValueError, NoSuchElementException):
  108.         printOutput('No Previous Encounters')
  109.         return 2
  110.  
  111.        
  112. def copyLastNote(date):
  113.     printOutput('Copying last note')
  114.     try:
  115.         WebDriverWait(driver, 20).until(EC.visibility_of(driver.find_element_by_id('ctl00_phFolderContent_panelView_ctl02_grdSection')))
  116.         closeAlerts()
  117.         driver.find_element_by_xpath('//*[@id="ctl00_phFolderContent_panelView_ctl02_grdSection"]/tbody/tr[2]/td[6]/img').click()
  118.     except TimeoutException:
  119.         printOutput ("Loading took too much time!")
  120.     fillEncounterDate(date)
  121.        
  122.        
  123. def fillEncounterDate(date):
  124.     WebDriverWait(driver, 10).until(EC.visibility_of(driver.find_element_by_id('ctl00_phFolderContent_ucSOAPNote_EncounterDate_Month')))
  125.     inputDateMonth = driver.find_element_by_id('ctl00_phFolderContent_ucSOAPNote_EncounterDate_Month').send_keys(Keys.BACKSPACE + Keys.BACKSPACE + date[0] + date[1] + date[2])
  126.     driver.implicitly_wait(10)
  127.     #time.sleep(1)
  128.     submitNoteUpdate()
  129.    
  130.    
  131. def submitNoteUpdate():
  132.     WebDriverWait(driver, 10).until(EC.visibility_of(driver.find_element_by_id('btnSOAPUpdate1')))
  133.     driver.find_element_by_id('btnSOAPUpdate1').click()
  134.    
  135.    
  136. def openClinicalSummary():
  137.     #time.sleep(2)
  138.     driver.implicitly_wait(20)
  139.     firstName = driver.find_element_by_id('ctl00_phFolderContent_myPatientHeader_lblFirstName').text
  140.     lastName = driver.find_element_by_id('ctl00_phFolderContent_myPatientHeader_lblLastName').text
  141.     driver.execute_script('javascript:document.getElementById(\"fraViewDocument\").contentWindow.ViewDoc(\'PatientVisitSummary\')')
  142.     printOutput('Opening clinical summary')
  143.     waitForSummary()
  144.     #time.sleep(1)
  145.     driver.execute_script('javascript:document.getElementById(\"fraViewDocument\").contentWindow.openMU()')
  146.     driver.implicitly_wait(10)
  147.     checkboxValue = driver.execute_script('return document.getElementById("MUChecklist").contentWindow.document.getElementById("dd2").value')
  148.     if checkboxValue is 7 or 2:
  149.         driver.implicitly_wait(20)
  150.         driver.find_element_by_xpath('//*[@id="ucMUChecklist_btnUpdate"]').send_keys(Keys.RETURN)
  151.         if (checkLastNoteDate() is 1):
  152.             printOutput((firstName + ' ' + lastName + ' Successfully Added'))
  153.             time.sleep(2)
  154.             closeTab()
  155.         else:
  156.             print "oh shit"
  157.     else:
  158.         openClinicalSummary()
  159.    
  160.    
  161. def waitForMoreTabs(initial):
  162.     if len(driver.window_handles) is not initial:
  163.         return 1
  164.     else:
  165.         time.sleep(1)
  166.         waitForMoreTabs(initial)
  167.    
  168.    
  169. def waitForSummary():
  170.     driver.implicitly_wait(20)
  171.     initialTabCount = len(driver.window_handles)
  172.     currentIndex = driver.window_handles.index(driver.current_window_handle)
  173.     waitForMoreTabs(initialTabCount)
  174.     driver.switch_to_window(driver.window_handles[currentIndex + 1])
  175.     WebDriverWait(driver, 10).until(EC.visibility_of(driver.find_element_by_xpath('//*[@id="form1"]/div[3]/div[2]')))
  176.     closeTab()
  177.  
  178.    
  179. def createNewNote(date):
  180.     printOutput('Creating new note')
  181.     driver.implicitly_wait(20)
  182.     ActionChains(driver).move_to_element(driver.find_element_by_xpath('//*[@id="mn-oamPatientCharts"]/div/div[2]/div[1]/a')).perform()
  183.     driver.implicitly_wait(10)
  184.     #time.sleep(1)
  185.     WebDriverWait(driver, 10).until(EC.visibility_of(driver.find_element_by_xpath('//*[@id="mn-oamPatientCharts"]/div/div[2]/div[1]/a')))
  186.     driver.find_element_by_xpath('//*[@id="mn-oamPatientCharts"]/div/div[2]/div[2]/table/tbody/tr/td[2]/ul/li/a').click()
  187.     fillEncounterDate(date)
  188.    
  189.    
  190. def openLastDay():
  191.     driver.implicitly_wait(10)
  192.     printOutput('Opening last note')
  193.     printOutput('(No new encounter created)')
  194.     closeAlerts()
  195.     driver.find_element_by_xpath('//*[@id="ctl00_phFolderContent_panelView_ctl02_grdSection"]/tbody/tr[2]/td[4]/img').click()
  196.    
  197.    
  198. def alreadyExists(name):
  199.     printOutput(('Encounter for ' + name + ' already exists on ' + niceCurrentDate))
  200.     f = open(patientsFile,"r")
  201.     fileLines = f.readlines()
  202.     f.close()
  203.     f = open(patientsFile,"w")
  204.     for line in fileLines:
  205.         if line == name+"\n":
  206.             f.write(name + " --Note Already Exists\n")
  207.         else:
  208.             f.write(line)
  209.     f.close()
  210.  
  211.  
  212. def editLastNote(name):
  213.     lastTab()
  214.     try:
  215.         closeAlerts()
  216.         driver.find_element_by_xpath('//*[@id="ctl00_phFolderContent_panelView_ctl02_grdSection"]/tbody/tr[2]/td[5]/img').click()
  217.         driver.implicitly_wait(10)
  218.         #time.sleep(1)
  219.         element = driver.find_element_by_id('imgHistory')
  220.         driver.execute_script("arguments[0].scrollIntoView(true);", element)
  221.         driver.find_element_by_xpath('//*[@id="ctl00_phFolderContent_ucSOAPNote_S_Allergies"]').send_keys(name)
  222.         driver.find_element_by_xpath('//*[@id="ctl00_phFolderContent_ucSOAPNote_S_Allergies"]').send_keys(Keys.CONTROL + 'a')
  223.     except NoSuchElementException:
  224.         printOutput('Error editing last note')
  225.     driver.implicitly_wait(10)
  226.     firstTab()
  227.  
  228.  
  229. def lastTab():
  230.     driver.implicitly_wait(10)
  231.     driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + '9')
  232.     driver.switch_to_window(driver.window_handles[len(driver.window_handles)-1])
  233.     driver.implicitly_wait(10)
  234.     window.update()
  235.  
  236.  
  237. def firstTab():
  238.     driver.implicitly_wait(10)
  239.     driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + '1')
  240.     driver.switch_to_window(driver.window_handles[0])
  241.  
  242.  
  243. def addNewEncounters(patients):
  244.     resetErrors()
  245.     closeNews()
  246.     #closePopup()
  247.     driver.implicitly_wait(10)
  248.     WebDriverWait(driver, 10).until(EC.visibility_of(driver.find_element_by_xpath('//*[@id="ctl00_phFolderContent_myCustomGrid_myGrid_pager_center"]/table/tbody/tr/td[4]/input')))
  249.     for currentPatient in patients:
  250.         print currentPatient
  251.         currentPatientList = currentPatient.split(" ")
  252.         print currentPatientList
  253.         # if patient has 3 names, merge the first two
  254.         if (len(currentPatientList) > 2):
  255.             print "merging names"
  256.             currentPatientList[0] = str(currentPatientList[0]) + " " + str(currentPatientList[1])
  257.         print str(currentPatientList)
  258.         if (searchFor(currentPatientList) is not 1):
  259.             nextTab();
  260.             checkDate = checkLastNoteDate()
  261.             if checkDate is 0: # Already exists
  262.                 openLastDay()
  263.                 openClinicalSummary()
  264.                 printOutput(('Encounter for ' + currentPatient.title() + ' already exists'))
  265.             if checkDate is 1: # Previous note exists
  266.                 copyLastNote(currentDate)
  267.                 openClinicalSummary()
  268.             if checkDate is 2: # No previous notes
  269.                 createNewNote(currentDate)
  270.                 openClinicalSummary()
  271.                
  272.     if errorCount is 0:
  273.         printOutput('-------------------------------')
  274.         printOutput('ALL PATIENTS SUCCESSFULLY ADDED')
  275.         printOutput('-------------------------------')
  276.     else:
  277.         printOutput('--------------------------------------')
  278.         printOutput(('ERRORS: ' + str(errorCount) + '. CHECK ABOVE FOR MORE INFO'))
  279.         printOutput('--------------------------------------')
  280.        
  281.  
  282. def openDrugs(patients):
  283.     resetErrors()
  284.     closePopup()
  285.     closeNews()
  286.     driver.implicitly_wait(10)
  287.     WebDriverWait(driver, 10).until(EC.visibility_of(driver.find_element_by_xpath('//*[@id="ctl00_phFolderContent_myCustomGrid_myGrid_pager_center"]/table/tbody/tr/td[4]/input')))
  288.     for currentPatient in patients:
  289.         currentPatientList = currentPatient.split(' ')
  290.         searchFor(currentPatientList)
  291.         lastTab()
  292.         checkDate = checkLastNoteDate()
  293.         editLastNote(currentPatient)
  294.     printOutput('All patients opened')
  295.  
  296. def resetErrors():
  297.     global errorCount
  298.     errorCount = 0
  299.  
  300.  
  301. def updateDate(date):
  302.     lines = open('config.txt', 'r').readlines()
  303.     lines[2] = "last date: " + date
  304.     out = open('config.txt', 'w')
  305.     out.writelines(lines)
  306.     out.close()
  307.  
  308.  
  309. def getDate():
  310.     dateInput = tkDateInput.get()
  311.     updateDate(dateInput)
  312.     printOutput(('Date entered: ' + dateInput))
  313.     global currentDate
  314.     currentDate = dateInput.split('/')
  315.     global niceCurrentDate
  316.     niceCurrentDate = currentDate[0] + "/" + currentDate[1] + "/" + currentDate[2]
  317.  
  318.    
  319. def printOutput(text):
  320.     tkOutput.config(state=NORMAL)
  321.     tkOutput.insert(END, str(text) + '\n')
  322.     tkOutput.yview(END)
  323.     tkOutput.config(state=DISABLED)
  324.     window.update()
  325.  
  326.  
  327. def buttonOne():
  328.     getDate()
  329.     patients = tkPatientList.get(1.0, END).split('\n')
  330.     print patients[:-1]
  331.     addNewEncounters(patients[:-1])
  332.  
  333.  
  334. def buttonTwo():
  335.     getDate()
  336.     patients = tkPatientList.get(1.0, END).split('\n')
  337.     print patients[:-1]
  338.     openDrugs(patients[:-1])
  339.  
  340.  
  341. def initiateBrowser():
  342.     global driver
  343.     driver = webdriver.Chrome("Drivers\chromedriver.exe")
  344.     #window.update()
  345.     driver.set_window_size((window.winfo_screenwidth() - 655), (window.winfo_screenheight() - 100))
  346.     login()
  347.     #window.update()
  348.     closePopup()
  349.     #window.update()
  350.     closeNews()
  351.  
  352.  
  353. def closeWindow():
  354.     f = open('output.txt',"w")
  355.     output = tkOutput.get(1.0, END)
  356.     for line in output:
  357.         f.write(line)
  358.     f.close()
  359.     window.quit()
  360.     driver.quit()
  361.  
  362.  
  363. def pasteClipboard():
  364.     text = window.selection_get(selection='CLIPBOARD')
  365.     tkPatientList.insert('insert', text)
  366.  
  367.  
  368.  
  369. with open('config.txt', 'r') as file:
  370.     lines = [line.strip() for line in file]
  371. username = lines[0][10:]
  372. password = lines[1][10:]
  373.  
  374. errorCount = 0
  375.  
  376.  
  377.  
  378. window = Tk()
  379. window.title('Office Ally')
  380.  
  381. tkPatientListLabel = Label(window, text="Patients:                                                                                                   Output log:                                     ")
  382. tkPatientListLabel.pack()
  383.  
  384. tkPatientList = Text(window, width=24)
  385. tkPatientList.pack(side = LEFT)
  386. tkPatientList.focus_set()
  387.  
  388. tkDateInput = Entry(window, width=10)
  389.  
  390. tkOutput = ScrolledText(window, width=48, state = DISABLED)
  391. tkOutput.pack(side = RIGHT)
  392.    
  393. b1 = Button(window, text="Add Encounters", width=16, command=buttonOne)
  394. b2 = Button(window, text="Add Drugs", width=16, command=buttonTwo)
  395. b3 = Button(window, text="Paste clipboard", width=16, command=pasteClipboard)
  396. b31 = Button(window, text="Open New Browser", width=16, command=initiateBrowser)
  397. b4 = Button(window, text="QUIT", fg="red", command=closeWindow)
  398. tkDateInput.pack(side = TOP)
  399. b1.pack(side = TOP)
  400. b2.pack(side = TOP)
  401. b3.pack(side = TOP)
  402. b31.pack(side = TOP)
  403. b4.pack(side = BOTTOM)
  404.  
  405. text = ScrolledText(window)
  406. window.geometry("+%d+%d" % (window.winfo_screenwidth() - 645, window.winfo_screenheight() - 1075))
  407. tkDateInput.insert(0, time.strftime("%m") + "/" + time.strftime("%d") + "/" + time.strftime("%Y"))
  408.  
  409.  
  410.  
  411. initiateBrowser()
  412. mainloop()
  413. closePopup()
  414. closeNews()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement