drought_backup/graphic/views.py

76 lines
1.9 KiB
Python

import datetime
from urllib.parse import unquote
import httplib2
from django.http import JsonResponse, HttpResponse, HttpResponseNotAllowed
from django.shortcuts import render
# Create your views here.
from dashboard.models import Spi
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 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)