1 | from flask import Flask, render_template, request
|
---|
2 | import psycopg2
|
---|
3 | from psycopg2.extras import RealDictCursor
|
---|
4 |
|
---|
5 | # Flask app setup
|
---|
6 | app = Flask(__name__)
|
---|
7 |
|
---|
8 | # Database connection setup
|
---|
9 | DB_CONFIG = {
|
---|
10 | "dbname": "db_202425z_va_prj_personalteacher",
|
---|
11 | "user": "db_202425z_va_prj_personalteacher_owner",
|
---|
12 | "password": "5ad8d6cf55cc",
|
---|
13 | "host": "localhost",
|
---|
14 | "port": 9999,
|
---|
15 | }
|
---|
16 |
|
---|
17 | # Function to get all subjects
|
---|
18 | def get_subjects():
|
---|
19 | try:
|
---|
20 | with psycopg2.connect(**DB_CONFIG) as conn:
|
---|
21 | with conn.cursor(cursor_factory=RealDictCursor) as cursor:
|
---|
22 | cursor.execute("SELECT * FROM Subject")
|
---|
23 | return cursor.fetchall()
|
---|
24 | except Exception as e:
|
---|
25 | print("Database error:", e)
|
---|
26 | return []
|
---|
27 |
|
---|
28 |
|
---|
29 | def get_teachers(from_date=None, to_date=None):
|
---|
30 | try:
|
---|
31 | with psycopg2.connect(**DB_CONFIG) as conn:
|
---|
32 | with conn.cursor(cursor_factory=RealDictCursor) as cursor:
|
---|
33 | # Base query
|
---|
34 | query = "SELECT * FROM Teacher join Public_user on Teacher.id_user = Public_user.id_user"
|
---|
35 | conditions = []
|
---|
36 |
|
---|
37 | # Add conditions based on the dates
|
---|
38 | if from_date:
|
---|
39 | conditions.append(f"hire_date >= '{from_date}'")
|
---|
40 |
|
---|
41 | # If there are any conditions, append them to the query
|
---|
42 | if conditions:
|
---|
43 | query += " WHERE " + " AND ".join(conditions)
|
---|
44 |
|
---|
45 | cursor.execute(query)
|
---|
46 | return cursor.fetchall()
|
---|
47 | except Exception as e:
|
---|
48 | print("Database error:", e)
|
---|
49 | return []
|
---|
50 | def get_users():
|
---|
51 | try:
|
---|
52 | with psycopg2.connect(**DB_CONFIG) as conn:
|
---|
53 | with conn.cursor(cursor_factory=RealDictCursor) as cursor:
|
---|
54 | cursor.execute("SELECT * FROM Public_user")
|
---|
55 | result = cursor.fetchall()
|
---|
56 | return result
|
---|
57 | except Exception as e:
|
---|
58 | print("Database error:", e)
|
---|
59 | return []
|
---|
60 |
|
---|
61 |
|
---|
62 | @app.route("/")
|
---|
63 | def index():
|
---|
64 | #users = get_users()
|
---|
65 | return render_template("index.html")
|
---|
66 |
|
---|
67 |
|
---|
68 | @app.route("/subjects")
|
---|
69 | def subjects():
|
---|
70 | subjects = get_subjects()
|
---|
71 | return render_template("subjects.html", subjects=subjects)
|
---|
72 |
|
---|
73 |
|
---|
74 | @app.route("/teachers", methods=["GET"])
|
---|
75 | def teachers():
|
---|
76 | # Get date filters from the request
|
---|
77 | from_date = request.args.get("from_date")
|
---|
78 |
|
---|
79 | # Fetch filtered teachers data
|
---|
80 | teachers = get_teachers(from_date)
|
---|
81 |
|
---|
82 | return render_template("teachers.html", teachers=teachers)
|
---|
83 | @app.route("/users")
|
---|
84 | def users():
|
---|
85 | users = get_users()
|
---|
86 | return render_template("users.html", users=users)
|
---|
87 |
|
---|
88 |
|
---|
89 | if __name__ == "__main__":
|
---|
90 | app.run(port=3000, debug=True)
|
---|