-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
78 lines (61 loc) · 2.3 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
#!/usr/bin/env python3
from app_setup import app
from hash_model import checkForUpdate
from device_model import Device
from flask_cors import CORS
from urllib.parse import unquote
CORS(app)
app.app_context().push()
checkForUpdate()
def convertToJSON(deviceList):
return ([r.dictRep() for r in deviceList])
with app.app_context():
grouped_iPhones = convertToJSON(Device.query.filter(Device.name.contains('iPhone')).order_by(Device.id).all())
grouped_iPads = convertToJSON(Device.query.filter(Device.name.contains('iPad')).all())
grouped_Macs = convertToJSON(Device.query.filter(Device.name.contains('Mac')).all())
grouped_iPods = convertToJSON(Device.query.filter(Device.name.contains('iPod')).all())
grouped_Watches = convertToJSON(Device.query.filter(Device.name.contains('Watch')).all())
column_headers = ["iOS", "iPadOS", "MacOS / WatchOS"]
# Get latest firmwares for each OS type
latest_firmwares = {
"iOS": "",
"iPadOS": "",
"MacOS": "",
"WatchOS": "",
}
try:
if grouped_iPhones:
latest_firmwares["iOS"] = grouped_iPhones[-1]['firmware']
except (IndexError, KeyError):
pass
try:
if grouped_iPads:
latest_firmwares["iPadOS"] = grouped_iPads[-1]['firmware']
except (IndexError, KeyError):
pass
try:
if grouped_Macs:
latest_firmwares["MacOS"] = grouped_Macs[-1]['firmware']
except (IndexError, KeyError):
pass
try:
if grouped_Watches:
latest_firmwares["WatchOS"] = grouped_Watches[-1]['firmware']
except (IndexError, KeyError):
pass
@ app.route("/")
@ app.route("/home")
def home():
return { 'devices' : [{'name': 'iOS', 'data' : [*grouped_iPhones[::-1], *grouped_iPods[::-1]]}, {'name': 'iPadOS', 'data': grouped_iPads[::-1]}, {'name': 'MacOS/WatchOS', 'data': [*grouped_Macs, *grouped_Watches[::-1]]}], 'firmwares': latest_firmwares}
@app.route("/getDeviceByName/<device_name>")
def getDeviceByName(device_name):
decoded_device_name = unquote(device_name)
print("Searching for device with name:", decoded_device_name)
first_entry = Device.query.filter(Device.name == decoded_device_name).first()
print(first_entry)
if first_entry:
return first_entry.dictRep()
else:
return f"No device found with name: {decoded_device_name}", 404
if __name__ == "__main__":
app.run(debug=False)