newmediamonitoring/backstage/views.py

175 lines
7.9 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
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):
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/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:
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-organization-update.html')