import time
import os

from django.shortcuts import render, redirect
from django.http import JsonResponse
from django.views import View
from django.conf import settings


from .forms import File_uploadForm
from .models import File_upload, SummaryRes

from sim_v1.textsummary import TEXTSummary
summary_document_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)),'media','sum_v1','upload')
#summary_document_dir = r'C:\Users\ERDIG\Dropbox\Python\nlp_v1\media\sum_v1\upload'

summary_extraction_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)),'media','sum_v1','temp')
#summary_extraction_dir = r'C:\Users\ERDIG\Dropbox\Python\nlp_v1\media\sum_v1\temp'

summary_ratio = 0.01

class Upload(View):
    def post(self, request):
        time.sleep(1)  # You don't need this line. This is just to delay the process so you can see the progress bar testing locally.

        form = File_uploadForm(self.request.POST, self.request.FILES)

        print(form.errors)

        if form.is_valid():

            document = form.save()
            data = {'is_valid': True, 'name': document.file.name, 'url': document.file.url}

        else:
            data = {'is_valid': False}

        return JsonResponse(data)

    def get(self, request):

        for document in File_upload.objects.all():
            document.file.delete()
            document.delete()

        doc_list = File_upload.objects.all()

        form = File_uploadForm()

        return render(self.request, 'upload.html', {'documents': doc_list, 'form': form,})

def sum_words(request):
    if request.method == 'POST':

        form = File_uploadForm(request.POST)

        if form.is_valid():
            form.save()
            sum_words = form.cleaned_data['sum_words']
            request.session['sum_words'] = sum_words

        else:
            pass
    else:
        pass

    return redirect('sum_v1:summarize')

def clear_database(request):
    for document in File_upload.objects.all():
        document.file.delete()
        document.delete()
    return redirect(request.POST.get('next'))

def Summarize(request):
    SummaryRes.objects.all().delete()

    summary_word_count = request.session['sum_words']

    for document in os.listdir(summary_document_dir):

        for filename in os.listdir(summary_extraction_dir):
            os.remove(os.path.join(summary_extraction_dir, filename))

        text_dir = os.path.join(summary_document_dir, document)

        summary = TEXTSummary(text_dir, summary_extraction_dir, summary_ratio, summary_word_count)
        summary.textextraction()
        summary.summary()

        SummaryRes.objects.create(doc = document, summary = summary.summary)

    results = SummaryRes.objects.all()

    return render(request, 'summarize.html', {'results': results})

[((67, 11, 67, 39), 'django.shortcuts.redirect', 'redirect', ({(67, 20, 67, 38): '"""sum_v1:summarize"""'}, {}), "('sum_v1:summarize')", False, 'from django.shortcuts import render, redirect\n'), ((80, 20, 80, 52), 'os.listdir', 'os.listdir', ({(80, 31, 80, 51): 'summary_document_dir'}, {}), '(summary_document_dir)', False, 'import os\n'), ((95, 11, 95, 66), 'django.shortcuts.render', 'render', ({(95, 18, 95, 25): 'request', (95, 27, 95, 43): '"""summarize.html"""', (95, 45, 95, 65): "{'results': results}"}, {}), "(request, 'summarize.html', {'results': results})", False, 'from django.shortcuts import render, redirect\n'), ((14, 52, 14, 77), 'os.path.dirname', 'os.path.dirname', ({(14, 68, 14, 76): '__file__'}, {}), '(__file__)', False, 'import os\n'), ((17, 54, 17, 79), 'os.path.dirname', 'os.path.dirname', ({(17, 70, 17, 78): '__file__'}, {}), '(__file__)', False, 'import os\n'), ((24, 8, 24, 21), 'time.sleep', 'time.sleep', ({(24, 19, 24, 20): '(1)'}, {}), '(1)', False, 'import time\n'), ((38, 15, 38, 33), 'django.http.JsonResponse', 'JsonResponse', ({(38, 28, 38, 32): 'data'}, {}), '(data)', False, 'from django.http import JsonResponse\n'), ((50, 15, 50, 90), 'django.shortcuts.render', 'render', ({(50, 22, 50, 34): 'self.request', (50, 36, 50, 49): '"""upload.html"""', (50, 51, 50, 89): "{'documents': doc_list, 'form': form}"}, {}), "(self.request, 'upload.html', {'documents': doc_list, 'form': form})", False, 'from django.shortcuts import render, redirect\n'), ((82, 24, 82, 58), 'os.listdir', 'os.listdir', ({(82, 35, 82, 57): 'summary_extraction_dir'}, {}), '(summary_extraction_dir)', False, 'import os\n'), ((85, 19, 85, 63), 'os.path.join', 'os.path.join', ({(85, 32, 85, 52): 'summary_document_dir', (85, 54, 85, 62): 'document'}, {}), '(summary_document_dir, document)', False, 'import os\n'), ((87, 18, 87, 98), 'sim_v1.textsummary.TEXTSummary', 'TEXTSummary', ({(87, 30, 87, 38): 'text_dir', (87, 40, 87, 62): 'summary_extraction_dir', (87, 64, 87, 77): 'summary_ratio', (87, 79, 87, 97): 'summary_word_count'}, {}), '(text_dir, summary_extraction_dir, summary_ratio, summary_word_count\n    )', False, 'from sim_v1.textsummary import TEXTSummary\n'), ((83, 22, 83, 68), 'os.path.join', 'os.path.join', ({(83, 35, 83, 57): 'summary_extraction_dir', (83, 59, 83, 67): 'filename'}, {}), '(summary_extraction_dir, filename)', False, 'import os\n')]