<gh_stars>0
# dicomproject/app/main.py
from flask import Flask, request, render_template, send_from_directory, jsonify, abort
import psycopg2
import os
import env 

DATABASE = env.DATABASE
DB_USER = env.DB_USER
DB_PASSWORD = env.DB_PASSWORD 
DB_HOST = env.DB_HOST
DB_PORT = env.DB_PORT
SERVER_IP = env.SERVER_IP
SERVER_PORT = env.SERVER_PORT
IMAGES_URL = 'http://%s:%s/images'%(SERVER_IP,SERVER_PORT)

app = Flask(__name__, static_url_path='')
app.config["IMAGE_FOLDER_PATH"] = 'static/images'


@app.route("/", methods=['GET','POST'])                                         # modified
def index(): 
    patient_list = []
    if request.method == 'POST':                                                # new
        query_text = '''SELECT * FROM PATIENT INNER JOIN PATIENT_IMAGE
        ON PATIENT_IMAGE.PATIENT_ID=PATIENT.PATIENT_ID''' 
        search_text = request.form['search_text']
        if search_text != '':
            query_text += ''' WHERE first_name LIKE '%%%s%%'
                        OR last_name LIKE '%%%s%%'
                        OR hospital_id LIKE '%%%s%%'
                        ;'''%(search_text, search_text, search_text)


        con = psycopg2.connect(database=DATABASE, user=DB_USER, 
                    password=<PASSWORD>, host=DB_HOST, port=DB_PORT)  
        cur = con.cursor()
        cur.execute(query_text)
        patient_list = cur.fetchall()
    return render_template('index.html', patient_list=patient_list) 


@app.route('/images/<path:path>')
def get_patient_file(path):
    try:
        return send_from_directory(app.config["IMAGE_FOLDER_PATH"], path)    
    except:
        abort(404, description="Image %s not found."%path)

@app.route('/_images_list')
def load_images():
    image_list = os.listdir(app.config["IMAGE_FOLDER_PATH"])
    return jsonify(image_list = image_list)

@app.route('/_patient_list')
def load_patients():
    query_text = '''SELECT * FROM PATIENT'''
    con = psycopg2.connect(database=DATABASE, user=DB_USER, 
                password=DB_PASSWORD, host=DB_HOST, port=DB_PORT)  
    # print("DATABASE connection established successfully!")
    cur = con.cursor()
    cur.execute(query_text)
    patient_list = cur.fetchall()
    return jsonify(patient_list = patient_list)

app.run(host=SERVER_IP, port=SERVER_PORT)
# dicomproject/app/main.py
from flask import Flask, request, render_template, send_from_directory, jsonify, abort
import psycopg2
import os
import env 

DATABASE = env.DATABASE
DB_USER = env.DB_USER
DB_PASSWORD = env.DB_PASSWORD 
DB_HOST = env.DB_HOST
DB_PORT = env.DB_PORT
SERVER_IP = env.SERVER_IP
SERVER_PORT = env.SERVER_PORT
IMAGES_URL = 'http://%s:%s/images'%(SERVER_IP,SERVER_PORT)

app = Flask(__name__, static_url_path='')
app.config["IMAGE_FOLDER_PATH"] = 'static/images'


@app.route("/", methods=['GET','POST'])                                         # modified
def index(): 
    patient_list = []
    if request.method == 'POST':                                                # new
        query_text = '''SELECT * FROM PATIENT INNER JOIN PATIENT_IMAGE
        ON PATIENT_IMAGE.PATIENT_ID=PATIENT.PATIENT_ID''' 
        search_text = request.form['search_text']
        if search_text != '':
            query_text += ''' WHERE first_name LIKE '%%%s%%'
                        OR last_name LIKE '%%%s%%'
                        OR hospital_id LIKE '%%%s%%'
                        ;'''%(search_text, search_text, search_text)


        con = psycopg2.connect(database=DATABASE, user=DB_USER, 
                    password=<PASSWORD>, host=DB_HOST, port=DB_PORT)  
        cur = con.cursor()
        cur.execute(query_text)
        patient_list = cur.fetchall()
    return render_template('index.html', patient_list=patient_list) 


@app.route('/images/<path:path>')
def get_patient_file(path):
    try:
        return send_from_directory(app.config["IMAGE_FOLDER_PATH"], path)    
    except:
        abort(404, description="Image %s not found."%path)

@app.route('/_images_list')
def load_images():
    image_list = os.listdir(app.config["IMAGE_FOLDER_PATH"])
    return jsonify(image_list = image_list)

@app.route('/_patient_list')
def load_patients():
    query_text = '''SELECT * FROM PATIENT'''
    con = psycopg2.connect(database=DATABASE, user=DB_USER, 
                password=DB_PASSWORD, host=DB_HOST, port=DB_PORT)  
    # print("DATABASE connection established successfully!")
    cur = con.cursor()
    cur.execute(query_text)
    patient_list = cur.fetchall()
    return jsonify(patient_list = patient_list)

app.run(host=SERVER_IP, port=SERVER_PORT)
# dicomproject/app/main.py # modified # new SELECT * FROM PATIENT INNER JOIN PATIENT_IMAGE         ON PATIENT_IMAGE.PATIENT_ID=PATIENT.PATIENT_ID WHERE first_name LIKE '%%%s%%'                         OR last_name LIKE '%%%s%%'                         OR hospital_id LIKE '%%%s%%'                         ; SELECT * FROM PATIENT # print("DATABASE connection established successfully!")