-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
144 lines (118 loc) · 4.47 KB
/
app.py
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
from flask import Flask, jsonify, request , render_template , redirect
from flask_sqlalchemy import SQLAlchemy
import json
app = Flask(__name__,static_folder="static")
# Configure the SQLite database
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///devices.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
# Define the Device model
class Device(db.Model):
id = db.Column(db.Integer, primary_key=True)
description = db.Column(db.String(200), nullable=False)
date=db.Column(db.Date())
class_section = db.Column(db.String(100), nullable=False)
make = db.Column(db.String(100), nullable=False)
condition = db.Column(db.String(100), nullable=False)
condition_remark = db.Column(db.String(200))
serial_no = db.Column(db.String(100), nullable=False)
school_serial_no = db.Column(db.String(100), nullable=False)
# Initialize the database
with app.app_context():
db.create_all()
@app.route('/')
def index():
return render_template('index.html')
@app.route('/credits')
def credits():
return render_template('credits.html')
@app.route('/adddevice')
def add():
return render_template('add_device.html')
# Get all devices
@app.route('/api/devices', methods=['GET'])
def get_devices():
class_section = request.args.get('class_section', None)
if class_section:
devices = Device.query.filter_by(class_section=class_section).all()
else:
devices = Device.query.all()
return jsonify([{
'id': device.id,
'description': device.description,
'date': device.date.strftime('%Y-%m-%d') if device.date else None,
'class_section': device.class_section,
'make': device.make,
'condition': device.condition,
'condition_remark': device.condition_remark,
'serial_no': device.serial_no,
'school_serial_no': device.school_serial_no
} for device in devices])
@app.route('/api/devices/<int:id>', methods=['GET'])
def get_device(id):
device = Device.query.get_or_404(id)
# Convert device data to JSON
device_data = {
'id': device.id,
'description': device.description,
'date': device.date.strftime('%Y-%m-%d'), # Format the date as a string
'class_section': device.class_section,
'make': device.make,
'condition': device.condition,
'condition_remark': device.condition_remark,
'serial_no': device.serial_no,
'school_serial_no': device.school_serial_no
}
return jsonify(device_data)
# Add a new device
from datetime import datetime
@app.route('/api/devices', methods=['POST'])
def add_device():
data = request.json
date_str = data.get('date')
# Convert date from string to Python datetime object
device_date = datetime.strptime(date_str, '%Y-%m-%d').date() if date_str else None
new_device = Device(
description=data['description'],
date=device_date, # Save the parsed date
class_section=data['class_section'],
make=data['make'],
condition=data['condition'],
condition_remark=data.get('condition_remark', ''),
serial_no=data['serial_no'],
school_serial_no=data['school_serial_no']
)
db.session.add(new_device)
db.session.commit()
return jsonify({'message': 'Device added successfully!'}), 201
# Edit a device
@app.route('/api/devices/<int:id>', methods=['PUT'])
def edit_device(id):
device = Device.query.get_or_404(id)
data = request.json
date_str = data.get('date')
# Parse the date string if provided
device.date = datetime.strptime(date_str, '%Y-%m-%d').date() if date_str else device.date
device.description = data['description']
device.class_section = data['class_section']
device.make = data['make']
device.condition = data['condition']
device.condition_remark = data.get('condition_remark', '')
device.serial_no = data['serial_no']
device.school_serial_no = data['school_serial_no']
db.session.commit()
return jsonify({'message': 'Device updated successfully!'})
@app.route('/api/classes')
def clasin():
with open ("static/classes.json","r") as file:
classes=json.load(file)
return jsonify(classes)
# Delete a device
@app.route('/api/devices/<int:id>', methods=['DELETE'])
def delete_device(id):
device = Device.query.get_or_404(id)
db.session.delete(device)
db.session.commit()
return jsonify({'message': 'Device deleted successfully!'})
if __name__ == '__main__':
app.run(debug=True)