from django.contrib.auth.decorators import login_required # Create your views here. from django.forms import model_to_dict 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, TemplateView from dashboard.models import Spi, Hydro, Landuse, Finance, Revenue, Population, GDP from users.models import YearRange def index(request): return render(request, 'dashboard/index.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') @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') class SpiImportView(TemplateView): template_name = 'dashboard/spi_import.html' def SpiDataDetailView(request): 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] return render(request, 'dashboard/spi_data_detail.html', {'spis': spis}) @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') class HydroImportView(TemplateView): template_name = 'dashboard/hydro_import.html' def HydroDataDetailView(request): 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] return render(request, 'dashboard/hydro_data_detail.html', {'hydros': hydros}) @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') class LanduseImportView(TemplateView): template_name = 'dashboard/landuse_import.html' def LanduseDataDetailView(request): 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] return render(request, 'dashboard/landuse_data_detail.html', {'landuses': landuses}) @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') class FinanceImportView(TemplateView): template_name = 'dashboard/finance_import.html' @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): template_name = 'dashboard/revenue_import.html' def RevenueDataDetailView(request): 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] 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): user = request.user year_range = YearRange.objects.get(user_id = user.id) start_year = year_range.start_year end_year = year_range.end_year populations = Population.objects.filter(date__range = (start_year,end_year))[:100] 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): model = GDP success_url = reverse_lazy('data-gdp-list') class GDPImportView(TemplateView): template_name = 'dashboard/gdp_import.html' def GDPDataDetailView(request): 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)) return render(request, 'dashboard/gdp_data_detail.html', {'gdps': gdps}) 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) 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 print(str(o) + "社会经济") results.append(o) 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)