added docx endpoints, inited celery

This commit is contained in:
Alexander Karpov 2022-08-26 20:04:45 +03:00
parent 2773493a6b
commit dd4ae8ee4e
20 changed files with 143 additions and 11 deletions

0
checker/api/__init__.py Normal file
View File

View File

@ -0,0 +1,14 @@
from django.core.files.uploadedfile import InMemoryUploadedFile
from rest_framework import serializers
from checker.models import Docx
class DocxSerializer(serializers.ModelSerializer):
class Meta:
model = Docx
fields = ["uuid", "file"]
extra_kwargs = {"uuid": {"read_only": True}}
def validate_file(self, file: InMemoryUploadedFile):
return file

11
checker/api/views.py Normal file
View File

@ -0,0 +1,11 @@
from rest_framework import generics
from rest_framework.parsers import MultiPartParser, FormParser
from checker.api.serializers import DocxSerializer
from checker.models import Docx
class ListCreateDocxApiView(generics.ListCreateAPIView):
parser_classes = [FormParser, MultiPartParser]
serializer_class = DocxSerializer
queryset = Docx.objects.all()

View File

@ -4,3 +4,6 @@ from django.apps import AppConfig
class CheckerConfig(AppConfig): class CheckerConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField" default_auto_field = "django.db.models.BigAutoField"
name = "checker" name = "checker"
def ready(self):
import checker.signals

View File

@ -0,0 +1,30 @@
# Generated by Django 4.1 on 2022-08-26 14:23
from django.db import migrations, models
import uuid
class Migration(migrations.Migration):
initial = True
dependencies = []
operations = [
migrations.CreateModel(
name="Docx",
fields=[
(
"uuid",
models.UUIDField(
default=uuid.uuid4,
editable=False,
primary_key=True,
serialize=False,
unique=True,
),
),
("file", models.FileField(upload_to="")),
],
),
]

View File

@ -1,3 +1,11 @@
import uuid as uuid
from django.db import models from django.db import models
# Create your models here. # Create your models here.
class Docx(models.Model):
uuid = models.UUIDField(
default=uuid.uuid4, editable=False, unique=True, primary_key=True
)
file = models.FileField(upload_to="")

View File

0
checker/services/file.py Normal file
View File

View File

12
checker/signals.py Normal file
View File

@ -0,0 +1,12 @@
from django.db.models.signals import post_save
from django.dispatch import receiver
from checker.models import Docx
from checker.tasks import process_file
@receiver(post_save, sender=Docx)
def create_player(sender, instance, created, **kwargs):
if created:
process_file.delay(file=instance)
return

8
checker/tasks.py Normal file
View File

@ -0,0 +1,8 @@
from celery import shared_task
from checker.models import Docx
@shared_task
def process_file(file: Docx):
return

View File

@ -1,3 +0,0 @@
from django.test import TestCase
# Create your tests here.

View File

@ -1,3 +0,0 @@
from django.shortcuts import render
# Create your views here.

View File

@ -0,0 +1,5 @@
# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app
__all__ = ("celery_app",)

View File

@ -1 +1,8 @@
urlpatterns = [] from django.urls import path, include
from checker.api.views import ListCreateDocxApiView
urlpatterns = [
path("health/", include("health_check.urls")),
path("docx/", ListCreateDocxApiView.as_view(), name="list_create_docx")
]

23
conf/celery.py Normal file
View File

@ -0,0 +1,23 @@
import os
from celery import Celery
# Set the default Django settings module for the 'celery' program.
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "conf.settings.local")
app = Celery("mistake_checker_hack_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 configuration keys
# should have a `CELERY_` prefix.
app.config_from_object("django.conf:settings", namespace="CELERY")
# Load task modules from all registered Django apps.
app.autodiscover_tasks()
# TODO REMOVE THIS
@app.task(bind=True)
def debug_task(self):
print(f"Request: {self.request!r}")

View File

@ -61,6 +61,7 @@ THIRD_PARTY_APPS = [
"rest_framework", "rest_framework",
"drf_yasg", "drf_yasg",
"corsheaders", "corsheaders",
"django_celery_results"
] ]
HEALTH_CHECKS = [ HEALTH_CHECKS = [
@ -188,8 +189,14 @@ REST_FRAMEWORK = {
"rest_framework_simplejwt.authentication.JWTAuthentication", "rest_framework_simplejwt.authentication.JWTAuthentication",
), ),
"DEFAULT_PERMISSION_CLASSES": ("rest_framework.permissions.IsAuthenticated",), "DEFAULT_PERMISSION_CLASSES": ("rest_framework.permissions.IsAuthenticated",),
"DEFAULT_SCHEMA_CLASS": "drf_spectacular.openapi.AutoSchema",
} }
# django-cors-headers # django-cors-headers
CORS_ALLOW_ALL_ORIGINS = True CORS_ALLOW_ALL_ORIGINS = True
# Celery
CELERY_BROKER_URL = 'redis://localhost:6379/0'
CELERY_TIMEZONE = "Europe/Moscow"
CELERY_TASK_TRACK_STARTED = True
CELERY_TASK_TIME_LIMIT = 30 * 60

View File

@ -51,7 +51,6 @@ REST_FRAMEWORK = {
"rest_framework_simplejwt.authentication.JWTAuthentication", "rest_framework_simplejwt.authentication.JWTAuthentication",
), ),
"DEFAULT_PERMISSION_CLASSES": ("rest_framework.permissions.AllowAny",), "DEFAULT_PERMISSION_CLASSES": ("rest_framework.permissions.AllowAny",),
"DEFAULT_SCHEMA_CLASS": "drf_spectacular.openapi.AutoSchema",
} }
# STATIC # STATIC

View File

@ -13,3 +13,8 @@ services:
- "5432:5432" - "5432:5432"
network_mode: "host" network_mode: "host"
redis:
image: "redis:alpine"
ports:
- "6379:6379"

View File

@ -7,5 +7,11 @@ djangorestframework-simplejwt==5.2.0
django-health-check==3.16.5 django-health-check==3.16.5
django-cors-headers==3.13.0 django-cors-headers==3.13.0
drf-yasg==1.21.3 drf-yasg==1.21.3
celery==5.2.7
Redis==4.3.4
django_celery_results==2.4.0
psutil psutil
dj-database-url dj-database-url
uuid
python-docx