diff --git a/backstage/urls.py b/backstage/urls.py index b24cb62..9f01967 100644 --- a/backstage/urls.py +++ b/backstage/urls.py @@ -9,6 +9,7 @@ urlpatterns = [ path('backstage/comment/',views.backstage_comment,name='backstage-comment'), path('backstage/user/',views.backstage_user,name='backstage-user'), path('backstage/organization/update/',views.backstage_organization_update,name='backstage-organization_update'), + path('backstage/data/download/',views.backstage_data_download,name='backstage-data-download'), path('backstage/weixin/update/',views.backstage_weixin_update,name='backstage-weixin_update'), path('backstage/douyin/update/',views.backstage_douyin_update,name='backstage-douyin_update'), path('backstage/weibo/update/',views.backstage_weibo_update,name='backstage-weibo_update'), diff --git a/backstage/views.py b/backstage/views.py index bc55ca4..bd44975 100644 --- a/backstage/views.py +++ b/backstage/views.py @@ -6,7 +6,7 @@ from datetime import datetime import xlrd from django.contrib import messages -from django.http import HttpResponse, HttpResponseRedirect +from django.http import HttpResponse, HttpResponseRedirect, FileResponse from django.shortcuts import render # Create your views here. @@ -444,3 +444,95 @@ def add_unqualified_media(request): print(media_id) 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) + # print(results) + t = int(time.time()) + os.popen('mkdir /var/www/p3/newmediamonitoring/static/upload/%s' % (t)) + f = '/var/www/p3/newmediamonitoring/static/upload/%s' % (t) + 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: + # print(r['type']) + 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' + return response + + + diff --git a/dashboard/templates/dashboard/base/left.html b/dashboard/templates/dashboard/base/left.html index ec452bf..dd5dedd 100644 --- a/dashboard/templates/dashboard/base/left.html +++ b/dashboard/templates/dashboard/base/left.html @@ -191,8 +191,8 @@ href="{% url 'backstage-comment' %}">评论