Files
project-work/sources/tdd-task-list-plan.md
Eric Bell 2460343d79 add new sources
- permissions - all about the permission system
- selected task - requirements for the task
- list-wireframe - html mock of the page
- tdd - tdd step through implemention
2026-11-01 13:12:28 -05:00

12 KiB

TDD Plan: Task List View with Filtering, Sorting, Pagination

Use this in LiveCompare — each step = one test + implementation cycle


Test File Location

NearBeach/tests/tests_views/tests_task/test_task_list.py

Run Single Test Command

cd /workspace/nearbeach
python3 manage.py test NearBeach.tests.tests_views.tests_task.test_task_list.TestTaskList.<test_method> -v 2

STEP 1: URL + Template Rendering

TEST PROMPT (send to model)

Write a test test_task_list_url_accessible in test_task_list.py that:

  • Creates a superuser
  • Logs in the client
  • GETs /tasks/
  • Asserts status code 200
  • Asserts template NearBeach/tasks/task_list.html is used
  • Asserts "All Tasks" appears in response content

Run it — it should FAIL (no view/URL/template exists yet).

IMPL PROMPT (after test fails)

Implement the minimal code to make test_task_list_url_accessible pass:

  1. Add task_list(request) view in NearBeach/views/task_views.py with @login_required, returns render of NearBeach/tasks/task_list.html with context {"nearbeach_title": "All Tasks"}
  2. Add URL route path("tasks/", views.task_list, name="task_list") in NearBeach/urls.py
  3. Create minimal template NearBeach/templates/NearBeach/tasks/task_list.html extending base, showing "All Tasks" heading
  4. Run the test again — should PASS

STEP 2: Permission Filtering (Core)

TEST PROMPT

Write test test_task_list_filters_by_user_projects:

  • Create two users: user_alpha, user_beta
  • Create two groups: group_alpha, group_beta with permission_set__task=2
  • Create two projects: project_alpha, project_beta
  • Create ObjectAssignment(group_alpha, project_alpha) and ObjectAssignment(group_beta, project_beta)
  • Create tasks: task_alpha in project_alpha, task_beta in project_beta
  • Login as user_alpha → GET /tasks/ → assert task_alpha in response, task_beta NOT in response
  • Login as user_beta → GET /tasks/ → assert task_beta in response, task_alpha NOT in response
  • Login as superuser → GET /tasks/ → assert BOTH tasks in response

IMPL PROMPT

Update task_list view to filter tasks by user's project access via ObjectAssignment:

  1. Get user's groups: UserGroup.objects.filter(is_deleted=False, username=request.user).values('group_id')
  2. Get accessible project IDs: ObjectAssignment.objects.filter(is_deleted=False, group_id__in=user_groups, project_id__isnull=False).values_list('project_id', flat=True).distinct()
  3. If superuser: Task.objects.filter(is_deleted=False) else: Task.objects.filter(is_deleted=False, project_id__in=accessible_project_ids)
  4. Pass tasks queryset to template
  5. Update template to loop through tasks and display task_short_description
  6. Run test — should PASS

STEP 3: Filter by Status

TEST PROMPT

Write test test_task_list_filter_by_status:

  • Setup: user with access to project, 3 tasks with different statuses (To Do, In Progress, Done)
  • GET /tasks/?status=in_progress → assert only "In Progress" task shown
  • GET /tasks/?status=todo&status=done → assert To Do and Done shown, In Progress hidden
  • GET /tasks/ (no filter) → assert all 3 shown

IMPL PROMPT

Add status filtering to task_list view:

  1. Create TaskFilterForm in forms.py with status = forms.MultipleChoiceField(choices=ListOfTaskStatus.choices, required=False, widget=forms.CheckboxSelectMultiple)
  2. In view: instantiate form with request.GET, if valid apply tasks_qs = tasks_qs.filter(task_status__in=form.cleaned_data['status']) when status provided
  3. Pass filter_form to template
  4. Update template: render status checkboxes in sidebar, preserve other filters in form action
  5. Run test — should PASS

STEP 4: Filter by Assignee

TEST PROMPT

Write test test_task_list_filter_by_assignee:

  • Setup: user with project access, 2 assignees (user1, user2), 3 tasks (2 assigned to user1, 1 to user2, 1 unassigned)
  • GET /tasks/?assignee=user1_id → assert only user1's tasks shown
  • GET /tasks/?assignee=unassigned → assert only unassigned task shown
  • GET /tasks/?assignee=user1_id&assignee=unassigned → assert both shown

