From ec2eb211cdd200790c8bc9c1ad746f7e66ad50e1 Mon Sep 17 00:00:00 2001 From: cdez Date: Tue, 25 Mar 2025 01:38:06 -0700 Subject: [PATCH] Add the task editor as a separate page/route, task sorting? --- app.py | 102 +++++++++++++++++++++++++++++++-- templates/completed_tasks.html | 38 +++++++----- templates/edit_task.html | 39 +++++++++++++ templates/index.html | 97 ++++++++++++++++++------------- 4 files changed, 217 insertions(+), 59 deletions(-) create mode 100644 templates/edit_task.html diff --git a/app.py b/app.py index 810a2e5..239ddbb 100644 --- a/app.py +++ b/app.py @@ -18,6 +18,7 @@ class Task(db.Model): status = db.Column(db.String(10), default='pending') due_date = db.Column(db.Date, nullable=True) # Due date for the task completed_at = db.Column(db.DateTime, nullable=True) # Timestamp for when the task was completed + start_time = db.Column(db.Time, nullable=True) # Start time for the task def format_duration(minutes): """Convert duration in minutes to 'H M' format.""" @@ -49,15 +50,19 @@ def index(): # Dynamically sort tasks based on the parameter if sort_by == 'priority': - tasks = Task.query.filter(Task.status == 'pending').order_by(Task.priority).all() + in_progress_tasks = Task.query.filter(Task.status == 'in_progress').order_by(Task.priority).all() + pending_tasks = Task.query.filter(Task.status == 'pending').order_by(Task.priority).all() elif sort_by == 'due_date': - tasks = Task.query.filter(Task.status == 'pending').order_by(Task.due_date).all() + in_progress_tasks = Task.query.filter(Task.status == 'in_progress').order_by(Task.due_date).all() + pending_tasks = Task.query.filter(Task.status == 'pending').order_by(Task.due_date).all() elif sort_by == 'duration': - tasks = Task.query.filter(Task.status == 'pending').order_by(Task.duration).all() + in_progress_tasks = Task.query.filter(Task.status == 'in_progress').order_by(Task.duration).all() + pending_tasks = Task.query.filter(Task.status == 'pending').order_by(Task.duration).all() else: - tasks = Task.query.filter(Task.status == 'pending').all() # Default: no sorting + in_progress_tasks = Task.query.filter(Task.status == 'in_progress').all() # Default: no sorting + pending_tasks = Task.query.filter(Task.status == 'pending').all() - return render_template('index.html', tasks=tasks, sort_by=sort_by) + return render_template('index.html', in_progress_tasks=in_progress_tasks, pending_tasks=pending_tasks, sort_by=sort_by) @app.route('/add_task', methods=['POST']) def add_task(): @@ -106,9 +111,47 @@ def complete_task(task_id): task.status = 'completed' task.completed_at = datetime.utcnow() # Store the timestamp in UTC db.session.commit() - socketio.emit('task_update', {'action': 'complete', 'task_id': task_id}) + # Emit a real-time update to notify all clients + socketio.emit('task_update', { + 'action': 'complete', + 'task_id': task_id, + 'status': 'completed' + }) return redirect(url_for('index')) +@app.route('/start_task/') +def start_task(task_id): + task = Task.query.get(task_id) + if task: + task.status = 'in_progress' + db.session.commit() + socketio.emit('task_update', {'action': 'start', 'task_id': task_id}) + return redirect(url_for('index')) + +@app.route('/move_to_pending/') +def move_to_pending(task_id): + task = Task.query.get(task_id) + if task and task.status == 'in_progress': # Ensure the task is currently "in_progress" + task.status = 'pending' + db.session.commit() + socketio.emit('task_update', {'action': 'move_to_pending', 'task_id': task_id}) + return redirect(url_for('index')) + +@app.route('/move_to_pending_from_completed/') +def move_to_pending_from_completed(task_id): + task = Task.query.get(task_id) + if task and task.status == 'completed': # Ensure the task is currently "completed" + task.status = 'pending' + task.completed_at = None # Clear the completed timestamp + db.session.commit() + # Emit a real-time update to all connected clients + socketio.emit('task_update', { + 'action': 'move_to_pending', + 'task_id': task_id, + 'status': 'pending' + }) + return redirect(url_for('completed_tasks')) + @app.route('/completed_tasks') def completed_tasks(): cleanup_completed_tasks() # Remove old completed tasks @@ -130,6 +173,53 @@ def completed_tasks(): return render_template('completed_tasks.html', tasks=tasks, sort_by=sort_by) +@app.route('/edit_task/', methods=['GET', 'POST']) +def edit_task(task_id): + task = Task.query.get(task_id) + if not task: + return "Task not found", 404 + + if request.method == 'POST': + # Update task details from the form + task.name = request.form['name'] + task.priority = request.form['priority'] + duration_input = request.form['duration'] + due_date_input = request.form['due_date'] + + # Parse duration (e.g., "10M" -> 10 minutes, "2H" -> 120 minutes) + if duration_input.endswith('M'): + task.duration = int(duration_input[:-1]) # Extract the number before 'M' + elif duration_input.endswith('H'): + task.duration = int(duration_input[:-1]) * 60 # Convert hours to minutes + else: + return "Invalid duration format. Use 'M' for minutes or 'H' for hours.", 400 + + # Parse due date (e.g., "2025-03-30") + try: + task.due_date = datetime.strptime(due_date_input, '%Y-%m-%d').date() if due_date_input else None + except ValueError: + return "Invalid due date format. Use 'YYYY-MM-DD'.", 400 + + db.session.commit() + return redirect(url_for('index')) + + return render_template('edit_task.html', task=task) + +@app.route('/update_task_order', methods=['POST']) +def update_task_order(): + data = request.get_json() + if not data or 'order' not in data: + return jsonify({'error': 'Invalid data'}), 400 + + # Update the order of tasks in the database (optional: add an "order" field to the Task model) + for index, task_id in enumerate(data['order']): + task = Task.query.get(task_id) + if task: + task.order = index # Assuming you add an "order" column to the Task model + db.session.commit() + + return jsonify({'success': True}) + if __name__ == '__main__': with app.app_context(): # Create database tables within the app context diff --git a/templates/completed_tasks.html b/templates/completed_tasks.html index 52a9d36..4cf3526 100644 --- a/templates/completed_tasks.html +++ b/templates/completed_tasks.html @@ -23,20 +23,30 @@ -
    - {% for task in tasks %} -
  • -
    - {{ task.name }} - (Priority: {{ task.priority }}, Duration: {{ format_duration(task.duration) }}) - {% if task.due_date %} - - Due: {{ task.due_date }} - {% endif %} - - Completed At: {{ task.completed_at }} -
    -
  • - {% endfor %} -
+ + + + + + + + + + + + {% for task in tasks %} + + + + + + + + {% endfor %} + +
NamePriorityDurationDue DateActions
{{ task.name }}{{ task.priority }}{{ format_duration(task.duration) }}{{ task.due_date }} + Move to Pending +
+ + \ No newline at end of file