Skip to content

Commit

Permalink
增加导出功能。
Browse files Browse the repository at this point in the history
  • Loading branch information
Epsirom committed May 26, 2014
1 parent 99c16e9 commit 9a0cfb7
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 9 deletions.
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
django==1.6.1
MySQL-python==1.2.4
MySQL-python==1.2.4
xlwt==0.7.5
5 changes: 4 additions & 1 deletion urlhandler/adminpage/static/js/activity_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ var tdMap = {
return true;
}
},
'export': function(act) {
return true;
},
'detail': function(act) {
return true;
}
Expand Down Expand Up @@ -164,7 +167,7 @@ var tdMap = {
return '<span id="del'+act[key]+'" class="td-ban glyphicon glyphicon-ban-circle" ></span>';
}
else{
return '<a href="#" id="'+act[key]+'" onclick="deleteact('+act[key]+')"><span class="glyphicon glyphicon-trash"></span></a>';
return '<a href="javascript:void(0);" id="'+act[key]+'" onclick="deleteact('+act[key]+')"><span class="glyphicon glyphicon-trash"></span></a>';
}
}
}, smartTimeMap = {
Expand Down
2 changes: 1 addition & 1 deletion urlhandler/adminpage/templates/activity_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,8 @@
<h1>处理结果</h1>
<pre id="resultHolder"></pre>
<div class="col-sm-offset-2">
<a class="btn btn-success" href="{% url "adminpage.views.activity_list" %}">返回列表</a>
<button class="btn btn-info" id="continueBtn">继续修改</button>
<a class="btn btn-success" href="{% url "adminpage.views.activity_list" %}">返回列表</a>
</div>
</div>

Expand Down
8 changes: 4 additions & 4 deletions urlhandler/adminpage/templates/activity_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@
'end_time': new Date('{{ activity.end_time|date:"r" }}'),
'place': '{{ activity.place|escapejs }}',
{% if permission %}'delete':{{ activity.id }},{% endif %}
'operations': {{% if permission %}'detail':'{% url "adminpage.views.activity_detail" actid=activity.id %}',{% endif %}'checkin':'{% url "adminpage.views.activity_checkin" actid=activity.id %}'},
'operations': {{% if permission %}'detail':'{% url "adminpage.views.activity_detail" actid=activity.id %}','export':'{% url "adminpage.views.activity_export_stunum" actid=activity.id %}', {% endif %}'checkin':'{% url "adminpage.views.activity_checkin" actid=activity.id %}'},
'book_start': new Date('{{ activity.book_start|date:"r" }}'),
'book_end': new Date('{{ activity.book_end|date:"r" }}')
}{% if forloop.last %}{% else %},{% endif %}
{% endfor %}
];
var operations_name = {{% if permission %}'detail':'详情',{% endif %}'checkin':'检票'};
var operations_icon = {{% if permission %}'detail':'pencil',{% endif %}'checkin':'check'};
var operations_target = {{% if permission %}'detail':'',{% endif %}'checkin':'_blank'};
var operations_name = {{% if permission %}'detail':'详情', 'export':'导出', {% endif %}'checkin':'检票'};
var operations_icon = {{% if permission %}'detail':'pencil', 'export':'export', {% endif %}'checkin':'check'};
var operations_target = {{% if permission %}'detail':'', 'export':'_blank', {% endif %}'checkin':'_blank'};
</script>
<script src="{% static "js/activity_list.js" %}"></script>
{% endblock %}
Expand Down
1 change: 1 addition & 0 deletions urlhandler/adminpage/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
url(r'^detail/(?P<actid>\d+)/$', 'adminpage.views.activity_detail'),
url(r'^checkin/(?P<actid>\d+)/$', 'adminpage.views.activity_checkin'),
url(r'^checkin/(?P<actid>\d+)/check/', 'adminpage.views.activity_checkin_post'),
url(r'^export/(?P<actid>\d+)/$', 'adminpage.views.activity_export_stunum'),
url(r'^add/$', 'adminpage.views.activity_add'),
url(r'^delete/$', 'adminpage.views.activity_delete'),
url(r'^modify/$', 'adminpage.views.activity_post'),
Expand Down
40 changes: 40 additions & 0 deletions urlhandler/adminpage/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
from weixinlib.settings import get_custom_menu_with_book_acts, WEIXIN_BOOK_HEADER
from adminpage.safe_reverse import *

import xlwt
import re
from django.utils.http import urlquote
from django.utils.encoding import smart_str



@csrf_protect
def home(request):
Expand Down Expand Up @@ -450,3 +456,37 @@ def custom_menu_modify_post(request):
content_type='application/json')


def activity_export_stunum(request, actid):
if not request.user.is_authenticated():
return HttpResponseRedirect(s_reverse_admin_home())
try:
activity = Activity.objects.get(id=actid)
except:
raise Http404

tickets = Ticket.objects.filter(activity=activity)
wb = xlwt.Workbook()

def write_row(ws, row, data):
for index, cell in enumerate(data):
ws.write(row, index, cell)

ws = wb.add_sheet(activity.name)
row = 1
write_row(ws, 0, [u'学号', u'状态', u'座位'])
statusMap = [u'已取消', u'未入场', u'已入场']
for ticket in tickets:
write_row(ws, row, [ticket.stu_id, statusMap[ticket.status], ticket.seat])
row = row + 1
##########################################定义Content-Disposition,让浏览器能识别,弹出下载框
fname = 'activity' + actid + '.xls'
agent=request.META.get('HTTP_USER_AGENT')
if agent and re.search('MSIE',agent):
response = HttpResponse(content_type="application/vnd.ms-excel") # 解决ie不能下载的问题
response['Content-Disposition'] = 'attachment; filename=%s' % urlquote(fname) # 解决文件名乱码/不显示的问题
else:
response = HttpResponse(content_type="application/ms-excel") # 解决ie不能下载的问题
response['Content-Disposition'] = 'attachment; filename=%s' % smart_str(fname) # 解决文件名乱码/不显示的问题
##########################################保存
wb.save(response)
return response
2 changes: 1 addition & 1 deletion urlhandler/urlhandler/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
'ENGINE': 'django.db.backends.mysql',
'NAME': 'tsinghuatuan',
'USER': 'root',
'PASSWORD': 'root',
'PASSWORD': '',
'HOST': '127.0.0.1',
'PORT': '3306',
}
Expand Down
2 changes: 1 addition & 1 deletion urlhandler/userpage/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def ticket_view(request, uid):
if act_endtime < now:#表示活动已经结束
ticket_status = 3
ticket_seat = ticket[0].seat
act_photo = "http://tsinghuaqr.duapp.com/fit/"+uid
act_photo = "http://qr.ssast.org/fit/"+uid
variables=RequestContext(request,{'act_id':act_id, 'act_name':act_name,'act_place':act_place, 'act_begintime':act_begintime,
'act_endtime':act_endtime,'act_photo':act_photo, 'ticket_status':ticket_status,
'ticket_seat':ticket_seat,
Expand Down

0 comments on commit 9a0cfb7

Please sign in to comment.