Skip to content

Commit 6cbbf00

Browse files
Update app.py
1 parent f36ae75 commit 6cbbf00

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

app.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from flask import Flask, render_template, request, jsonify, session, redirect, url_for
2-
from flask_session import Session
32
from werkzeug.security import generate_password_hash, check_password_hash
43
import smtplib
54
import random
@@ -17,12 +16,7 @@
1716

1817
app = Flask(__name__)
1918
app.secret_key = os.getenv('SECRET_KEY') # Load secret key from .env
20-
app.config['SESSION_TYPE'] = 'filesystem'
21-
app.config['SESSION_PERMANENT'] = True
22-
app.config['SESSION_FILE_DIR'] = './flask_sessions' # Folder to store session files
23-
app.config['SESSION_USE_SIGNER'] = True
24-
app.config['SESSION_KEY_PREFIX'] = 'session:' # Prefix to distinguish session files
25-
Session(app)
19+
2620
# Construct Firebase credentials from environment variables
2721
firebase_credentials = {
2822
"type": os.getenv("FIREBASE_TYPE"),
@@ -241,14 +235,23 @@ def load_tasks():
241235
tasks[doc.id] = data['slots']
242236
return tasks
243237

244-
# Forgot Password
238+
def rearrange_slots(email, date):
239+
"""
240+
Rearrange the slots for a given date in ascending order of slot deadlines.
241+
"""
242+
doc_ref = tasks_ref.document(email).collection("tasks").document(date)
243+
doc = doc_ref.get()
244+
if doc.exists:
245+
slots = doc.to_dict().get('slots', [])
246+
slots.sort(key=lambda slot: datetime.strptime(slot['deadline'], "%H:%M"))
247+
doc_ref.set({'slots': slots})
245248

246249
@app.route('/forgot_password', methods=['GET', 'POST'])
247250
def forgot_password():
248251
if request.method == 'POST':
249252
email = request.form.get('email')
250253
user_doc = users_ref.document(email).get()
251-
if user_doc.exists:
254+
if user_doc.exists():
252255
otp = random.randint(100000, 999999)
253256
send_otp(email, otp)
254257
session['reset_password'] = {'email': email, 'otp': otp}
@@ -293,10 +296,6 @@ def get_user_details():
293296
return jsonify({'name': user_data.get('username', ''), 'email': email})
294297
return jsonify({'error': 'User not logged in'}), 401
295298

296-
297-
298-
# ...existing code...
299-
300299
@app.route('/add_slot', methods=['POST'])
301300
def add_slot():
302301
if 'user' not in session:
@@ -311,7 +310,8 @@ def add_slot():
311310
slots = doc.to_dict().get('slots', []) if doc.exists else []
312311
slots.append({'deadline': slot_deadline, 'tasks': []})
313312
doc_ref.set({'slots': slots})
314-
313+
rearrange_slots(email, date) # Rearrange slots after adding
314+
315315
return jsonify({"status": "success"})
316316

317317
@app.route('/add_task', methods=['POST'])
@@ -330,6 +330,7 @@ def add_task():
330330
if slot_index < len(slots):
331331
slots[slot_index]['tasks'].append({'task': task_name, 'checked': False, 'progress': 0, 'deleted': False})
332332
doc_ref.set({'slots': slots})
333+
rearrange_slots(email, date) # Rearrange slots after modifying tasks
333334

334335
return jsonify({"status": "success"})
335336

@@ -398,6 +399,7 @@ def delete_slot():
398399
del slots[slot_index] # Remove the correct slot
399400
logging.debug(f"Deleted slot {slot_index} for date {date}")
400401
doc_ref.set({'slots': slots})
402+
rearrange_slots(email, date) # Rearrange slots after deletion
401403
else:
402404
logging.warning(f"Slot index {slot_index} out of range for date {date}")
403405
else:
@@ -422,6 +424,7 @@ def delete_task():
422424
del slots[slot_index]['tasks'][task_index] # Remove task completely
423425
logging.debug(f"Deleted task {task_index} in slot {slot_index} for date {date}")
424426
doc_ref.set({'slots': slots})
427+
rearrange_slots(email, date) # Rearrange slots after modifying tasks
425428
else:
426429
logging.warning(f"Task index {task_index} or slot index {slot_index} out of range for date {date}")
427430
else:

0 commit comments

Comments
 (0)