Repository navigation
Expand file tree
/
Copy pathcsvSearch.py
More file actions
85 lines (65 loc) · 2.35 KB
/
Copy pathcsvSearch.py
File metadata and controls
85 lines (65 loc) · 2.35 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
import pandas as pd
from flask import Flask, request, jsonify, send_file
# Stores the 2 values users may sort by
class Criteria_Class:
def __init__(self, country, compNum):
self.country = country
self.compNum = compNum
def getCountry(self):
return self.country
def getCompNum(self):
return self.compNum
def setCountry(self, country):
self.country = country
def setCompNum(self, compNum):
self.compNum = compNum
# Check against the CSV if a comp number is taken
class Checker:
def __init__(self):
# Load once on startup
self.df = pd.read_csv("OGN.csv")
# Check if the comp number exists in that country
def check_for_cn(self, criteria):
# Normalize to string for robust matching
country = str(criteria.getCountry()).strip()
print(country)
target_cn = str(criteria.getCompNum()).strip()
# Filter by country
df = self.df.copy()
# Filter by country and comp number
filtered = df[(df["COUNTRY"].str.lower() == country.lower()) & (df["CN"] == target_cn.upper())]
if filtered.empty:
return {"taken": False}
else:
registration = filtered.loc[filtered.index[0], "REGISTRATION"]
model = filtered.loc[filtered.index[0], "AIRCRAFT_MODEL"]
info = {"taken": True, "model": model, "registration" : registration}
return (info)
def UI(self, cn, country):
criteria = Criteria_Class(country, cn)
return self.check_for_cn(criteria)
# --- Flask Web App ---
app = Flask(__name__)
checker = Checker()
@app.route("/")
def home():
# Serve the frontend HTML
return send_file("index.html")
@app.route("/main.js")
def home_js():
return send_file("main.js")
@app.route("/check", methods=["POST"])
def check_endpoint():
try:
data = request.get_json(force=True, silent=True) or {}
comp_num = data.get("compNum", "")
country = data.get("country", "")
if comp_num is None or country is None:
return jsonify({"error": "Missing compNum or country"}), 400
taken = checker.UI(comp_num, country)
return jsonify(taken)
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == "__main__":
# Run the web server
app.run(host="0.0.0.0", port=10000, debug=True)