views.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. from django.db.models import Sum, Count
  2. from django.shortcuts import render
  3. from django.contrib import admin
  4. from django.contrib.auth.decorators import permission_required
  5. import datetime
  6. # from slick_reporting.views import ReportView
  7. # from slick_reporting.fields import SlickReportField
  8. from .models import Policy, DiseaseType
  9. from django.views.generic import TemplateView
  10. from chartjs.views.lines import BaseLineChartView
  11. class LineChartJSONView(BaseLineChartView):
  12. def get_labels(self):
  13. """Return 7 labels for the x-axis."""
  14. return ["January", "February", "March", "April", "May", "June", "July"]
  15. def get_providers(self):
  16. """Return names of datasets."""
  17. return ["Central", "Eastside", "Westside"]
  18. def get_data(self):
  19. """Return 3 datasets to plot."""
  20. return [[75, 44, 92, 11, 44, 95, 35],
  21. [41, 92, 18, 3, 73, 87, 92],
  22. [87, 21, 94, 3, 90, 13, 65]]
  23. line_chart = TemplateView.as_view(template_name='line_chart.html')
  24. line_chart_json = LineChartJSONView.as_view()
  25. @permission_required("policies.add_policy")
  26. def PolicyVisualizationView(request):
  27. # 获取传染病分类列表
  28. disease_types = DiseaseType.objects.all()
  29. # 获取当前年份
  30. current_year = datetime.datetime.now().year
  31. # 处理查询参数
  32. start_year = request.GET.get('start_year')
  33. end_year = request.GET.get('end_year')
  34. exclude_ids = request.GET.get('exclude_ids')
  35. # 构造查询条件
  36. query = Policy.objects.all()
  37. selected_disease_types = request.GET.getlist('disease_types', [])
  38. if selected_disease_types:
  39. query = query.filter(disease_types__id__in=selected_disease_types)
  40. # if category:
  41. # query = query.filter(disease_types__name__in=category)
  42. if start_year:
  43. query = query.filter(year__gte=start_year)
  44. if end_year:
  45. query = query.filter(year__lte=end_year)
  46. if exclude_ids and len(exclude_ids) > 0:
  47. exclude_id_list = exclude_ids.split(',')
  48. # Filter out any non-numeric values.
  49. exclude_id_list = [id_ for id_ in exclude_id_list if id_.isdigit()]
  50. if exclude_id_list: # Only apply the filter if there are valid IDs to exclude.
  51. query = query.exclude(id__in=exclude_id_list)
  52. # 是否按照传染病类别统计政策文件数量
  53. group_by_category = request.GET.get('group_by_category')
  54. show_raw_data = request.GET.get('show_raw_data')
  55. # 查询政策数量按年份分组
  56. # 在进行年份统计时,同一条政策记录如果属于多个传染病类别,会被重复计算, Count 聚合函数在默认情况下不会考虑重复的记录。
  57. policy_count_by_year = query.values('year').annotate(count=Count('id', distinct=True)).order_by('year')
  58. # 计算数据总量
  59. total_count = query.distinct().count()
  60. context = admin.site.each_context(request)
  61. # 添加其他上下文变量
  62. #context['other_context'] = 'other value'
  63. other_context = {
  64. 'disease_types': disease_types,
  65. 'policy_list': query.distinct(),
  66. 'policy_count_by_year': list(policy_count_by_year),
  67. 'selected_disease_types': [int(id) for id in request.GET.getlist('disease_types', [])],
  68. 'current_year': current_year,
  69. 'show_raw_data': show_raw_data,
  70. 'start_year': start_year,
  71. 'end_year': end_year,
  72. 'group_by_category': group_by_category,
  73. 'total_count': total_count, # 添加数据总量
  74. }
  75. if exclude_ids:
  76. other_context['exclude_ids'] = exclude_ids
  77. if group_by_category:
  78. policy_count_by_disease_type = query.values('disease_types__name').annotate(count=Count('id', distinct=True)).order_by('disease_types')
  79. other_context['policy_count_by_category'] = policy_count_by_disease_type
  80. context.update(other_context)
  81. return render(request, 'policy_visualization.html', context)