|
14 | 14 | __date__ = '22/10/2024'
|
15 | 15 | __copyright__ = ('Copyright 2023, Unicef')
|
16 | 16 |
|
17 |
| -from django.contrib import admin |
| 17 | +import os |
| 18 | +import re |
| 19 | +import mimetypes |
| 20 | +from datetime import datetime |
| 21 | +from django.conf import settings |
| 22 | +from django.contrib import admin, messages |
| 23 | +from django.urls import re_path, reverse |
| 24 | +from django.http import ( |
| 25 | + HttpResponseRedirect, |
| 26 | + HttpResponse, |
| 27 | + Http404, |
| 28 | + FileResponse |
| 29 | +) |
| 30 | +from django.utils.html import format_html |
| 31 | + |
| 32 | +from geosight.machine_info_fetcher.models import MachineInfo, LogFile |
| 33 | + |
| 34 | + |
| 35 | +def list_log_files(parent_dir): |
| 36 | + """Get list of log files from parent_dir.""" |
| 37 | + log_files = [] |
| 38 | + for root, dirs, files in os.walk(parent_dir): |
| 39 | + for file in files: |
| 40 | + if ".log" in file: |
| 41 | + file_path = os.path.join(root, file) |
| 42 | + file_size = os.path.getsize(file_path) |
| 43 | + created_on = datetime.fromtimestamp( |
| 44 | + os.path.getctime(file_path) |
| 45 | + ).strftime('%Y-%m-%d %H:%M:%S') |
| 46 | + log_files.append((file_path, file_size, created_on)) |
| 47 | + return log_files |
| 48 | + |
| 49 | + |
| 50 | +class LogFileAdmin(admin.ModelAdmin): |
| 51 | + """Admin class for LogFile.""" |
| 52 | + |
| 53 | + list_display = ( |
| 54 | + 'filename', 'size', 'created_on', 'download_link') |
| 55 | + actions = ['refresh_log_files'] |
| 56 | + |
| 57 | + def download_link(self, obj): |
| 58 | + return format_html( |
| 59 | + '<a href="{}">Download</a>', |
| 60 | + reverse('admin:dashboard_download_log_file', |
| 61 | + args=[obj.pk]) |
| 62 | + ) |
| 63 | + download_link.short_description = 'Download Log File' |
| 64 | + |
| 65 | + def get_urls(self): |
| 66 | + urls = super().get_urls() |
| 67 | + custom_urls = [ |
| 68 | + re_path(r'^download-log-file/(?P<pk>\d+)$', |
| 69 | + self.download_log_file, |
| 70 | + name='dashboard_download_log_file' |
| 71 | + ), |
| 72 | + ] |
| 73 | + return custom_urls + urls |
| 74 | + |
| 75 | + def download_log_file(self, request, pk): |
| 76 | + try: |
| 77 | + log_file = LogFile.objects.get(pk=pk) |
| 78 | + file_path = log_file.path |
| 79 | + file_handle = open(file_path, 'rb') |
| 80 | + |
| 81 | + # Use FileResponse for efficient streaming |
| 82 | + response = FileResponse( |
| 83 | + file_handle, |
| 84 | + content_type=mimetypes.guess_type(file_path)[0] |
| 85 | + ) |
| 86 | + response['Content-Disposition'] = ( |
| 87 | + f'attachment; filename="{log_file.filename()}"' |
| 88 | + ) |
| 89 | + return response |
| 90 | + except LogFile.DoesNotExist: |
| 91 | + raise Http404("Log file not found") |
| 92 | + except Exception as e: |
| 93 | + return HttpResponse(f"Error: {e}", status=500) |
| 94 | + |
| 95 | + def refresh_log_files(self, request, queryset): |
| 96 | + """Refresh log file list.""" |
| 97 | + LogFile.objects.all().delete() |
| 98 | + |
| 99 | + # Insert new log files |
| 100 | + new_log_files = list_log_files(settings.LOGS_DIRECTORY) |
| 101 | + log_entries = [ |
| 102 | + LogFile(path=path, size=size, created_on=created_on) |
| 103 | + for path, size, created_on in new_log_files |
| 104 | + ] |
| 105 | + LogFile.objects.bulk_create(log_entries) |
| 106 | + |
| 107 | + # Send a success message |
| 108 | + self.message_user( |
| 109 | + request, |
| 110 | + "Log files have been refreshed.", |
| 111 | + level=messages.SUCCESS |
| 112 | + ) |
| 113 | + |
| 114 | + # Redirect back to the log file list page |
| 115 | + return HttpResponseRedirect(request.get_full_path()) |
| 116 | + |
| 117 | + refresh_log_files.short_description = "Refresh log files" |
18 | 118 |
|
19 |
| -from geosight.machine_info_fetcher.models import MachineInfo |
20 | 119 |
|
21 | 120 | admin.site.register(MachineInfo, admin.ModelAdmin)
|
| 121 | +admin.site.register(LogFile, LogFileAdmin) |
0 commit comments