from django.core.files.storage import FileSystemStorage
from django.shortcuts import render, redirect
from core.forms import DocumentForm

from core.models import Document
from media import image_cv2


def home(request):
    documents = Document.objects.all()
    number = len(image_cv2.myList)
    return render(request, 'core/home.html', {'documents': documents, 'number': number})


def simple_upload(request):
    if request.method == 'POST' and request.FILES['myfile']:
        myfile = request.FILES['myfile']
        fs = FileSystemStorage()
        filename = fs.save(myfile.name, myfile)
        uploaded_file_url = fs.url(filename)
        return render(request, 'core/simple_upload.html', {
            'uploaded_file_url': uploaded_file_url
        })
    return render(request, 'core/simple_upload.html')


def model_form_upload(request):
    if request.method == 'POST':
        form = DocumentForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            return redirect('home')
    else:
        form = DocumentForm()
    return render(request, 'core/model_form_upload.html', {
        'form': form
    })

[((237, 259), 'core.models.Document.objects.all', 'Document.objects.all', ([], {}), '()\n', (257, 259), False, 'from core.models import Document\n'), ((306, 383), 'django.shortcuts.render', 'render', (['request', '"""core/home.html"""', "{'documents': documents, 'number': number}"], {}), "(request, 'core/home.html', {'documents': documents, 'number': number})\n", (312, 383), False, 'from django.shortcuts import render, redirect\n'), ((775, 817), 'django.shortcuts.render', 'render', (['request', '"""core/simple_upload.html"""'], {}), "(request, 'core/simple_upload.html')\n", (781, 817), False, 'from django.shortcuts import render, redirect\n'), ((1081, 1143), 'django.shortcuts.render', 'render', (['request', '"""core/model_form_upload.html"""', "{'form': form}"], {}), "(request, 'core/model_form_upload.html', {'form': form})\n", (1087, 1143), False, 'from django.shortcuts import render, redirect\n'), ((529, 548), 'django.core.files.storage.FileSystemStorage', 'FileSystemStorage', ([], {}), '()\n', (546, 548), False, 'from django.core.files.storage import FileSystemStorage\n'), ((657, 745), 'django.shortcuts.render', 'render', (['request', '"""core/simple_upload.html"""', "{'uploaded_file_url': uploaded_file_url}"], {}), "(request, 'core/simple_upload.html', {'uploaded_file_url':\n    uploaded_file_url})\n", (663, 745), False, 'from django.shortcuts import render, redirect\n'), ((900, 941), 'core.forms.DocumentForm', 'DocumentForm', (['request.POST', 'request.FILES'], {}), '(request.POST, request.FILES)\n', (912, 941), False, 'from core.forms import DocumentForm\n'), ((1055, 1069), 'core.forms.DocumentForm', 'DocumentForm', ([], {}), '()\n', (1067, 1069), False, 'from core.forms import DocumentForm\n'), ((1013, 1029), 'django.shortcuts.redirect', 'redirect', (['"""home"""'], {}), "('home')\n", (1021, 1029), False, 'from django.shortcuts import render, redirect\n')]