diff --git a/backend/api/__init__.py b/backend/api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/api/admin.py b/backend/api/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/backend/api/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/backend/api/apps.py b/backend/api/apps.py new file mode 100644 index 0000000..66656fd --- /dev/null +++ b/backend/api/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class ApiConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'api' diff --git a/backend/api/migrations/__init__.py b/backend/api/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/api/models.py b/backend/api/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/backend/api/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/backend/api/task.py b/backend/api/task.py new file mode 100644 index 0000000..4973e5e --- /dev/null +++ b/backend/api/task.py @@ -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' diff --git a/backend/api/tests.py b/backend/api/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/backend/api/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/backend/api/views.py b/backend/api/views.py new file mode 100644 index 0000000..1753789 --- /dev/null +++ b/backend/api/views.py @@ -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}) diff --git a/backend/backend/settings.py b/backend/backend/settings.py index e7e2397..f152247 100644 --- a/backend/backend/settings.py +++ b/backend/backend/settings.py @@ -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' diff --git a/backend/backend/urls.py b/backend/backend/urls.py index 5edb75c..08bda50 100644 --- a/backend/backend/urls.py +++ b/backend/backend/urls.py @@ -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) ] diff --git a/backend/celery.py b/backend/celery.py new file mode 100644 index 0000000..312fe2e --- /dev/null +++ b/backend/celery.py @@ -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() diff --git a/backend/db.sqlite3 b/backend/db.sqlite3 new file mode 100644 index 0000000..a594b9d Binary files /dev/null and b/backend/db.sqlite3 differ