import uuid from datetime import datetime, date def create_todo(text, priority="Medium", due_date=None): """Create a new todo item""" if due_date is None: due_date = date.today().strftime('%Y-%m-%d') return { 'id': str(uuid.uuid4()), 'text': text, 'completed': False, 'priority': priority, 'due_date': due_date, 'created_at': datetime.now().strftime('%Y-%m-%d %H:%M:%S') } def toggle_todo(todos, todo_id): """Toggle the completion status of a todo""" for todo in todos: if todo['id'] == todo_id: todo['completed'] = not todo['completed'] break return todos def delete_todo(todos, todo_id): """Delete a todo item""" return [todo for todo in todos if todo['id'] != todo_id] def update_todo(todos, todo_id, new_text, new_priority, new_due_date): """Update an existing todo""" for todo in todos: if todo['id'] == todo_id: todo['text'] = new_text todo['priority'] = new_priority todo['due_date'] = new_due_date break return todos def clear_completed_todos(todos): """Remove all completed todos""" return [todo for todo in todos if not todo['completed']] def filter_todos(todos, filter_type='all', search_query='', priorities=None): """Filter todos based on completion status and search query""" if priorities is None: priorities = ['High', 'Medium', 'Low'] filtered = todos # Filter by completion status if filter_type == 'active': filtered = [todo for todo in filtered if not todo['completed']] elif filter_type == 'completed': filtered = [todo for todo in filtered if todo['completed']] # Filter by priority filtered = [todo for todo in filtered if todo['priority'] in priorities] # Filter by search query if search_query: search_query = search_query.lower() filtered = [todo for todo in filtered if search_query in todo['text'].lower()] # Sort by priority and due date priority_order = {'High': 0, 'Medium': 1, 'Low': 2} filtered.sort(key=lambda x: (x['completed'], priority_order[x['priority']], x['due_date'])) return filtered def get_todo_stats(todos): """Get statistics about todos""" total = len(todos) completed = len([todo for todo in todos if todo['completed']]) pending = total - completed return { 'total': total, 'completed': completed, 'pending': pending } def get_priority_stats(todos): """Get statistics by priority""" stats = {'High': 0, 'Medium': 0, 'Low': 0} for todo in todos: if not todo['completed']: stats[todo['priority']] += 1 return stats def export_todos(todos): """Export todos to JSON format""" return json.dumps(todos, indent=2) def import_todos(json_data): """Import todos from JSON data""" try: return json.loads(json_data) except json.JSONDecodeError: return None