157 lines
5.1 KiB
Python
157 lines
5.1 KiB
Python
from rest_framework import viewsets
|
|
from .models import Levels, StandardsList, FileEvent
|
|
from .serializers import LevelSerializer, StandardsSerializer, FileEventSerializer
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
import os
|
|
from pathlib import Path
|
|
from rest_framework.decorators import api_view
|
|
from rest_framework.response import Response
|
|
from django.db.models import Max
|
|
import shutil
|
|
from .PreprocessFile import PreprocessFile
|
|
from .CreateIndexES import CreateIndexES
|
|
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# Create your views here.
|
|
|
|
class LevelViewSet(viewsets.ModelViewSet):
|
|
queryset = Levels.objects.all()
|
|
serializer_class = LevelSerializer
|
|
|
|
class StandardsViewSet(viewsets.ModelViewSet):
|
|
queryset = StandardsList.objects.all().order_by('levelNumber')
|
|
serializer_class = StandardsSerializer
|
|
|
|
class FileEventsViewSet(viewsets.ModelViewSet):
|
|
queryset = FileEvent.objects.all().filter(fileStatus='Pending')
|
|
serializer_class = FileEventSerializer
|
|
|
|
BASE_DIR = Path(__file__).resolve().parent.parent
|
|
|
|
# ################################################################
|
|
# #######################Levels###################################
|
|
# ################################################################
|
|
def check_level_folder():
|
|
level_numbers = Levels.objects.values_list('levelNumber', flat=True)
|
|
for level_number in level_numbers:
|
|
level_folder = os.path.join(BASE_DIR,'media/data/Standards/'+str(level_number)+'/')
|
|
if not os.path.exists(level_folder):
|
|
os.makedirs(level_folder)
|
|
|
|
@csrf_exempt
|
|
@api_view(['POST'])
|
|
def levelSubmit(request):
|
|
data = request.data['params']['editingRow']
|
|
|
|
if 'id' in data:
|
|
level = Levels.objects.get(id=data['id'])
|
|
serializer = LevelSerializer(instance=level, data=data)
|
|
if serializer.is_valid():
|
|
serializer.save()
|
|
check_level_folder()
|
|
|
|
else:
|
|
newLevelNumber = Levels.objects.aggregate(Max('levelNumber'))['levelNumber__max']+1
|
|
data['levelNumber'] = newLevelNumber
|
|
serializer = LevelSerializer(data=data)
|
|
if serializer.is_valid():
|
|
serializer.save()
|
|
check_level_folder()
|
|
|
|
return Response('')
|
|
|
|
@csrf_exempt
|
|
@api_view(['POST'])
|
|
def levelDelete(request):
|
|
data = request.data['params']['rowData']
|
|
level = Levels.objects.get(id=data['id'])
|
|
level.delete()
|
|
level_del_dir = os.path.join(BASE_DIR, 'media/data/Standards/'+str(data['levelNumber'])+'/')
|
|
shutil.rmtree(level_del_dir)
|
|
|
|
return Response('')
|
|
|
|
# ################################################################
|
|
# #######################Standards################################
|
|
# ################################################################
|
|
|
|
@csrf_exempt
|
|
@api_view(['POST'])
|
|
def upload_standard(request):
|
|
if request.method == 'POST':
|
|
|
|
level_number = request.data['selectedLevel']
|
|
pdfFile = request.FILES['pdfFile']
|
|
wordFile = request.FILES['wordFile']
|
|
|
|
standard_level_id = Levels.objects.filter(levelNumber=level_number).values_list('id', flat=True)[0]
|
|
|
|
standard = {
|
|
'levelID': standard_level_id,
|
|
'levelNumber': level_number,
|
|
'standardFilePDF': pdfFile,
|
|
'standardFileWord': wordFile,
|
|
'standardTitle': pdfFile.name.split('.pdf')[0]
|
|
}
|
|
serializer = StandardsSerializer(data=standard)
|
|
if serializer.is_valid():
|
|
serializer.save()
|
|
# Create a FileEvent for the upload
|
|
FileEvent.objects.create(event_type='UPLOAD', file_name=pdfFile.name.split('.pdf')[0])
|
|
|
|
else:
|
|
print('Invalid')
|
|
|
|
return Response('Done!')
|
|
|
|
|
|
@csrf_exempt
|
|
@api_view(['POST'])
|
|
def standardDelete(request):
|
|
data = request.data['params']['stdData']
|
|
standard = StandardsList.objects.get(id = data['id'])
|
|
delete_file(standard.standardFilePDF.path)
|
|
delete_file(standard.standardFileWord.path)
|
|
standard.delete()
|
|
# Create a FileEvent for the delete
|
|
FileEvent.objects.create(event_type='DELETE', file_name=standard.standardFilePDF.name.split('.pdf')[0])
|
|
|
|
|
|
return Response('')
|
|
|
|
|
|
|
|
def delete_file(file_path):
|
|
try:
|
|
os.remove(file_path)
|
|
print(f"File {file_path} deleted successfully.")
|
|
except FileNotFoundError:
|
|
print(f"File {file_path} not found.")
|
|
except PermissionError:
|
|
print(f"Permission error: Unable to delete {file_path}.")
|
|
except Exception as e:
|
|
print(f"An error occurred: {e}")
|
|
|
|
@csrf_exempt
|
|
@api_view(['POST'])
|
|
def processFiles(request):
|
|
logger.info('Starting file Processing ...')
|
|
try:
|
|
process_files = PreprocessFile()
|
|
process_files.process_standards()
|
|
craetIndex = CreateIndexES()
|
|
craetIndex.createIndex()
|
|
except Exception as e:
|
|
logger.error(e)
|
|
raise
|
|
# delete all records in the FileEvents table
|
|
# FileEvent.objects.all().delete()
|
|
# set all records in FileEvents table in column indexed to true
|
|
FileEvent.objects.all().update(fileStatus='Indexed')
|
|
|
|
|
|
logger.info('File Processing completed')
|
|
return Response('Procssed successfully') |