Add the task editor as a separate page/route, task sorting?
This commit is contained in:
parent
15b5bd63e1
commit
ec2eb211cd
4 changed files with 217 additions and 59 deletions
102
app.py
102
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/<int:task_id>')
|
||||
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/<int:task_id>')
|
||||
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/<int:task_id>')
|
||||
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/<int:task_id>', 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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue