D
D
DrowLegend2019-12-22 19:35:43
Django
DrowLegend, 2019-12-22 19:35:43

Django: How to make loading status change with Celery?

The essence of the task is that there is a form for uploading a file with the upload status. It is necessary that after the file is uploaded, its status should be Pending, and after a few seconds change to Success.
models.py

class UploadFile(models.Model):
    file = models.FileField()
    is_active = models.BooleanField(default='False')

views.py
def home(request):
    files = UploadFile.objects.all()

    if request.method == 'POST':
        upload_file_form = UploadFileForm(request.POST, request.FILES)

        if upload_file_form.is_valid():
            upload_file_form.save()
            return redirect(home)
    else:
        upload_file_form = UploadFileForm()
    return render(request, 'testfileapp/home.html', {
        'files': files,
        'upload_file_form': upload_file_form,
    })

signals.py
from django.dispatch import receiver
from django.db.models.signals import post_save
from .models import UploadFile
from .tasks import set_status_as_inactive


@receiver(post_save, sender=UploadFile)
def notify(sender, instance, created, **kwargs):
    if created:
        set_status_as_inactive.delay(instance.pk)

tasks.py (with this code it gives an error that it cannot import UploadFile)
from celery import shared_task
from .models import UploadFile


@shared_task
def set_status_as_inactive(pk):
    UploadFile.objects.get(pk=pk)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
antonksa, 2019-12-22
@antonksa

You approached the issue from the wrong side. The classic POST does not provide for chunks and loading in parts, so it makes no sense to try to implement this (your statuses) from the server side. All this is easily done from the JS side in the browser. There you can use promises that will resolve at the end of the download and you can change the status to Success or Failed depending on the result.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question