import datetime from urllib.parse import unquote import httplib2 from django.db.models import Count from django.http import JsonResponse, HttpResponse, HttpResponseNotAllowed from django.shortcuts import render # Create your views here. from dashboard.models import Spi, Hydro, Revenue, Landuse, Population, GDP PROXY_FORMAT = 'http://%s:%d%s' % ('210.77.68.250', 8080, '%s') def index(request): return render(request, 'graphic/index.html') def timeline(request): return render(request, 'graphic/timeline.html') def timeline1(request): return render(request, 'graphic/timeline1.html') def timeline2(request): return render(request, 'graphic/timeline2.html') def timeline3(request): return render(request, 'graphic/timeline3.html') def proxy(request): conn = httplib2.Http() if request.method == 'GET': url = request.GET.urlencode() url = url.replace('url=', '') url = unquote(url) response, content = conn.request(url, request.method) return HttpResponse(content, status=int(response['status']), content_type=response['content-type']) else: return HttpResponseNotAllowed('post') def spi(request): spi = dict() spi['type'] = 'FeatureCollection' spi['crs'] = { 'type': 'link', 'properties': { 'href': 'http://spatialreference.org/ref/epsg/4326/', 'type': 'proj4' } } spis = Spi.objects.all() fs = [] for s in spis: epoch = datetime.datetime.utcfromtimestamp(0) t = datetime.datetime(s.year, s.month, 1, 1, 1, 1) start = t.timestamp() * 1000.0 o = { 'id': s.id, 'type': 'Feature', 'properties': { 'code': s.code, 'time': int(start), 'mean_precipitation': float(s.mean_precipitation) }, 'geometry': { 'type': 'Point', 'coordinates': [s.longitude, s.latitude] } } fs.append(o) spi['features'] = fs return JsonResponse(spi) def spi_chart_code(request): code = request.GET.get('code', 'V7001') year_from = request.GET.get('year_from', '1979') year_to = request.GET.get('year_to', '1989') codes = Spi.objects.only('code').values('code').annotate(total=Count('code')).order_by('code') years = range(1979, 2018) months = range(1, 13) spis = Spi.objects.filter(code=code, year__range=(year_from, year_to)) return render(request, 'graphic/spi_chart_code.html', {'codes': codes, 'years': years, 'months': months, 'spis': spis, 'code': code, 'year_from': year_from, 'year_to': year_to, }) def spi_chart_time(request): year = request.GET.get('year', '1979') month = request.GET.get('month', '1') years = range(1979, 2018) months = range(1, 13) spis = Spi.objects.filter(year=year, month=month) return render(request, 'graphic/spi_chart_time.html', {'years': years, 'months': months, 'spis': spis, 'year': year, 'month': month}) def spi_chart_time_list(request): code = request.GET.get('code', 'V7001') year_from = request.GET.get('year_from', '1979') year_to = request.GET.get('year_to', '1989') codes = Spi.objects.only('code').values('code').annotate(total=Count('code')).order_by('code') years = range(1979, 2018) months = range(1, 13) spis = Spi.objects.filter(code=code, year__range=(year_from, year_to)) return render(request, 'dashboard/spi_list.html', {'codes': codes, 'years': years, 'months': months, 'spis': spis, 'code': code, 'year_from': year_from, 'year_to': year_to, }) def hydro_chart_time_list(request): year = request.GET.get('date', '1979') years = range(1979, 2018) hydro = Hydro.objects.filter(date=year) print(str(hydro)+"+++++++++++++++++") return render(request, 'dashboard/hydro_list.html', {'years': years, 'hydro': hydro, 'year': year, }) def revenue_chart_time_list(request): year = request.GET.get('date', '1979') years = range(1979, 2018) revenue = Revenue.objects.filter(data=year) print(str(revenue)+"社会经济") return render(request, 'dashboard/revenue_list.html', {'years': years, 'revenue': revenue, 'year': year, }) def landuse_chart_time_list(request): year = request.GET.get('date','2016') years = range(1979,2018) landuse = Landuse.objects.filter(date = year) return render(request,'dashboard/landuse_list.html',{'years':years,'year':year,'landuse':landuse}) def population_chart_time_list(request): year = request.GET.get('date','2016') years = range(1979,2018) population = Population.objects.filter(date = year) return render(request,'dashboard/population_list.html',{'years':years,'year':year,'population':population}) def gdp_chart_time_list(request): year = request.GET.get('date','2016') years = range(2016,2018) gdp = GDP.objects.filter(date = year) return render(request,'dashboard/gdp_list.html',{'years':years,'year':year,'gdp':gdp})