diff --git a/app/apps/itinerary/admin.py b/app/apps/itinerary/admin.py index a5fafdc3..93328d5a 100644 --- a/app/apps/itinerary/admin.py +++ b/app/apps/itinerary/admin.py @@ -1,3 +1,5 @@ +from datetime import timedelta + from apps.itinerary.models import ( Itinerary, ItineraryItem, @@ -10,7 +12,6 @@ from django.contrib import admin from django.http import JsonResponse from django.utils import timezone -from datetime import timedelta class PostalCodeSettingsInline(admin.StackedInline): @@ -41,33 +42,37 @@ class ItineraryItemInline(admin.StackedInline): class CreatedAtFilter(admin.SimpleListFilter): - title = 'created_at' - parameter_name = 'created_at' + title = "created_at" + parameter_name = "created_at" def lookups(self, request, model_admin): return ( - ('today', 'Today'), - ('past_7_days', 'Past 7 days'), - ('this_month', 'This month'), - ('longer_than_a_month', 'Longer than a month ago'), + ("today", "Today"), + ("past_7_days", "Past 7 days"), + ("this_month", "This month"), + ("longer_than_a_month", "Longer than a month ago"), ) def queryset(self, request, queryset): - if self.value() == 'today': - today_start = timezone.now().replace(hour=0, minute=0, second=0, microsecond=0) + if self.value() == "today": + today_start = timezone.now().replace( + hour=0, minute=0, second=0, microsecond=0 + ) today_end = today_start + timedelta(days=1) return queryset.filter(created_at__range=(today_start, today_end)) - elif self.value() == 'past_7_days': + elif self.value() == "past_7_days": past_7_days_start = timezone.now() - timedelta(days=7) return queryset.filter(created_at__gte=past_7_days_start) - elif self.value() == 'this_month': - first_day_of_month = timezone.now().replace(day=1, hour=0, minute=0, second=0, microsecond=0) + elif self.value() == "this_month": + first_day_of_month = timezone.now().replace( + day=1, hour=0, minute=0, second=0, microsecond=0 + ) next_month = first_day_of_month.replace(month=first_day_of_month.month + 1) return queryset.filter(created_at__range=(first_day_of_month, next_month)) - elif self.value() == 'longer_than_a_month': + elif self.value() == "longer_than_a_month": one_month_ago = timezone.now() - timedelta(days=30) return queryset.filter(created_at__lt=one_month_ago) @@ -82,9 +87,7 @@ class ItineraryAdmin(admin.ModelAdmin): "start_case", ) search_fields = ["team_members__user__email"] - list_filter = ( - CreatedAtFilter, - ) + list_filter = (CreatedAtFilter,) inlines = [ ItineraryTeamMemberInline, @@ -117,6 +120,7 @@ class ItineraryItemAdmin(admin.ModelAdmin): "external_state_id", ) list_display = ( - "case", "itinerary", + "case", + "itinerary", ) search_fields = ["case__case_id"] diff --git a/app/apps/itinerary/tasks.py b/app/apps/itinerary/tasks.py new file mode 100644 index 00000000..12decf9a --- /dev/null +++ b/app/apps/itinerary/tasks.py @@ -0,0 +1,30 @@ +import logging +from datetime import timedelta + +from apps.itinerary.models import Itinerary +from celery import shared_task +from django.utils import timezone + +logger = logging.getLogger("celery") + +DEFAULT_RETRY_DELAY = 10 +DAYS_UNTIL_DELETION = 30 + + +@shared_task(bind=True, default_retry_delay=DEFAULT_RETRY_DELAY) +def clean_up_itineraries_task(self): + """ + Clean up itineraries older than 30 days. + """ + logger.info("Started cleanup of itineraries") + + try: + one_month_ago = timezone.now() - timedelta(days=DAYS_UNTIL_DELETION) + deleted_count, _ = Itinerary.objects.filter( + created_at__lt=one_month_ago + ).delete() + logger.info(f"Ended itineraries cleanup, deleted {deleted_count} itineraries") + + except Exception as exception: + logger.error(f"Exception occurred during itineraries cleanup: {exception}") + self.retry(exc=exception) diff --git a/app/apps/visits/admin.py b/app/apps/visits/admin.py index 26ab6f0e..4d2dec5d 100644 --- a/app/apps/visits/admin.py +++ b/app/apps/visits/admin.py @@ -1,3 +1,5 @@ +from datetime import timedelta + from apps.visits.models import ( Observation, Situation, @@ -7,50 +9,58 @@ ) from django.contrib import admin from django.utils import timezone -from datetime import timedelta class StartTimeFilter(admin.SimpleListFilter): - title = 'start_time' - parameter_name = 'start_time' + title = "start_time" + parameter_name = "start_time" def lookups(self, request, model_admin): return ( - ('today', 'Today'), - ('past_7_days', 'Past 7 days'), - ('this_month', 'This month'), - ('longer_than_a_month', 'Longer than a month ago'), + ("today", "Today"), + ("past_7_days", "Past 7 days"), + ("this_month", "This month"), + ("longer_than_a_month", "Longer than a month ago"), ) def queryset(self, request, queryset): - if self.value() == 'today': - today_start = timezone.now().replace(hour=0, minute=0, second=0, microsecond=0) + if self.value() == "today": + today_start = timezone.now().replace( + hour=0, minute=0, second=0, microsecond=0 + ) today_end = today_start + timedelta(days=1) return queryset.filter(start_time__range=(today_start, today_end)) - elif self.value() == 'past_7_days': + elif self.value() == "past_7_days": past_7_days_start = timezone.now() - timedelta(days=7) return queryset.filter(start_time__gte=past_7_days_start) - elif self.value() == 'this_month': - first_day_of_month = timezone.now().replace(day=1, hour=0, minute=0, second=0, microsecond=0) + elif self.value() == "this_month": + first_day_of_month = timezone.now().replace( + day=1, hour=0, minute=0, second=0, microsecond=0 + ) next_month = first_day_of_month.replace(month=first_day_of_month.month + 1) return queryset.filter(start_time__range=(first_day_of_month, next_month)) - elif self.value() == 'longer_than_a_month': + elif self.value() == "longer_than_a_month": one_month_ago = timezone.now() - timedelta(days=30) return queryset.filter(start_time__lt=one_month_ago) return queryset - + + @admin.register(Visit) class VisitAdmin(admin.ModelAdmin): - list_display = ("id", "case_id", "author", "start_time", "completed", "situation", ) - search_fields = ("case_id__case_id",) - list_filter = ( - StartTimeFilter, - "completed" + list_display = ( + "id", + "case_id", + "author", + "start_time", + "completed", + "situation", ) + search_fields = ("case_id__case_id",) + list_filter = (StartTimeFilter, "completed") @admin.register(VisitMetaData) diff --git a/app/apps/visits/tasks.py b/app/apps/visits/tasks.py index d5c46889..5381e71b 100644 --- a/app/apps/visits/tasks.py +++ b/app/apps/visits/tasks.py @@ -1,8 +1,10 @@ import logging +from datetime import timedelta import requests from celery import shared_task from django.conf import settings +from django.utils import timezone from utils.queries_zaken_api import get_headers from .models import Visit @@ -12,6 +14,7 @@ DEFAULT_RETRY_DELAY = 10 CONNECT_TIMEOUT = 10 READ_TIMEOUT = 60 +DAYS_UNTIL_DELETION = 30 def get_serialized_visit(visit_id): @@ -57,3 +60,20 @@ def push_visit(self, visit_id, auth_header=None, task_name_ids=[]): self.retry(exc=exception) return f"visit_id: {visit_id}" + + +@shared_task(bind=True, default_retry_delay=DEFAULT_RETRY_DELAY) +def clean_up_visits_task(self): + """ + Clean up visits older than 30 days. + """ + logger.info("Started cleanup of visits") + + try: + one_month_ago = timezone.now() - timedelta(days=DAYS_UNTIL_DELETION) + deleted_count, _ = Visit.objects.filter(start_time__lt=one_month_ago).delete() + logger.info(f"Ended visits cleanup, deleted {deleted_count} visits") + + except Exception as exception: + logger.error(f"Exception occurred during visits cleanup: {exception}") + self.retry(exc=exception)