Operating on a page called foo.com/process
, you have the capability to generate data reports. At times, when dealing with substantial amounts of data, the generation process can be time-consuming. To address this, a Celery task was implemented to manage the PDF generation aspect. Reports are initiated by clicking on a PDF image:
<i data-processid="{{process.id}}" data-filename="{{process.pdf}}" class="fas fa-file-download getPdf"></i>
Upon clicking the FA icon, an AJAX POST request is triggered towards the Django view:
$(".getPdf").on('click', function(event) {
var thisId = $(this).data('processid');
var filename = $(this).data('filename');
$.post({
url: "{% url 'process' %}", // Refers to foo.com/process
data: {
csrfmiddlewaretoken: "{{ csrf_token }}",
id: thisId
},
success: function(data) {
... handle success
},
error: function(data) {
... handle error
},
traditional: true
}).done();
downloadPdf(filename);
function downloadPdf(filename) {
setTimeout(function() { window.location.href = filename; }, 10000);
};
});
The AJAX operation forwards the request to the backend to commence the Celery process for generating the PDF, followed by immediate execution of a downloadPfd
function - a delayed process assumed to facilitate file downloading via JavaScript. The timeout allocated gives allowance for the file completion.
Despite creating and positioning the PDF in the principal project directory (with intentions to later adjust), a "file not found" issue arises.
Considering alternatives, utilizing Django might offer a more viable solution, which I've explored as well.
Concerning the POST segment of my Django view, here's what it contains:
class ProcessView(generic.DetailView):
template_name = 'pages/process.html'
def get(self, request):
# Handling GET operations
return render(request, self.template_name, {'process_form': process_form, 'process': process_list})
def post(self, request):
# Obtain Process ID from incoming request
process_id = request.POST.get('id')
# Extract necessary objects and execute queries
... python and DB processes ...
# Triggers a Celery task for asynchronous PDF generation
task = create_pdf.delay(sensor, range_period)
return HttpResponseRedirect(self.request.path_info, {"task_id": task.task_id })
The POST functionality functions proficiently. Upon button click, an AJAX POST initiates, leading to the creation of a Celery task that generates a file. However, acquiring the task_id
at the frontend poses a challenge.
The complexity arises when attempting to utilize the Celery task.task_id
on the frontend to intermittently monitor my Django view for PDF completion status and eventually downloading the relevant file. Equipped with both the filename and task ID, navigating this process remains intricate.
Attempting to follow guidance from a concise tutorial, implementing the script component proves challenging due to limited expertise in JS. Struggling with understanding the section related to "Checking periodically for file existence and initiating download," mainly due to the disparity between function-based views in the tutorial and my reliance on class-based views within my project.