")
def _handle_index(self):
with get_db() as conn:
cursor = conn.cursor()
# Get available tablets
cursor.execute("SELECT * FROM tablets WHERE status = 'available'")
available_tablets = cursor.fetchall()
# Get active loans
cursor.execute('''
SELECT l.id, t.brand, t.model, t.serial_number, u.name, l.loan_date
FROM loans l
JOIN tablets t ON l.tablet_id = t.id
JOIN users u ON l.user_id = u.id
WHERE l.status = 'active'
''')
active_loans = cursor.fetchall()
html = f"""
Tablet Management System
")
def _handle_loan_tablet_form(self):
with get_db() as conn:
cursor = conn.cursor()
cursor.execute("SELECT * FROM tablets WHERE status = 'available'")
available_tablets = cursor.fetchall()
cursor.execute("SELECT * FROM users")
users = cursor.fetchall()
html = """
Loan Tablet
Loan Tablet
"""
self._send_html(html)
def _handle_loan_tablet(self):
form_data = self._parse_form_data()
tablet_id = form_data['tablet_id'][0]
user_id = form_data['user_id'][0]
with get_db() as conn:
cursor = conn.cursor()
# Update tablet status
cursor.execute("UPDATE tablets SET status = 'loaned' WHERE id = ?", (tablet_id,))
# Create loan record
loan_date = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
cursor.execute('''
INSERT INTO loans (tablet_id, user_id, loan_date, status)
VALUES (?, ?, ?, 'active')
''', (tablet_id, user_id, loan_date))
conn.commit()
self.send_response(303)
self.send_header('Location', '/')
self.end_headers()
def _handle_return_tablet(self, path):
loan_id = path.split('/')[-1]
with get_db() as conn:
cursor = conn.cursor()
# Get loan information
cursor.execute("SELECT tablet_id FROM loans WHERE id = ?", (loan_id,))
loan = cursor.fetchone()
if loan:
tablet_id = loan['tablet_id']
# Update loan status and return date
return_date = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
cursor.execute('''
UPDATE loans SET status = 'returned', return_date = ? WHERE id = ?
''', (return_date, loan_id))
# Update tablet status
cursor.execute("UPDATE tablets SET status = 'available' WHERE id = ?", (tablet_id,))
conn.commit()
self.send_response(303)
self.send_header('Location', '/')
self.end_headers()
def _handle_history(self):
with get_db() as conn:
cursor = conn.cursor()
cursor.execute('''
SELECT l.id, t.brand, t.model, t.serial_number, u.name,
l.loan_date, l.return_date, l.status
FROM loans l
JOIN tablets t ON l.tablet_id = t.id
JOIN users u ON l.user_id = u.id
ORDER BY l.loan_date DESC
''')
loans = cursor.fetchall()
html = """
Loan History
"""
self._send_html(html)
def run_server():
"""Run the HTTP server"""
server_address = ('', 8000)
httpd = HTTPServer(server_address, SimpleHTTPRequestHandler)
print(f"Server running on http://localhost:8000")
print("Press Ctrl+C to stop the server")
httpd.serve_forever()
if __name__ == '__main__':
# Initialize database
init_db()
# Run the server
run_server()