182 lines
6.4 KiB
Python
182 lines
6.4 KiB
Python
|
|
#!/usr/bin/env python3
|
||
|
|
"""
|
||
|
|
Working web server for tablet management system
|
||
|
|
"""
|
||
|
|
|
||
|
|
from http.server import HTTPServer, BaseHTTPRequestHandler
|
||
|
|
import sqlite3
|
||
|
|
from urllib.parse import urlparse, parse_qs
|
||
|
|
import json
|
||
|
|
|
||
|
|
class TabletHandler(BaseHTTPRequestHandler):
|
||
|
|
def _set_headers(self, status=200, content_type='text/html'):
|
||
|
|
self.send_response(status)
|
||
|
|
self.send_header('Content-type', content_type)
|
||
|
|
self.end_headers()
|
||
|
|
|
||
|
|
def do_GET(self):
|
||
|
|
parsed = urlparse(self.path)
|
||
|
|
|
||
|
|
if parsed.path == '/':
|
||
|
|
self._serve_main_page()
|
||
|
|
elif parsed.path == '/api/tablets':
|
||
|
|
self._serve_tablets()
|
||
|
|
elif parsed.path == '/api/users':
|
||
|
|
self._serve_users()
|
||
|
|
elif parsed.path == '/api/loans':
|
||
|
|
self._serve_loans()
|
||
|
|
else:
|
||
|
|
self._set_headers(404)
|
||
|
|
self.wfile.write(b'404 Not Found')
|
||
|
|
|
||
|
|
def _serve_main_page(self):
|
||
|
|
html = '''<!DOCTYPE html>
|
||
|
|
<html>
|
||
|
|
<head>
|
||
|
|
<title>Tablet Management System</title>
|
||
|
|
<style>
|
||
|
|
body { font-family: Arial, sans-serif; margin: 20px; }
|
||
|
|
h1 { color: #4CAF50; }
|
||
|
|
.container { max-width: 800px; margin: 0 auto; }
|
||
|
|
.section { margin-bottom: 20px; padding: 15px; background: #f5f5f5; border-radius: 5px; }
|
||
|
|
table { width: 100%; border-collapse: collapse; }
|
||
|
|
th, td { padding: 8px; text-align: left; border-bottom: 1px solid #ddd; }
|
||
|
|
th { background-color: #4CAF50; color: white; }
|
||
|
|
.btn { padding: 8px 16px; background-color: #4CAF50; color: white; text-decoration: none; border-radius: 4px; }
|
||
|
|
.btn-danger { background-color: #f44336; }
|
||
|
|
</style>
|
||
|
|
</head>
|
||
|
|
<body>
|
||
|
|
<div class="container">
|
||
|
|
<h1>Tablet Management System</h1>
|
||
|
|
<p>SQLite backend - Port 8000</p>
|
||
|
|
|
||
|
|
<div class="section">
|
||
|
|
<h2>Available Tablets</h2>
|
||
|
|
<div id="tablets"></div>
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<div class="section">
|
||
|
|
<h2>Active Loans</h2>
|
||
|
|
<div id="loans"></div>
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<div class="section">
|
||
|
|
<h2>System Info</h2>
|
||
|
|
<p>✓ Database: tablets.db</p>
|
||
|
|
<p>✓ Status: Operational</p>
|
||
|
|
<p>Port: 8000</p>
|
||
|
|
</div>
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<script>
|
||
|
|
// Load data from API
|
||
|
|
fetch('/api/tablets')
|
||
|
|
.then(r => r.json())
|
||
|
|
.then(data => {
|
||
|
|
let html = '<table><thead><tr><th>ID</th><th>Brand</th><th>Model</th><th>Serial</th><th>Status</th></tr></thead><tbody>';
|
||
|
|
data.forEach(tablet => {
|
||
|
|
html += `<tr><td>${tablet.id}</td><td>${tablet.brand}</td><td>${tablet.model}</td><td>${tablet.serial_number}</td><td>${tablet.status}</td></tr>`;
|
||
|
|
});
|
||
|
|
html += '</tbody></table>';
|
||
|
|
document.getElementById('tablets').innerHTML = html;
|
||
|
|
});
|
||
|
|
|
||
|
|
fetch('/api/loans')
|
||
|
|
.then(r => r.json())
|
||
|
|
.then(data => {
|
||
|
|
let html = '<table><thead><tr><th>ID</th><th>Tablet</th><th>User</th><th>Loan Date</th><th>Status</th></tr></thead><tbody>';
|
||
|
|
data.forEach(loan => {
|
||
|
|
html += `<tr><td>${loan.id}</td><td>${loan.tablet}</td><td>${loan.user}</td><td>${loan.loan_date}</td><td>${loan.status}</td></tr>`;
|
||
|
|
});
|
||
|
|
html += '</tbody></table>';
|
||
|
|
document.getElementById('loans').innerHTML = html;
|
||
|
|
});
|
||
|
|
</script>
|
||
|
|
</body>
|
||
|
|
</html>'''
|
||
|
|
|
||
|
|
self._set_headers()
|
||
|
|
self.wfile.write(html.encode('utf-8'))
|
||
|
|
|
||
|
|
def _serve_tablets(self):
|
||
|
|
try:
|
||
|
|
conn = sqlite3.connect('tablets.db')
|
||
|
|
conn.row_factory = sqlite3.Row
|
||
|
|
cursor = conn.cursor()
|
||
|
|
cursor.execute('SELECT * FROM tablets')
|
||
|
|
tablets = [dict(row) for row in cursor.fetchall()]
|
||
|
|
conn.close()
|
||
|
|
|
||
|
|
self._set_headers(content_type='application/json')
|
||
|
|
self.wfile.write(json.dumps(tablets).encode('utf-8'))
|
||
|
|
except Exception as e:
|
||
|
|
self._set_headers(500)
|
||
|
|
self.wfile.write(json.dumps({'error': str(e)}).encode('utf-8'))
|
||
|
|
|
||
|
|
def _serve_users(self):
|
||
|
|
try:
|
||
|
|
conn = sqlite3.connect('tablets.db')
|
||
|
|
conn.row_factory = sqlite3.Row
|
||
|
|
cursor = conn.cursor()
|
||
|
|
cursor.execute('SELECT * FROM users')
|
||
|
|
users = [dict(row) for row in cursor.fetchall()]
|
||
|
|
conn.close()
|
||
|
|
|
||
|
|
self._set_headers(content_type='application/json')
|
||
|
|
self.wfile.write(json.dumps(users).encode('utf-8'))
|
||
|
|
except Exception as e:
|
||
|
|
self._set_headers(500)
|
||
|
|
self.wfile.write(json.dumps({'error': str(e)}).encode('utf-8'))
|
||
|
|
|
||
|
|
def _serve_loans(self):
|
||
|
|
try:
|
||
|
|
conn = sqlite3.connect('tablets.db')
|
||
|
|
cursor = conn.cursor()
|
||
|
|
cursor.execute('''
|
||
|
|
SELECT l.id, l.tablet_id, l.user_id, l.loan_date, l.return_date, l.status,
|
||
|
|
t.brand || ' ' || t.model as tablet,
|
||
|
|
u.name as user
|
||
|
|
FROM loans l
|
||
|
|
JOIN tablets t ON l.tablet_id = t.id
|
||
|
|
JOIN users u ON l.user_id = u.id
|
||
|
|
''')
|
||
|
|
|
||
|
|
loans = []
|
||
|
|
for row in cursor.fetchall():
|
||
|
|
loans.append({
|
||
|
|
'id': row[0],
|
||
|
|
'tablet_id': row[1],
|
||
|
|
'user_id': row[2],
|
||
|
|
'loan_date': row[3],
|
||
|
|
'return_date': row[4],
|
||
|
|
'status': row[5],
|
||
|
|
'tablet': row[6],
|
||
|
|
'user': row[7]
|
||
|
|
})
|
||
|
|
conn.close()
|
||
|
|
|
||
|
|
self._set_headers(content_type='application/json')
|
||
|
|
self.wfile.write(json.dumps(loans).encode('utf-8'))
|
||
|
|
except Exception as e:
|
||
|
|
self._set_headers(500)
|
||
|
|
self.wfile.write(json.dumps({'error': str(e)}).encode('utf-8'))
|
||
|
|
|
||
|
|
def run_server():
|
||
|
|
server_address = ('', 8000)
|
||
|
|
httpd = HTTPServer(server_address, TabletHandler)
|
||
|
|
|
||
|
|
print("Tablet Management System - Web Interface")
|
||
|
|
print("========================================")
|
||
|
|
print("Server running on: http://localhost:8000")
|
||
|
|
print("Database: tablets.db (SQLite)")
|
||
|
|
print("Press Ctrl+C to stop the server")
|
||
|
|
print()
|
||
|
|
|
||
|
|
try:
|
||
|
|
httpd.serve_forever()
|
||
|
|
except KeyboardInterrupt:
|
||
|
|
print("\nServer stopped")
|
||
|
|
|
||
|
|
if __name__ == '__main__':
|
||
|
|
run_server()
|