|
| 1 | +import os |
| 2 | +import flask |
| 3 | +from flask import Flask, jsonify, request |
| 4 | +from flask import render_template |
| 5 | +from flask_cors import CORS |
| 6 | +from flask import session, redirect, url_for, request, Response |
| 7 | +import oracledb |
| 8 | + |
| 9 | +# Environment Variables for Oracle Database Connectivity |
| 10 | +user = os.environ['ORACLE_USER'] |
| 11 | +password = os.environ['ORACLE_PASSWORD'] |
| 12 | +dsn = os.environ['ORACLE_DSN'] |
| 13 | + |
| 14 | +app = Flask(__name__, template_folder='/') |
| 15 | +CORS(app) |
| 16 | + |
| 17 | +# List of API User and Auth Token |
| 18 | +VALID_USERS = {'user1': 'password1', 'user2': 'password2'} |
| 19 | + |
| 20 | +# Connect to the Oracle database |
| 21 | +con = oracledb.connect(user=user, password=password, dsn=dsn) |
| 22 | + |
| 23 | +# HTML Form Method to Create a New Employee |
| 24 | +@app.route('/api/add_employee', methods=['GET', 'POST']) |
| 25 | +def add_employee_form(): |
| 26 | + # Require authentication for all requests |
| 27 | + auth = request.authorization |
| 28 | + if not auth or not check_auth(auth.username, auth.password): |
| 29 | + return authenticate() |
| 30 | + |
| 31 | + if request.method == 'POST': |
| 32 | + # Extract the employee data from the form |
| 33 | + name = request.form['name'] |
| 34 | + email = request.form['email'] |
| 35 | + department = request.form['department'] |
| 36 | + |
| 37 | + # Insert the employee into the database |
| 38 | + cur = con.cursor() |
| 39 | + cur.execute("INSERT INTO employees (name, email, department) VALUES (:name, :email, :department)", |
| 40 | + {'name': name, 'email': email, 'department': department}) |
| 41 | + con.commit() |
| 42 | + |
| 43 | + # Return the ID of the new employee |
| 44 | + return redirect(url_for('get_all')) |
| 45 | + else: |
| 46 | + return render_template('add_employee.html') |
| 47 | + |
| 48 | + |
| 49 | +# HTML Method to Get a List of All Employees |
| 50 | +@app.route('/api/getall') |
| 51 | +def get_all(): |
| 52 | + # Require authentication for all requests |
| 53 | + auth = request.authorization |
| 54 | + if not auth or not check_auth(auth.username, auth.password): |
| 55 | + return authenticate() |
| 56 | + |
| 57 | + cur = con.cursor() |
| 58 | + cur.prefetchrows = 100 |
| 59 | + cur.arraysize = 100 |
| 60 | + cur.execute("SELECT * FROM employees") |
| 61 | + rows = cur.fetchall() |
| 62 | + employees = [] |
| 63 | + for row in rows: |
| 64 | + employee = {'id': row[0], 'name': row[1], 'email': row[2], 'department': row[3]} |
| 65 | + employees.append(employee) |
| 66 | + return render_template('get_employees.html', employees=employees) |
| 67 | + |
| 68 | + |
| 69 | +# HTML Method to Update en Employee |
| 70 | +@app.route('/api/update_employee/<int:id>', methods=['GET','POST']) |
| 71 | +def update_employee(id): |
| 72 | + |
| 73 | + # Require authentication for all requests |
| 74 | + auth = request.authorization |
| 75 | + if not auth or not check_auth(auth.username, auth.password): |
| 76 | + return authenticate() |
| 77 | + |
| 78 | + cur = con.cursor() |
| 79 | + if request.method == 'POST': |
| 80 | + name = request.form['name'] |
| 81 | + email = request.form['email'] |
| 82 | + department = request.form['department'] |
| 83 | + cur.execute('UPDATE employees SET name=:name, email=:email, department=:department WHERE id=:id', |
| 84 | + {'name': name, 'email': email, 'department': department, 'id': id}) |
| 85 | + con.commit() |
| 86 | + cur.close() |
| 87 | + return redirect(url_for('get_all')) |
| 88 | + else: |
| 89 | + cur.execute('SELECT * FROM employees WHERE id=:id', {'id': id}) |
| 90 | + employee = cur.fetchone() |
| 91 | + cur.close() |
| 92 | + return render_template('update_employee.html', employee=employee) |
| 93 | + |
| 94 | + |
| 95 | +# HTML Method to Delete an Employee |
| 96 | +@app.route('/api/delete_employee/<int:id>', methods=['GET', 'POST']) |
| 97 | +def delete_employee(id): |
| 98 | + # Require authentication for all requests |
| 99 | + auth = request.authorization |
| 100 | + if not auth or not check_auth(auth.username, auth.password): |
| 101 | + return authenticate() |
| 102 | + |
| 103 | + if request.method == 'POST': |
| 104 | + cur = con.cursor() |
| 105 | + cur.execute('DELETE FROM employees WHERE id=:id', {'id': id}) |
| 106 | + con.commit() |
| 107 | + cur.close() |
| 108 | + return redirect(url_for('get_all')) |
| 109 | + else: |
| 110 | + cur = con.cursor() |
| 111 | + cur.execute('SELECT * FROM employees WHERE id=:id', {'id': id}) |
| 112 | + employee = cur.fetchone() |
| 113 | + cur.close() |
| 114 | + return render_template('confirm_delete_employee.html', employee=employee) |
| 115 | + |
| 116 | +# Main Page for Listing All Methods |
| 117 | +@app.route('/api/main', methods=['GET']) |
| 118 | +def crud_options(): |
| 119 | + # Require authentication for all requests |
| 120 | + auth = request.authorization |
| 121 | + if not auth or not check_auth(auth.username, auth.password): |
| 122 | + return authenticate() |
| 123 | + |
| 124 | + return render_template('crud_options.html') |
| 125 | + |
| 126 | +# HTML Method to Search for an Employee by ID |
| 127 | +@app.route('/api/search_employee/<int:id>', methods=['GET', 'POST']) |
| 128 | +def search_employee(id): |
| 129 | + # Require authentication for all requests |
| 130 | + auth = request.authorization |
| 131 | + if not auth or not check_auth(auth.username, auth.password): |
| 132 | + return authenticate() |
| 133 | + |
| 134 | + if request.method == 'POST': |
| 135 | + cur = con.cursor() |
| 136 | + cur.execute('SELECT * FROM employees WHERE id=:id', {'id': id}) |
| 137 | + employee = cur.fetchone() |
| 138 | + cur.close() |
| 139 | + |
| 140 | + if employee is None: |
| 141 | + # Display a message if the employee is not found |
| 142 | + return render_template('employee_not_found.html', id=id) |
| 143 | + else: |
| 144 | + # Display the employee details if found |
| 145 | + return render_template('search_employee.html', employee=employee) |
| 146 | + else: |
| 147 | + # Create an empty employee object if a GET request is received |
| 148 | + return render_template('search_employee.html', employee=None) |
| 149 | + |
| 150 | + |
| 151 | +def check_auth(username, password): |
| 152 | + """Check if a username/password combination is valid.""" |
| 153 | + return username in VALID_USERS and password == VALID_USERS[username] |
| 154 | + |
| 155 | +def authenticate(): |
| 156 | + """Send a 401 Unauthorized response that prompts the user to authenticate.""" |
| 157 | + return Response('Could not verify your access level for that URL.\n' |
| 158 | + 'You have to login with proper credentials', 401, |
| 159 | + {'WWW-Authenticate': 'Basic realm="Login Required"'}) |
| 160 | + |
| 161 | +if __name__ == '__main__': |
| 162 | + # Print out all the routes and their associated URLs |
| 163 | + for rule in app.url_map.iter_rules(): |
| 164 | + print(f"{rule.endpoint:50s} {rule.methods} {rule.rule}") |
| 165 | + |
| 166 | + # Start the HTTPS server |
| 167 | + app.run(host='0.0.0.0', port=4443, ssl_context=('cert.pem', 'key.pem')) |
0 commit comments