from django.shortcuts import render from rest_framework import viewsets from search_tfidf.models import Level, Standards from search_tfidf.serializer import LevelSerializer, StandardsSerializer from django.http import JsonResponse from django.views.decorators.csrf import csrf_exempt import json import os from pathlib import Path from rest_framework.decorators import api_view # 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 @csrf_exempt @api_view(['GET']) def contentList(request): # Get the values from the request parameters level = request.GET.get('level') standard = request.GET.get('standard') data = "" #module_path = filePath(level, standard) print(level, standard) # Read the JSON file #with open(module_path) as f: # data = json.load(f) # Return the filtered data as a JSON response return JsonResponse({'contents': data}) BASE_DIR = Path(__file__).resolve().parent.parent def filePath(level_input, standard_input): standards_dir = os.path.join(BASE_DIR, 'static/data/') file_path = '' levels = next(os.walk(os.path.join(BASE_DIR, 'static/data')), (None, None, []))[1] if str(level_input) in levels: filenames = next(os.walk(standards_dir+level_input), (None, None, []))[2] for file in filenames: if str(standard_input) in file: file_path = standards_dir+str(level_input)+'/'+file return file_path