Skip to content

Commit 0d2d896

Browse files
committed
Merge branch 'main' into restructuring-queue
2 parents cb14104 + 8b70926 commit 0d2d896

14 files changed

Lines changed: 569 additions & 167 deletions

File tree

app.py

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,12 @@ def update_design_image(design_id):
162162
)
163163

164164

165+
# @app.route("/design/<int:design_id>", methods=['GET'])
166+
# @jwt_required()
167+
# def get_design():
168+
# handler = Design(email=get_jwt_identity())
169+
# return handler.get_design(design_id=request.form.get('design_id'))
170+
165171
@app.route("/design/<int:design_id>", methods=['GET'])
166172
@jwt_required()
167173
def get_design(design_id):
@@ -376,27 +382,16 @@ def remove_scheduled_item(schedule_id):
376382
return jsonify({'error': str(e)}), 500
377383

378384
# UploadHistory-----------------------------------------------------------------------------------------------------------
379-
@app.route("/upload_history", methods=['GET', 'POST'])
380-
def handleUploadHistory():
381-
if request.method == 'GET':
382-
handler = UploadHistory()
383-
return handler.getAllUploadHistory()
384-
else: # POST
385-
try:
386-
data = request.json
387-
if not data:
388-
return jsonify("No data provided"), 400
389-
390-
valid_keys = {'design_id', 'attempt_time', 'file_size', 'status'}
391-
if not any(key in data for key in valid_keys):
392-
return jsonify("Missing a key"), 400
393385

394-
handler = UploadHistory()
395-
return handler.addNewUploadHistory(data)
396-
except Exception as e:
397-
print("Error processing request:", e)
398-
return jsonify("Invalid JSON data provided"), 400
386+
@app.route("/upload_history", methods=['GET'])
387+
@jwt_required()
388+
def get_upload_history_for_current_user():
389+
return jsonify(UploadHistory().getAllUploadHistory()), 200
399390

391+
@app.route("/upload_history/pagination", methods=['GET'])
392+
@jwt_required()
393+
def get_upload_history_paginated():
394+
return UploadHistory().getAllUploadHistoryPaginated()
400395

401396
@app.route("/upload_history/<int:history_id>", methods=['GET', 'PUT', 'DELETE'])
402397
def handleUploadHistoryById(history_id):
@@ -410,7 +405,7 @@ def handleUploadHistoryById(history_id):
410405
return jsonify("No data provided"), 400
411406

412407
# For partial updates, you might remove this check or adapt it
413-
valid_keys = {'design_id', 'attempt_time', 'file_size', 'status'}
408+
valid_keys = {'design_id', 'attempt_time', 'status'}
414409
if not any(key in data for key in valid_keys):
415410
return jsonify("Missing a key"), 400
416411

controller/queue_item.py

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
from flask import jsonify
2+
from flask_jwt_extended import jwt_required, get_jwt_identity
3+
4+
from controller.upload_history import UploadHistory
25
from controller.user import User
36
from model.queue_item import QueueItemDAO
7+
from datetime import datetime
8+
49

510

611
class QueueItem:
@@ -51,9 +56,20 @@ def addNewQueueItem(self, data):
5156
display_duration = data['display_duration']
5257
scheduled = data['scheduled']
5358
scheduled_at = data['scheduled_at']
59+
5460
dao = QueueItemDAO()
55-
queue_item = dao.addNewQueueItem(design_id, start_time, end_time, display_duration, scheduled, scheduled_at)
61+
queue_item = dao.addNewQueueItem(
62+
design_id, start_time, end_time,
63+
display_duration, scheduled, scheduled_at
64+
)
5665
result = self.make_json_one(queue_item)
66+
67+
UploadHistory().addNewUploadHistory({
68+
'design_id': design_id,
69+
'attempt_time': datetime.utcnow().isoformat(),
70+
'status': 'successful'
71+
})
72+
5773
return result
5874

