import csv import os import re import xlrd from django.contrib import messages from django.http import HttpResponse, HttpResponseRedirect from django.shortcuts import render # Create your views here. from NewMediaMonitoring import settings from dashboard.models import TimelinessMonitoring, Wrongly, Comment def backstage_new_media_public_opinion(request): if request.method == 'POST': filename = request.FILES.get('file') date = request.POST.get("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/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/') 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) 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') def backstage_error(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/error/') 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] != '错误': print(v[0]) error = v[0] idea = v[1] site = v[2] n_type = v[3] n_name = v[4] date = v[5] title = v[6] city = v[7] jc_date = v[8] wrongly = Wrongly(error=error, idea=idea, site=site, n_type=n_type, n_name=n_name, date=date, title=title, city=city, jc_date=jc_date) wrongly.save() except: print(v) else: messages.error(request, '请上传正确的文件类型!!!') return HttpResponseRedirect('/backstage/backstage/error/') 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,'')