|
1 | 1 | import csv
|
| 2 | +import os |
| 3 | +import uuid |
2 | 4 |
|
3 | 5 | from django import forms
|
| 6 | +from django.conf import settings |
4 | 7 | 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 |
7 | 9 | 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 |
11 | 11 |
|
12 | 12 | from sde_collections.models.delta_patterns import (
|
13 | 13 | DeltaDivisionPattern,
|
|
20 | 20 | from .models.collection_choice_fields import TDAMMTags
|
21 | 21 | from .models.delta_url import CuratedUrl, DeltaUrl, DumpUrl
|
22 | 22 | 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 |
24 | 24 |
|
25 | 25 |
|
26 | 26 | def fetch_and_replace_text_for_server(modeladmin, request, queryset, server_name):
|
@@ -300,70 +300,82 @@ def included_curated_urls_count(self, obj) -> int:
|
300 | 300 | fetch_full_text_xli_action,
|
301 | 301 | ]
|
302 | 302 | ordering = ("cleaning_order",)
|
303 |
| - |
| 303 | + |
304 | 304 | def changelist_view(self, request, extra_context=None):
|
305 | 305 | """
|
306 | 306 | To add a button for metrics download
|
307 | 307 | """
|
308 | 308 | 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/" |
311 | 311 | return super().changelist_view(request, extra_context=extra_context)
|
312 |
| - |
| 312 | + |
313 | 313 | def get_urls(self):
|
314 | 314 | """
|
315 | 315 | To add custom endpoints for metrics functionality
|
316 | 316 | """
|
317 | 317 | urls = super().get_urls()
|
318 | 318 | 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 | + ), |
321 | 327 | ]
|
322 | 328 | return custom_urls + urls
|
323 |
| - |
| 329 | + |
324 | 330 | def download_metrics(self, request):
|
325 | 331 | """Custom view that starts metrics generation and returns to collection list"""
|
326 | 332 | task_id = str(uuid.uuid4())
|
327 | 333 | 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 | + |
331 | 337 | messages.add_message(
|
332 | 338 | request,
|
333 | 339 | 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 | + ), |
335 | 343 | )
|
336 |
| - return HttpResponse(status=303, headers={"Location": request.path.replace('/metrics/', '')}) |
337 |
| - |
| 344 | + return HttpResponse(status=303, headers={"Location": request.path.replace("/metrics/", "")}) |
| 345 | + |
338 | 346 | def get_metrics_file(self, request, task_id):
|
339 | 347 | """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 | + |
343 | 351 | # Create the retry URL
|
344 | 352 | current_url = request.build_absolute_uri()
|
345 |
| - |
| 353 | + |
346 | 354 | # Check if file exists and is not empty (minimum size 100 bytes)
|
347 | 355 | 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"' |
350 | 358 | return response
|
351 | 359 | else:
|
352 | 360 | # 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") |
354 | 362 | if os.path.exists(temp_file_path):
|
355 | 363 | messages.add_message(
|
356 | 364 | request,
|
357 | 365 | 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 | + ), |
359 | 369 | )
|
360 | 370 | else:
|
361 | 371 | messages.add_message(
|
362 | 372 | request,
|
363 | 373 | 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 | + ), |
365 | 377 | )
|
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}/", "")}) |
367 | 379 |
|
368 | 380 |
|
369 | 381 | @admin.action(description="Exclude URL and all children")
|
|
0 commit comments