drought/graphic/views.py

160 lines
5.1 KiB
Python
Raw Normal View History

2018-07-07 09:42:32 +00:00
import datetime
2018-07-07 15:00:25 +00:00
from urllib.parse import unquote
2018-07-07 09:42:32 +00:00
2018-07-07 15:00:25 +00:00
import httplib2
2018-07-09 12:23:16 +00:00
from django.db.models import Count
2018-07-07 15:00:25 +00:00
from django.http import JsonResponse, HttpResponse, HttpResponseNotAllowed
2018-07-06 11:22:59 +00:00
from django.shortcuts import render
# Create your views here.
2018-12-08 12:22:03 +00:00
from dashboard.models import Spi, Hydro, Revenue, Landuse, Population, GDP
2018-07-07 09:42:32 +00:00
2018-07-07 15:00:25 +00:00
PROXY_FORMAT = 'http://%s:%d%s' % ('210.77.68.250', 8080, '%s')
2018-07-06 11:22:59 +00:00
def index(request):
return render(request, 'graphic/index.html')
2018-07-06 13:34:51 +00:00
def timeline(request):
return render(request, 'graphic/timeline.html')
2018-07-07 09:42:32 +00:00
2018-07-07 15:00:25 +00:00
def timeline1(request):
return render(request, 'graphic/timeline1.html')
2018-07-08 05:10:47 +00:00
def timeline2(request):
return render(request, 'graphic/timeline2.html')
2018-07-08 14:49:31 +00:00
2018-07-08 14:41:55 +00:00
def timeline3(request):
return render(request, 'graphic/timeline3.html')
2018-07-08 05:10:47 +00:00
2018-07-08 14:49:31 +00:00
2018-07-09 15:00:48 +00:00
2018-07-08 14:49:31 +00:00
2018-07-07 15:00:25 +00:00
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')
2018-07-07 09:42:32 +00:00
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)
2018-07-09 03:52:42 +00:00
2018-11-29 07:03:47 +00:00
2018-07-09 12:23:16 +00:00
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})
2018-11-29 07:03:47 +00:00
def spi_chart_time_list(request):
2018-12-11 07:51:37 +00:00
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')
2018-11-29 07:03:47 +00:00
years = range(1979, 2018)
months = range(1, 13)
2018-12-11 07:51:37 +00:00
spis = Spi.objects.filter(code=code, year__range=(year_from, year_to))
2018-11-29 07:03:47 +00:00
return render(request, 'dashboard/spi_list.html',
2018-12-11 07:51:37 +00:00
{'codes': codes, 'years': years, 'months': months, 'spis': spis, 'code': code, 'year_from': year_from,
'year_to': year_to, })
2018-11-29 07:03:47 +00:00
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,
})
2018-12-08 12:22:03 +00:00
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')
2018-12-12 11:32:48 +00:00
years = range(2016,2018)
2018-12-08 12:22:03 +00:00
gdp = GDP.objects.filter(date = year)
2018-12-10 08:45:57 +00:00
return render(request,'dashboard/gdp_list.html',{'years':years,'year':year,'gdp':gdp})