Skip to content

Commit

Permalink
fix: standardize expiration thresholds in settings
Browse files Browse the repository at this point in the history
might be better placed in SiteSettings so it can be db editable and
configurable at runtime but good enough for now
  • Loading branch information
alee committed Sep 11, 2023
1 parent 975d763 commit 5a8662c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
12 changes: 7 additions & 5 deletions django/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,9 @@ def get_expired_q(self):
started less than 2 days ago
"""
now = timezone.now().date()
start_date_threshold = now - timedelta(days=Event.EXPIRED_EVENT_DAYS_THRESHOLD)
start_date_threshold = now - timedelta(
days=settings.EXPIRED_EVENT_DAYS_THRESHOLD
)
return models.Q(start_date__lt=start_date_threshold) | models.Q(
end_date__lt=now, end_date__isnull=False
)
Expand All @@ -557,7 +559,6 @@ def public(self):

@add_to_comses_permission_whitelist
class Event(index.Indexed, ClusterableModel):
EXPIRED_EVENT_DAYS_THRESHOLD = 2
title = models.CharField(max_length=300)
date_created = models.DateTimeField(default=timezone.now)
last_modified = models.DateTimeField(auto_now=True)
Expand Down Expand Up @@ -656,11 +657,12 @@ def upcoming(self, **kwargs):
def get_expired_q(self):
"""
returns a Q object for all Jobs with a non-null application deadline before today or
posted/modified in the last [POST_DATE_DAYS_AGO_TRESHOLD] days if application deadline is null
posted/modified in the last [settings.EXPIRED_JOB_DAYS_THRESHOLD] days if
application deadline is null
"""
today = timezone.now()
post_date_days_ago_threshold = settings.POST_DATE_DAYS_AGO_THRESHOLD
post_date_threshold = today - timedelta(days=post_date_days_ago_threshold)
threshold = settings.EXPIRED_JOB_DAYS_THRESHOLD
post_date_threshold = today - timedelta(days=threshold)
return models.Q(
application_deadline__isnull=False, application_deadline__lt=today
) | models.Q(
Expand Down
12 changes: 8 additions & 4 deletions django/core/settings/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,16 @@ def is_test(self):

# regular settings

POST_DATE_DAYS_AGO_THRESHOLD = config.getint(
"default", "POST_DATE_DAYS_AGO_THRESHOLD", fallback=180
EXPIRED_JOB_DAYS_THRESHOLD = config.getint(
"default", "EXPIRED_JOB_DAYS_THRESHOLD", fallback=180
)

EXPIRED_EVENT_DAYS_THRESHOLD = config.getint(
"default", "EXPIRED_EVENT_DAYS_THRESHOLD", fallback=2
)

# Database configuration
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases

DEFAULT_AUTO_FIELD = "django.db.models.AutoField"

Expand Down Expand Up @@ -530,7 +534,7 @@ def is_test(self):
"discourse", "DISCOURSE_API_USERNAME", fallback="unconfigured"
)

# https://docs.djangoproject.com/en/3.2/ref/settings/#templates
# https://docs.djangoproject.com/en/4.2/ref/settings/#templates
TEMPLATES = [
{
"BACKEND": "django.template.backends.jinja2.Jinja2",
Expand Down
9 changes: 5 additions & 4 deletions django/core/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def setUp(self):
self.job_factory = JobFactory(submitter=self.user)
today = timezone.now()

self.threshold = settings.POST_DATE_DAYS_AGO_THRESHOLD
self.threshold = settings.EXPIRED_JOB_DAYS_THRESHOLD

# active jobs
self.active_job = self.job_factory.create(
Expand Down Expand Up @@ -123,7 +123,7 @@ def setUp(self):
end_date=None,
title="No End Date Current Event",
)
for threshold in range(Event.EXPIRED_EVENT_DAYS_THRESHOLD)
for threshold in range(settings.EXPIRED_EVENT_DAYS_THRESHOLD)
]

# expired events
Expand All @@ -132,11 +132,12 @@ def setUp(self):
end_date=now - timedelta(days=7),
title="Expired Event",
)
expired_days_threshold = settings.EXPIRED_EVENT_DAYS_THRESHOLD + 1
sample_expired_event_thresholds = set(
random.sample(range(Event.EXPIRED_EVENT_DAYS_THRESHOLD + 1, 365), 10)
random.sample(range(expired_days_threshold, 365), 10)
)
sample_expired_event_thresholds.add(
Event.EXPIRED_EVENT_DAYS_THRESHOLD + 1
expired_days_threshold
) # always test the edge
self.no_end_date_expired_events = [
self.event_factory.create(
Expand Down

0 comments on commit 5a8662c

Please sign in to comment.