Added pruning command

This commit is contained in:
Keannu Christian Bernasol 2023-10-01 16:44:21 +08:00
parent c8fca412b8
commit 13ba0cb12a
3 changed files with 89 additions and 4 deletions

View file

@ -0,0 +1,34 @@
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
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
time_threshold = now - timezone.timedelta(hours=8)
# Delete StudyGroup entries older than 8 hours
StudyGroup.objects.filter(timestamp__lt=time_threshold).delete()
# Get StudentStatus entries older than 8 hours
old_statuses = StudentStatus.objects.filter(
timestamp__lt=time_threshold)
# Set the fields of the old statuses to the required values
for status in old_statuses:
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'))

View file

@ -0,0 +1,21 @@
# Generated by Django 4.2.3 on 2023-10-01 05:25
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('accounts', '0003_alter_customuser_options_and_more'),
]
operations = [
migrations.AlterModelOptions(
name='customuser',
options={'verbose_name': 'user', 'verbose_name_plural': 'users'},
),
migrations.AlterUniqueTogether(
name='customuser',
unique_together=set(),
),
]