# coding=utf-8 from django.shortcuts import render, render_to_response from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage from .models import * from graphics.models import * from django.core.serializers import serialize from django.http.response import JsonResponse import psycopg2 import time #将场地分为G214和试验场 def part_land(str): lands = Land.objects.all().order_by('code') test_land=list() g214_land=list() for l in lands: if l.section_set.all(): if l.section_set.all()[0].owner == 'land': test_land.append(l) else: g214_land.append(l) else: g214_land.append(l) if str == 'land': return test_land elif str == 'road': return g214_land #场地 def land(request): type = request.GET.get('type') lands = Land.objects.all().order_by('code') return render(request, 'base/index.html', \ {'test_land': part_land('land'), 'g214_land': part_land('road'), 'lands': lands, 'type': type}) #钻孔详情 def drilling_details(request): id = request.GET.get('id') type = request.GET.get('type') drilling = Drilling.objects.get(id=id) drilling_probe = drilling.probe_set.all().order_by('name') drilling_soiltest = drilling.soiltest_set.all().order_by('top_depth') return render(request, 'base/drilling_details.html', \ {'drilling': drilling, 'drilling_probe': drilling_probe, \ 'test_land': part_land('land'), 'g214_land': part_land('road'), \ 'type': type, 'drilling_soiltest':drilling_soiltest}) #试验场详情 def land_details(request): id = request.GET.get('id') type = request.GET.get('type') land = Land.objects.get(id=id) land_equipment = Equipment.objects.filter(land__id=id) land_section = Section.objects.filter(land__id=id).order_by('name') land_drilling = Drilling.objects.filter(section__land__id=id).order_by('name') land_sources = Sources.objects.filter(land__id=id).order_by('filename') return render(request, 'base/land_details.html', \ {'land': land, 'land_section': land_section, 'land_drilling': land_drilling, 'type': type, \ 'land_equipment': land_equipment, 'id': id, \ 'test_land': part_land('land'), 'land_sources':land_sources,\ 'g214_land': part_land('road')}) #设备详情 def equipment_details(request): id = request.GET.get('id') type = request.GET.get('type') equipment = Equipment.objects.get(id=id) return render(request, 'base/equipment_details.html', \ {'equipment': equipment, 'test_land': part_land('land'), 'g214_land': part_land('road'), 'type': type}) #探头详情 def probe_details(request): id = request.GET.get('id') type = request.GET.get('type') probe = Probe.objects.get(id=id) probe_data = ProbeData.objects.filter(probe_code=probe.code).order_by('-recorded')[:50] return render(request, 'base/probe_details.html', \ {'probe': probe, 'test_land': part_land('land'), 'g214_land': part_land('road'), 'type': type, 'probe_data': probe_data}) #原始文件 def sources(request): sources = Sources.objects.filter(land_id=None) return render(request, 'base/sources.html', \ {'sources':sources, 'test_land':part_land('land'), 'g214_land':part_land('road')}) #截面详情 def section_details(request): id = request.GET.get('id') type = request.GET.get('type') section = Section.objects.get(id=id) section_drilling = section.drilling_set.all().order_by('name') section_probe = Probe.objects.filter(drilling__section__id=id).order_by('name') # 转换坐标格式 if section.the_geom: the_geom = str(section.the_geom).split('(')[1].split(')')[0] the_geom = the_geom.split(' ') the_geom = 'E' + the_geom[0] + ' N' + the_geom[1] else: the_geom=None f = FloorPlan(title=section.name+u"的平面图") f.link() f.road_lines() f.line_box() f.drilling() f_script, f_div = f.components() s = SectionalPlan(title=section.name+u"的剖面图", left_foot_height=10, right_foot_height=10, \ left_shoulder_height=15, right_shoulder_height=15 ) s.road_lines() s.drilling() s.line_box() s_script, s_div = s.components() probeDate20 = ProbeData.objects.filter(section_code=section.code).order_by('-recorded')[:20] return render(request, 'base/section_details.html', \ {'section': section, 'section_drilling': section_drilling, 'section_probe': section_probe, 'type': type,\ 'f_script': f_script, 'f_div':f_div, 's_script': s_script, 's_div': s_div, 'test_land': part_land('land'), \ 'g214_land': part_land('road'), 'the_geom':the_geom, 'probeData20': probeDate20}) #搜索函数 def search(request): name = request.GET.get('name') lands = Land.objects.filter(name__contains=name).order_by('code') return render(request, 'base/land_index.html', {'lands': lands, 'category': 1}) # 返回探头图表json def probedata_json(request): probe_code = request.GET.get('probe_code') probedata = ProbeData.objects.filter(probe_code=probe_code) data = [] for pd in probedata: data.append([pd.recorded.strftime('%Y-%m-%d %H:%M:%S'), pd.value2]) return JsonResponse(data, safe=False) # 返回钻孔详情页图表所需json def drilling_json(request): drilling_id = request.GET.get('drilling_id') starttime = request.GET.get('starttime') endtime = request.GET.get('endtime') probes = Probe.objects.filter(drilling_id=drilling_id) data = {} for probe in probes: probe_data = ProbeData.objects.filter(probe_code=probe.code, recorded__range=(starttime, endtime)) d = [] for pd in probe_data: d.append([pd.recorded.strftime('%Y-%m-%d %H:%M:%S'), pd.value2]) data[probe.depth]=d return JsonResponse(data, safe=False)