from django.shortcuts import render from dashboard.models import Douyin, NewMedia, Qita, Toutiao, Weibo, Weixin from django.db.models import Count, Sum COMPARTMENTS = { '620100000000': '兰州市', '620200000000': '嘉峪关市', '620300000000': '金昌市', '620400000000': '白银市', '620500000000': '天水市', '620600000000': '武威市', '620700000000': '张掖市', '620800000000': '平凉市', '620900000000': '酒泉市', '621000000000': '庆阳市', '621100000000': '定西市', '621200000000': '陇南市', '622900000000': '临夏回族自治州', '623000000000': '甘南藏族自治州', '620171000000': '兰州新区' } def monitor_statistics(request): wbc = Weibo.objects.count() wxc = Weixin.objects.count() ttc = Toutiao.objects.count() qtc = Qita.objects.count() dyc = Douyin.objects.count() total = wbc+wxc+ttc+qtc+dyc chart1_data = [] chart1_data.append({'name': '微信', 'value': wxc}) chart1_data.append({'name': '微博', 'value': wbc}) chart1_data.append({'name': '头条', 'value': ttc}) chart1_data.append({'name': '抖音', 'value': dyc}) chart1_data.append({'name': '其它', 'value': qtc}) results = dict() q = Weixin.objects.values('organization__cities').order_by( 'organization__cities').annotate(num_media=Count('organization__cities')) for row in q: code = row['organization__cities'] compartment = COMPARTMENTS[code] if code in results: nums = results[compartment] nums.append(row['num_media']) else: results[compartment] = [row['num_media']] q = Weibo.objects.values('organization__cities').order_by( 'organization__cities').annotate(num_media=Count('organization__cities')) for row in q: code = row['organization__cities'] compartment = COMPARTMENTS[code] if compartment in results: nums = results[compartment] nums.append(row['num_media']) else: results[compartment] = [row['num_media']] q = Toutiao.objects.values('organization__cities').order_by( 'organization__cities').annotate(num_media=Count('organization__cities')) for row in q: code = row['organization__cities'] compartment = COMPARTMENTS[code] if compartment in results: nums = results[compartment] nums.append(row['num_media']) else: results[compartment] = [row['num_media']] q = Douyin.objects.values('organization__cities').order_by( 'organization__cities').annotate(num_media=Count('organization__cities')) for row in q: code = row['organization__cities'] compartment = COMPARTMENTS[code] if compartment in results: nums = results[compartment] nums.append(row['num_media']) else: results[compartment] = [row['num_media']] q = Qita.objects.values('organization__cities').order_by( 'organization__cities').annotate(num_media=Count('organization__cities')) for row in q: code = row['organization__cities'] compartment = COMPARTMENTS[code] if compartment in results: nums = results[compartment] nums.append(row['num_media']) else: results[compartment] = [row['num_media']] chart2_data = [] for k in results: chart2_data.append({'name': k, 'value': sum(results[k])}) total1 = NewMedia.category_one_count() total2 = NewMedia.category_two_count() total3 = NewMedia.category_three_count() return render(request, 'polls/monitor_statistics.html', {'chart1_data': chart1_data, 'total': total, 'chart2_data': chart2_data, 'total1': total1, 'total2': total2, 'total3': total3})