Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions makeabilitylab/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@ def parse_allowed_hosts(value, django_env):
CSRF_COOKIE_SECURE = True

# Makeability Lab Global Variables, including Makeability Lab version
ML_WEBSITE_VERSION = "2.35.5" # Keep this updated with each release and also change the short description below
ML_WEBSITE_VERSION_DESCRIPTION = "Each of the three login-throttle pages under Administration (Access attempts, logs, failures) now explains what it records and how to unlock an account."
ML_WEBSITE_VERSION = "2.35.6" # Keep this updated with each release and also change the short description below
ML_WEBSITE_VERSION_DESCRIPTION = "A project now counts as active through its end date everywhere: its page, the Projects listing, and the landing page agree."
DATE_MAKEABILITYLAB_FORMED = datetime.date(2012, 1, 1) # Date Makeability Lab was formed
MAX_BANNERS = 7 # Maximum number of banners on a page

Expand Down
9 changes: 7 additions & 2 deletions website/models/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,8 +489,13 @@ def has_artifact(self):
return self.get_most_recent_artifact() is not None

def has_ended(self):
"""Returns true if the project has ended"""
return self.end_date is not None and self.end_date < date.today()
"""
Returns true if the project has ended, i.e., its end_date is strictly in
the past (a project is still active on its end date). This is the
canonical rule; the projects listing and landing page querysets mirror
it (pinned by tests/test_project_status.py).
"""
return self.end_date is not None and self.end_date < timezone.localdate()

def get_most_recent_artifact_date(self):
"""
Expand Down
55 changes: 55 additions & 0 deletions website/tests/test_project_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""
Tests that every place deciding whether a Project is "active" or "completed"
agrees, especially on the boundary day.

The canonical rule is ``Project.has_ended()``: a project has ended once its
``end_date`` is strictly in the past (``end_date < today``), so a project is
still active on its end date itself. This matches ``Position``'s rule.

Before this was pinned, three call sites disagreed:

* ``has_ended()`` (project page chip) used ``end_date < today``;
* the projects listing used ``end_date <= today`` for "completed", so a
project ending today was listed as Completed while its page said Active;
* the landing page required ``end_date IS NULL``, so a project with a future
end date was Active on the listing but missing from the landing page.
"""

from datetime import timedelta

from django.urls import reverse
from django.utils import timezone

from website.tests.base import DatabaseTestCase


class ProjectStatusConsistencyTests(DatabaseTestCase):
"""has_ended(), the projects listing, and the landing page agree."""

def setUp(self):
today = timezone.localdate()
self.ongoing = self.make_project(name="Ongoing", is_visible=True)
self.ends_future = self.make_project(
name="Ends Future", is_visible=True, end_date=today + timedelta(days=30))
self.ends_today = self.make_project(
name="Ends Today", is_visible=True, end_date=today)
self.ended = self.make_project(
name="Ended", is_visible=True, end_date=today - timedelta(days=1))
self.active = [self.ongoing, self.ends_future, self.ends_today]

def test_has_ended_boundary(self):
for project in self.active:
self.assertFalse(project.has_ended(), project.name)
self.assertTrue(self.ended.has_ended())

def test_listing_splits_on_has_ended(self):
response = self.client.get(reverse("website:projects"))
active_ids = {p.id for p in response.context["active_projects"]}
completed_ids = {p.id for p in response.context["completed_projects"]}
self.assertEqual(active_ids, {p.id for p in self.active})
self.assertEqual(completed_ids, {self.ended.id})

def test_landing_page_shows_all_active(self):
response = self.client.get(reverse("website:index"))
shown_ids = {p.id for p in response.context["projects"]}
self.assertEqual(shown_ids, {p.id for p in self.active})
10 changes: 7 additions & 3 deletions website/views/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from website.utils.metadata import absolute_url, render_jsonld
from django.templatetags.static import static
from django.shortcuts import render # for render https://docs.djangoproject.com/en/4.0/topics/http/shortcuts/#render
from django.db.models import OuterRef, Subquery, F
from django.db.models import OuterRef, Subquery, F, Q
from django.utils import timezone

from django.db.models import Sum # for summing grant funding amounts
from django.db.models.functions import Coalesce # for replacing None with 0 when summing grant funding amounts
Expand Down Expand Up @@ -54,10 +55,13 @@ def index(request):
latest_publication_dates = Publication.objects.filter(projects=OuterRef('pk')).order_by('-date')

# Get all visible, active projects (i.e., marked is_visible and with no end
# date), ordered by most recent pub date and limited to MAX_NUM_PROJECTS.
# date OR an end date today or in the future; same rule as
# Project.has_ended() and the projects listing), ordered by most recent pub date and limited to MAX_NUM_PROJECTS.
# Visibility is governed solely by the is_visible flag (#1300). nulls_last
# keeps a visible project that has no publication yet from sorting to the top.
active_projects = (Project.objects.filter(is_visible=True, end_date__isnull=True)
today = timezone.localdate()
active_projects = (Project.objects.filter(is_visible=True)
.filter(Q(end_date__isnull=True) | Q(end_date__gte=today))
.annotate(most_recent_publication=Subquery(latest_publication_dates.values('date')[:1]))
.order_by(F('most_recent_publication').desc(nulls_last=True), 'id').distinct())[:MAX_NUM_PROJECTS]

Expand Down
10 changes: 5 additions & 5 deletions website/views/project_listing.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,24 @@ def project_listing(request):
_logger.debug(f"Starting views/projects at {func_start_time:0.4f}")

# Get today's date for filtering completed projects
today = timezone.now().date()
today = timezone.localdate()

# Get the most recent publication date for each project
latest_publication_dates = Publication.objects.filter(projects=OuterRef('pk')).order_by('-date')

# Get all visible, active projects (i.e., marked is_visible and with no end
# date OR an end date today or in the future), ordered by most recent pub
# date. Visibility is governed solely by the is_visible flag (#1300).
# date OR an end date today or in the future; same rule as
# Project.has_ended()), ordered by most recent pub date. Visibility is governed solely by the is_visible flag (#1300).
# nulls_last keeps a visible project that has no publication yet from
# sorting to the top.
active_projects = (Project.objects.filter(is_visible=True)
.filter(Q(end_date__isnull=True) | Q(end_date__gt=today))
.filter(Q(end_date__isnull=True) | Q(end_date__gte=today))
.annotate(most_recent_publication=Subquery(latest_publication_dates.values('date')[:1]))
.order_by(F('most_recent_publication').desc(nulls_last=True), 'id').distinct())

# Get visible, completed projects (an end date before today),
# ordered by most recent pub date
completed_projects = (Project.objects.filter(is_visible=True, end_date__isnull=False, end_date__lte=today)
completed_projects = (Project.objects.filter(is_visible=True, end_date__isnull=False, end_date__lt=today)
.annotate(most_recent_publication=Subquery(latest_publication_dates.values('date')[:1]))
.order_by(F('most_recent_publication').desc(nulls_last=True), 'id').distinct())

Expand Down
Loading