g214/metadata/views.py

216 lines
8.5 KiB
Python

# coding=utf-8
import codecs
from django.db.models import Count
from django.shortcuts import render, get_object_or_404, HttpResponseRedirect, HttpResponse
from django.views.decorators.csrf import csrf_protect
from metadata.models import Metadata, Comment, Favorite, Cart, LineItem
from base.models import Sources, Meteorological, MeteorologicalImage
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.contrib import messages
from django.http import JsonResponse
from base.models import *
from django.db.models.functions import TruncMonth, TruncYear
import csv
from django.http import StreamingHttpResponse
def index(request):
category = request.GET.get('category', 1)
if category == '1':
metadatas = Metadata.objects.filter(category__name=u'基础地理数据').all()
elif category == '2':
metadatas = Metadata.objects.filter(category__name=u'遥感数据').all()
elif category == '3':
metadatas = Metadata.objects.filter(category__name=u'G214国道').all()
elif category == '4':
metadatas = Metadata.objects.filter(category__name=u'冻土观测基地(旧)').all()
elif category == '5':
metadatas = Metadata.objects.filter(category__name=u'冻土观测基地(新)').all()
elif category == '6':
metadatas = Metadata.objects.filter(category__name=u'施工设计资料').all()
else:
metadatas = Metadata.objects.all()
paginator = Paginator(metadatas, 20)
page = int(request.GET.get('page', 1))
try:
metadatas = paginator.page(page)
except PageNotAnInteger:
metadatas = paginator.page(1)
except EmptyPage:
metadatas = paginator.page(paginator.num_pages)
return render(request, 'metadata/index.html', {'metadatas': metadatas, 'name': category})
def detail(request, metadata_id):
metadata = get_object_or_404(Metadata, pk=metadata_id)
metadata.hit_count = metadata.hit_count + 1
metadata.save()
if metadata_id is not None:
comments = Comment.objects.filter(metadata_id=metadata_id).all().order_by('-created')
return render(request, 'metadata/detail.html', {'metadata': metadata, 'comments': comments})
def comment(request):
if request.method == 'POST':
meta_id = request.POST.get('meta_id')
content = request.POST.get('content')
if request.user.is_authenticated():
user_id = request.user.id
c = Comment()
c.metadata_id = meta_id
c.user_id = user_id
c.content = content
c.save()
messages.success(request, u'添加评论成功')
return HttpResponseRedirect('/metadata/detail/' + meta_id)
else:
messages.error(request, u'用户未登录')
return HttpResponseRedirect('/metadata/detail/' + meta_id)
@csrf_protect
def favorite(request):
if request.user.is_authenticated():
user = request.user
metadata_id = request.POST.get('metadata_id')
metadata = get_object_or_404(Metadata, pk=metadata_id)
favorite = Favorite.objects.filter(metadata=metadata, user=user)
if not favorite:
f = Favorite(metadata=metadata, user=user)
f.save()
return JsonResponse({"code": 1, "message": u"已成功收藏该数据。"})
else:
return JsonResponse({"code": 0, "message": u"请登录,再进行操作。"})
@csrf_protect
def add_to_cart(request):
if request.user.is_authenticated():
user = request.user
metadata_id = request.POST.get('metadata_id')
metadata = get_object_or_404(Metadata, pk=metadata_id)
c = Cart.objects.filter(user=user).first()
if not c:
c = Cart(user=user)
c.save()
line_item = LineItem.objects.filter(cart=c, metadata=metadata).first()
if line_item:
line_item.quantity += 1
else:
line_item = LineItem(cart=c, metadata=metadata)
line_item.quantity += 1
line_item.save()
count = LineItem.objects.filter(cart=c).count()
request.session['count'] = count
return JsonResponse({"code": 1, "message": u'已成功添加至购物车。', "count": count})
else:
return JsonResponse({"code": 0, "message": u'请登录,再进行操作。'})
def observation(request):
id = request.GET.get('id')
metadata = Metadata.objects.get(id=id)
probeData_y = ProbeData.objects.filter(probe__drilling__section__land__metadata__id=id).annotate(
year=TruncYear('date')).values('year').annotate(c=Count('id')).values('year', 'c')
probeData_m = ProbeData.objects.filter(probe__drilling__section__land__metadata__id=id).annotate(
month=TruncMonth('date')).values('month').annotate(c=Count('id')).values('month', 'c').order_by('-month')
probeData_all = ProbeData.objects.filter(probe__drilling__section__land__metadata__id=id).order_by('date')[:1]
print probeData_all
for p in probeData_all:
probeData = ProbeData.objects.filter(probe__id=p.probe.id).order_by('date')
return render(request, 'metadata/observation.html',
{"metadata": metadata, "probeData": probeData, "probeData_y": probeData_y,
"probeData_m": probeData_m})
def render_csv(request):
metadata_id = request.GET.get('metadata')
metadata = Metadata.objects.get(id=metadata_id)
year = request.GET.get('year')
month = request.GET.get('month')
if year:
if month:
results = ProbeData.objects.filter(date__year=year).filter(date__month=month).filter(
probe__drilling__section__land__metadata__id=metadata_id).select_related('probe', 'probe__drilling',
'probe__drilling__section',
'probe__drilling__section__land').order_by('date')
else:
results = ProbeData.objects.filter(date__year=year).filter(
probe__drilling__section__land__metadata__id=metadata_id).select_related('probe', 'probe__drilling',
'probe__drilling__section',
'probe__drilling__section__land').order_by('date')
rows = []
rhead = ['试验场', '截面', '钻孔', '探头', '时间', '']
rows.append(rhead)
for d in results:
r = []
if d.probe.drilling.section.land.name:
r.append(d.probe.drilling.section.land.name.encode('utf-8'))
else:
print "1111", d.probe.drilling.section.land.name
if d.probe.drilling.section.name:
r.append(d.probe.drilling.section.name.encode('utf-8'))
else:
print "2222", d.probe.drilling.section.name
if d.probe.drilling.name:
r.append(d.probe.drilling.name.encode('utf-8'))
else:
print "3333", d.probe.drilling.name
if d.probe.name:
r.append(d.probe.name.encode('utf-8'))
else:
print "4444", d.probe.name
r.append(d.date)
r.append(d.value)
rows.append(r)
response = HttpResponse(content_type='text/csv')
response.write(codecs.BOM_UTF8)
writer = csv.writer(response)
for r in rows:
writer.writerow(r)
if year:
if month:
filename = str(metadata.id) + '-' + year + '-' + month + '.csv'
else:
filename = str(metadata.id) + '-' + year + '.csv'
response['Content-Disposition'] = u'attachment; filename="%s"' % (filename,)
return response
def sources(request):
sources = Sources.objects.filter(land=None)
return render(request, 'metadata/sources.html', {'sources':sources})
def meteorological(request):
meteorologicals = Meteorological.objects.all().order_by('-created')
paginator = Paginator(meteorologicals, 10)
p = int(request.GET.get('page', 1))
try:
contents = paginator.page(p)
except PageNotAnInteger:
contents = paginator.page(1)
except EmptyPage:
contents = paginator.page(paginator.num_pages)
return render(request, 'metadata/meteorological.html', {'contents': contents, 'p': p})
def meteorological_details(request):
id = request.GET.get('id')
meteorological = Meteorological.objects.get(id=id)
images = MeteorologicalImage.objects.filter(meteorological__id=id)
return render(request, 'metadata/meteorological_details.html', { 'm': meteorological, 'images': images})