IMPL PROMPT

Add assignee filtering:

  1. Add assignee = forms.ChoiceField(choices=[("", "All Assignees"), ("unassigned", "Unassigned")] + dynamic user choices, required=False) to TaskFilterForm
  2. In view: if assignee == "unassigned" → tasks_qs.filter(assigned_user__isnull=True); elif assignee ID → tasks_qs.filter(assigned_user_id=assignee)
  3. Populate assignee choices from users in accessible projects (or all users)
  4. Update template: assignee dropdown in sidebar
  5. Run test — should PASS

STEP 5: Filter by Project

TEST PROMPT

Write test test_task_list_filter_by_project:

  • Setup: user with access to 2 projects (project_a, project_b), tasks in each
  • GET /tasks/?project=project_a_id → assert only project_a tasks shown
  • GET /tasks/?project=project_b_id → assert only project_b tasks shown

IMPL PROMPT

Add project filtering:

  1. Add project = forms.ChoiceField(choices=[("", "All Projects")] + dynamic project choices, required=False) to TaskFilterForm
  2. In view: if project selected → tasks_qs.filter(project_id=project)
  3. Project choices = user's accessible projects (from Step 2 queryset)
  4. Update template: project dropdown in sidebar
  5. Run test — should PASS

STEP 6: Filter by Date Range

TEST PROMPT

Write test test_task_list_filter_by_date_range:

  • Setup: tasks with start_date/end_date across October 2025
  • GET /tasks/?start_date=2025-10-15&end_date=2025-10-25 → assert only tasks with start_date >= 15th AND end_date <= 25th shown
  • GET /tasks/?start_date=2025-10-01 (only start) → assert tasks with start_date >= 1st
  • GET /tasks/?end_date=2025-10-31 (only end) → assert tasks with end_date <= 31st

IMPL PROMPT

Add date range filtering:

  1. Add start_date = forms.DateField(required=False, widget=forms.DateInput(attrs={'type': 'date'})) and end_date to TaskFilterForm
  2. In view: if start_date → tasks_qs.filter(task_start_date__gte=start_date); if end_date → tasks_qs.filter(task_end_date__lte=end_date)
  3. Update template: two date inputs in sidebar
  4. Run test — should PASS

STEP 7: Sorting by Column Headers

TEST PROMPT

Write test test_task_list_sorting:

  • Setup: 5 tasks with different created, start_date, end_date, priority, status, project
  • GET /tasks/?sort=created → assert tasks ordered by created ASC
  • GET /tasks/?sort=-created → assert tasks ordered by created DESC
  • GET /tasks/?sort=priority → assert priority order (High > Medium > Low)
  • GET /tasks/?sort=project → assert ordered by project name
  • Verify toggle: clicking same column again reverses order

IMPL PROMPT

Add sorting:

  1. Add sort = forms.ChoiceField(choices=[("created", "Created"), ("-created", "Created ↓"), ("start_date", "Start Date"), ("-start_date", "Start Date ↓"), ("end_date", "End Date"), ("-end_date", "End Date ↓"), ("priority", "Priority"), ("-priority", "Priority ↓"), ("status", "Status"), ("-status", "Status ↓"), ("project", "Project"), ("-project", "Project ↓")], required=False) to form
  2. In view: parse sort param, apply .order_by(sort_field) to queryset
  3. For priority: custom ordering using Case/When (High=1, Medium=2, Low=3)
  4. Update template: table headers as links with ?sort=... preserving other filters, show ▲/▼ indicator
  5. Run test — should PASS

STEP 8: Pagination

TEST PROMPT

Write test test_task_list_pagination:

  • Setup: 50 tasks in user's project
  • GET /tasks/?page_size=10&page=1 → assert 10 tasks shown, page 1 active
  • GET /tasks/?page_size=10&page=3 → assert tasks 21-30 shown
  • GET /tasks/?page_size=25 → assert 25 tasks shown
  • GET /tasks/?page_size=100 → assert all 50 shown (single page)
  • Verify page navigation links (prev/next, page numbers, ellipsis)

IMPL PROMPT

