Coverage for tasks/views.py: 72.00%

75 statements  

« prev     ^ index     » next       coverage.py v7.17.5, created at 2026-11-28 11:01 -0800

1from typing import ClassVar 1a

2 

3from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin 1a

4from django.urls import reverse, reverse_lazy 1a

5from django.views.generic import ( 1a

6 CreateView, 

7 DeleteView, 

8 ListView, 

9 UpdateView, 

10) 

11 

12from .models import ToDoItem, ToDoList 1a

13 

14 

15class ListListView(LoginRequiredMixin, ListView): 1a

16 model = ToDoList 1a

17 template_name = "index.html" 1a

18 

19 def get_queryset(self): 1a

20 return ToDoList.objects.filter(owner=self.request.user) 1a

21 

22 paginate_by = 5 1a

23 

24 

25class ItemListView(LoginRequiredMixin, ListView): 1a

26 model = ToDoItem 1a

27 template_name = "lists/todo_list.html" 1a

28 

29 def get_queryset(self): 1a

30 return ToDoItem.objects.filter(todo_list_id=self.kwargs["list_id"]) 1a

31 

32 def get_context_data(self): 1a

33 context = super().get_context_data() 1a

34 context["todo_list"] = ToDoList.objects.get(id=self.kwargs["list_id"]) 1a

35 return context 1a

36 

37 paginate_by = 5 1a

38 

39 

40class ListCreateView(LoginRequiredMixin, CreateView): 1a

41 model = ToDoList 1a

42 fields: ClassVar[list[str]] = ["title"] 1a

43 template_name = "lists/todolist_new.html" 1a

44 

45 """ 1a

46 def form_valid(self, form): 

47 form.instance.owner = self.request.user 

48 return super().form_valid(form) 

49 """ 

50 

51 def get_context_data(self): 1a

52 # context = super(ListCreateView, self).get_context_data() 

53 context = super().get_context_data() 

54 context["title"] = "Add a new list" 

55 return context 

56 

57 

58class ItemCreateView(LoginRequiredMixin, CreateView): 1a

59 model = ToDoItem 1a

60 fields: ClassVar[list[str]] = [ 1a

61 "todo_list", 

62 "title", 

63 "description", 

64 "due_date", 

65 ] 

66 template_name = "lists/todoitem_new.html" 1a

67 

68 def get_initial(self): 1a

69 # initial_data = super(ItemCreateView, self).get_initial() 

70 initial_data = super().get_initial() 

71 todo_list = ToDoList.objects.get(id=self.kwargs["list_id"]) 

72 initial_data["todo_list"] = todo_list 

73 return initial_data 

74 

75 def get_context_data(self): 1a

76 # context = super(ItemCreateView, self).get_context_data() 

77 context = super().get_context_data() 

78 todo_list = ToDoList.objects.get(id=self.kwargs["list_id"]) 

79 context["todo_list"] = todo_list 

80 context["title"] = "Create a new item" 

81 return context 

82 

83 def get_success_url(self): 1a

84 return reverse("list", args=[self.object.todo_list_id]) 

85 

86 

87class ItemUpdateView(LoginRequiredMixin, UpdateView): 1a

88 model = ToDoItem 1a

89 fields: ClassVar[list[str]] = [ 1a

90 "todo_list", 

91 "title", 

92 "description", 

93 "due_date", 

94 ] 

95 template_name = "lists/todoitem_new.html" 1a

96 

97 def get_context_data(self): 1a

98 # context = super(ItemUpdateView, self).get_context_data() 

99 context = super().get_context_data() 

100 context["todo_list"] = self.object.todo_list 

101 context["title"] = "Edit item" 

102 return context 

103 

104 def get_success_url(self): 1a

105 return reverse("list", args=[self.object.todo_list_id]) 

106 

107 

108class ListDeleteView(LoginRequiredMixin, UserPassesTestMixin, DeleteView): 1a

109 model = ToDoList 1a

110 template_name = "lists/todolist_confirm_delete.html" 1a

111 success_url = reverse_lazy("home") 1a

112 

113 def test_func(self): 1a

114 obj = self.get_object() 1a

115 return obj.owner == self.request.user 1a

116 

117 

118# class ItemDeleteView(LoginRequiredMixin, UserPassesTestMixin, DeleteView): 

119class ItemDeleteView(LoginRequiredMixin, DeleteView): 1a

120 model = ToDoItem 1a

121 template_name = "lists/todoitem_confirm_delete.html" 1a

122 

123 """ 1a

124 def test_func(self): 

125 obj = self.get_object() 

126 return obj.todo_list.owner == self.request.user 

127 """ 

128 

129 def get_success_url(self): 1a

130 return reverse_lazy("list", args=[self.kwargs["list_id"]]) 1a

131 

132 def get_context_data(self, **kwargs): 1a

133 context = super().get_context_data(**kwargs) 

134 context["todo_list"] = self.object.todo_list 

135 return context