class_management_system

Senior Project - Spring 2019
Log | Files | Refs | README

main.py (6745B)


      1 import logging
      2 logging.basicConfig(level=logging.DEBUG)
      3 import os
      4 import subprocess
      5 import img2pdf
      6 from flask import Flask, request, redirect, url_for, send_from_directory, \
      7 render_template, session
      8 from flaskext.mysql import MySQL
      9 from werkzeug import secure_filename
     10 import sys
     11 
     12 UPLOAD_FOLDER = '/tmp/upload'
     13 ALLOWED_EXTENSIONS = set(['pdf', 'docx'])
     14 
     15 app = Flask(__name__)
     16 app.secret_key = 'test'
     17 
     18 mysql = MySQL()
     19 app.config['MYSQL_DATABASE_USER'] = 'root'
     20 app.config['MYSQL_DATABASE_PASSWORD'] = 'senior'
     21 app.config['MYSQL_DATABASE_DB'] = 'curricular_management'
     22 app.config['MYSQL_DATABASE_HOST'] = 'localhost'
     23 mysql.init_app(app)
     24 
     25 
     26 conn = mysql.connect()
     27 cursor =conn.cursor()
     28 
     29 app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
     30 
     31 def allowed_file(filename):
     32     return '.' in filename and \
     33            filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
     34 
     35 def set_proposal_session(proposal_id):
     36     sql_query = """SELECT assessement_form_id, library_form_id, supporting_document_id, program_guide_id from proposal WHERE proposal_id = %s"""
     37     cursor.execute(sql_query, proposal_id)
     38     result = cursor.fetchone()
     39     session['proposal'] = proposal_id
     40     session['assessement'] = result[0]
     41     session['library'] = result[1]
     42     session['support'] = result[2]
     43     session['program'] = result[3]
     44 
     45 
     46 @app.route("/", methods=['GET', 'POST', 'PUT'])
     47 def index():
     48     if not session.get('department'):
     49         return redirect(url_for('login'))
     50 
     51     cursor.execute("SELECT proposal_id, proposal_title FROM proposal")
     52     if request.method == 'POST':
     53         prop_id = request.form['proposal']
     54         set_proposal_session(prop_id)
     55         return redirect(url_for('status'))
     56         
     57     return render_template('index.html', prop=cursor.fetchall())
     58 
     59 @app.route("/login", methods=['GET', 'POST'])
     60 def login():
     61     cursor.execute("SELECT department_id, department_name FROM department")
     62     if request.method == 'POST':
     63         session['department'] = request.form['department']
     64         return redirect(url_for('index'))
     65 
     66     return render_template('login.html', dept=cursor.fetchall())
     67 
     68 @app.route("/create")
     69 def createProposal():
     70     return render_template('create.html')
     71 
     72 @app.route("/upload", methods=['GET', 'POST'])
     73 def uploadpage():
     74     if request.method == 'POST':
     75         file = request.files['file']
     76         doctype = request.form.get('doc_select') 
     77         uname = session['proposal']
     78 
     79         if doctype == "-a":
     80             doc_id = session['assessement']
     81             sql_query = """INSERT INTO assessement_form_revision (assessement_form_id, assessement_form_file_path, assessement_form_datetime) values (%s, %s, NOW())"""
     82         elif doctype == "-l":
     83             doc_id = session['library']
     84             sql_query = """INSERT INTO library_form_revision (library_form_id, library_form_file_path, library_form_datetime) values (%s, %s, NOW())"""
     85         elif doctype == "-s":
     86             doc_id = session['support']
     87             sql_query = """INSERT INTO supporting_document_revision (supporting_document_id, supporting_document_file_path, supporting_document_datetime) values (%s, %s, NOW())"""
     88         elif doctype == "-p":
     89             doc_id = session['program']
     90             sql_query = """INSERT INTO program_guide_revision (program_guide_id, program_guide_file_path, program_guide_datetime) values (%s, %s, NOW())"""
     91         elif doctype == "-c":
     92             #doc_id = session['consult']
     93             sql_query = """INSERT INTO consult_letter_revision (consult_letter_id, consult_letter_file_path, consult_letter_datetime) values (%s, %s, NOW())"""
     94 
     95         if file and allowed_file(file.filename):
     96             filename = secure_filename(file.filename)
     97             file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
     98 
     99             # doctype = request.form.get('doc_select') 
    100 
    101             if filename.endswith('.pdf'):
    102                 # doctype = "-s"
    103                 cmd = "./archiver.sh -n " + uname + " -f " + filename + " " + doctype
    104 
    105             if filename.endswith('.docx'):
    106                 conv = "soffice --convert-to pdf /tmp/" + filename + " --outdir " + UPLOAD_FOLDER + " --headless"
    107                 subprocess.call(conv, shell=True)
    108                 uname = "Kubach"
    109                 # doctype = "-s"
    110                 cmd = "./archiver.sh -n " + uname + " -f " + filename.replace(".docx",".pdf") + " " + doctype
    111 
    112             if filename.endswith('png'):
    113                 conv = "convert" + filename + " --path " + UPLOAD_FOLDER + " --headless"
    114                 subprocess.call(conv, shell=True)
    115                 
    116             newFilename = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
    117             cursor.execute(sql_query, newFilename.stdout.readlines()[0].strip())
    118             conn.commit()
    119 
    120             return redirect(url_for('uploadpage', doctype = doctype))
    121     return render_template('upload.html')
    122 
    123 @app.route("/download", methods=['GET', 'POST'])
    124 def downloadpage():
    125     if request.method == 'POST':
    126         document = request.form.get('document')
    127 
    128         if selectDoc == "-a":
    129             sql_query = """SELECT assessement_form_file_path FROM assessement_form_revision WHERE assessement_form_id = %s"""
    130         elif selectDoc == "-l":
    131             sql_query = """SELECT library_form_file_path FROM library_form_revision WHERE library_form_id = %s"""
    132         elif selectDoc == "-s":
    133             sql_query = """SELECT assessement_form_file_path FROM assessement_form_revision WHERE assessement_form_id = %s"""
    134         elif selectDoc == "-p":
    135             sql_query = """SELECT assessement_form_file_path FROM assessement_form_revision WHERE assessement_form_id = %s"""
    136         elif selectDoc == "-c":
    137             sql_query = """SELECT assessement_form_file_path FROM assessement_form_revision WHERE assessement_form_id = %s"""
    138 
    139     elif request.method == 'GET':
    140         selectVer = request.form.get('select_version')
    141         #uploads = os.path.join(current_app.root_path, app.config['UPLOAD_FOLDER'])
    142         #return send_file('/tmp/upload/Kubach_Assessement_Form_20190418040324.pdf',mimetype='application/pdf', as_attachment=True)
    143         return send_from_directory(directory='/tmp/upload', filename='Kubach_Assessement_Form_20190404151301.pdf', as_attachment=True)
    144         #return redirect(url_for('downloadpage', doctype = selectDoc))
    145     return send_file('/tmp/upload/Kubach_Assessement_Form_20190404151301.pdf', as_attachment=True)
    146     #return send_from_directory(directory=uploads, filename="Kubach_Assessement_Form_20190418040324.pdf", as_attachment=True)
    147     #return render_template('index.html')
    148     #return render_template('download.html')
    149 
    150 @app.route("/status")
    151 def status():
    152     return render_template('status.html')
    153 
    154 if __name__ == "__main__":
    155     app.run(host='0.0.0.0', port=5001, debug=True)