447 lines
19 KiB
Python
447 lines
19 KiB
Python
import csv
|
|
import os
|
|
import re
|
|
import time
|
|
from datetime import datetime
|
|
|
|
import xlrd
|
|
from django.contrib import messages
|
|
from django.http import HttpResponse, HttpResponseRedirect
|
|
from django.shortcuts import render
|
|
|
|
# Create your views here.
|
|
from xlrd import xldate_as_tuple, xldate_as_datetime
|
|
|
|
from NewMediaMonitoring import settings
|
|
from dashboard.models import TimelinessMonitoring, Wrongly, Comment, Area_code_2020, Organization, Weixin, Douyin, \
|
|
Weibo, Toutiao, Qita, NewMedia
|
|
from polls.models import UnqualifiedMedia
|
|
|
|
|
|
def backstage_new_media_public_opinion(request):
|
|
if request.method == 'POST':
|
|
filename = request.FILES.get('file')
|
|
date = request.POST.get("date")
|
|
date_length = request.POST.get("date_length")
|
|
if not os.path.exists(settings.MEDIA_ROOT):
|
|
os.makedirs(settings.MEDIA_ROOT)
|
|
if filename is None:
|
|
messages.error(request, '请选择要上传的文件!!!')
|
|
return HttpResponseRedirect('/backstage/backstage/newmedia/public/opinion/')
|
|
|
|
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:
|
|
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()
|
|
except:
|
|
pass
|
|
messages.success(request,"上传成功")
|
|
return render(request, 'backstage/backstage_new_media_public_opinion.html')
|
|
|
|
|
|
def backstage_error(request):
|
|
if request.method == 'POST':
|
|
filename = request.FILES.get('file')
|
|
city = request.POST.get('city')
|
|
jc_date = request.POST.get('jc_date')
|
|
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/')
|
|
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:
|
|
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()
|
|
except:
|
|
pass
|
|
messages.success(request,'上传成功')
|
|
|
|
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')
|
|
|
|
|
|
def backstage_user(request):
|
|
return render(request, '')
|
|
|
|
def backstage_organization_update(request):
|
|
print("____________________________________________________________________")
|
|
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/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:
|
|
csv_code_list.append(table.row_values(row)[4])
|
|
organization = Organization.objects.all()
|
|
for o in organization:
|
|
o_code_list.append(o.name)
|
|
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)
|
|
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')
|
|
|
|
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)
|
|
return render(request, 'backstage/backstage-toutiao-update.html', {'data_results': data_results})
|
|
|
|
def add_unqualified_media(request):
|
|
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')
|