You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
feo-homepage/core/tasks.py

53 lines
1.9 KiB

from datetime import date
from celery import shared_task
from django.db import transaction
from django.db.models import Q
from django.utils import timezone
from core.models import EventPage, EventHistoryPage
@shared_task
def move_past_events_to_history():
"""
Daily task to:
1. Move past events that are not partner events to the history
2. Unpublish partner events that are past
"""
today = date.today()
# Find all live EventPage objects with a start_date in the past
past_events = EventPage.objects.live().filter(
Q(end_date__lt=timezone.now()) | Q(end_date__isnull=True, start_date__lt=timezone.now()))
# Find the first EventHistoryPage to use as the target for moving events
try:
history_page = EventHistoryPage.objects.live().first()
if not history_page:
# If no history page exists, we can't move events
return "No EventHistoryPage found. Cannot move past events."
except EventHistoryPage.DoesNotExist:
return "No EventHistoryPage found. Cannot move past events."
moved_count = 0
unpublished_count = 0
with transaction.atomic():
# Process each past event
for event in past_events:
if event.is_partner_event:
# Unpublish partner events
event.unpublish()
unpublished_count += 1
else:
# Move non-partner events to history
# Get the current parent page
current_parent = event.get_parent()
# Only move if not already under the history page
if current_parent.id != history_page.id:
# Move the event to be a child of the history page
event.move(history_page, pos='last-child')
moved_count += 1
return f"Processed past events: {moved_count} moved to history, {unpublished_count} partner events unpublished."