newmediamonitoring/backstage/views.py

539 lines
23 KiB
Python
Raw Normal View History

2020-11-17 10:25:42 +00:00
import csv
2020-11-13 16:22:22 +00:00
import os
2020-11-17 01:03:09 +00:00
import re
2021-01-06 07:14:57 +00:00
import time
from datetime import datetime
2020-11-13 16:22:22 +00:00
import xlrd
2020-11-17 01:03:09 +00:00
from django.contrib import messages
2021-04-02 08:45:37 +00:00
from django.http import HttpResponse, HttpResponseRedirect, FileResponse
2020-11-13 16:22:22 +00:00
from django.shortcuts import render
# Create your views here.
2021-01-06 07:14:57 +00:00
from xlrd import xldate_as_tuple, xldate_as_datetime
2020-11-13 16:22:22 +00:00
from NewMediaMonitoring import settings
2021-01-11 13:08:52 +00:00
from dashboard.models import TimelinessMonitoring, Wrongly, Comment, Area_code_2020, Organization, Weixin, Douyin, \
2021-02-03 08:15:45 +00:00
Weibo, Toutiao, Qita, NewMedia
from polls.models import UnqualifiedMedia
2020-11-13 16:22:22 +00:00
def backstage_new_media_public_opinion(request):
if request.method == 'POST':
filename = request.FILES.get('file')
2020-11-17 10:25:42 +00:00
date = request.POST.get("date")
2021-01-06 07:14:57 +00:00
date_length = request.POST.get("date_length")
2020-11-17 10:25:42 +00:00
if not os.path.exists(settings.MEDIA_ROOT):
os.makedirs(settings.MEDIA_ROOT)
if filename is None:
2020-11-18 07:48:17 +00:00
messages.error(request, '请选择要上传的文件!!!')
2020-11-17 10:25:42 +00:00
return HttpResponseRedirect('/backstage/backstage/newmedia/public/opinion/')
2021-01-06 07:14:57 +00:00
data = xlrd.open_workbook(filename=None, file_contents=filename.read())
table = data.sheets()[0]
for row in range(table.nrows):
v = table.row_values(row)[1]
if v != '单位全称':
2020-11-17 10:25:42 +00:00
try:
2021-01-06 07:14:57 +00:00
identificationcode = table.row_values(row)[3]
n_type = table.row_values(row)[7]
results = table.row_values(row)[21]
update = table.row_values(row)[22]
silent = table.row_values(row)[23]
start_data = table.row_values(row)[24]
d = datetime(*xldate_as_tuple(start_data, 0))
cell_start = d.strftime('%Y-%m-%d')
end_data = table.row_values(row)[25]
e = datetime(*xldate_as_tuple(end_data, 0))
cell_end = e.strftime('%Y-%m-%d')
city = table.row_values(row)[10]
cell_city = Area_code_2020.objects.get(name=city).code
district = table.row_values(row)[11]
cell_district = Area_code_2020.objects.get(name=district).code
timelinessmonitoring = TimelinessMonitoring(identificationcode=identificationcode, n_type=n_type,
results=results, update=update, silent=silent,
start_data=cell_start, end_data=cell_end, date=date,
date_length=date_length,city=cell_city,district=cell_district)
timelinessmonitoring.save()
2020-11-17 10:25:42 +00:00
except:
2021-01-06 07:14:57 +00:00
pass
messages.success(request,"上传成功")
2020-11-17 01:03:09 +00:00
return render(request, 'backstage/backstage_new_media_public_opinion.html')
2020-11-17 15:05:51 +00:00
2020-11-18 07:48:17 +00:00
def backstage_error(request):
if request.method == 'POST':
filename = request.FILES.get('file')
2021-01-06 07:14:57 +00:00
city = request.POST.get('city')
jc_date = request.POST.get('jc_date')
2020-11-18 07:48:17 +00:00
if not os.path.exists(settings.MEDIA_ROOT):
os.makedirs(settings.MEDIA_ROOT)
if filename is None:
messages.error(request, '请选择要上传的文件!!!')
return HttpResponseRedirect('/backstage/backstage/error/')
2021-01-06 07:14:57 +00:00
data = xlrd.open_workbook(filename=None, file_contents=filename.read())
table = data.sheets()[0]
for row in range(table.nrows):
v = table.row_values(row)[1]
if v != '建议':
2020-11-18 07:48:17 +00:00
try:
2021-01-06 07:14:57 +00:00
error = table.row_values(row)[0]
idea = table.row_values(row)[1]
site = table.row_values(row)[4]
n_type = table.row_values(row)[2]
n_name = table.row_values(row)[3]
date = table.row_values(row)[5]
d = datetime(*xldate_as_tuple(date, 0))
cell_date = d.strftime('%Y-%m-%d')
title = table.row_values(row)[6]
wrongly = Wrongly(error=error, idea=idea, site=site, n_type=n_type, n_name=n_name, date=cell_date,
title=title, city=city, jc_date=jc_date)
wrongly.save()
2020-11-18 07:48:17 +00:00
except:
2021-01-06 07:14:57 +00:00
pass
messages.success(request,'上传成功')
2020-11-18 07:48:17 +00:00
return render(request, 'backstage/backstage-error.html')
def backstage_comment(request):
if request.method == 'POST':
filename = request.FILES.get('file')
if not os.path.exists(settings.MEDIA_ROOT):
os.makedirs(settings.MEDIA_ROOT)
if filename is None:
messages.error(request, '请选择要上传的文件!!!')
return HttpResponseRedirect('/backstage/backstage/comment/')
if str(filename).split('.')[1] == 'csv':
data = filename.read().decode("utf-8")
line = str(data).split('\n')
for l in line:
v = l.split(',')
try:
if v[0] != '评论':
comment = v[0]
reply = v[1]
name = v[2]
title = v[3]
date = v[4]
url = v[5]
comment = Comment(comment=comment, reply=reply, name=name, title=title, date=date, url=url)
comment.save()
except:
print(v)
else:
messages.error(request, '请上传正确的文件类型!!!')
return HttpResponseRedirect('/backstage/backstage/comment/')
return render(request, 'backstage/backstage-comment.html')
2020-11-20 10:41:18 +00:00
2021-01-06 07:14:57 +00:00
2020-11-20 10:41:18 +00:00
def backstage_user(request):
2021-01-06 07:14:57 +00:00
return render(request, '')
2021-01-07 10:53:00 +00:00
def backstage_organization_update(request):
2021-01-11 13:08:52 +00:00
print("____________________________________________________________________")
data_results = []
2021-01-07 10:53:00 +00:00
if request.method == 'POST':
2021-01-11 13:08:52 +00:00
o_code_list = []
csv_code_list = []
2021-01-07 10:53:00 +00:00
filename = request.FILES.get('file')
2021-01-11 13:08:52 +00:00
2021-01-07 10:53:00 +00:00
if not os.path.exists(settings.MEDIA_ROOT):
os.makedirs(settings.MEDIA_ROOT)
if filename is None:
messages.error(request, '请选择要上传的文件!!!')
return HttpResponseRedirect('/backstage/backstage/organization/update/')
data = xlrd.open_workbook(filename=None, file_contents=filename.read())
table = data.sheets()[0]
for row in range(table.nrows):
v = table.row_values(row)[1]
if v != '单位全称':
try:
2021-01-11 13:08:52 +00:00
csv_code_list.append(table.row_values(row)[4])
organization = Organization.objects.all()
for o in organization:
o_code_list.append(o.name)
2021-01-07 10:53:00 +00:00
except:
pass
2021-01-11 13:08:52 +00:00
for i in list(set( o_code_list ) ^ set( csv_code_list )):
x = dict()
x['o'] = i
if i in o_code_list and i not in csv_code_list:
x['c'] = '删除'
data_results.append(x)
elif i in csv_code_list and i not in o_code_list:
x['c'] = '新增'
data_results.append(x)
else:
x['c'] = '异常'
data_results.append(x)
return render(request, 'backstage/backstage-organization-update.html',{'data_results':data_results})
def backstage_weixin_update(request):
data_results = []
if request.method == 'POST':
o_code_list = []
csv_code_list = []
filename = request.FILES.get('file')
if not os.path.exists(settings.MEDIA_ROOT):
os.makedirs(settings.MEDIA_ROOT)
if filename is None:
messages.error(request, '请选择要上传的文件!!!')
return HttpResponseRedirect('/backstage/backstage/weixin'
'/update/')
data = xlrd.open_workbook(filename=None, file_contents=filename.read())
table = data.sheets()[0]
for row in range(table.nrows):
v = table.row_values(row)[1]
if v != '单位全称':
try:
csv_code_list.append(table.row_values(row)[4])
weixin = Weixin.objects.all()
for o in weixin:
o_code_list.append(o.code)
except:
pass
for i in list(set(o_code_list) ^ set(csv_code_list)):
x = dict()
x['o'] = i
if i in o_code_list and i not in csv_code_list:
x['c'] = '删除'
data_results.append(x)
elif i in csv_code_list and i not in o_code_list:
x['c'] = '新增'
data_results.append(x)
else:
x['c'] = '异常'
data_results.append(x)
print(data_results)
return render(request, 'backstage/backstage-weixin-update.html', {'data_results': data_results})
def backstage_douyin_update(request):
data_results = []
if request.method == 'POST':
o_code_list = []
csv_code_list = []
filename = request.FILES.get('file')
if not os.path.exists(settings.MEDIA_ROOT):
os.makedirs(settings.MEDIA_ROOT)
if filename is None:
messages.error(request, '请选择要上传的文件!!!')
return HttpResponseRedirect('/backstage/backstage/douyin'
'/update/')
data = xlrd.open_workbook(filename=None, file_contents=filename.read())
table = data.sheets()[0]
for row in range(table.nrows):
v = table.row_values(row)[1]
if v != '单位全称':
try:
csv_code_list.append(table.row_values(row)[4])
douyin = Douyin.objects.all()
for o in douyin:
o_code_list.append(o.code)
except:
pass
for i in list(set(o_code_list) ^ set(csv_code_list)):
x = dict()
x['o'] = i
if i in o_code_list and i not in csv_code_list:
x['c'] = '删除'
data_results.append(x)
elif i in csv_code_list and i not in o_code_list:
x['c'] = '新增'
data_results.append(x)
else:
x['c'] = '异常'
data_results.append(x)
print(data_results)
return render(request, 'backstage/backstage-douyin-update.html', {'data_results': data_results})
def backstage_weibo_update(request):
data_results = []
if request.method == 'POST':
o_code_list = []
csv_code_list = []
filename = request.FILES.get('file')
if not os.path.exists(settings.MEDIA_ROOT):
os.makedirs(settings.MEDIA_ROOT)
if filename is None:
messages.error(request, '请选择要上传的文件!!!')
return HttpResponseRedirect('/backstage/backstage/weibo'
'/update/')
data = xlrd.open_workbook(filename=None, file_contents=filename.read())
table = data.sheets()[0]
for row in range(table.nrows):
v = table.row_values(row)[1]
if v != '单位全称':
try:
csv_code_list.append(table.row_values(row)[4])
weibo = Weibo.objects.all()
for o in weibo:
o_code_list.append(o.code)
except:
pass
for i in list(set(o_code_list) ^ set(csv_code_list)):
x = dict()
x['o'] = i
if i in o_code_list and i not in csv_code_list:
x['c'] = '删除'
data_results.append(x)
elif i in csv_code_list and i not in o_code_list:
x['c'] = '新增'
data_results.append(x)
else:
x['c'] = '异常'
data_results.append(x)
print(data_results)
return render(request, 'backstage/backstage-weibo-update.html', {'data_results': data_results})
def backstage_toutiao_update(request):
data_results = []
if request.method == 'POST':
o_code_list = []
csv_code_list = []
filename = request.FILES.get('file')
if not os.path.exists(settings.MEDIA_ROOT):
os.makedirs(settings.MEDIA_ROOT)
if filename is None:
messages.error(request, '请选择要上传的文件!!!')
return HttpResponseRedirect('/backstage/backstage/toutiao'
'/update/')
data = xlrd.open_workbook(filename=None, file_contents=filename.read())
table = data.sheets()[0]
for row in range(table.nrows):
v = table.row_values(row)[1]
if v != '单位全称':
try:
csv_code_list.append(table.row_values(row)[4])
toutiao = Toutiao.objects.all()
for o in toutiao:
o_code_list.append(o.code)
except:
pass
for i in list(set(o_code_list) ^ set(csv_code_list)):
x = dict()
x['o'] = i
if i in o_code_list and i not in csv_code_list:
x['c'] = '删除'
data_results.append(x)
elif i in csv_code_list and i not in o_code_list:
x['c'] = '新增'
data_results.append(x)
else:
x['c'] = '异常'
data_results.append(x)
print(data_results)
return render(request, 'backstage/backstage-toutiao-update.html', {'data_results': data_results})
def backstage_qita_update(request):
data_results = []
if request.method == 'POST':
o_code_list = []
csv_code_list = []
filename = request.FILES.get('file')
2021-01-07 10:53:00 +00:00
2021-01-11 13:08:52 +00:00
if not os.path.exists(settings.MEDIA_ROOT):
os.makedirs(settings.MEDIA_ROOT)
if filename is None:
messages.error(request, '请选择要上传的文件!!!')
return HttpResponseRedirect('/backstage/backstage/qita'
'/update/')
data = xlrd.open_workbook(filename=None, file_contents=filename.read())
table = data.sheets()[0]
for row in range(table.nrows):
v = table.row_values(row)[1]
if v != '单位全称':
try:
csv_code_list.append(table.row_values(row)[4])
qita = Qita.objects.all()
for o in qita:
o_code_list.append(o.code)
except:
pass
for i in list(set(o_code_list) ^ set(csv_code_list)):
x = dict()
x['o'] = i
if i in o_code_list and i not in csv_code_list:
x['c'] = '删除'
data_results.append(x)
elif i in csv_code_list and i not in o_code_list:
x['c'] = '新增'
data_results.append(x)
else:
x['c'] = '异常'
data_results.append(x)
print(data_results)
2021-02-03 03:48:42 +00:00
return render(request, 'backstage/backstage-toutiao-update.html', {'data_results': data_results})
def add_unqualified_media(request):
2021-02-03 08:15:45 +00:00
if request.method == 'POST':
filename = request.FILES.get('file')
version = request.POST.get('version')
if not os.path.exists(settings.MEDIA_ROOT):
os.makedirs(settings.MEDIA_ROOT)
if filename is None:
messages.error(request, '请选择要上传的文件!!!')
return HttpResponseRedirect('/backstage/add/unqualified/media/')
data = xlrd.open_workbook(filename=None, file_contents=filename.read())
table = data.sheets()[0]
for row in range(table.nrows):
v = table.row_values(row)[1]
if v != '单位全称':
media_type = table.row_values(row)[7]
media_code = table.row_values(row)[3]
name = table.row_values(row)[4]
unit = table.row_values(row)[1]
media_id = None
type = None
print(media_code)
try:
if "微信" in str(media_type):
media_id = Weixin.objects.get(code=name).id
type = 0
elif "微博" in str(media_type):
media_id = Weibo.objects.get(code=name).id
type = 1
elif "抖音" in str(media_type):
media_id = Douyin.objects.get(code=name).id
type = 2
elif "头条" in str(media_type):
media_id = Toutiao.objects.get(code=name).id
type = 3
else:
media_id = Qita.objects.get(code=name).id
type = 4
if Area_code_2020.objects.filter(name=table.row_values(row)[9]).count() > 0:
province = Area_code_2020.objects.get(name=table.row_values(row)[9]).code
else:
province = table.row_values(row)[9]
if Area_code_2020.objects.filter(name=table.row_values(row)[10]).count() > 0:
cities = Area_code_2020.objects.get(name=table.row_values(row)[10]).code
else:
cities = table.row_values(row)[10]
if Area_code_2020.objects.filter(name=table.row_values(row)[11]).count() > 0:
district = Area_code_2020.objects.get(name=table.row_values(row)[11]).code
else:
district = table.row_values(row)[11]
result = table.row_values(row)[20]
cou = table.row_values(row)[21]
dos = table.row_values(row)[22]
sdos = time.strftime('%Y-%m-%d', time.localtime(table.row_values(row)[23]))
edos = time.strftime('%Y-%m-%d', time.localtime(table.row_values(row)[24]))
unqualifiedmedia = UnqualifiedMedia(media_id=media_id, name=name, unit=unit, type=type,
province=province, cities=cities, district=district,
result=result, cou=cou, dos=dos, sdos=sdos, edos=edos,
version=version)
unqualifiedmedia.save()
except:
pass
print(media_id)
messages.success(request,"上传成功")
return render(request,'backstage/backstage_add_unqualified_media.html')
2021-04-02 08:45:37 +00:00
def backstage_data_download(request):
user = request.user
weixin = Weixin.objects.all()
weibo = Weibo.objects.all()
toutiao = Toutiao.objects.all()
douyin = Douyin.objects.all()
qita = Qita.objects.all()
results = []
for wx in weixin:
o_weixin = dict()
o_weixin['type'] = "微信"
o_weixin['code'] = wx.code
o_weixin['organization'] = wx.organization.name
o_weixin['alias'] = wx.alias
o_weixin['status'] = wx.status
o_weixin['id'] = wx.weixinid
o_weixin['attention'] = wx.attention
o_weixin['identificationcode'] = wx.identificationcode
o_weixin['function'] = wx.function
o_weixin['articleurl'] = wx.articleurl
results.append(o_weixin)
for wb in weibo:
o_weibo = dict()
o_weibo['type'] = "微博"
o_weibo['code'] = wb.code
o_weibo['organization'] = wb.organization.name
o_weibo['alias'] = wb.alias
o_weibo['status'] = wb.status
o_weibo['id'] = wb.weiboid
o_weibo['attention'] = wb.attention
o_weibo['identificationcode'] = wb.identificationcode
o_weibo['function'] = wb.function
o_weibo['articleurl'] = wb.articleurl
results.append(o_weibo)
for tt in toutiao:
o_toutiao = dict()
o_toutiao['type'] = "今日头条"
o_toutiao['code'] = tt.code
o_toutiao['organization'] = tt.organization.name
o_toutiao['alias'] = tt.alias
o_toutiao['status'] = tt.status
o_toutiao['id'] = tt.toutiaoid
o_toutiao['attention'] = tt.attention
o_toutiao['identificationcode'] = tt.identificationcode
o_toutiao['function'] = tt.function
o_toutiao['articleurl'] = tt.articleurl
results.append(o_toutiao)
for dy in douyin:
o_douyin = dict()
o_douyin['type'] = "抖音"
o_douyin['code'] = dy.code
o_douyin['organization'] = dy.organization.name
o_douyin['alias'] = dy.alias
o_douyin['status'] = dy.status
o_douyin['id'] = dy.douyinid
o_douyin['attention'] = dy.attention
o_douyin['identificationcode'] = dy.identificationcode
o_douyin['function'] = dy.function
o_douyin['articleurl'] = dy.articleurl
results.append(o_douyin)
for qt in qita:
o_qita = dict()
o_qita['type'] = qt.type
o_qita['code'] = qt.code
o_qita['organization'] = qt.organization.name
o_qita['alias'] = qt.alias
o_qita['status'] = qt.status
o_qita['id'] = qt.qitaid
o_qita['attention'] = qt.attention
o_qita['identificationcode'] = qt.identificationcode
o_qita['function'] = qt.function
o_qita['articleurl'] = qt.articleurl
results.append(o_qita)
# print(results)
t = int(time.time())
2021-04-02 08:53:16 +00:00
os.popen('mkdir /var/www/p3/newmediamonitoring/static/upload')
f = '/var/www/p3/newmediamonitoring/static/upload'
2021-04-02 08:45:37 +00:00
with open(f+'/'+user.username+'.csv','w', newline='',encoding='utf-8') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(["类型","名称", "单位", "别名", "状态", "ID", "关注量","新媒体标识码","功能","文章URL"])
for r in results:
# print(r['type'])
writer.writerow([r['type'], r['code'], r['organization'],r['alias'], r['status'], r['id'], r['attention'],r['identificationcode'],r['function'], r['articleurl']])
file = open(f+'/'+user.username+'.csv','rb')
response = FileResponse(file)
response['Content-Type'] = 'application/octet-stream'
2021-04-02 08:56:56 +00:00
response['Content-Disposition'] = 'attachment;filename="data.csv"'
2021-04-02 08:45:37 +00:00
return response