This commit is contained in:
karthikeyan-ks
2025-01-06 22:17:09 +05:30
parent 2ee423f35f
commit 10181e4d17
12 changed files with 77 additions and 0 deletions
View File
+3
View File
@@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.
+6
View File
@@ -0,0 +1,6 @@
from django.apps import AppConfig
class ApiConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'api'
View File
+3
View File
@@ -0,0 +1,3 @@
from django.db import models
# Create your models here.
+9
View File
@@ -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'
+3
View File
@@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.
+31
View File
@@ -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})
+4
View File
@@ -37,6 +37,7 @@ INSTALLED_APPS = [
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'api'
]
MIDDLEWARE = [
@@ -121,3 +122,6 @@ STATIC_URL = 'static/'
# https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
CELERY_BROKER_URL = 'redis://localhost:6379/0'
CELERY_ACCEPT_CONTENT = ['json']
CELERY_TASK_SERIALIZER = 'json'
+2
View File
@@ -16,7 +16,9 @@ Including another URLconf
"""
from django.contrib import admin
from django.urls import path
from api.views import upload
urlpatterns = [
path('admin/', admin.site.urls),
path('upload/',upload)
]
+16
View File
@@ -0,0 +1,16 @@
from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'backend.settings')
app = Celery('backend')
# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# Namespace 'CELERY' means all celery-related configs must start with 'CELERY_'
app.config_from_object('django.conf:settings', namespace='CELERY')
# Load task modules from all registered Django app configs.
app.autodiscover_tasks()
Binary file not shown.