-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
159 lines (117 loc) · 4.72 KB
/
app.py
File metadata and controls
159 lines (117 loc) · 4.72 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import os
from dotenv import load_dotenv
from flask import Flask, render_template, request, url_for, session, jsonify, redirect
from contribute import process_submit_pr
from search import search
from util import fetch_data_from_github
# TODO: Add filters
# TODO: OpenVault API for developers?
load_dotenv()
app = Flask(__name__)
app.secret_key = os.getenv("SECRET_KEY")
@app.context_processor
def inject_active_route():
# Provide the current route name to all templates
# print(session["curr_template"])
return {"active_route": session["curr_template"]}
@app.route("/")
def index():
session["curr_template"] = "ftc/index.html"
return render_template("ftc/index.html")
@app.route("/contribute")
def contribute():
session["curr_template"] = "ftc/contribute.html"
return render_template("ftc/contribute.html", submitted=False)
@app.route("/<base>/<category>")
def render_page(base, category):
records = fetch_data_from_github(base, category)
session["records"] = records
session["curr_template"] = f"ftc/{base}/{category}.html"
session["base"] = base
session["category"] = category
return render_template(session["curr_template"], records=records)
# For legacy purposes/ease of use. You can access portfolios through /portfolios/portfolios
# Simply redirects to /portfolios/portfolios
@app.route("/portfolios")
def portfolios():
session["curr_template"] = "ftc/portfolios/portfolios.html"
return redirect(url_for("render_page", base="portfolios", category="portfolios"))
@app.route("/api/search", methods=["POST"])
def search_api():
search_query = request.json.get("query", "").strip()
curr_template = session.get("curr_template")
base = session.get("base")
category = session.get("category")
# Always fetch fresh data for search to ensure we have the latest content
if base and category:
records = fetch_data_from_github(base, category)
# Update session with fresh data
session["records"] = records
else:
records = session.get("records")
if not records:
return jsonify({"error": "No records available"}), 400
if not search_query:
return (
jsonify({"template": render_template(curr_template, records=records)}),
200,
)
try:
# Use new Whoosh-based search
similarities, ranked_indices = search(search_query, records=records)
# Filter results with meaningful similarity scores
threshold = 0.01
filtered = [
(ranked_indices[i], similarities[i])
for i in range(len(similarities))
if similarities[i] >= threshold
]
# Sort by similarity score (already sorted by Whoosh, but ensuring order)
filtered = sorted(filtered, key=lambda x: x[1], reverse=True)
filtered_records = [records[i] for i, _ in filtered]
rendered_template = render_template(curr_template, records=filtered_records)
return jsonify({"template": rendered_template})
except Exception as e:
print(f"Search error: {e}")
# Fallback to original records on error
rendered_template = render_template(curr_template, records=records)
return jsonify({"template": rendered_template})
@app.route("/api/refresh-search-index", methods=["POST"])
def refresh_search_index():
"""Manually refresh the search index with latest data from GitHub"""
base = session.get("base")
category = session.get("category")
if not base or not category:
return jsonify({"error": "No active category to refresh"}), 400
try:
# Fetch fresh data from GitHub
records = fetch_data_from_github(base, category)
session["records"] = records
# Force rebuild of search index by clearing the hash
from search import force_index_rebuild
force_index_rebuild()
return jsonify(
{
"success": True,
"message": f"Search index refreshed with {len(records)} records",
}
)
except Exception as e:
return jsonify({"error": f"Failed to refresh search index: {str(e)}"}), 500
@app.route("/api/search-stats", methods=["GET"])
def search_stats():
"""Get statistics about the current search index"""
records = session.get("records", [])
try:
from search import get_search_stats
stats = get_search_stats(records)
return jsonify(stats)
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/submit-pr", methods=["POST"])
def submit_pr():
"""Handles form submission and creates a PR."""
session["curr_template"] = "ftc/contribute.html"
return process_submit_pr(request)
if __name__ == "__main__":
app.run(debug=True)