from django.shortcuts import render
from django.http import HttpResponse,JsonResponse
from django.shortcuts import render
from .forms import AddForm
import json
import re
from collections import defaultdict
from .models import Scheme,specificScheme
from django.core import serializers
import os
# Create your views here.
def add(request):
return HttpResponse("""
{}
""".format(int(request.GET['a'])+int(request.GET['b'])))
def index(request):
if request.method == 'POST':
form = AddForm(request.POST)
if form.is_valid():
a = form.cleaned_data['a']
b = form.cleaned_data['b']
return HttpResponse(str((int(a)+int(b))),status=404)
else:
form = AddForm()
return render(request,'index.html',{'form':form},status=404)
def ajax_list(request):
a = list(range(100))
return JsonResponse(a,safe = False)
def ajax_dict(request):
name_dict = {'twz':'love python and django','hello':'world'}
return JsonResponse(name_dict)
def getJson(request):
"""
with open('./static/年级学院专业.txt','r') as f:
academys = defaultdict(dict)
i = 0
for line in f.readlines():
data = line.split('@')
i+=1
print(i)
Scheme.objects.create(
grade = data[0].strip(),
academy = data[1].strip(),
major = data[2].strip(),
scheme_name = data[3].strip(),
key = data[-1].strip()
)
academy = data[1]
major = data[2].strip()
if major not in academys[academy]:
academys[academy][major] = []
s = re.sub("\d+级",'',data[3].strip())
s = re.sub("\d+",'',s)
academys[academy][major].append(s)
else:
s = re.sub("\d+级",'',data[3].strip())
s = re.sub("\d+",'',s)
academys[academy][major].append(s)
for academy_ in academys.keys():
for key,item in academys[academy_].items():
academys[academy_][key] = set(item)
print('学院数量:',len(academys.keys()))
for academy in academys.keys():
print(academy,'有 {} 个专业'.format(len(academys[academy].keys())))
"""
"""
files = os.listdir('./static/All')
max1 = 0
i = 0
for file in files:
with open('./static/All/'+file,'r') as f:
specificScheme.objects.create(
key = file.split('_')[0].strip(),
content = f.read()
)
i+=1
print(i)
"""
grade = request.GET['class']+'级'
academy = request.GET['academy']
result = Scheme.objects.filter(grade=grade,academy=academy)
json_data = serializers.serialize('json', result)
with open('./static/All/0043f0d3922c4a238868422b8c046632_87eb47ff228a4786ad9cbd6498bc4430.json','r') as f:
content = json.loads(f.read())
content['check'] = json.loads(json_data)
return HttpResponse(json.dumps(content))
def getSchemeByKey(request):
key = request.GET['key']
result = specificScheme.objects.filter(key=key)
json_data = serializers.serialize('json',result)
res = json.loads(json_data)
for i,item in enumerate(res):
temp = res[i]['fields']['content']
res[i]['fields']['content'] = None
res[i]['fields']['content'] = json.loads(temp)
return HttpResponse(json.dumps(res))