Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for unfinished downloads before allowing new CSV, PDF, or XLS requests #4145

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions bims/api_views/download_request.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from datetime import datetime, timedelta

from django.contrib.sites.models import Site
from django.utils.timezone import make_aware
from preferences import preferences
import ast

Expand Down Expand Up @@ -57,6 +60,35 @@ def post(self, request):
preferences.SiteSetting.enable_download_request_approval
)

if resource_type in ['CSV', 'PDF', 'XLS']:
# Check unfinished big download tasks e.g. download occurrences or taxa list
today = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
start_date = today - timedelta(days=5)
start_date = make_aware(start_date)
end_date = make_aware(today + timedelta(days=1))

download_requests = DownloadRequest.objects.filter(
resource_name__in=['Occurrence Data', 'Taxa List'],
resource_type__in=['CSV', 'PDF', 'XLS'],
requester=self.request.user,
request_date__range=(start_date, end_date),
)
for download_request in download_requests:
progress = download_request.progress
try:
completed, total = progress.split('/')
completed = int(completed)
total = int(total)

if completed < total:
return Response(
{'error':
'There are still ongoing download requests. '
'Please wait for them to complete before trying again.'},
status=status.HTTP_401_UNAUTHORIZED)
except ValueError:
continue

if not resource_name or not resource_type or not purpose:
raise Http404('Missing required field.')

Expand Down