newmediamonitoring/backstage/views.py

132 lines
5.6 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
2020-11-17 10:25:42 +00:00
from django.http import HttpResponse, HttpResponseRedirect
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-06 07:14:57 +00:00
from dashboard.models import TimelinessMonitoring, Wrongly, Comment, Area_code_2020
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, '')