From 4a117eff83125543dc6e2110d6f925591c9d3c3a Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Thu, 27 Feb 2020 22:45:50 +0000 Subject: [PATCH] Return stats about last backup download times --- apiserver/apiserver/api/views.py | 36 ++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/apiserver/apiserver/api/views.py b/apiserver/apiserver/api/views.py index e6b650e..f05d341 100644 --- a/apiserver/apiserver/api/views.py +++ b/apiserver/apiserver/api/views.py @@ -388,20 +388,34 @@ class BackupView(views.APIView): backup_user = secrets.BACKUP_TOKENS.get(auth_token, None) - if not backup_user: - raise exceptions.PermissionDenied() + if backup_user: + backup_path = cache.get(backup_user['cache_key'], None) - backup_path = cache.get(backup_user['cache_key'], None) + if not backup_path: + raise Http404 - if not backup_path: - raise Http404 + backup_url = 'https://static.{}/backups/{}'.format( + settings.PRODUCTION_HOST, + backup_path, + ) + cache.set(backup_user['name'], datetime.datetime.now()) - backup_url = 'https://static.{}/backups/{}'.format( - settings.PRODUCTION_HOST, - backup_path, - ) - - return redirect(backup_url) + return redirect(backup_url) + else: + backup_stats = [] + for backup_user in secrets.BACKUP_TOKENS.values(): + download_time = cache.get(backup_user['name'], None) + if download_time: + time_delta = datetime.datetime.now() - download_time + less_than_24h = bool(time_delta.days == 0) + else: + less_than_24h = False + backup_stats.append(dict( + backup_user=backup_user['name'], + download_time=download_time, + less_than_24h=less_than_24h, + )) + return Response(backup_stats) class PasteView(views.APIView):