Repository navigation
Expand file tree
/
Copy pathflaskServer.py
More file actions
277 lines (209 loc) · 8.48 KB
/
Copy pathflaskServer.py
File metadata and controls
277 lines (209 loc) · 8.48 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
from flask import Flask, render_template, request, jsonify
import os
import json
'''from SMTplugins.pluginImports import *
from SMTplugins.Calendar.calendar_plugin import calendar_bp
from SMTplugins.Package.package_plugin import package_bp
'''
from flask import Flask, render_template
from flask import jsonify, request
import os
import json
import sqlite3
from SMTplugins.pluginImports import *
from SMTplugins.Calendar.calendar_plugin import calendar_bp
from SMTplugins.Package.package_plugin import package_bp
app = Flask(__name__)
#######PLUGINS#######
app.register_blueprint(clock_bp)
app.register_blueprint(fTemp_bp)
app.register_blueprint(weather_bp)
app.register_blueprint(timeZone_bp)
app.register_blueprint(date_bp)
app.register_blueprint(blackjack_bp) #Blackjack
app.register_blueprint(googleTasks_bp)
app.register_blueprint(spotify_bp)
app.register_blueprint(calendar_bp)
app.register_blueprint(package_bp)
app.register_blueprint(fakeLight_bp)
app.register_blueprint(arriving_today_bp)
#allows for easier starting of flask from start file
def run_flask():
app.run(debug=True, port=5000, use_reloader=False)
@app.route("/")
def clientHomeStart():
return render_template('start_page.html')
'''# This could be from a database for now its json layout_client.json
DATA_FILE = "layout_client.json"
with open(DATA_FILE, 'r') as f:
data = json.load(f)
# Passes the list of widgets to the template
return render_template('index.html', widgets=data['widgets'])
#return render_template("index.html");'''
@app.route("/<client_id>")
def clientHome(client_id):
conn = sqlite3.connect('smt_database.db')
conn.row_factory = sqlite3.Row
cursor = conn.cursor()
# Query the database for this specific client's widgets
query = '''
SELECT w.id, w.name, w.class, w.css_name, cl.row, cl.col
FROM widgets w
JOIN client_layouts cl ON w.id = cl.widget_id
WHERE cl.client_id = ?
'''
cursor.execute(query, (client_id,))
widgets = [dict(row) for row in cursor.fetchall()]
conn.close()
return render_template('index.html', widgets=widgets, client_id=client_id)
@app.route('/settings')
def settings():
conn = sqlite3.connect('smt_database.db')
conn.row_factory = sqlite3.Row
cursor = conn.cursor()
# Fetch all registered clients for the dropdown
cursor.execute("SELECT id, name FROM clients")
clients = cursor.fetchall()
# Fetch all available widgets to display in the box format
cursor.execute("SELECT * FROM widgets")
available_widgets = cursor.fetchall()
conn.close()
return render_template('settingspage.html', clients=clients, available_widgets=available_widgets)
@app.route('/api/get_layout/<client_id>')
def get_layout(client_id):
conn = sqlite3.connect('smt_database.db')
conn.row_factory = sqlite3.Row
cursor = conn.cursor()
cursor.execute("SELECT widget_id, row, col FROM client_layouts WHERE client_id = ?", (client_id,))
layout = [dict(row) for row in cursor.fetchall()]
conn.close()
return jsonify(layout)
# ── ADD THIS IMPORT at the top of flaskServer.py ──
# from flask import request
# ── ADD THIS ROUTE to flaskServer.py ──
@app.route('/api/save_layout', methods=['POST'])
def save_layout():
data = request.get_json()
client_id = data.get('client_id')
layout = data.get('layout', []) # [{widget_id, row, col}, ...]
if not client_id:
return jsonify({'error': 'client_id required'}), 400
conn = sqlite3.connect('smt_database.db')
cursor = conn.cursor()
# Replace the client's layout entirely
cursor.execute("DELETE FROM client_layouts WHERE client_id = ?", (client_id,))
for entry in layout:
cursor.execute(
"INSERT INTO client_layouts (client_id, widget_id, row, col) VALUES (?, ?, ?, ?)",
(client_id, entry['widget_id'], entry['row'], entry['col'])
)
conn.commit()
conn.close()
return jsonify({'message': f"Layout saved for {client_id} ({len(layout)} widgets)"})
@app.route('/api/get_prefs/<client_id>')
def get_prefs(client_id):
conn = sqlite3.connect('smt_database.db')
conn.row_factory = sqlite3.Row
cursor = conn.cursor()
# Fetch preferences for this specific client
cursor.execute("SELECT widget_id, prefs FROM widgetPrefs WHERE client_id = ?", (client_id,))
prefs = [dict(row) for row in cursor.fetchall()]
conn.close()
return jsonify(prefs)
@app.route('/api/save_prefs', methods=['POST'])
def save_prefs():
data = request.get_json()
client_id = data.get('client_id')
prefs_list = data.get('prefs', []) # Expects [{widget_id: "...", prefs: "..."}, ...]
if not client_id:
return jsonify({'error': 'client_id required'}), 400
conn = sqlite3.connect('smt_database.db')
cursor = conn.cursor()
for item in prefs_list:
w_id = item.get('widget_id')
val = item.get('prefs')
# Use an INSERT OR REPLACE (UPSERT) logic
# First, check if a preference exists
cursor.execute("SELECT 1 FROM widgetPrefs WHERE client_id = ? AND widget_id = ?", (client_id, w_id))
if cursor.fetchone():
cursor.execute(
"UPDATE widgetPrefs SET prefs = ? WHERE client_id = ? AND widget_id = ?",
(val, client_id, w_id)
)
else:
cursor.execute(
"INSERT INTO widgetPrefs (client_id, widget_id, prefs) VALUES (?, ?, ?)",
(client_id, w_id, val)
)
conn.commit()
conn.close()
return jsonify({'message': 'Preferences updated successfully'})
@app.route('/api/create_client', methods=['POST'])
def create_client():
data = request.get_json()
client_id = data.get('client_id')
if not client_id:
return jsonify({'error': 'Client ID is required'}), 400
try:
conn = sqlite3.connect('smt_database.db')
cursor = conn.cursor()
# Check if client already exists to avoid duplicates
cursor.execute("SELECT id FROM clients WHERE id = ?", (client_id,))
if cursor.fetchone():
conn.close()
return jsonify({'message': 'Client already exists', 'client_id': client_id}), 200
# Insert the new client into the clients table
# Note: 'name' is set to the ID as a default if your table requires a name
cursor.execute("INSERT INTO clients (id, name) VALUES (?, ?)", (client_id, client_id))
conn.commit()
conn.close()
return jsonify({'message': 'Client created successfully', 'client_id': client_id}), 201
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/api/rename_client', methods=['POST'])
def rename_client():
data = request.get_json()
client_id = data.get('client_id')
new_name = data.get('new_name')
conn = sqlite3.connect('smt_database.db')
cursor = conn.cursor()
cursor.execute("UPDATE clients SET name = ? WHERE id = ?", (new_name, client_id))
conn.commit()
conn.close()
return jsonify({"status": "success"})
@app.route('/api/delete_client', methods=['POST'])
def delete_client():
data = request.get_json()
client_id = data.get('client_id')
conn = sqlite3.connect('smt_database.db')
cursor = conn.cursor()
# Remove client and their specific layout settings
cursor.execute("DELETE FROM clients WHERE id = ?", (client_id,))
cursor.execute("DELETE FROM client_layouts WHERE client_id = ?", (client_id,))
cursor.execute("DELETE FROM widgetPrefs WHERE client_id = ?", (client_id,))
conn.commit()
conn.close()
return jsonify({"status": "success"})
# Slated for removal-- starting through start.py now
if __name__ == '__main__':
app.run(debug=True)
@app.route("/api/layout", methods=["GET"])
def get_layout():
with open("layout_client.json", "r") as file:
layout = json.load(file)
return jsonify(layout)
@app.route("/api/layout", methods=["POST"])
def save_layout():
new_layout = request.json
with open("layout_client.json", "w") as file:
json.dump(new_layout, file, indent=4)
return jsonify({"message": "Layout saved"})
@app.route("/api/layout/default", methods=["POST"])
def reset_layout():
with open("default_layout_client.json", "r") as file:
default_layout = json.load(file)
with open("layout_client.json", "w") as file:
json.dump(default_layout, file, indent=4)
return jsonify(default_layout)
if __name__ == '__main__':
app.run(debug=True)