2023-10-01 16:44:21 +08:00
|
|
|
from django.core.management.base import BaseCommand
|
|
|
|
from django.utils import timezone
|
|
|
|
from django.contrib.gis.geos import Point
|
|
|
|
from study_groups.models import StudyGroup
|
|
|
|
from student_status.models import StudentStatus
|
2023-10-01 16:50:01 +08:00
|
|
|
import logging
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2023-10-01 16:44:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
help = 'Removes old study groups and resets old student statuses'
|
|
|
|
|
|
|
|
def handle(self, *args, **kwargs):
|
|
|
|
# Get the current time
|
|
|
|
now = timezone.now()
|
|
|
|
|
|
|
|
# Get the time 8 hours ago
|
2023-10-17 21:39:47 +08:00
|
|
|
time_threshold = now - timezone.timedelta(hours=4)
|
2023-10-01 16:44:21 +08:00
|
|
|
|
2023-10-01 16:50:01 +08:00
|
|
|
# Get StudyGroup entries older than 8 hours
|
|
|
|
old_groups = StudyGroup.objects.filter(timestamp__lt=time_threshold)
|
|
|
|
|
|
|
|
# Log the old groups
|
|
|
|
for group in old_groups:
|
2023-10-17 21:39:47 +08:00
|
|
|
self.stdout.write(self.style.SUCCESS(
|
|
|
|
f'Deleting StudyGroup: {group}'))
|
2023-10-01 16:50:01 +08:00
|
|
|
|
|
|
|
# Delete the old groups
|
|
|
|
old_groups.delete()
|
2023-10-01 16:44:21 +08:00
|
|
|
|
|
|
|
# Get StudentStatus entries older than 8 hours
|
2023-10-17 21:39:47 +08:00
|
|
|
old_statuses = StudentStatus.objects.filter(active=True).filter(
|
2023-10-01 16:44:21 +08:00
|
|
|
timestamp__lt=time_threshold)
|
|
|
|
|
|
|
|
# Set the fields of the old statuses to the required values
|
|
|
|
for status in old_statuses:
|
2023-10-17 21:39:47 +08:00
|
|
|
self.stdout.write(self.style.SUCCESS(
|
|
|
|
f'Resetting StudentStatus: {status}'))
|
2023-10-13 12:34:20 +08:00
|
|
|
status.active = False
|
2023-10-01 16:44:21 +08:00
|
|
|
status.location = Point(0, 0)
|
|
|
|
status.subject = None
|
|
|
|
status.landmark = None
|
|
|
|
status.study_group = None
|
|
|
|
status.save()
|
|
|
|
|
|
|
|
self.stdout.write(self.style.SUCCESS(
|
|
|
|
'Successfully cleaned old entries'))
|