File size: 1,582 Bytes
b74eb81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from flask import render_template, request, jsonify
from ..utils.helper import execute_qry
import json
import os

current_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
with open(os.path.join(current_dir, "utils", "index_queries.json"), "r") as file:
    queries = json.load(file)

def index():
    return render_template('index.html')

def search_page():
    """
    Search Page template displaying all department names
    """

    q = queries["department_name_query"]
    results = execute_qry(q,())
    departments =  [row[0] for row in results]
    return render_template("search.html", departments=departments)

def search():
    """
    search method that calls query in database to look up professors by name or by course. 
    """
    query = request.args.get("q", "").strip()
    mode = request.args.get("mode", "course")
    dept_filter = request.args.get("department", "all")
    sort_order = request.args.get("sort", "desc")

    if not query:
        return jsonify([])
    
    dept_param = None if dept_filter == "all" else dept_filter
    sort_asc = sort_order == "asc"

    if mode == "course":
        q = queries["procedure_call_1"]
    
    else: 
        q = queries["procedure_call_2"]
    
    results = execute_qry(q, (query, dept_param, sort_asc))

    formatted = []
    for prof in results:
        prof_data = {
            "first_name": prof[0],
            "last_name": prof[1],
            "rating": float(prof[2]) if prof[2] else 0,
        }
        
        formatted.append(prof_data)
            
    return jsonify(formatted)