drought_backup/dashboard/views.py

526 lines
16 KiB
Python
Raw Normal View History

2018-07-06 09:47:36 +00:00
from django.contrib.auth.decorators import login_required
2018-07-06 06:08:28 +00:00
# Create your views here.
2018-07-08 06:25:32 +00:00
from django.http import JsonResponse
2018-07-06 09:47:36 +00:00
from django.shortcuts import render
from django.urls import reverse_lazy
from django.utils.decorators import method_decorator
2018-07-09 12:23:16 +00:00
from django.views.generic import ListView, DetailView, CreateView, UpdateView, DeleteView, TemplateView
2018-07-06 09:47:36 +00:00
2018-12-08 12:22:03 +00:00
from dashboard.models import Spi, Hydro, Landuse, Finance, Revenue, Population, GDP
2018-12-26 10:52:34 +00:00
from users.models import YearRange
2018-07-06 09:47:36 +00:00
2018-07-06 06:08:28 +00:00
def index(request):
2018-07-06 09:47:36 +00:00
return render(request, 'dashboard/index.html')
2018-07-08 03:56:29 +00:00
def finance_chart(request):
2018-07-08 04:31:22 +00:00
return render(request, 'dashboard/finance_chart.html')
2018-07-08 03:56:29 +00:00
def hydro_chart(request):
2018-07-08 04:31:22 +00:00
return render(request, 'dashboard/hydro_chart.html')
2018-07-08 03:56:29 +00:00
def landuse_chart(request):
2018-07-08 04:31:22 +00:00
return render(request, 'dashboard/landuse_chart.html')
2018-07-06 10:27:06 +00:00
@method_decorator(login_required, name='dispatch')
class SpiListView(ListView):
context_object_name = 'spis'
2018-07-07 09:42:32 +00:00
paginate_by = 100
2018-07-06 10:27:06 +00:00
def get_queryset(self):
self.keyword = self.request.GET.get('keyword', '')
if len(self.keyword) != 0:
return Spi.objects.filter(name__contains=self.keyword)
return Spi.objects.all()
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['keyword'] = self.keyword
return context
class SpiDetailView(DetailView):
context_object_name = "spi"
model = Spi
class SpiCreateView(CreateView):
model = Spi
2018-07-06 12:53:41 +00:00
fields = ['code', 'longitude', 'latitude', 'year', 'month', 'mean_precipitation', 'mean_temperature']
2018-07-06 10:27:06 +00:00
class SpiUpdateView(UpdateView):
model = Spi
2018-07-06 12:53:41 +00:00
fields = ['code', 'longitude', 'latitude', 'year', 'month', 'mean_precipitation', 'mean_temperature']
2018-07-06 10:27:06 +00:00
class SpiDeleteView(DeleteView):
model = Spi
success_url = reverse_lazy('data-spi-list')
2018-07-06 11:59:04 +00:00
2018-07-09 12:23:16 +00:00
class SpiImportView(TemplateView):
template_name = 'dashboard/spi_import.html'
2018-12-08 12:22:03 +00:00
2018-11-29 07:03:47 +00:00
def SpiDataDetailView(request):
2018-12-27 04:14:18 +00:00
user = request.user
year_range = YearRange.objects.get(user_id=user.id)
start_year = year_range.start_year
end_year = year_range.end_year
spis = Spi.objects.filter(year__range = (start_year,end_year))[:100]
2018-12-08 12:22:03 +00:00
return render(request, 'dashboard/spi_data_detail.html', {'spis': spis})
2018-11-29 07:03:47 +00:00
2018-07-09 12:23:16 +00:00
2018-07-06 11:59:04 +00:00
@method_decorator(login_required, name='dispatch')
class HydroListView(ListView):
context_object_name = 'hydros'
def get_queryset(self):
self.keyword = self.request.GET.get('keyword', '')
if len(self.keyword) != 0:
2018-07-06 12:53:41 +00:00
return Hydro.objects.filter(name__contains=self.keyword)
return Hydro.objects.all()
2018-07-06 11:59:04 +00:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['keyword'] = self.keyword
return context
class HydroDetailView(DetailView):
context_object_name = "hydro"
2018-07-06 12:53:41 +00:00
model = Hydro
2018-07-06 11:59:04 +00:00
class HydroCreateView(CreateView):
model = Hydro
2018-07-06 12:53:41 +00:00
fields = ['name', 'date', 'average_water_level', 'average_flow_rate', 'precipitation']
2018-07-06 11:59:04 +00:00
class HydroUpdateView(UpdateView):
model = Hydro
2018-07-06 12:53:41 +00:00
fields = ['name', 'date', 'average_water_level', 'average_flow_rate', 'precipitation']
2018-07-06 11:59:04 +00:00
class HydroDeleteView(DeleteView):
model = Hydro
success_url = reverse_lazy('data-hydro-list')
2018-07-09 12:23:16 +00:00
class HydroImportView(TemplateView):
template_name = 'dashboard/hydro_import.html'
2018-12-08 12:22:03 +00:00
2018-11-29 07:03:47 +00:00
def HydroDataDetailView(request):
2018-12-27 04:14:18 +00:00
user = request.user
year_range = YearRange.objects.get(user_id=user.id)
start_year = year_range.start_year
end_year = year_range.end_year
hydros = Hydro.objects.filter(date__range = (start_year,end_year))[:100]
2018-12-08 12:22:03 +00:00
return render(request, 'dashboard/hydro_data_detail.html', {'hydros': hydros})
2018-11-29 07:03:47 +00:00
2018-07-09 12:23:16 +00:00
2018-07-06 11:59:04 +00:00
@method_decorator(login_required, name='dispatch')
class LanduseListView(ListView):
2018-07-06 12:53:41 +00:00
context_object_name = 'landuses'
2018-07-06 11:59:04 +00:00
def get_queryset(self):
self.keyword = self.request.GET.get('keyword', '')
if len(self.keyword) != 0:
2018-07-06 12:53:41 +00:00
return Landuse.objects.filter(name__contains=self.keyword)
return Landuse.objects.all()
2018-07-06 11:59:04 +00:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['keyword'] = self.keyword
return context
class LanduseDetailView(DetailView):
context_object_name = "landuse"
model = Landuse
class LanduseCreateView(CreateView):
model = Landuse
2018-07-06 12:53:41 +00:00
fields = ['name', 'date', 'Cultivated_land_area', 'Irrigated_land', 'eatch_land', 'eatch_Irrigated_land']
2018-07-06 11:59:04 +00:00
class LanduseUpdateView(UpdateView):
model = Landuse
2018-07-06 12:53:41 +00:00
fields = ['name', 'date', 'Cultivated_land_area', 'Irrigated_land', 'eatch_land', 'eatch_Irrigated_land']
2018-07-06 11:59:04 +00:00
class LanduseDeleteView(DeleteView):
model = Landuse
success_url = reverse_lazy('data-landuse-list')
2018-07-06 12:53:41 +00:00
2018-07-09 12:23:16 +00:00
class LanduseImportView(TemplateView):
template_name = 'dashboard/landuse_import.html'
2018-11-29 07:03:47 +00:00
def LanduseDataDetailView(request):
2018-12-27 04:14:18 +00:00
user = request.user
year_range = YearRange.objects.get(user_id=user.id)
start_year = year_range.start_year
end_year = year_range.end_year
landuses = Landuse.objects.filter(date__range=(start_year,end_year))[:100]
2018-12-08 12:22:03 +00:00
return render(request, 'dashboard/landuse_data_detail.html', {'landuses': landuses})
2018-11-29 07:03:47 +00:00
2018-07-06 12:53:41 +00:00
@method_decorator(login_required, name='dispatch')
class FinanceListView(ListView):
context_object_name = 'finances'
def get_queryset(self):
self.keyword = self.request.GET.get('keyword', '')
if len(self.keyword) != 0:
return Finance.objects.filter(name__contains=self.keyword)
return Finance.objects.all()
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['keyword'] = self.keyword
return context
class FinanceDetailView(DetailView):
context_object_name = "finance"
model = Finance
class FinanceCreateView(CreateView):
model = Finance
fields = ['region', 'level', 'date', 'financial_revenue', 'tax_revenue', 'value_added_tax', 'sales_tax',
'corporate_income_tax', 'individual_income_tax', 'resource_tax', 'urban_maintenance_construction_tax',
'property_tax', 'stamp_duty', 'urban_land_use_tax', 'land_value_added_tax', 'vehicle_vessel_tax',
'arable_land_occupancy_tax', 'deed_tax', 'tobacco_tax', 'non_tax_revenue']
class FinanceUpdateView(UpdateView):
model = Finance
fields = ['region', 'level', 'date', 'financial_revenue', 'tax_revenue', 'value_added_tax', 'sales_tax',
'corporate_income_tax', 'individual_income_tax', 'resource_tax', 'urban_maintenance_construction_tax',
'property_tax', 'stamp_duty', 'urban_land_use_tax', 'land_value_added_tax', 'vehicle_vessel_tax',
'arable_land_occupancy_tax', 'deed_tax', 'tobacco_tax', 'non_tax_revenue']
class FinanceDeleteView(DeleteView):
model = Finance
success_url = reverse_lazy('data-finance-list')
2018-07-06 13:31:05 +00:00
2018-07-09 12:23:16 +00:00
class FinanceImportView(TemplateView):
template_name = 'dashboard/finance_import.html'
2018-07-08 06:25:32 +00:00
@method_decorator(login_required, name='dispatch')
class RevenueListView(ListView):
context_object_name = 'revenues'
def get_queryset(self):
self.keyword = self.request.GET.get('keyword', '')
if len(self.keyword) != 0:
return Revenue.objects.filter(name__contains=self.keyword)
return Revenue.objects.all()
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['keyword'] = self.keyword
return context
class RevenueDetailView(DetailView):
context_object_name = "revenue"
model = Revenue
class RevenueCreateView(CreateView):
model = Revenue
fields = ['index', 'data', 'city', 'municipal_level', 'city_level', 'gaoxinqu', 'district_and_county',
'taibaixian', 'chencangqu', 'fengxiangxian', 'qishanxian',
'linyouxian', 'longxian', 'qianyangxian', 'fengxian', 'fufengxian',
'meixian', 'jintaiqu', 'weibinqu']
class RevenueUpdateView(UpdateView):
model = Revenue
fields = ['index', 'data', 'city', 'municipal_level', 'city_level', 'gaoxinqu', 'district_and_county',
'taibaixian', 'chencangqu', 'fengxiangxian', 'qishanxian',
'linyouxian', 'longxian', 'qianyangxian', 'fengxian', 'fufengxian',
'meixian', 'jintaiqu', 'weibinqu']
class RevenueDeleteView(DeleteView):
model = Revenue
success_url = reverse_lazy('data-revenue-list')
class RevenueImportView(TemplateView):
2018-12-10 08:45:57 +00:00
template_name = 'dashboard/revenue_import.html'
2018-12-08 12:22:03 +00:00
2018-11-29 07:03:47 +00:00
def RevenueDataDetailView(request):
2018-12-27 04:14:18 +00:00
user = request.user
year_range = YearRange.objects.get(user_id=user.id)
start_year = year_range.start_year
end_year = year_range.end_year
revenues = Revenue.objects.filter(data__range = (start_year,end_year))[:100]
2018-12-08 12:22:03 +00:00
return render(request, 'dashboard/revenue_data_detail.html', {'revenues': revenues})
@method_decorator(login_required, name='dispatch')
class PopulationListView(ListView):
context_object_name = 'populations'
def get_queryset(self):
self.keyword = self.request.GET.get('keyword', '')
if len(self.keyword) != 0:
return Population.objects.filter(name__contains=self.keyword)
return Population.objects.all()
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['keyword'] = self.keyword
return context
class PopulationDetailView(DetailView):
context_object_name = "population"
model = Population
class PopulationCreateView(CreateView):
model = Population
fields = ['name', 'date', 'inhabitant', 'households', 'registered_population', 'man', 'women',
'natality', 'fatalities', 'population_growth', 'demographic_urbanization', ]
class PopulationUpdateView(UpdateView):
model = Population
fields = ['name', 'date', 'inhabitant', 'households', 'registered_population', 'man', 'women',
'natality', 'fatalities', 'population_growth', 'demographic_urbanization', ]
class PopulationDeleteView(DeleteView):
model = Population
success_url = reverse_lazy('data-population-list')
class PopulationImportView(TemplateView):
template_name = 'dashboard/revenue.html'
def PopulationDataDetailView(request):
2018-12-26 10:52:34 +00:00
user = request.user
year_range = YearRange.objects.get(user_id = user.id)
start_year = year_range.start_year
end_year = year_range.end_year
2018-12-27 04:14:18 +00:00
populations = Population.objects.filter(date__range = (start_year,end_year))[:100]
2018-12-08 12:22:03 +00:00
return render(request, 'dashboard/population_data_detail.html', {'populations': populations})
@method_decorator(login_required, name='dispatch')
class GDPListView(ListView):
context_object_name = 'gdps'
def get_queryset(self):
self.keyword = self.request.GET.get('keyword', '')
if len(self.keyword) != 0:
return GDP.objects.filter(name__contains=self.keyword)
return GDP.objects.all()
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['keyword'] = self.keyword
return context
class GDPDetailView(DetailView):
context_object_name = "gdp"
model = GDP
class GDPCreateView(CreateView):
model = GDP
fields = ['date', 'name', 'agricultural_population', 'total_population', 'population_density',
'agricultural_acreage', 'county_area',
'per_capita_area', 'agriculturegdp', 'gdp', 'proportion_agriculture',
'proportion_population', 'proportion_economy']
class GDPUpdateView(UpdateView):
model = GDP
fields = ['date', 'name', 'agricultural_population', 'total_population', 'population_density',
'agricultural_acreage', 'county_area',
'per_capita_area', 'agriculturegdp', 'gdp', 'proportion_agriculture',
'proportion_population', 'proportion_economy']
class GDPDeleteView(DeleteView):
2018-12-10 08:45:57 +00:00
model = GDP
2018-12-08 12:22:03 +00:00
success_url = reverse_lazy('data-gdp-list')
class GDPImportView(TemplateView):
2018-12-10 08:45:57 +00:00
template_name = 'dashboard/gdp_import.html'
2018-12-08 12:22:03 +00:00
def GDPDataDetailView(request):
2018-12-27 04:14:18 +00:00
user = request.user
year_range = YearRange.objects.get(user_id=user.id)
start_year = year_range.start_year
end_year = year_range.end_year
gdps = GDP.objects.filter(date__range=(start_year,end_year))
2018-12-08 12:22:03 +00:00
return render(request, 'dashboard/gdp_data_detail.html', {'gdps': gdps})
2018-11-29 07:03:47 +00:00
2018-07-08 06:25:32 +00:00
def spi_json(request):
code = request.GET.get('code')
year_from = request.GET.get('year_from')
2018-07-08 06:25:32 +00:00
year_to = request.GET.get('year_to')
2018-07-08 10:34:46 +00:00
year = request.GET.get('year')
month = request.GET.get('month')
2018-07-08 06:25:32 +00:00
q = Spi.objects.all()
if code:
q = q.filter(code=code)
if year_from and year_to:
q = q.filter(year__range=(year_from, year_to))
2018-07-08 10:34:46 +00:00
if year and month:
q = q.filter(year=year, month=month)
2018-07-08 06:25:32 +00:00
results = []
for spi in q:
o = dict()
o['code'] = spi.code
o['year'] = str(spi.year)
o['month'] = str(spi.month)
o['mean_precipitation'] = spi.mean_precipitation
o['mean_temperature'] = spi.mean_temperature
results.append(o)
return JsonResponse(results, safe=False)
2018-11-29 07:03:47 +00:00
def hydro_json(request):
name = request.GET.get('name')
year_from = request.GET.get('year_from')
year_to = request.GET.get('year_to')
date = request.GET.get('date')
h = Hydro.objects.all()
if name:
h = h.filter(name=name)
if year_from and year_to:
h = h.filter(year__range=(year_from, year_to))
if date:
h = h.filter(date=date)
results = []
for hydro in h:
o = dict()
o['name'] = hydro.name
o['date'] = str(hydro.date)
o['average_water_level'] = hydro.average_water_level
o['precipitation'] = hydro.precipitation
results.append(o)
return JsonResponse(results, safe=False)
def revenue_json(request):
index = request.GET.get('index')
year_from = request.GET.get('year_from')
year_to = request.GET.get('year_to')
date = request.GET.get('date')
r = Revenue.objects.all()
if index:
r = r.filter(index=index)
if year_from and year_to:
r = r.filter(year__range=(year_from, year_to))
if date:
r = r.filter(date=date)
results = []
for revenue in r:
o = dict()
o['index'] = revenue.index
o['date'] = str(revenue.data)
o['city'] = revenue.city
o['district_and_county'] = revenue.district_and_county
2018-12-08 12:22:03 +00:00
print(str(o) + "社会经济")
2018-11-29 07:03:47 +00:00
results.append(o)
2018-12-08 12:22:03 +00:00
return JsonResponse(results, safe=False)
def landuse_json(request):
name = request.GET.get('name')
year_from = request.GET.get('year_from')
year_to = request.GET.get('year_to')
date = request.GET.get('date')
l = Landuse.objects.all()
if name:
l = l.filter(name=name)
if year_from and year_to:
l = l.filter(year__range=(year_from, year_to))
if date:
l = l.filter(date=date)
results = []
for landuse in l:
o = dict()
o['name'] = landuse.name
o['date'] = str(landuse.date)
o['cultivated_land_area'] = landuse.cultivated_land_area
o['irrigated_land'] = landuse.irrigated_land
print(str(o) + "**************")
results.append(o)
return JsonResponse(results, safe=False)
def population_json(request):
name = request.GET.get('name')
year_from = request.GET.get('year_from')
year_to = request.GET.get('year_to')
date = request.GET.get('date')
p = Population.objects.all()
if name:
p = p.filter(name=name)
if year_from and year_to:
p = p.filter(year__range=(year_from, year_to))
if date:
p = p.filter(date=date)
results = []
for population in p:
o = dict()
o['name'] = population.name
o['date'] = str(population.date)
o['population_growth'] = population.population_growth
o['demographic_urbanization'] = population.demographic_urbanization
print(str(o) + "**************")
results.append(o)
return JsonResponse(results, safe=False)
def gdp_json(request):
name = request.GET.get('name')
year_from = request.GET.get('year_from')
year_to = request.GET.get('year_to')
date = request.GET.get('date')
g = GDP.objects.all()
if name:
g = g.filter(name=name)
if year_from and year_to:
g = g.filter(year__range=(year_from, year_to))
if date:
g = g.filter(date=date)
results = []
for gdp in g:
o = dict()
o['name'] = gdp.name
o['date'] = str(gdp.date)
o['total_population'] = gdp.total_population
o['gdp'] = gdp.gdp
print(str(o) + "**************")
results.append(o)
return JsonResponse(results, safe=False)