From e034d3f8c927ffb277a51d1db32521004a692b28 Mon Sep 17 00:00:00 2001 From: xieshen <1397910458@qq.com> Date: Tue, 17 Nov 2020 18:25:42 +0800 Subject: [PATCH] #add message --- NewMediaMonitoring/settings.py | 1 - .../backstage_new_media_public_opinion.html | 10 +- backstage/views.py | 101 +++++++++++------- dashboard/models.py | 5 +- dashboard/templates/dashboard/base/left.html | 2 +- management/urls.py | 2 +- 6 files changed, 74 insertions(+), 47 deletions(-) diff --git a/NewMediaMonitoring/settings.py b/NewMediaMonitoring/settings.py index 6a4057a..8bebb98 100644 --- a/NewMediaMonitoring/settings.py +++ b/NewMediaMonitoring/settings.py @@ -159,7 +159,6 @@ MEDIA_ROOT = '/var/www/p3/newmediamonitoring/media/' """用户模块扩展部分""" AUTH_PROFILE_MODULE = 'djangoadmin.myadmin.UserProfile' """用户模块扩展完成""" -UPLOAD_ROOT = os.path.join(BASE_DIR,'/static/upload') CORS_ORIGIN_ALLOW_ALL = True CORS_URLS_REGEX = r'^/polls/.*$' diff --git a/backstage/templates/backstage/backstage_new_media_public_opinion.html b/backstage/templates/backstage/backstage_new_media_public_opinion.html index 79fcc2a..a12b09e 100644 --- a/backstage/templates/backstage/backstage_new_media_public_opinion.html +++ b/backstage/templates/backstage/backstage_new_media_public_opinion.html @@ -32,11 +32,17 @@
新媒体舆情数据录入
-
{% csrf_token %} + {% csrf_token %} +
+ + +
-

请上传excel文件

+

请上传csv文件(文件表头依次为:新媒体类型,新媒体名称,主体类型,代表主体名称,市,县/区,备注,结果,更新次数,最大连续静默日数,开始时间,结束时间。时间格式为(2020/11/17).更新次数和最大连续静默日必须是数字)

diff --git a/backstage/views.py b/backstage/views.py index b3503cb..63c71af 100644 --- a/backstage/views.py +++ b/backstage/views.py @@ -1,59 +1,78 @@ +import csv import os import re import xlrd from django.contrib import messages -from django.http import HttpResponse +from django.http import HttpResponse, HttpResponseRedirect from django.shortcuts import render # Create your views here. from NewMediaMonitoring import settings +from NewMediaMonitoring.local_settings import MEDIA_ROOT from dashboard.models import TimelinessMonitoring def backstage_new_media_public_opinion(request): if request.method == 'POST': filename = request.FILES.get('file') - workbook = xlrd.open_workbook(settings.UPLOAD_ROOT + "/" + str(filename)) - # sheet = readboot.sheet_by_index(0) - # # 获取excel的行和列 - # nrows = sheet.nrows - # ncols = sheet.ncols - table = workbook.sheets()[0] - for row in range(table.nrows): - v = table.row_values(row) - if v[0] != '新媒体类型' or v[1] != '新媒体名称' or v[2] != '主体类型' or v[3] != '代表主体名称' or v[4] != '市' or v[ - 6] != '备注' or v[7] != '结果' or v[8] != '更新次数' or v[9] != '最大连续静默日数' or v[10] != '开始时间' or v[ - 11] != '结束时间': - messages.error(request, '请上传正确的文件格式!!!') - return render(request, 'backstage/backstage_new_media_public_opinion.html') - mat1 = re.search(r"(\d{4}-\d{1,2}-\d{1,2})", v[10]) - mat2 = re.search(r"(\d{4}-\d{1,2}-\d{1,2})", v[11]) - if mat1 is None or mat2 is None: - messages.error(request, '请上传正确的时间格式!!!') - return render(request, 'backstage/backstage_new_media_public_opinion.html') - if v[8].isdigit() and v[9].isdigit(): - if v != '账号名称': - n_type = v[0] - n_name = v[1] - o_type = v[2] - o_name = v[3] - city = v[4] - counties = v[5] - remark = v[6] - results = v[7] - update = v[8] - silet = v[9] - start_data = v[10] - end_data = v[11] - timelinessmonitoring = TimelinessMonitoring(n_type=n_type, n_name=n_name, o_type=o_type, o_name=o_name, - city=city, counties=counties, remark=remark, - results=results, update=update, silet=silet, - start_data=start_data, end_data=end_data,) - print(timelinessmonitoring) + date = request.POST.get("date") + print(filename) + 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/') + if str(filename).split('.')[1] == 'csv': + data = filename.read().decode("utf-8") + line = str(data).split('\n') + for l in line: + v = l.split(',') + # print(v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7],v[8],v[9],v[10],v[11]) + # if str(v[0]) != '新媒体类型' or str(v[1]) != '新媒体名称' or str(v[2]) != '主体类型' or str(v[3]) != '代表主体名称' or str(v[4]) != '市' or str(v[6]) != '备注' or str(v[7]) != '结果' or str(v[8]) != '更新次数' or str(v[9]) != '最大连续静默日数' or str(v[10]) != '开始时间' or str(v[11]) != '结束时间': + # messages.error(request, '请上传正确的文件格式!!!') + # return HttpResponseRedirect('/backstage/backstage/newmedia/public/opinion/') + mat1 = None + mat2 = None + # if v[10] is not None and v[11] is not None: + # mat1 = re.search(r"(\d{4}/\d{1,2}/\d{1,2})", v[10]) + # mat2 = re.search(r"(\d{4}/\d{1,2}/\d{1,2})", v[11]) + # if mat1 is None or mat2 is None: + # messages.error(request, '请上传正确的时间格式!!!') + # return HttpResponseRedirect('/backstage/backstage/newmedia/public/opinion/') - else: - messages.error(request, '更新次数和最大连续静默日数只能为数字!!!') - return render(request, 'backstage/backstage_new_media_public_opinion.html') + + try: + if v[8] != '更新次数': + # if v[8].isdigit(): + n_type = v[0] + n_name = v[1] + o_type = v[2] + o_name = v[3] + city = v[4] + counties = v[5] + remark = v[6] + results = v[7] + update = v[8] + silet = v[9] + start_data = v[10] + end_data = v[11] + timelinessmonitoring = TimelinessMonitoring(n_type=n_type, n_name=n_name, o_type=o_type, + o_name=o_name, + city=city, counties=counties, remark=remark, + results=results, update=update, silet=silet, + start_data=start_data, end_data=end_data, + comment=0, date=date) + print(timelinessmonitoring.n_type) + timelinessmonitoring.save() + # else: + # messages.error(request, '更新次数只能为数字!!!') + # return HttpResponseRedirect('/backstage/backstage/newmedia/public/opinion/') + except: + print(v) + + else: + messages.error(request, '请上传正确的文件类型!!!') + return HttpResponseRedirect('/backstage/backstage/newmedia/public/opinion/') return render(request, 'backstage/backstage_new_media_public_opinion.html') diff --git a/dashboard/models.py b/dashboard/models.py index 3657287..8dffda5 100644 --- a/dashboard/models.py +++ b/dashboard/models.py @@ -731,7 +731,7 @@ class NewmediaSentimentToutiao(models.Model): class TimelinessMonitoring(models.Model): - id = models.UUIDField('id', primary_key=True, default=uuid.uuid4()) + id = models.UUIDField('id', primary_key=True, default=uuid.uuid4) n_type = models.CharField('新媒体类型', max_length=256, null=True, blank=True) n_name = models.CharField('新媒体名称', max_length=256, null=True, blank=True) o_type = models.CharField('主体类型', max_length=256, null=True, blank=True) @@ -766,3 +766,6 @@ class Wrongly(models.Model): def __str__(self): return self.title +#时效性监测文件存储表 + + diff --git a/dashboard/templates/dashboard/base/left.html b/dashboard/templates/dashboard/base/left.html index 7de2908..a1e060e 100644 --- a/dashboard/templates/dashboard/base/left.html +++ b/dashboard/templates/dashboard/base/left.html @@ -174,7 +174,7 @@