From 0ef4ce8465a09ec2981cece29e1283f66569a9c7 Mon Sep 17 00:00:00 2001 From: Irwan Fathurrahman Date: Wed, 8 May 2024 11:42:04 +0700 Subject: [PATCH] Fix clean download data task (#130) --- admin/download_request.py | 2 +- models/download_request.py | 6 ++++++ tasks/clean.py | 6 ++---- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/admin/download_request.py b/admin/download_request.py index d08e14a2..af0cca8f 100644 --- a/admin/download_request.py +++ b/admin/download_request.py @@ -7,7 +7,7 @@ class DownloadRequestAdmin(admin.ModelAdmin): list_display = ( 'uuid', 'first_name', 'last_name', 'organization', 'organization_types', - 'email', 'country', 'request_at' + 'email', 'country', 'request_at', 'age_hours' ) filter_horizontal = ('countries',) search_fields = ( diff --git a/models/download_request.py b/models/download_request.py index ef47780f..79240e03 100644 --- a/models/download_request.py +++ b/models/download_request.py @@ -6,6 +6,7 @@ from django.conf import settings from django.contrib.gis.db import models +from django.utils import timezone from django.utils.translation import ugettext_lazy as _ from gwml2.models.general import Country @@ -209,3 +210,8 @@ def file(self): return file else: return None + + @property + def age_hours(self): + diff = timezone.now() - self.request_at + return diff.total_seconds() / (60 * 60) diff --git a/tasks/clean.py b/tasks/clean.py index 56f2d771..91ccccf6 100644 --- a/tasks/clean.py +++ b/tasks/clean.py @@ -2,7 +2,6 @@ from celery import shared_task from celery.utils.log import get_task_logger -from django.utils import timezone from gwml2.models.download_request import DownloadRequest @@ -11,13 +10,12 @@ @shared_task( bind=True, - name='gwml2.tasks.harvester.clean_download_file', + name='gwml2.tasks.clean.clean_download_file', queue='geoserver.events') def clean_download_file(self): """Run clean download file.""" for download in DownloadRequest.objects.all(): - diff = timezone.now() - download.request_at - if diff.days >= 1: + if download.age_hours >= 5: _file = download.file() if _file: os.remove(_file)