128 lines
3.5 KiB
Python
128 lines
3.5 KiB
Python
|
from django.shortcuts import render
|
||
|
from django.http import JsonResponse
|
||
|
|
||
|
import os
|
||
|
from pathlib import Path
|
||
|
import json
|
||
|
|
||
|
from rest_framework.decorators import api_view
|
||
|
from rest_framework.response import Response
|
||
|
|
||
|
# Create your views here.
|
||
|
|
||
|
@api_view(['GET'])
|
||
|
def apiOverview(request):
|
||
|
api_urls={
|
||
|
'List':'/content-list/<str:level>/<str:tandard>',
|
||
|
'Detail':'/content-detail/<str:level>/<str:standard>/<str:pk>/',
|
||
|
'Create':'/content-create/',
|
||
|
'Update':'/content-update/<str:level>/<str:standard>/<str:pk>/',
|
||
|
'Delete':'/content-delete/<str:pk>/',
|
||
|
}
|
||
|
return Response(api_urls)
|
||
|
|
||
|
@api_view(['GET'])
|
||
|
def contentList(request,level,standard):
|
||
|
|
||
|
module_path = filePath(level, standard)
|
||
|
|
||
|
with open(module_path) as json_file:
|
||
|
data = json.load(json_file)
|
||
|
|
||
|
return Response(data)
|
||
|
|
||
|
@api_view(['GET'])
|
||
|
def contentDetail(request,level,standard, pk):
|
||
|
module_path = filePath(level, standard)
|
||
|
|
||
|
with open(module_path) as json_file:
|
||
|
data = json.load(json_file)
|
||
|
|
||
|
for obj in data:
|
||
|
if obj['ID'] == int(pk):
|
||
|
data = obj
|
||
|
|
||
|
return Response(data)
|
||
|
|
||
|
@api_view(['POST'])
|
||
|
def contentCreate(request):
|
||
|
BASE_DIR = Path(__file__).resolve().parent.parent
|
||
|
module_path = os.path.join(BASE_DIR, 'static/data/2 Concepts, Policy and Strategy of the IDDRS/2.10 The UN Approach to DDR.json')
|
||
|
|
||
|
with open(module_path) as json_file:
|
||
|
data = json.load(json_file)
|
||
|
|
||
|
ids = []
|
||
|
for obj in data:
|
||
|
ids.append(obj['ID'])
|
||
|
new_id = max(ids)+1
|
||
|
|
||
|
new_obj = request.data
|
||
|
new_obj['ID'] = new_id
|
||
|
data.append(new_obj)
|
||
|
|
||
|
with open(module_path, 'w') as f:
|
||
|
json.dump(data, f)
|
||
|
|
||
|
return Response(new_obj)
|
||
|
|
||
|
@api_view(['POST'])
|
||
|
def contentUpdate(request,level,standard, pk):
|
||
|
module_path = filePath(level, standard)
|
||
|
|
||
|
with open(module_path) as json_file:
|
||
|
data = json.load(json_file)
|
||
|
|
||
|
for obj in data:
|
||
|
if obj['ID'] == int(pk):
|
||
|
content_to_update = obj
|
||
|
break
|
||
|
|
||
|
updated_content = request.data
|
||
|
|
||
|
for obj in data:
|
||
|
if obj['ID'] == int(pk):
|
||
|
data[int(pk)] = updated_content
|
||
|
|
||
|
with open(module_path, 'w') as f:
|
||
|
json.dump(data, f)
|
||
|
|
||
|
return Response(content_to_update)
|
||
|
|
||
|
@api_view(['DELETE'])
|
||
|
def contentDelete(request, pk):
|
||
|
BASE_DIR = Path(__file__).resolve().parent.parent
|
||
|
module_path = os.path.join(BASE_DIR, 'static/data/2 Concepts, Policy and Strategy of the IDDRS/2.10 The UN Approach to DDR.json')
|
||
|
|
||
|
with open(module_path) as json_file:
|
||
|
data = json.load(json_file)
|
||
|
|
||
|
for i in range(len(data)):
|
||
|
if data[i]['ID'] == int(pk):
|
||
|
data.pop(i)
|
||
|
break
|
||
|
# data = filter(lambda x: x['ID']!=int(pk), data)
|
||
|
with open(module_path, 'w') as f:
|
||
|
json.dump(data, f)
|
||
|
|
||
|
return Response('ITEM succesesfully deleted!')
|
||
|
|
||
|
|
||
|
# Finde the targeted module to store the new paragraph in
|
||
|
# it returns the file path
|
||
|
def filePath(level_input, standard_input):
|
||
|
BASE_DIR = Path(__file__).resolve().parent.parent
|
||
|
standards_dir = os.path.join(BASE_DIR, 'static/data/')
|
||
|
file_path = ''
|
||
|
levels = []
|
||
|
for rootdir, dirs, files in os.walk(standards_dir):
|
||
|
for subdir in dirs:
|
||
|
levels.append(subdir)
|
||
|
for level in levels:
|
||
|
if level[0] == str(level_input):
|
||
|
filenames = next(os.walk(standards_dir+level), (None, None, []))[2]
|
||
|
for file in filenames:
|
||
|
if file[:4] == str(standard_input):
|
||
|
file_path = standards_dir+level+'/'+file
|
||
|
|
||
|
return file_path
|