IDDRS/App/views.py

179 lines
6.2 KiB
Python
Raw Normal View History

2022-06-17 12:06:51 +00:00
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
2022-06-27 13:48:56 +00:00
2022-07-25 10:03:35 +00:00
from django_pandas.io import read_frame
from django.template.loader import render_to_string
from weasyprint import HTML
import tempfile
2022-06-17 12:06:51 +00:00
# Create your views here.
def home(request):
levels = list(range(1, 7))
BASE_DIR = Path(__file__).resolve().parent.parent
2022-07-25 10:03:35 +00:00
path = os.path.join(BASE_DIR, 'static/IDDRSStandards/')
2022-06-17 12:06:51 +00:00
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}
2022-07-25 10:03:35 +00:00
return render(request, 'App/home.html', args)
2022-06-17 12:06:51 +00:00
def how_to(request):
2022-07-25 10:03:35 +00:00
return render(request, 'App/how_to.html', {'navbar': 'how_to'})
2022-06-17 12:06:51 +00:00
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):
2022-07-25 10:03:35 +00:00
return render(request, 'App/faq.html', {'navbar': 'faq'})
def contact(request):
return render(request, 'App/contact.html', {'navbar': 'contact'})
2022-06-17 12:06:51 +00:00
2022-07-25 10:03:35 +00:00
# def pdf(request):
# return render(request, 'pdf-outut.html', {'navbar': 'pdf'})
2022-06-17 12:06:51 +00:00
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','')
2022-07-25 10:03:35 +00:00
excelFile = ''
pdfFile = ''
2022-06-17 12:06:51 +00:00
2022-07-25 10:03:35 +00:00
if request.method == 'GET':
2022-06-17 12:06:51 +00:00
form = searchForm(request.GET)
2022-07-25 10:03:35 +00:00
if form.is_valid() or 'searchedPhrase' in request.GET :
2022-06-17 12:06:51 +00:00
if 'searchedPhrase' in request.GET:
phrase = request.GET.get('searchedPhrase')
else:
phrase = form.cleaned_data["phrase"]
2022-06-27 13:48:56 +00:00
searchResults = cosine_similarity(30, phrase)
2022-06-17 12:06:51 +00:00
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()
2022-07-25 10:03:35 +00:00
2022-06-17 12:06:51 +00:00
results = SearchResults.objects.filter(uniqueID=uid)
2022-07-25 10:03:35 +00:00
excelFile = createExcel(results, phrase)
pdfFile = createPDF(results, phrase)
createPDF(results, phrase)
2022-06-27 13:48:56 +00:00
2022-06-17 12:06:51 +00:00
if selectedChoices:
results = SearchResults.objects.filter(uniqueID=uid)
myFilter = StandardsFilter(request.GET, queryset=results)
results = myFilter.qs
2022-07-25 10:03:35 +00:00
excelFile = createExcel(results, phrase)
pdfFile = createPDF(results, phrase)
2022-06-17 12:06:51 +00:00
else:
form = searchForm()
2022-07-25 10:03:35 +00:00
2022-06-17 12:06:51 +00:00
# Create pagination for results
2022-06-27 13:48:56 +00:00
paginator = Paginator(results, per_page=10)
2022-06-17 12:06:51 +00:00
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,
2022-07-25 10:03:35 +00:00
'pages':paginator, 'pNumber':int(pageNumber), 'excelFile':excelFile,'pdfFile':pdfFile}
2022-06-17 12:06:51 +00:00
2022-07-25 10:03:35 +00:00
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']
2022-06-17 12:06:51 +00:00
2022-07-25 10:03:35 +00:00
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))
2022-06-17 12:06:51 +00:00
2022-07-25 10:03:35 +00:00
# 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))
2022-06-17 12:06:51 +00:00
2022-07-25 10:03:35 +00:00
return fileName