changes
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
||||
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class ApiConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'api'
|
||||
@@ -0,0 +1,3 @@
|
||||
from django.db import models
|
||||
|
||||
# Create your models here.
|
||||
@@ -0,0 +1,9 @@
|
||||
from __future__ import absolute_import, unicode_literals
|
||||
from celery import shared_task
|
||||
import time
|
||||
|
||||
@shared_task
|
||||
def process_uploaded_file(file_path):
|
||||
# Simulate long-running task
|
||||
time.sleep(300) # Replace with actual file processing logic
|
||||
return 'File processed'
|
||||
@@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
@@ -0,0 +1,31 @@
|
||||
from django.shortcuts import render
|
||||
from django.http import JsonResponse
|
||||
from .tasks import process_uploaded_file
|
||||
from .forms import UploadFileForm
|
||||
import os
|
||||
|
||||
# Create your views here.
|
||||
def handle_uploaded_file(f, file_path):
|
||||
with open(file_path, 'wb+') as destination:
|
||||
for chunk in f.chunks():
|
||||
destination.write(chunk)
|
||||
|
||||
def upload(request):
|
||||
if request.method == 'POST':
|
||||
form = UploadFileForm(request.POST, request.FILES)
|
||||
if form.is_valid():
|
||||
file = form.cleaned_data['file']
|
||||
file_path = os.path.join('uploads', file.name) # Change 'uploads' to your desired directory
|
||||
handle_uploaded_file(file, file_path)
|
||||
task = process_uploaded_file.delay(file_path)
|
||||
return JsonResponse({'task_id': task.id})
|
||||
|
||||
else:
|
||||
form = UploadFileForm()
|
||||
return render(request, 'upload.html', {'form': form})
|
||||
|
||||
from celery.result import AsyncResult
|
||||
|
||||
def check_task_status(request, task_id):
|
||||
task_result = AsyncResult(task_id)
|
||||
return JsonResponse({'status': task_result.status, 'result': task_result.result})
|
||||
Reference in New Issue
Block a user