From 819ebabb1238f3b92f84b50d3aae090958bc7f26 Mon Sep 17 00:00:00 2001 From: Ihsan Ullah Date: Sun, 20 Aug 2023 13:56:05 +0500 Subject: [PATCH] submission file size added --- src/apps/pages/views.py | 22 ++++++++++++++++++++++ src/templates/pages/server_status.html | 2 ++ 2 files changed, 24 insertions(+) diff --git a/src/apps/pages/views.py b/src/apps/pages/views.py index 7395d416a..f5be309d3 100644 --- a/src/apps/pages/views.py +++ b/src/apps/pages/views.py @@ -69,8 +69,30 @@ def get_context_data(self, *args, **kwargs): context = super().get_context_data(*args, **kwargs) context['submissions'] = qs[:250] + + for submission in context['submissions']: + # Get filesize from each submissions's data + submission.file_size = self.format_file_size(submission.data.file_size) + return context + def format_file_size(self, file_size): + """ + A custom function to convert file size to KB, MB, GB and return with the unit + """ + try: + n = float(file_size) + except ValueError: + return "" + + units = ['KB', 'MB', 'GB'] + i = 0 + while n >= 1000 and i < len(units) - 1: + n /= 1000 + i += 1 + + return f"{n:.1f} {units[i]}" + def page_not_found_view(request, exception): print(request) diff --git a/src/templates/pages/server_status.html b/src/templates/pages/server_status.html index 8e4bc172b..26afaa0c9 100644 --- a/src/templates/pages/server_status.html +++ b/src/templates/pages/server_status.html @@ -11,6 +11,7 @@

Recent submissions (up to 250 or 2 days old)

Competition Submission PK + Size Submitter Hostname Submitted at @@ -26,6 +27,7 @@

Recent submissions (up to 250 or 2 days old)

{{ submission.phase.competition.title }} {{ submission.pk }} + {{ submission.file_size }} {{ submission.owner.username }} {{ submission.worker_hostname }} {{ submission.created_when|timesince }} ago