def test_util_expires_download_without_file(self): """expire_user_data_download handles missing download file""" data_download = request_user_data_download(self.user) data_download.status = DataDownload.STATUS_READY expire_user_data_download(data_download) self.assertEqual(data_download.status, DataDownload.STATUS_EXPIRED)
def test_util_marks_download_as_expired(self): """expire_user_data_download changed data download status to expired""" data_download = request_user_data_download(self.user) data_download.status = DataDownload.STATUS_READY with open(TEST_FILE_PATH, 'rb') as download_file: data_download.file = File(download_file) data_download.save() expire_user_data_download(data_download) self.assertEqual(data_download.status, DataDownload.STATUS_EXPIRED)
def handle(self, *args, **options): downloads_expired = 0 queryset = DataDownload.objects.select_related('user') queryset = queryset.filter( status=DataDownload.STATUS_READY, expires_on__lte=timezone.now(), ) for data_download in chunk_queryset(queryset): expire_user_data_download(data_download) downloads_expired += 1 self.stdout.write("Data downloads expired: %s" % downloads_expired)
def test_util_deletes_file(self): """expire_user_data_download deleted file associated with download""" data_download = request_user_data_download(self.user) data_download.status = DataDownload.STATUS_READY with open(TEST_FILE_PATH, 'rb') as download_file: data_download.file = File(download_file) data_download.save() download_file_path = data_download.file.path expire_user_data_download(data_download) self.assertFalse(data_download.file) self.assertFalse(os.path.isdir(download_file_path))
def action_expire(self, request, data_downloads): for data_download in data_downloads: expire_user_data_download(data_download) messages.success(request, _("Selected data downloads have been set as expired."))