newmediamonitoring/polls/views/monitor.py

144 lines
5.3 KiB
Python

from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
from dashboard.models import Douyin, NewMedia, Qita, Toutiao, Weibo, Weixin, Userprofile
from django.db.models import Count, Sum
COMPARTMENTS = {
'620100000000': '兰州市',
'620200000000': '嘉峪关市',
'620300000000': '金昌市',
'620400000000': '白银市',
'620500000000': '天水市',
'620600000000': '武威市',
'620700000000': '张掖市',
'620800000000': '平凉市',
'620900000000': '酒泉市',
'621000000000': '庆阳市',
'621100000000': '定西市',
'621200000000': '陇南市',
'622900000000': '临夏回族自治州',
'623000000000': '甘南藏族自治州',
'620171000000': '兰州新区',
'': '未知',
}
@csrf_exempt
def monitor_statistics(request):
profile_status_0_count = Userprofile.objects.filter(status=0).count()
profile_status_1_count = Userprofile.objects.filter(status=1).count()
user_total = profile_status_0_count + profile_status_1_count
chart4_data = []
chart4_data.append({'name': '激活', 'value': profile_status_1_count})
chart4_data.append({'name': '未激活', 'value': profile_status_0_count})
wbc = Weibo.objects.exclude(status=0).count()
wxc = Weixin.objects.exclude(status=0).count()
ttc = Toutiao.objects.exclude(status=0).count()
qtc = Qita.objects.exclude(status=0).count()
dyc = Douyin.objects.exclude(status=0).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.exclude(organization__cities='', status=0).values('organization__cities').order_by(
'organization__cities').annotate(num_media=Count('organization__cities'))
for row in q:
code = row['organization__cities']
if not code:
continue
compartment = COMPARTMENTS[code]
if code in results:
nums = results[compartment]
nums.append(row['num_media'])
else:
results[compartment] = [row['num_media']]
q = Weibo.objects.exclude(organization__cities='', status=0).values('organization__cities').order_by(
'organization__cities').annotate(num_media=Count('organization__cities'))
for row in q:
code = row['organization__cities']
if not code:
continue
compartment = COMPARTMENTS[code]
if compartment in results:
nums = results[compartment]
nums.append(row['num_media'])
else:
results[compartment] = [row['num_media']]
q = Toutiao.objects.exclude(organization__cities='', status=0).values('organization__cities').order_by(
'organization__cities').annotate(num_media=Count('organization__cities'))
for row in q:
code = row['organization__cities']
if not code:
continue
compartment = COMPARTMENTS[code]
if compartment in results:
nums = results[compartment]
nums.append(row['num_media'])
else:
results[compartment] = [row['num_media']]
q = Douyin.objects.exclude(organization__cities='', status=0).values('organization__cities').order_by(
'organization__cities').annotate(num_media=Count('organization__cities'))
for row in q:
code = row['organization__cities']
if not code:
continue
compartment = COMPARTMENTS[code]
if compartment in results:
nums = results[compartment]
nums.append(row['num_media'])
else:
results[compartment] = [row['num_media']]
q = Qita.objects.exclude(organization__cities='', status=0).values('organization__cities').order_by(
'organization__cities').annotate(num_media=Count('organization__cities'))
for row in q:
code = row['organization__cities']
if not code:
continue
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()
chart3_data = []
chart3_data.append({'name': '省直各部门', 'value': total1})
chart3_data.append({'name': '市州政府及部门', 'value': total2})
chart3_data.append({'name': '县区政府及部门、乡镇', 'value': total3})
return render(request, 'polls/monitor_statistics.html', {'chart1_data': chart1_data, 'total': total, 'chart2_data': chart2_data, 'chart3_data': chart3_data, 'user_total': user_total, 'chart4_data': chart4_data})
@csrf_exempt
def media_statistics(request):
return render(request, 'polls/media_statistics.html')
@csrf_exempt
def city_ranking(request):
return render(request, 'polls/city_ranking.html')
def monitor_result(request):
return render(request, 'polls/monitor_result.html')