from django.shortcuts import render from .forms import searchForm from pathlib import Path import os import pandas as pd from .similarity import phrase_preprocessing, similarityCheck from .tfidfSimilarity import cosine_similarity from django.core.paginator import Paginator from.models import Standards, Level, SearchResults from .filters import StandardsFilter import uuid from datetime import date, timedelta from django_pandas.io import read_frame from django.template.loader import render_to_string from weasyprint import HTML import tempfile # Create your views here. def home(request): levels = list(range(1, 7)) BASE_DIR = Path(__file__).resolve().parent.parent path = os.path.join(BASE_DIR, 'static/IDDRSStandards/') df = pd.DataFrame(columns=["Level","FileNumber","FileName","Summary","FilePath"]) all_standards = Standards.objects.all() i = 0 # list_of_standardas = [] for level in os.listdir(path): for file in os.listdir(path+'/'+level): df.loc[i, 'Level'] = level df.loc[i, 'FileName'] = file i+= 1 args = {'all_standards': all_standards,'navbar': 'home','levels':levels} return render(request, 'App/home.html', args) def how_to(request): return render(request, 'App/how_to.html', {'navbar': 'how_to'}) def search(request): BASE_DIR = Path(__file__).resolve().parent.parent preprocessedData = pd.read_json(os.path.join(BASE_DIR, 'App/static/json/processedIDDRS.json')) phrase = '' results = [] if request.method == 'POST': form = searchForm(request.POST) if form.is_valid(): phrase = form.cleaned_data["phrase"] preprocessedPhrase = phrase_preprocessing(phrase) results = similarityCheck(preprocessedData, preprocessedPhrase) else: form = searchForm() args = {'form': form, 'phrase': phrase, 'navbar': 'search', 'results':results} return render(request, 'search.html', args) def faq(request): return render(request, 'App/faq.html', {'navbar': 'faq'}) def contact(request): return render(request, 'App/contact.html', {'navbar': 'contact'}) # def pdf(request): # return render(request, 'pdf-outut.html', {'navbar': 'pdf'}) def tfidf(request): SearchResults.objects.filter(session_expiry_date=date.today()).delete() searchResults = [] results = [] categories = Level.objects.all() standards = Standards.objects.all() myFilter = StandardsFilter() selectedChoices = request.GET.getlist('title','') phrase = request.GET.get('phrase','') excelFile = '' pdfFile = '' if request.method == 'GET': form = searchForm(request.GET) if form.is_valid() or 'searchedPhrase' in request.GET : if 'searchedPhrase' in request.GET: phrase = request.GET.get('searchedPhrase') else: phrase = form.cleaned_data["phrase"] searchResults = cosine_similarity(30, phrase) uid = str(uuid.uuid1())[:8]+phrase for result in searchResults: searchedData = SearchResults( session_key = request.session.session_key, uniqueID = uid, session_expiry_date = date.today() + timedelta(days=1), index = result['Index'], level = result['Level'], levelName = result['LevelName'], title = result['Title'], paragraph = result['Paragraph'], color = result['Color'], module = result['Module'], heading1 = result['Heading1'], heading2 = result['Heading2'], heading3 = result['Heading3'], heading4 = result['Heading4'], pageNumber = result['PageNum'], sentence = result['Sentence'] ) searchedData.save() results = SearchResults.objects.filter(uniqueID=uid) excelFile = createExcel(results, phrase) pdfFile = createPDF(results, phrase) createPDF(results, phrase) if selectedChoices: results = SearchResults.objects.filter(uniqueID=uid) myFilter = StandardsFilter(request.GET, queryset=results) results = myFilter.qs excelFile = createExcel(results, phrase) pdfFile = createPDF(results, phrase) else: form = searchForm() # Create pagination for results paginator = Paginator(results, per_page=10) pageNumber = request.GET.get('page', 1) page_obj = paginator.get_page(pageNumber) args = {'form': form, 'phrase': phrase, 'navbar': 'tfidf', 'results':page_obj.object_list, 'levels':categories, 'myFilter':myFilter, 'standards':standards, 'selectedChoices':selectedChoices, 'pages':paginator, 'pNumber':int(pageNumber), 'excelFile':excelFile,'pdfFile':pdfFile} return render(request, 'App/tfidf.html', args) # This function export the list of results to excel file def createExcel(results, phrase): df = read_frame(results) df = df.drop(columns=['id','session_key','uniqueID','session_expiry_date','index','level','title','color','heading2','heading3','heading4','sentence']) df = df[['levelName','module','heading1','pageNumber','paragraph']] df.columns = ['Level', 'IDDRS', 'Title', 'Page', 'Paragraph'] BASE_DIR = Path(__file__).resolve().parent.parent fileName = phrase+'.xlsx' # creating excel writer object writer = pd.ExcelWriter(os.path.join(BASE_DIR, 'static/usersResults/'+fileName)) # write dataframe to excel df.to_excel(writer,sheet_name='IDDRS') # save the excel writer.save() return(fileName) def createPDF(results, phrase): BASE_DIR = Path(__file__).resolve().parent.parent html_String = render_to_string(os.path.join(BASE_DIR, 'templates/App/pdf-output.html'), {'results':results, 'phrase':phrase}) html = HTML(string = html_String) fileName = phrase+'.pdf' html.write_pdf(os.path.join(BASE_DIR, 'static/usersResults/'+fileName)) return fileName