IDDRS/data_entry/views.py

120 lines
3.6 KiB
Python
Raw Normal View History

2022-07-25 10:03:35 +00:00
from django.shortcuts import render
from pathlib import Path
import os
import json
from App.models import Standards, Level
# Create your views here.
def index(request):
args = {}
return render(request, 'data_entry/index.html',args)
def contentList(request):
levels = Level.objects.all()
standards = Standards.objects.all()
args = {'levels':levels,'standards':standards}
return render(request, 'data_entry/content.html',args)
def contentDetail(request):
level = request.GET.get('level')
standard = request.GET.get('standard')
pk = request.GET.get('pk')
args = {'level':level,'standard':standard,'pk':pk}
return render(request, 'data_entry/content_detail.html',args)
def contentUpdate(request):
level = request.GET.get('level')
standard = request.GET.get('standard')
pk = request.GET.get('pk')
args = {'level':level,'standard':standard,'pk':pk}
return render(request, 'data_entry/content_update.html',args)
def new_entry(request):
levels = Level.objects.all()
standards = Standards.objects.all()
args = {'levels':levels,'standards':standards}
return render(request, 'data_entry/new_entry.html',args)
# def new_entry(request):
# BASE_DIR = Path(__file__).resolve().parent.parent
# p = os.path.join(BASE_DIR, 'static/data')
# levels = Level.objects.all()
# standards = Standards.objects.all()
# if request.method == "GET" and 'level' in request.GET:
# new_obj = {}
# level = request.GET.get('level')
# standard = request.GET.get('standard')
# module_path = filePath(level, standard)
# with open(module_path) as json_file:
# data = json.load(json_file)
# new_obj['ID'] = get_id(module_path)
# new_obj['Paragraph'] = request.GET.get('paragraph')
# new_obj['Color'] = data[0]['Color']
# new_obj['Level'] = level
# new_obj['LevelName'] = data[0]['LevelName']
# new_obj['Title'] = data[0]['Title']
# new_obj['PageNum'] = request.GET.get('pageNum')
# new_obj['Heading1'] = request.GET.get('heading1')
# new_obj['Heading2'] = request.GET.get('heading2')
# new_obj['Heading3'] = request.GET.get('heading3')
# new_obj['Heading4'] = request.GET.get('heading4')
# new_obj['Module'] = data[0]['Module']
# data.append(new_obj)
# with open(module_path, 'w') as f:
# json.dump(data, f)
# args = {'levels':levels,'standards':standards}
# return render(request, 'data_entry/new_entry.html', args)
def edit_entry(request):
args = {}
return render(request, 'data_entry/edit_entry.html', args)
# 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
# Create the new ID
def get_id(file_path):
with open(file_path) as json_file:
data = json.load(json_file)
ids = []
for obj in data:
ids.append(obj['ID'])
return max(ids)+1