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
    })

[((10, 16, 10, 38), 'core.models.Document.objects.all', 'Document.objects.all', ({}, {}), '()', False, 'from core.models import Document\n'), ((12, 11, 12, 88), 'django.shortcuts.render', 'render', ({(12, 18, 12, 25): 'request', (12, 27, 12, 43): '"""core/home.html"""', (12, 45, 12, 87): "{'documents': documents, 'number': number}"}, {}), "(request, 'core/home.html', {'documents': documents, 'number': number})", False, 'from django.shortcuts import render, redirect\n'), ((24, 11, 24, 53), 'django.shortcuts.render', 'render', ({(24, 18, 24, 25): 'request', (24, 27, 24, 52): '"""core/simple_upload.html"""'}, {}), "(request, 'core/simple_upload.html')", False, 'from django.shortcuts import render, redirect\n'), ((35, 11, 37, 6), 'django.shortcuts.render', 'render', ({(35, 18, 35, 25): 'request', (35, 27, 35, 56): '"""core/model_form_upload.html"""', (35, 58, 37, 5): "{'form': form}"}, {}), "(request, 'core/model_form_upload.html', {'form': form})", False, 'from django.shortcuts import render, redirect\n'), ((18, 13, 18, 32), 'django.core.files.storage.FileSystemStorage', 'FileSystemStorage', ({}, {}), '()', False, 'from django.core.files.storage import FileSystemStorage\n'), ((21, 15, 23, 10), 'django.shortcuts.render', 'render', ({(21, 22, 21, 29): 'request', (21, 31, 21, 56): '"""core/simple_upload.html"""', (21, 58, 23, 9): "{'uploaded_file_url': uploaded_file_url}"}, {}), "(request, 'core/simple_upload.html', {'uploaded_file_url':\n    uploaded_file_url})", False, 'from django.shortcuts import render, redirect\n'), ((29, 15, 29, 56), 'core.forms.DocumentForm', 'DocumentForm', ({(29, 28, 29, 40): 'request.POST', (29, 42, 29, 55): 'request.FILES'}, {}), '(request.POST, request.FILES)', False, 'from core.forms import DocumentForm\n'), ((34, 15, 34, 29), 'core.forms.DocumentForm', 'DocumentForm', ({}, {}), '()', False, 'from core.forms import DocumentForm\n'), ((32, 19, 32, 35), 'django.shortcuts.redirect', 'redirect', ({(32, 28, 32, 34): '"""home"""'}, {}), "('home')", False, 'from django.shortcuts import render, redirect\n')]