Skip to content

Commit b7e666c

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 80fd262 commit b7e666c

File tree

3 files changed

+177
-153
lines changed

3 files changed

+177
-153
lines changed

sde_collections/admin.py

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import csv
2+
import os
3+
import uuid
24

35
from django import forms
6+
from django.conf import settings
47
from django.contrib import admin, messages
5-
from django.http import HttpResponse, FileResponse
6-
from django.utils.safestring import mark_safe
8+
from django.http import FileResponse, HttpResponse
79
from django.urls import path
8-
import uuid
9-
import os
10-
from django.conf import settings
10+
from django.utils.safestring import mark_safe
1111

1212
from sde_collections.models.delta_patterns import (
1313
DeltaDivisionPattern,
@@ -20,7 +20,7 @@
2020
from .models.collection_choice_fields import TDAMMTags
2121
from .models.delta_url import CuratedUrl, DeltaUrl, DumpUrl
2222
from .models.pattern import DivisionPattern, IncludePattern, TitlePattern
23-
from .tasks import fetch_full_text, import_candidate_urls_from_api, generate_metrics
23+
from .tasks import fetch_full_text, generate_metrics, import_candidate_urls_from_api
2424

2525

2626
def fetch_and_replace_text_for_server(modeladmin, request, queryset, server_name):
@@ -300,70 +300,82 @@ def included_curated_urls_count(self, obj) -> int:
300300
fetch_full_text_xli_action,
301301
]
302302
ordering = ("cleaning_order",)
303-
303+
304304
def changelist_view(self, request, extra_context=None):
305305
"""
306306
To add a button for metrics download
307307
"""
308308
extra_context = extra_context or {}
309-
extra_context['show_metrics_button'] = True
310-
extra_context['metrics_url'] = request.path + 'metrics/'
309+
extra_context["show_metrics_button"] = True
310+
extra_context["metrics_url"] = request.path + "metrics/"
311311
return super().changelist_view(request, extra_context=extra_context)
312-
312+
313313
def get_urls(self):
314314
"""
315315
To add custom endpoints for metrics functionality
316316
"""
317317
urls = super().get_urls()
318318
custom_urls = [
319-
path('metrics/', self.admin_site.admin_view(self.download_metrics), name='sde_collections_collection_metrics'),
320-
path('metrics/<str:task_id>/', self.admin_site.admin_view(self.get_metrics_file), name='sde_collections_get_metrics'),
319+
path(
320+
"metrics/", self.admin_site.admin_view(self.download_metrics), name="sde_collections_collection_metrics"
321+
),
322+
path(
323+
"metrics/<str:task_id>/",
324+
self.admin_site.admin_view(self.get_metrics_file),
325+
name="sde_collections_get_metrics",
326+
),
321327
]
322328
return custom_urls + urls
323-
329+
324330
def download_metrics(self, request):
325331
"""Custom view that starts metrics generation and returns to collection list"""
326332
task_id = str(uuid.uuid4())
327333
task = generate_metrics.delay(task_id)
328-
329-
download_url = request.path.rsplit('metrics/', 1)[0] + f'metrics/{task_id}/'
330-
334+
335+
download_url = request.path.rsplit("metrics/", 1)[0] + f"metrics/{task_id}/"
336+
331337
messages.add_message(
332338
request,
333339
messages.INFO,
334-
mark_safe(f"Metrics generation started. Please wait a moment and then <a href='{download_url}'>click here to download</a> when ready.")
340+
mark_safe(
341+
f"Metrics generation started. Please wait a moment and then <a href='{download_url}'>click here to download</a> when ready."
342+
),
335343
)
336-
return HttpResponse(status=303, headers={"Location": request.path.replace('/metrics/', '')})
337-
344+
return HttpResponse(status=303, headers={"Location": request.path.replace("/metrics/", "")})
345+
338346
def get_metrics_file(self, request, task_id):
339347
"""Serve the generated metrics file if it exists and is valid"""
340-
341-
file_path = os.path.join(settings.MEDIA_ROOT, 'metrics', f'metrics_{task_id}.csv')
342-
348+
349+
file_path = os.path.join(settings.MEDIA_ROOT, "metrics", f"metrics_{task_id}.csv")
350+
343351
# Create the retry URL
344352
current_url = request.build_absolute_uri()
345-
353+
346354
# Check if file exists and is not empty (minimum size 100 bytes)
347355
if os.path.exists(file_path) and os.path.getsize(file_path) > 100:
348-
response = FileResponse(open(file_path, 'rb'), content_type='text/csv')
349-
response['Content-Disposition'] = 'attachment; filename="metrics.csv"'
356+
response = FileResponse(open(file_path, "rb"), content_type="text/csv")
357+
response["Content-Disposition"] = 'attachment; filename="metrics.csv"'
350358
return response
351359
else:
352360
# Also check if there's a temporary file indicating task is still running
353-
temp_file_path = os.path.join(settings.MEDIA_ROOT, 'metrics', f'metrics_{task_id}.tmp')
361+
temp_file_path = os.path.join(settings.MEDIA_ROOT, "metrics", f"metrics_{task_id}.tmp")
354362
if os.path.exists(temp_file_path):
355363
messages.add_message(
356364
request,
357365
messages.INFO,
358-
mark_safe(f"The metrics file is still being generated. <a href='{current_url}'>Click here to try again</a>.")
366+
mark_safe(
367+
f"The metrics file is still being generated. <a href='{current_url}'>Click here to try again</a>."
368+
),
359369
)
360370
else:
361371
messages.add_message(
362372
request,
363373
messages.WARNING,
364-
mark_safe(f"The metrics file is not ready yet. <a href='{current_url}'>Click here to try again</a>.")
374+
mark_safe(
375+
f"The metrics file is not ready yet. <a href='{current_url}'>Click here to try again</a>."
376+
),
365377
)
366-
return HttpResponse(status=303, headers={"Location": request.path.replace(f'/metrics/{task_id}/', '')})
378+
return HttpResponse(status=303, headers={"Location": request.path.replace(f"/metrics/{task_id}/", "")})
367379

368380

369381
@admin.action(description="Exclude URL and all children")

0 commit comments

Comments
 (0)