File size: 3,058 Bytes
7de8fc8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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