406 lines
12 KiB
Python
406 lines
12 KiB
Python
from django.contrib.auth.decorators import login_required
|
|
# Create your views here.
|
|
from django.db.models import Count
|
|
from django.http import JsonResponse
|
|
from django.shortcuts import render
|
|
from django.urls import reverse_lazy
|
|
from django.utils.decorators import method_decorator
|
|
from django.views.generic import ListView, DetailView, CreateView, UpdateView, DeleteView
|
|
|
|
from dashboard.models import Country, Spi, Hydro, Landuse, Finance, Popu, Crop, Admdiv
|
|
|
|
|
|
def index(request):
|
|
return render(request, 'dashboard/index.html')
|
|
|
|
|
|
@method_decorator(login_required, name='dispatch')
|
|
class CountryListView(ListView):
|
|
context_object_name = 'countries'
|
|
|
|
def get_queryset(self):
|
|
self.keyword = self.request.GET.get('keyword', '')
|
|
if len(self.keyword) != 0:
|
|
return Country.objects.filter(name__contains=self.keyword)
|
|
return Country.objects.all()
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super().get_context_data(**kwargs)
|
|
context['keyword'] = self.keyword
|
|
return context
|
|
|
|
|
|
class CountryDetailView(DetailView):
|
|
context_object_name = "country"
|
|
model = Country
|
|
|
|
|
|
class CountryCreateView(CreateView):
|
|
model = Country
|
|
fields = ['name', 'parent_name', 'level', 'datetime', 'area', 'arable_land_area', 'population',
|
|
'agricultural_population', 'gross_national_product', 'total_agricultural_output',
|
|
'arable_land_per_capita']
|
|
|
|
|
|
class CountryUpdateView(UpdateView):
|
|
model = Country
|
|
fields = ['name', 'parent_name', 'level', 'datetime', 'area', 'arable_land_area', 'population',
|
|
'agricultural_population', 'gross_national_product', 'total_agricultural_output',
|
|
'arable_land_per_capita']
|
|
|
|
|
|
class CountryDeleteView(DeleteView):
|
|
model = Country
|
|
|
|
success_url = reverse_lazy('data-country-list')
|
|
|
|
|
|
def country_chart(request):
|
|
return render(request, 'dashboard/country_chart.html')
|
|
|
|
|
|
def admdiv_chart(request):
|
|
return render(request, 'dashboard/admdiv_chart.html')
|
|
|
|
|
|
def crop_chart(request):
|
|
return render(request, 'dashboard/crop_chart.html')
|
|
|
|
|
|
def finance_chart(request):
|
|
return render(request, 'dashboard/finance_chart.html')
|
|
|
|
|
|
def hydro_chart(request):
|
|
return render(request, 'dashboard/hydro_chart.html')
|
|
|
|
|
|
def landuse_chart(request):
|
|
return render(request, 'dashboard/landuse_chart.html')
|
|
|
|
|
|
def popu_chart(request):
|
|
return render(request, 'dashboard/popu_chart.html')
|
|
|
|
|
|
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, 'dashboard/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)
|
|
return render(request, 'dashboard/spi_chart_time.html',
|
|
{'years': years, 'months': months, 'year': year,
|
|
'month': month})
|
|
|
|
|
|
|
|
|
|
@method_decorator(login_required, name='dispatch')
|
|
class SpiListView(ListView):
|
|
context_object_name = 'spis'
|
|
paginate_by = 100
|
|
|
|
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
|
|
fields = ['code', 'longitude', 'latitude', 'year', 'month', 'mean_precipitation', 'mean_temperature']
|
|
|
|
|
|
class SpiUpdateView(UpdateView):
|
|
model = Spi
|
|
fields = ['code', 'longitude', 'latitude', 'year', 'month', 'mean_precipitation', 'mean_temperature']
|
|
|
|
|
|
class SpiDeleteView(DeleteView):
|
|
model = Spi
|
|
|
|
success_url = reverse_lazy('data-spi-list')
|
|
|
|
|
|
@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:
|
|
return Hydro.objects.filter(name__contains=self.keyword)
|
|
return Hydro.objects.all()
|
|
|
|
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"
|
|
model = Hydro
|
|
|
|
|
|
class HydroCreateView(CreateView):
|
|
model = Hydro
|
|
fields = ['name', 'date', 'average_water_level', 'average_flow_rate', 'precipitation']
|
|
|
|
|
|
class HydroUpdateView(UpdateView):
|
|
model = Hydro
|
|
fields = ['name', 'date', 'average_water_level', 'average_flow_rate', 'precipitation']
|
|
|
|
|
|
class HydroDeleteView(DeleteView):
|
|
model = Hydro
|
|
|
|
success_url = reverse_lazy('data-hydro-list')
|
|
|
|
|
|
@method_decorator(login_required, name='dispatch')
|
|
class LanduseListView(ListView):
|
|
context_object_name = 'landuses'
|
|
|
|
def get_queryset(self):
|
|
self.keyword = self.request.GET.get('keyword', '')
|
|
if len(self.keyword) != 0:
|
|
return Landuse.objects.filter(name__contains=self.keyword)
|
|
return Landuse.objects.all()
|
|
|
|
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
|
|
fields = ['name', 'date', 'Cultivated_land_area', 'Irrigated_land', 'eatch_land', 'eatch_Irrigated_land']
|
|
|
|
|
|
class LanduseUpdateView(UpdateView):
|
|
model = Landuse
|
|
fields = ['name', 'date', 'Cultivated_land_area', 'Irrigated_land', 'eatch_land', 'eatch_Irrigated_land']
|
|
|
|
|
|
class LanduseDeleteView(DeleteView):
|
|
model = Landuse
|
|
|
|
success_url = reverse_lazy('data-landuse-list')
|
|
|
|
|
|
@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')
|
|
|
|
|
|
@method_decorator(login_required, name='dispatch')
|
|
class PopuListView(ListView):
|
|
context_object_name = 'popus'
|
|
|
|
def get_queryset(self):
|
|
self.keyword = self.request.GET.get('keyword', '')
|
|
if len(self.keyword) != 0:
|
|
return Popu.objects.filter(name__contains=self.keyword)
|
|
return Popu.objects.all()
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super().get_context_data(**kwargs)
|
|
context['keyword'] = self.keyword
|
|
return context
|
|
|
|
|
|
class PopuDetailView(DetailView):
|
|
context_object_name = "popu"
|
|
model = Popu
|
|
|
|
|
|
class PopuCreateView(CreateView):
|
|
model = Popu
|
|
fields = ['region', 'date', 'permanent_population', 'households', 'census_register_population', 'male',
|
|
'female_sex', 'birth_rate', 'mortality', 'growth_rate', 'urbanization_rate']
|
|
|
|
|
|
class PopuUpdateView(UpdateView):
|
|
model = Popu
|
|
fields = ['region', 'date', 'permanent_population', 'households', 'census_register_population', 'male',
|
|
'female_sex', 'birth_rate', 'mortality', 'growth_rate', 'urbanization_rate']
|
|
|
|
|
|
class PopuDeleteView(DeleteView):
|
|
model = Popu
|
|
|
|
success_url = reverse_lazy('data-popu-list')
|
|
|
|
|
|
@method_decorator(login_required, name='dispatch')
|
|
class CropListView(ListView):
|
|
context_object_name = 'crops'
|
|
|
|
def get_queryset(self):
|
|
self.keyword = self.request.GET.get('keyword', '')
|
|
if len(self.keyword) != 0:
|
|
return Crop.objects.filter(name__contains=self.keyword)
|
|
return Crop.objects.all()
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super().get_context_data(**kwargs)
|
|
context['keyword'] = self.keyword
|
|
return context
|
|
|
|
|
|
class CropDetailView(DetailView):
|
|
context_object_name = "crop"
|
|
model = Crop
|
|
|
|
|
|
class CropCreateView(CreateView):
|
|
model = Crop
|
|
fields = ['county_area', 'date', 'seeded_area', 'growth_rate1', 'yield1', 'growth_rate2', 'seeded_area',
|
|
'growth_rate3', 'yield2', 'growth_rate4']
|
|
|
|
|
|
class CropUpdateView(UpdateView):
|
|
model = Crop
|
|
fields = ['county_area', 'date', 'seeded_area', 'growth_rate1', 'yield1', 'growth_rate2', 'seeded_area',
|
|
'growth_rate3', 'yield2', 'growth_rate4']
|
|
|
|
|
|
class CropDeleteView(DeleteView):
|
|
model = Crop
|
|
|
|
success_url = reverse_lazy('data-crop-list')
|
|
|
|
|
|
@method_decorator(login_required, name='dispatch')
|
|
class AdmdivListView(ListView):
|
|
context_object_name = 'admdivs'
|
|
|
|
def get_queryset(self):
|
|
self.keyword = self.request.GET.get('keyword', '')
|
|
if len(self.keyword) != 0:
|
|
return Admdiv.objects.filter(name__contains=self.keyword)
|
|
return Admdiv.objects.all()
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super().get_context_data(**kwargs)
|
|
context['keyword'] = self.keyword
|
|
return context
|
|
|
|
|
|
class AdmdivDetailView(DetailView):
|
|
context_object_name = "admdiv"
|
|
model = Admdiv
|
|
|
|
|
|
class AdmdivCreateView(CreateView):
|
|
model = Admdiv
|
|
fields = ['county_area', 'date', 'government', 'villager_committee', 'street_office', 'residents_committee',
|
|
'land_area', 'proportion']
|
|
|
|
|
|
class AdmdivUpdateView(UpdateView):
|
|
model = Admdiv
|
|
fields = ['county_area', 'date', 'government', 'villager_committee', 'street_office', 'residents_committee',
|
|
'land_area', 'proportion']
|
|
|
|
|
|
class AdmdivDeleteView(DeleteView):
|
|
model = Admdiv
|
|
|
|
success_url = reverse_lazy('data-admdiv-list')
|
|
|
|
|
|
def spi_json(request):
|
|
code = request.GET.get('code')
|
|
year_from = request.GET.get('year_from')
|
|
year_to = request.GET.get('year_to')
|
|
year = request.GET.get('year')
|
|
month = request.GET.get('month')
|
|
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))
|
|
if year and month:
|
|
q = q.filter(year=year, month=month)
|
|
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)
|