40 lines
1.4 KiB
Python
40 lines
1.4 KiB
Python
|
from django.shortcuts import render
|
||
|
from rest_framework import viewsets
|
||
|
from .models import Level, Standards
|
||
|
from .serializer import LevelSerializer, StandardsSerializer
|
||
|
from django.http import JsonResponse
|
||
|
from django.views.decorators.csrf import csrf_exempt
|
||
|
import json
|
||
|
from .tfidfSearch import cosine_similarity
|
||
|
|
||
|
# Create your views here.
|
||
|
|
||
|
class LevelViewSet(viewsets.ModelViewSet):
|
||
|
queryset = Level.objects.all()
|
||
|
serializer_class = LevelSerializer
|
||
|
|
||
|
class StandardsViewSet(viewsets.ModelViewSet):
|
||
|
queryset = Standards.objects.all()
|
||
|
serializer_class = StandardsSerializer
|
||
|
|
||
|
# get the submetted search phrase from the front-end
|
||
|
@csrf_exempt
|
||
|
def get_input(request):
|
||
|
if request.method == "POST":
|
||
|
# data is a json object that holds the searched phrase in a key
|
||
|
# called phrase
|
||
|
data = json.loads(request.body.decode('utf-8'))
|
||
|
if data is not None:
|
||
|
phrase = data['data']['phrase']
|
||
|
print(phrase)
|
||
|
if phrase[0] == '"' and phrase[-1] == '"':
|
||
|
phrase = phrase[1:-1]
|
||
|
searchResults = cosine_similarity(phrase, title=True)
|
||
|
return JsonResponse({"message": "Data received", "results":searchResults})
|
||
|
|
||
|
else:
|
||
|
searchResults = cosine_similarity(phrase, title=False)
|
||
|
return JsonResponse({"message": "Data received", "results":searchResults})
|
||
|
|
||
|
|
||
|
return JsonResponse({"message": "Invalid request"})
|