From a5f8f48f1fe9d361919ac74b8a9136c2fbe2d46c Mon Sep 17 00:00:00 2001 From: Guillaume Pellerin Date: Tue, 22 Jun 2021 09:12:05 +0200 Subject: [PATCH] add command to delete old scripts and annotations --- .../commands/teleforma-delete-annotations.py | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 teleforma/management/commands/teleforma-delete-annotations.py diff --git a/teleforma/management/commands/teleforma-delete-annotations.py b/teleforma/management/commands/teleforma-delete-annotations.py new file mode 100644 index 00000000..81088ce6 --- /dev/null +++ b/teleforma/management/commands/teleforma-delete-annotations.py @@ -0,0 +1,31 @@ + +from optparse import make_option +from django.conf import settings +from django.core.management.base import BaseCommand, CommandError +from django.contrib.auth.models import User +from jqchat.models import Message +import datetime +from teleforma.exam.models import Script +from pdfannotator.models import Annotation, AnnotationComment + + +class Command(BaseCommand): + help = "Delete script annotations from previous year" + + def handle(self, *args, **options): + date = datetime.datetime.now() + date_old = date_now.replace(year=date_now.year-1, month=12, day=31) + print(date_old) + scripts = Script.objects.filter(date_submitted__lte=date_old) + print(scripts.count()) + + for script in scripts: + annotations = Annotation.objects.filter(uuid=script.uuid) + for annotation in annotations: + annotation.delete() + annotation_comments = AnnotationComment.objects.filter(uuid=script.uuid) + for annotation_comment in annotation_comments: + annotation_comment.delete() + script.delete() + + \ No newline at end of file -- 2.39.5