import csv import os import re import time from datetime import datetime import xlrd from django.contrib import messages from django.contrib.auth.models import User from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage from django.http import HttpResponse, HttpResponseRedirect, FileResponse 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, Userprofile 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: pass 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): 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) 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) 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) 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) 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) 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 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 messages.success(request,"上传成功") return render(request,'backstage/backstage_add_unqualified_media.html') def backstage_data_download(request): user = request.user weixin = Weixin.objects.all() weibo = Weibo.objects.all() toutiao = Toutiao.objects.all() douyin = Douyin.objects.all() qita = Qita.objects.all() results = [] for wx in weixin: o_weixin = dict() o_weixin['type'] = "微信" o_weixin['code'] = wx.code o_weixin['organization'] = wx.organization.name o_weixin['alias'] = wx.alias o_weixin['status'] = wx.status o_weixin['id'] = wx.weixinid o_weixin['attention'] = wx.attention o_weixin['identificationcode'] = wx.identificationcode o_weixin['function'] = wx.function o_weixin['articleurl'] = wx.articleurl results.append(o_weixin) for wb in weibo: o_weibo = dict() o_weibo['type'] = "微博" o_weibo['code'] = wb.code o_weibo['organization'] = wb.organization.name o_weibo['alias'] = wb.alias o_weibo['status'] = wb.status o_weibo['id'] = wb.weiboid o_weibo['attention'] = wb.attention o_weibo['identificationcode'] = wb.identificationcode o_weibo['function'] = wb.function o_weibo['articleurl'] = wb.articleurl results.append(o_weibo) for tt in toutiao: o_toutiao = dict() o_toutiao['type'] = "今日头条" o_toutiao['code'] = tt.code o_toutiao['organization'] = tt.organization.name o_toutiao['alias'] = tt.alias o_toutiao['status'] = tt.status o_toutiao['id'] = tt.toutiaoid o_toutiao['attention'] = tt.attention o_toutiao['identificationcode'] = tt.identificationcode o_toutiao['function'] = tt.function o_toutiao['articleurl'] = tt.articleurl results.append(o_toutiao) for dy in douyin: o_douyin = dict() o_douyin['type'] = "抖音" o_douyin['code'] = dy.code o_douyin['organization'] = dy.organization.name o_douyin['alias'] = dy.alias o_douyin['status'] = dy.status o_douyin['id'] = dy.douyinid o_douyin['attention'] = dy.attention o_douyin['identificationcode'] = dy.identificationcode o_douyin['function'] = dy.function o_douyin['articleurl'] = dy.articleurl results.append(o_douyin) for qt in qita: o_qita = dict() o_qita['type'] = qt.type o_qita['code'] = qt.code o_qita['organization'] = qt.organization.name o_qita['alias'] = qt.alias o_qita['status'] = qt.status o_qita['id'] = qt.qitaid o_qita['attention'] = qt.attention o_qita['identificationcode'] = qt.identificationcode o_qita['function'] = qt.function o_qita['articleurl'] = qt.articleurl results.append(o_qita) t = int(time.time()) os.popen('mkdir /var/www/p3/newmediamonitoring/static/upload') f = '/var/www/p3/newmediamonitoring/static/upload' with open(f+'/'+user.username+'.csv','w', newline='',encoding='utf-8') as csvfile: writer = csv.writer(csvfile) writer.writerow(["类型","名称", "单位", "别名", "状态", "ID", "关注量","新媒体标识码","功能","文章URL"]) for r in results: writer.writerow([r['type'], r['code'], r['organization'],r['alias'], r['status'], r['id'], r['attention'],r['identificationcode'],r['function'], r['articleurl']]) file = open(f+'/'+user.username+'.csv','rb') response = FileResponse(file) response['Content-Type'] = 'application/octet-stream' response['Content-Disposition'] = 'attachment;filename="data.csv"' return response def backstage_app_inform_manage(request): user_count = User.objects.all().count() user = request.user o = Organization.objects.get(userprofile__user_id=user.id) level = o.level.level province = o.province cities = o.cities district = o.district userpaginator = None # if level == 1: # userpaginator = Userprofile.objects.filter(organization__province=province).order_by('-created') # elif level == 2: # userpaginator = Userprofile.objects.filter(organization__province=province, # organization__cities=cities).order_by('-created') # elif level == 3: # userpaginator = Userprofile.objects.filter(organization__province=province, # organization__cities=cities, # organization__district=district).order_by('-created') if level == 9: userpaginator = Userprofile.objects.filter(message_status=1).order_by('-created') userallinfo = [] if userpaginator is not None: paginator = Paginator(userpaginator, 6) page = int(request.GET.get('page', 1)) try: userpaginator = paginator.page(page) except PageNotAnInteger: userpaginator = paginator.page(1) except EmptyPage: userpaginator = paginator.page(paginator.num_pages) for u in userpaginator: o = dict() o['id'] = str(u.id) o['user_id'] = str(u.user_id) if u.image: o['image'] = u.image else: o['image'] = '/profile/user_default.jpg' o['name'] = u.name o['phone'] = u.user.username o['organization'] = u.organization.name organization_id = u.organization.id if Organization.objects.get(id=organization_id).province and Organization.objects.get( id=organization_id).cities and Organization.objects.get( id=organization_id).district and Organization.objects.get( id=organization_id).town and Organization.objects.get(id=organization_id).village: o['administrativedivision'] = str(str( Area_code_2020.objects.get(code=Organization.objects.get(id=organization_id).province)) + '-' + str( Area_code_2020.objects.get(code=Organization.objects.get(id=organization_id).cities)) + '-' + str( Area_code_2020.objects.get(code=Organization.objects.get(id=organization_id).district)) + '-' + str( Area_code_2020.objects.get(code=Organization.objects.get(id=organization_id).town)) + '-' + str( Area_code_2020.objects.get(code=Organization.objects.get(id=organization_id).village))).replace( 'None', '') elif Organization.objects.get(id=organization_id).province and Organization.objects.get( id=organization_id).cities and Organization.objects.get( id=organization_id).district and Organization.objects.get(id=organization_id).town: o['administrativedivision'] = str(str( Area_code_2020.objects.get(code=Organization.objects.get(id=organization_id).province)) + '-' + str( Area_code_2020.objects.get(code=Organization.objects.get(id=organization_id).cities)) + '-' + str( Area_code_2020.objects.get(code=Organization.objects.get(id=organization_id).district)) + '-' + str( Area_code_2020.objects.get(code=Organization.objects.get(id=organization_id).town))).replace('None', '') elif Organization.objects.get(id=organization_id).province and Organization.objects.get( id=organization_id).cities and Organization.objects.get(id=organization_id).district: o['administrativedivision'] = str(str( Area_code_2020.objects.get(code=Organization.objects.get(id=organization_id).province)) + '-' + str( Area_code_2020.objects.get(code=Organization.objects.get(id=organization_id).cities)) + '-' + str( Area_code_2020.objects.get(code=Organization.objects.get(id=organization_id).district))).replace( 'None', '') elif Organization.objects.get(id=organization_id).province and Organization.objects.get( id=organization_id).cities: o['administrativedivision'] = str(str( Area_code_2020.objects.get(code=Organization.objects.get(id=organization_id).province)) + '-' + str( Area_code_2020.objects.get(code=Organization.objects.get(id=organization_id).cities))).replace( 'None', '') elif Organization.objects.get(id=organization_id).province: o['administrativedivision'] = str(str( Area_code_2020.objects.get(code=Organization.objects.get(id=organization_id).province))).replace( 'None', '') userallinfo.append(o) return render(request, 'backstage/backstage-app-inform-management.html', {'userallinfo': userallinfo, 'userpaginator': userpaginator, 'user_count': user_count}) def backstage_app_inform_delete(request,pk): user = Userprofile.objects.get(user_id=pk) user.message_status = 0 user.save() messages.success(request,'取消成功') return HttpResponseRedirect('/backstage/backstage/app/inform/manage/')