| 1 | import sys
|
|---|
| 2 | import getpass
|
|---|
| 3 |
|
|---|
| 4 | import bcrypt
|
|---|
| 5 |
|
|---|
| 6 | from db import get_connection, close_pool, pick_from_list
|
|---|
| 7 | from uc_browse import browse_resources
|
|---|
| 8 | from uc_reserve import make_reservation
|
|---|
| 9 | from uc_approve import approve_reservations
|
|---|
| 10 | from uc_analytics import view_analytics
|
|---|
| 11 | from uc_users import register_user
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 | def login():
|
|---|
| 15 | """Authenticate user with email and password. Returns (user_id, first_name, last_name, role) or None."""
|
|---|
| 16 | print("\n=== FRRUAS - Login ===\n")
|
|---|
| 17 | while True:
|
|---|
| 18 | email = input(" Email: ").strip()
|
|---|
| 19 | if not email:
|
|---|
| 20 | return None
|
|---|
| 21 | password = getpass.getpass(" Password: ").strip()
|
|---|
| 22 | if not password:
|
|---|
| 23 | return None
|
|---|
| 24 |
|
|---|
| 25 | try:
|
|---|
| 26 | with get_connection() as conn:
|
|---|
| 27 | with conn.cursor() as cur:
|
|---|
| 28 | cur.execute(
|
|---|
| 29 | """
|
|---|
| 30 | SELECT u.user_id, u.first_name, u.last_name, u.password, ut.type_name
|
|---|
| 31 | FROM users u
|
|---|
| 32 | JOIN user_types ut ON u.type_id = ut.type_id
|
|---|
| 33 | WHERE u.email = %s
|
|---|
| 34 | """,
|
|---|
| 35 | (email,),
|
|---|
| 36 | )
|
|---|
| 37 | row = cur.fetchone()
|
|---|
| 38 | except Exception as e:
|
|---|
| 39 | print(f"\n Database error: {e}")
|
|---|
| 40 | print(" Is the database running? (docker compose up -d)\n")
|
|---|
| 41 | return None
|
|---|
| 42 |
|
|---|
| 43 | if row and bcrypt.checkpw(password.encode(), row[3].encode()):
|
|---|
| 44 | user_id, first_name, last_name, _, role = row
|
|---|
| 45 | print(f"\n Welcome, {first_name} {last_name} ({role})!\n")
|
|---|
| 46 | return (user_id, first_name, last_name, role)
|
|---|
| 47 |
|
|---|
| 48 | print(" Invalid email or password. Please try again.\n")
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 | def main_menu(user_id, first_name, last_name, role):
|
|---|
| 52 | """Role-based main menu loop."""
|
|---|
| 53 | while True:
|
|---|
| 54 | print(f"\n=== Main Menu ({first_name} {last_name} - {role}) ===\n")
|
|---|
| 55 | options = []
|
|---|
| 56 | actions = []
|
|---|
| 57 |
|
|---|
| 58 | options.append("Browse Available Resources")
|
|---|
| 59 | actions.append(lambda: browse_resources())
|
|---|
| 60 |
|
|---|
| 61 | if role == "Teaching Staff":
|
|---|
| 62 | options.append("Make a Resource Reservation")
|
|---|
| 63 | actions.append(lambda uid=user_id: make_reservation(uid))
|
|---|
| 64 |
|
|---|
| 65 | if role == "Administrator":
|
|---|
| 66 | options.append("Approve or Reject Reservations")
|
|---|
| 67 | actions.append(lambda uid=user_id: approve_reservations(uid))
|
|---|
| 68 | options.append("View Resource Usage Analytics")
|
|---|
| 69 | actions.append(lambda: view_analytics())
|
|---|
| 70 | options.append("Register a New User")
|
|---|
| 71 | actions.append(lambda: register_user())
|
|---|
| 72 |
|
|---|
| 73 | options.append("Logout")
|
|---|
| 74 |
|
|---|
| 75 | idx = pick_from_list(" Choose an option: ", options)
|
|---|
| 76 | if idx is None or options[idx] == "Logout":
|
|---|
| 77 | print(" Logged out.\n")
|
|---|
| 78 | return
|
|---|
| 79 |
|
|---|
| 80 |
|
|---|
| 81 | def main():
|
|---|
| 82 | print("\n" + "=" * 55)
|
|---|
| 83 | print(" FRRUAS - Faculty Resource Reservation")
|
|---|
| 84 | print(" and Usage Analytics System")
|
|---|
| 85 | print("=" * 55)
|
|---|
| 86 |
|
|---|
| 87 | try:
|
|---|
| 88 | while True:
|
|---|
| 89 | user = login()
|
|---|
| 90 | if user is None:
|
|---|
| 91 | print(" Goodbye.")
|
|---|
| 92 | break
|
|---|
| 93 | user_id, first_name, last_name, role = user
|
|---|
| 94 | main_menu(user_id, first_name, last_name, role)
|
|---|
| 95 | except KeyboardInterrupt:
|
|---|
| 96 | print("\n\n Goodbye.")
|
|---|
| 97 | finally:
|
|---|
| 98 | close_pool()
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 | if __name__ == "__main__":
|
|---|
| 102 | main()
|
|---|