5975
def getQueueItemById(self, queue_id):
@@ -102,7 +118,6 @@ def deleteQueueItemById(self, queue_id):
102118
def getScheduledDesigns(self):
103119
dao = QueueItemDAO()
104120
rows = dao.getScheduledDesigns()
105-
# Build a list of dicts, merging fields from both tables
106121
result = []
107122
for row in rows:
108123
item = {
@@ -154,6 +169,25 @@ def get_all_items_paginated(self, page, page_size):
154169

155170
return jsonify(error="Unauthorized. No token."), 401
156171

157-
def update_active_items(self):
158-
dao = QueueItemDAO()
159-
dao.updateActiveItems()
172+
## MOVE TO OTHER FILE
173+
@jwt_required()
174+
def getUserHistory(self):
175+
user_email = get_jwt_identity()
176+
177+
rows = QueueItemDAO().getByUserEmail(user_email)
178+
179+
result = []
180+
for (history_id, design_id, created_at,
181+
display_duration, display_order, status,
182+
title, pixel_data) in rows:
183+
result.append({
184+
'history_id': history_id,
185+
'design_id': design_id,
186+
'created_at': created_at,
187+
'display_duration': display_duration,
188+
'display_order': display_order,
189+
'status': 'scheduled' if status else 'pending',
190+
'title': title,
191+
'pixel_data': pixel_data,
192+
})
193+
return result

controller/upload_history.py

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,88 @@
1-
from flask import jsonify
1+
import math
2+
3+
from flask import jsonify, request
4+
from flask_jwt_extended import jwt_required, get_jwt_identity
25
from model.upload_history import UploadHistoryDAO
36

47
class UploadHistory:
58
def make_json(self, tuples):
6-
79
result = []
810
for t in tuples:
9-
D = {}
10-
D['history_id'] = t[0]
11-
D['design_id'] = t[1]
12-
D['attempt_time'] = t[2]
13-
D['file_size'] = t[3]
14-
D['status'] = t[4]
11+
D = {
12+
'history_id': t[0],
13+
'design_id': t[1],
14+
'attempt_time': t[2],
15+
'status': t[3],
16+
'title': t[4],
17+
'pixel_data': t[5]
18+
}
1519
result.append(D)
1620
return result
1721

1822
def make_json_one(self, row):
23+
return {
24+
'history_id': row[0],
25+
'design_id': row[1],
26+
'attempt_time': row[2],
27+
'status': row[3],
28+
'title': row[4],
29+
'pixel_data': row[5]
30+
}
1931

20-
result = {}
21-
result['history_id'] = row[0]
22-
result['design_id'] = row[1]
23-
result['attempt_time'] = row[2]
24-
result['file_size'] = row[3]
25-
result['status'] = row[4]
26-
return result
27-
32+
@jwt_required()
2833
def getAllUploadHistory(self):
34+
email = get_jwt_identity()
2935
dao = UploadHistoryDAO()
30-
rows = dao.getAllUploadHistory()
36+
rows = dao.getByUserEmail(email)
3137
return self.make_json(rows)
3238

3339
def addNewUploadHistory(self, data):
34-
3540
design_id = data['design_id']
3641
attempt_time = data['attempt_time']
37-
file_size = data['file_size']
3842
status = data['status']
39-
4043
dao = UploadHistoryDAO()
41-
row = dao.addNewUploadHistory(design_id, attempt_time, file_size, status)
44+
row = dao.addNewUploadHistory(design_id, attempt_time, status)
4245
return self.make_json_one(row)
4346

4447
def getUploadHistoryById(self, history_id):
4548
dao = UploadHistoryDAO()
4649
record = dao.getUploadHistoryById(history_id)
4750
if not record:
4851
return jsonify("Not Found"), 404
49-
else:
50-
return self.make_json_one(record)
52+
return self.make_json_one(record)
5153

5254
def updateUploadHistoryById(self, history_id, data):
5355
dao = UploadHistoryDAO()
5456
record = dao.updateUploadHistoryById(history_id, data)
5557
if not record:
5658
return jsonify("Not Found"), 404
57-
else:
58-
return self.make_json_one(record)
59+
return self.make_json_one(record)
5960

6061
def deleteUploadHistoryById(self, history_id):
6162
dao = UploadHistoryDAO()
6263
deleted_id = dao.deleteUploadHistoryById(history_id)
6364
if not deleted_id:
6465
return jsonify("Not Found"), 404
65-
else:
66-
return jsonify("Successfully deleted UploadHistory with ID " + str(deleted_id) + "!"), 200
66+
return jsonify(f"Successfully deleted UploadHistory with ID {deleted_id}!"), 200
67+
68+
@jwt_required()
69+
def getAllUploadHistoryPaginated(self):
70+
try:
71+
email = get_jwt_identity()
72+
page = int(request.args.get('page', 1))
73+
size = int(request.args.get('size', 6))
74+
dao = UploadHistoryDAO()
75+
76+
total = dao.countByUserEmail(email)
77+
rows = dao.getByUserEmailPaginated(email, page, size)
78+
items = self.make_json(rows)
79+
pages = math.ceil(total / size) if size > 0 else 0
80+
81+
return jsonify({
82+
'items': items,
83+
'page': page,
84+
'pages': pages
85+
}), 200
86+
except Exception as e:
87+
print(f"Error in paginated upload history: {e}")
88+
return jsonify({'error': 'Failed to fetch paginated upload history'}), 500

model/queue_item.py

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,26 @@ def getQueueItemById(self, queue_id):
3737
cursor.close()
3838
return result
3939

40-
def addNewQueueItem(self, design_id, start_time, end_time, display_duration, scheduled, scheduled_at):
4140

41+
def addNewQueueItem(self, design_id, start_time, end_time,
42+
display_duration, scheduled, scheduled_at):
4243
cursor = self.conn.cursor()
44+
cursor.execute("SELECT COUNT(*) FROM queue_item;")
45+
display_order = cursor.fetchone()[0] + 1
4346

44-
count_query = """ SELECT COUNT(*)
45-
FROM queue_item"""
46-
cursor.execute(count_query)
47-
items = cursor.fetchone()[0]
48-
display_order = items + 1
47+
cursor.execute("""
48+
INSERT INTO queue_item
49+
(design_id, start_time, end_time, display_duration, display_order, scheduled, scheduled_at)
50+
VALUES (?, ?, ?, ?, ?, ?, ?);
51+
""", (design_id, start_time, end_time, display_duration,
52+
display_order, scheduled, scheduled_at))
4953

50-
query = "insert into queue_item (design_id, start_time, end_time, display_duration, display_order, scheduled, scheduled_at) values (?, ?, ?, ?, ?, ?, ?);"
51-
cursor.execute(query,
52-
(design_id, start_time, end_time, display_duration, display_order, scheduled, scheduled_at))
5354
self.conn.commit()
54-
query = "select * from queue_item order by queue_id desc limit 1"
55-
cursor.execute(query)
56-
result = cursor.fetchone()
55+
56+
cursor.execute("SELECT * FROM queue_item ORDER BY queue_id DESC LIMIT 1;")
57+
row = cursor.fetchone()
5758
cursor.close()
58-
return result
59+
return row
5960

6061
def updateQueueItemById(self, queue_id, data):
6162
cursor = self.conn.cursor()
@@ -250,6 +251,32 @@ def get_all_items_paginated(self, page, page_size):
250251
"error": str(e)
251252
}
252253

254+
255+
def getByUserEmail(self, email):
256+
cursor = self.conn.cursor()
257+
query = """
258+
SELECT
259+
qi.queue_id AS history_id,
260+
qi.design_id,
261+
qi.created_at AS created_at,
262+
qi.display_duration,
263+
qi.display_order,
264+
qi.scheduled AS status,
265+
d.title,
266+
d.pixel_data
267+
FROM queue_item qi
268+
JOIN design d
269+
ON d.design_id = qi.design_id
270+
JOIN user u
271+
ON u.user_id = d.user_id
272+
WHERE u.email = ?
273+
ORDER BY qi.start_time DESC;
274+
"""
275+
cursor.execute(query, (email,))
276+
rows = cursor.fetchall()
277+
cursor.close()
278+
return rows
279+
253280
def is_design_in_queue(self, design_id):
254281
cursor = self.conn.cursor()
255282
query = "SELECT 1 FROM queue_item WHERE design_id = ?"
@@ -272,3 +299,4 @@ def is_design_scheduled(self, design_id):
272299
return False
273300
finally:
274301
cursor.close()
302+

0 commit comments

Comments
 (0)