<file_sep>from django.http import JsonResponse
from celery.result import AsyncResult

from .models import Task
from .tasks import work_with_file


def submit(request):
    url = request.POST['url']
    email = request.POST.get('email', False)
    task = work_with_file.delay(url, email)
    Task.objects.create(url=url, identifier=task.id, status='running').save()
    answer = {'id': task.id}
    return JsonResponse(answer)


def check(request):
    ident = request.GET.get('id')
    try:
        task = Task.objects.get(identifier=ident)
    except Task.DoesNotExist:
        answer = {'status': 'task does not exist'}
        return JsonResponse(answer, status=404)
    if task.status == 'done':
        answer = {'md5': task.md5,
                  'status': 'done',
                  'url': task.url}
        return JsonResponse(answer)
    else:
        if Task.status == 'failed' or AsyncResult(ident).failed():
            answer = {'status': 'failed'}
            task.status = 'failed'
            task.save()
            return JsonResponse(answer, status=500)
        else:
            answer = {'status': 'running'}
            return JsonResponse(answer, status=102)
<file_sep>from django.urls import path, re_path
from . import views


urlpatterns = [
    path('submit', views.submit, name='submit'),
    path('check', views.check, name='check'),
]
<file_sep>from django.contrib import admin
from .models import Task

class TaskAdmin(admin.ModelAdmin):
    list_display = ('url', 'md5', 'status', 'identifier', 'email')


admin.site.register(Task, TaskAdmin)<file_sep>Django>=2.2.7,<2.3.0
celery>=4.3.0,<4.4.0
django-celery>=3.3.1,<3.4.0<file_sep>from django.db import models

class Task(models.Model):
    url = models.CharField(max_length=128)
    md5 = models.CharField(max_length=32, null=True, blank=True)
    status = models.CharField(max_length=20)
    identifier = models.CharField(max_length=32)
    email = models.CharField(max_length=32, null=True, blank=True)


<file_sep>import hashlib
from urllib.request import urlretrieve
from urllib.parse import urlsplit


from django.core.mail import send_mail
from .models import Task


from BostonGeneTestTask.celery import app


@app.task
def work_with_file(url, email):
    fname = download_file(url)
    fhash = md5(fname)
    if email:
        send_email(email, url, fhash)
    task = Task.objects.get(url=url, md5=None)
    task.status = 'done'
    task.md5 = fhash
    task.email = email
    task.save()
    return url, fhash


def download_file(url):
    split = urlsplit(url)
    fname = 'files/' + split.path.split('/')[-1]
    urlretrieve(url, fname)
    return fname


def md5(fname):
    hash_md5 = hashlib.md5()
    with open(fname, "rb") as f:
        for chunk in iter(lambda: f.read(4096), b""):
            hash_md5.update(chunk)
    return hash_md5.hexdigest()


def send_email(email, url, fhash):
    send_mail('md5 hash',
              'url: ' + url + '\nmd5: ' + fhash,
              '<EMAIL>',
              [email],
              fail_silently=False)<file_sep>#Запуск проекта:
>docker-compose up<file_sep>from __future__ import absolute_import, unicode_literals
import os
from celery import Celery

# Setting the Default Django settings module
from django.conf import settings

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'BostonGeneTestTask.settings')
app = Celery('BostonGeneTestTask')

# Using a String here means the worker will always find the configuration information
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)


@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))