def download(self, *args, **kwargs): cf = get_object_or_404(CachedFile, id=kwargs['cfid']) if cf.file: resp = ChunkBasedFileResponse(cf.file.file, content_type=cf.type) resp['Content-Disposition'] = 'attachment; filename="{}"'.format(cf.filename).encode("ascii", "ignore") return resp elif not settings.HAS_CELERY: return Response( {'status': 'failed', 'message': 'Unknown file ID or export failed'}, status=status.HTTP_410_GONE ) res = AsyncResult(kwargs['asyncid']) if res.failed(): if isinstance(res.info, dict) and res.info['exc_type'] == 'ExportError': msg = res.info['exc_message'] else: msg = 'Internal error' return Response( {'status': 'failed', 'message': msg}, status=status.HTTP_410_GONE ) return Response( { 'status': 'running' if res.state in ('PROGRESS', 'STARTED', 'SUCCESS') else 'waiting', 'percentage': res.result.get('value', None) if res.result else None, }, status=status.HTTP_409_CONFLICT )
def get(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: if 'ajax' in request.GET: return HttpResponse('1' if self.object.file else '0') elif self.object.file: resp = ChunkBasedFileResponse(self.object.file.file, content_type=self.object.type) resp['Content-Disposition'] = 'attachment; filename="{}"'.format(self.object.filename).encode('ascii', 'ignore') return resp else: return super().get(request, *args, **kwargs)