Add pagination:

  1. Add page_size = forms.ChoiceField(choices=[(10, "10"), (25, "25"), (50, "50"), (100, "100")], required=False, initial=25) to form
  2. In view: use Paginator(tasks_qs, page_size), get page_obj = paginator.get_page(page_number)
  3. Pass page_obj, paginator, page_size to template
  4. Update template: pagination controls (prev/next, page numbers with ellipsis, page size selector)
  5. Run test — should PASS

STEP 9: Combined Filters + Sort + Pagination

TEST PROMPT

Write test test_task_list_combined_filters_sort_pagination:

  • Setup: 100 tasks across 3 projects, various statuses, assignees, dates
  • GET /tasks/?status=in_progress&project=proj1_id&sort=-priority&page_size=10&page=2
  • Assert: only In Progress tasks from proj1, sorted by priority DESC, page 2 (items 11-20)

IMPL PROMPT

Ensure all filters/sort/pagination work together:

  1. In view: apply filters → sort → paginate in that order
  2. Template: all filter/sort inputs in same <form method="GET">, pagination links preserve all query params
  3. Run test — should PASS

STEP 10: UI Permissions (user_level, user_group_permissions)

TEST PROMPT

Write test test_task_list_ui_permissions:

  • Setup: user with task permission level 1 (read only), user with level 3 (create)
  • Login as level 1 → GET /tasks/ → assert "New Task" button NOT in response
  • Login as level 3 → GET /tasks/ → assert "New Task" button IS in response
  • Verify user_level in context equals max permission_set__task across accessible projects

IMPL PROMPT

Add permission context to template:

  1. In view: compute user_level = UserGroup.objects.filter(...).aggregate(Max("permission_set__task"))["permission_set__task__max"] or 0
  2. Pass user_level and user_group_permissions = get_user_group_permission(request.user, ["task"]) to template
  3. Template: {% if user_level >= 3 %}<a href="{% url 'new_task' %}">+ New Task</a>{% endif %}
  4. Run test — should PASS

STEP 11: Empty State + Edge Cases

TEST PROMPT

Write test test_task_list_edge_cases:

  • User with no project access → GET /tasks/ → assert "No tasks found" message, 0 tasks
  • Filter returns no results → assert "No tasks found"
  • Invalid page number → redirects to last page
  • Invalid sort param → defaults to created DESC

IMPL PROMPT

Handle edge cases:

  1. Template: {% empty %} clause in task loop showing "No tasks found"
  2. View: Paginator.get_page() handles invalid pages gracefully
  3. View: validate sort param against allowed list, default to -created
  4. Run test — should PASS

STEP 12: Full Test Suite Run

FINAL VERIFICATION PROMPT

Run the complete test suite:

python3 manage.py test NearBeach.tests.tests_views.tests_task -v 2

Should report 10+ tests passing covering all verification steps from the task brief.


Quick Reference: Test Class Structure

# NearBeach/tests/tests_views/tests_task/test_task_list.py
from django.test import TestCase, Client
from django.urls import reverse
from django.contrib.auth import get_user_model
from NearBeach.models import *

class TestTaskList(TestCase):
    def setUp(self):
        # Common setup: users, groups, projects, ObjectAssignments, tasks
        pass

    def test_task_list_url_accessible(self):
        pass  # Step 1

    def test_task_list_filters_by_user_projects(self):
        pass  # Step 2

    def test_task_list_filter_by_status(self):
        pass  # Step 3

    def test_task_list_filter_by_assignee(self):
        pass  # Step 4

    def test_task_list_filter_by_project(self):
        pass  # Step 5

    def test_task_list_filter_by_date_range(self):
        pass  # Step 6

    def test_task_list_sorting(self):
        pass  # Step 7

    def test_task_list_pagination(self):
        pass  # Step 8

    def test_task_list_combined_filters_sort_pagination(self):
        pass  # Step 9

    def test_task_list_ui_permissions(self):
        pass  # Step 10

    def test_task_list_edge_cases(self):
        pass  # Step 11

LiveCompare Session Tips

  1. Send TEST PROMPT first → model writes test
  2. Run in Sandbox → confirm FAIL
  3. Send IMPL PROMPT → model implements
  4. Run in Sandbox → confirm PASS
  5. Commit/continue to next step

If model writes buggy implementation → test fails → you say "Test still fails, here's the error: [paste error]" → model fixes.

This gives you granular control and visible progress at each step.