Last active
October 24, 2019 17:42
-
-
Save DavFount/a5b7f1ee07a78a9368a406c2b6ef96b1 to your computer and use it in GitHub Desktop.
Versions Used during testing: pymongo - 3.9.0 Python- 3.7.3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
import time | |
import tkinter as tk | |
from os import path | |
from tkinter import filedialog, END | |
from pymongo import MongoClient | |
uri = "mongodb://user:password@host/db?authSource=authdb" | |
client = MongoClient(uri) | |
# Set your Database here | |
database = client["SCC"] | |
# Add collections here | |
Collection_Choices = [ | |
'CPA', | |
'CAD' | |
] | |
# Add Test patient name patterns here | |
SCC_Test_Patients = [ | |
'ZZTEST', | |
'zztest' | |
] | |
def isTestPatient(PatientName): | |
for name in SCC_Test_Patients: | |
if name in PatientName: | |
return True | |
return False | |
def FileSelect(event=None): | |
filename = filedialog.askopenfilename( | |
initialdir="/", title="Select file", filetypes=(("CSV files", "*.csv"), )) | |
entryText.set(filename) | |
def ImportToMongo(): | |
filepath = entryText.get() | |
collection = choices_variable.get() | |
# Return error if file is lft blank or set to an invalid file. | |
if not path.exists(filepath) or not 'csv' in filepath: | |
error_text.set('Error:Invalid or Non-Existant File.') | |
return | |
# Return error if Collection is invalid | |
if collection == 'Select a collection': | |
error_text.set('Error: A collection is required.') | |
return | |
# Empty list to queue patients for upload | |
ChargeList = [] | |
# Clear Errors | |
error_text.set('') | |
with open(filepath) as csvFile: | |
csvReader = csv.DictReader(csvFile) | |
for row in csvReader: | |
if isTestPatient(row['PAT_NAME']): | |
continue | |
ChargeList.append(row) | |
# Get the pymongo collection object and insert the data. | |
dbCollection = database[collection] | |
dbCollection.insert_many(ChargeList) | |
last_run_text.set(f'Upload Finished {entryText.get()}') | |
entryText.set('') | |
root = tk.Tk() | |
root.title("Upload files to MongoDB") | |
root.minsize(410, 120) | |
# root.maxsize(410, 120) | |
entryText = tk.StringVar() | |
file_path = tk.Entry(root, textvariable=entryText, width=50) | |
file_path.grid(row=0, columnspan=2, column=0, pady=5, padx=5) | |
select_button = tk.Button(root, text='Select File', command=FileSelect) | |
select_button.grid(row=0, column=2, pady=5, padx=5) | |
choices_variable = tk.StringVar() | |
choices_variable.set('Select a collection') | |
collection_select = tk.OptionMenu( | |
root, choices_variable, *Collection_Choices) | |
collection_select.grid(row=1, column=0, pady=5) | |
upload_button_text = tk.StringVar() | |
upload_button = tk.Button(root, text='Upload File', | |
command=ImportToMongo) | |
upload_button.grid(row=1, column=1, pady=5, padx=5) | |
quit_button = tk.Button(root, text='Close', command=root.quit) | |
quit_button.grid(row=1, column=2, pady=5) | |
error_text = tk.StringVar() | |
error = tk.Label(root, textvariable=error_text) | |
error.config(fg='red') | |
error.grid(row=2, columnspan=3, pady=5, padx=2) | |
last_run_text = tk.StringVar() | |
last_run = tk.Label(root, textvariable=last_run_text) | |
last_run.grid(row=3, columnspan=3, pady=5, padx=2) | |
root.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment