Coverage for tasks/views.py: 71.62%
74 statements
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-11 15:56 -0700
« prev ^ index » next coverage.py v7.16.2, created at 2026-10-11 15:56 -0700
1from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin 1a
2from django.urls import reverse, reverse_lazy 1a
3from django.views.generic import ( 1a
4 CreateView,
5 DeleteView,
6 ListView,
7 UpdateView,
8)
10from .models import ToDoItem, ToDoList 1a
13class ListListView(LoginRequiredMixin, ListView): 1a
14 model = ToDoList 1a
15 template_name = "index.html" 1a
17 def get_queryset(self): 1a
18 return ToDoList.objects.filter(owner=self.request.user) 1d
20 paginate_by = 5 1a
23class ItemListView(LoginRequiredMixin, ListView): 1a
24 model = ToDoItem 1a
25 template_name = "lists/todo_list.html" 1a
27 def get_queryset(self): 1a
28 return ToDoItem.objects.filter(todo_list_id=self.kwargs["list_id"]) 1b
30 def get_context_data(self): 1a
31 context = super().get_context_data() 1b
32 context["todo_list"] = ToDoList.objects.get(id=self.kwargs["list_id"]) 1b
33 return context 1b
35 paginate_by = 5 1a
38class ListCreateView(LoginRequiredMixin, CreateView): 1a
39 model = ToDoList 1a
40 fields = ["title"] 1a
41 template_name = "lists/todolist_new.html" 1a
43 """ 1a
44 def form_valid(self, form):
45 form.instance.owner = self.request.user
46 return super().form_valid(form)
47 """
49 def get_context_data(self): 1a
50 context = super(ListCreateView, self).get_context_data()
51 context["title"] = "Add a new list"
52 return context
55class ItemCreateView(LoginRequiredMixin, CreateView): 1a
56 model = ToDoItem 1a
57 fields = [ 1a
58 "todo_list",
59 "title",
60 "description",
61 "due_date",
62 ]
63 template_name = "lists/todoitem_new.html" 1a
65 def get_initial(self): 1a
66 initial_data = super(ItemCreateView, self).get_initial()
67 todo_list = ToDoList.objects.get(id=self.kwargs["list_id"])
68 initial_data["todo_list"] = todo_list
69 return initial_data
71 def get_context_data(self): 1a
72 context = super(ItemCreateView, self).get_context_data()
73 todo_list = ToDoList.objects.get(id=self.kwargs["list_id"])
74 context["todo_list"] = todo_list
75 context["title"] = "Create a new item"
76 return context
78 def get_success_url(self): 1a
79 return reverse("list", args=[self.object.todo_list_id])
82class ItemUpdateView(LoginRequiredMixin, UpdateView): 1a
83 model = ToDoItem 1a
84 fields = [ 1a
85 "todo_list",
86 "title",
87 "description",
88 "due_date",
89 ]
90 template_name = "lists/todoitem_new.html" 1a
92 def get_context_data(self): 1a
93 context = super(ItemUpdateView, self).get_context_data()
94 context["todo_list"] = self.object.todo_list
95 context["title"] = "Edit item"
96 return context
98 def get_success_url(self): 1a
99 return reverse("list", args=[self.object.todo_list_id])
102class ListDeleteView(LoginRequiredMixin, UserPassesTestMixin, DeleteView): 1a
103 model = ToDoList 1a
104 template_name = "lists/todolist_confirm_delete.html" 1a
105 success_url = reverse_lazy("home") 1a
107 def test_func(self): 1a
108 obj = self.get_object() 1c
109 return obj.owner == self.request.user 1c
112# class ItemDeleteView(LoginRequiredMixin, UserPassesTestMixin, DeleteView):
113class ItemDeleteView(LoginRequiredMixin, DeleteView): 1a
114 model = ToDoItem 1a
115 template_name = "lists/todoitem_confirm_delete.html" 1a
117 """ 1a
118 def test_func(self):
119 obj = self.get_object()
120 return obj.todo_list.owner == self.request.user
121 """
123 def get_success_url(self): 1a
124 return reverse_lazy("list", args=[self.kwargs["list_id"]]) 1e
126 def get_context_data(self, **kwargs): 1a
127 context = super().get_context_data(**kwargs)
128 context["todo_list"] = self.object.todo_list
129 return context