Init commit
This commit is contained in:
54
files/scripts/app.py
Normal file
54
files/scripts/app.py
Normal file
@ -0,0 +1,54 @@
|
||||
from flask import Flask, jsonify, request
|
||||
import sqlite3
|
||||
import json
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
# Function to get all drive records from the database
|
||||
def get_all_drive_records():
|
||||
conn = sqlite3.connect('drive_records.db')
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("SELECT * FROM drive_records")
|
||||
rows = cursor.fetchall()
|
||||
conn.close()
|
||||
|
||||
drives = []
|
||||
for row in rows:
|
||||
drive = {
|
||||
'id': row[0],
|
||||
'serial': row[1],
|
||||
'model': row[2],
|
||||
'flavor': row[3],
|
||||
'capacity': row[4],
|
||||
'TBW': row[5],
|
||||
'smart': row[6]
|
||||
}
|
||||
drives.append(drive)
|
||||
return jsonify(drives)
|
||||
|
||||
# Function to check if a serial number exists in the database
|
||||
def check_serial_exists(serial):
|
||||
conn = sqlite3.connect('drive_records.db')
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("SELECT * FROM drive_records WHERE serial=?", (serial,))
|
||||
row = cursor.fetchone()
|
||||
conn.close()
|
||||
return bool(row)
|
||||
|
||||
# Route to check if a serial number exists in the database
|
||||
@app.route('/check', methods=['GET'])
|
||||
def check():
|
||||
serial_lookup = request.args.get('serial_lookup')
|
||||
if not serial_lookup:
|
||||
return jsonify({'error': 'No serial number provided'}), 400
|
||||
|
||||
exists = check_serial_exists(serial_lookup)
|
||||
return jsonify({'serial_number_exists': exists})
|
||||
|
||||
# Route to get all drive records in JSON format
|
||||
@app.route('/drives', methods=['GET'])
|
||||
def index():
|
||||
return get_all_drive_records()
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(debug=True, host='0.0.0.0', port=5000)
|
||||
Reference in New Issue
Block a user