FileController.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import base64
  4. import json
  5. import logging
  6. import os
  7. import time
  8. import zipfile
  9. import xlwt
  10. from django.http import StreamingHttpResponse, HttpResponse, QueryDict
  11. from django.utils.decorators import method_decorator
  12. from django.views import View
  13. from django.views.decorators.csrf import csrf_exempt
  14. from django.db import transaction
  15. from AnsjerUIDManage.config import BASE_DIR
  16. from Model.models import UIDModel, UserUIDModel, UserModel, LogModel, MacModel, OrderTaskModel, OrderUIDModel, \
  17. SerialNumberModel, OrderSerialNumberModel
  18. from Object.RedisObject import RedisObject
  19. from Object.TokenObject import TokenObject
  20. from Object.ResponseObject import ResponseObject
  21. from Service.CommonService import CommonService
  22. from bulk_update.helper import bulk_update
  23. class UploadUIDFileView(View):
  24. @method_decorator(csrf_exempt)
  25. def dispatch(self, request, *args, **kwargs):
  26. return super(UploadUIDFileView, self).dispatch(request, *args, **kwargs)
  27. def get(self, request, *args, **kwargs):
  28. request.encoding = 'utf-8'
  29. request_dict = request.GET
  30. fileName = request.FILES.get('fileName', None)
  31. return self.validate(fileName, request_dict, request)
  32. def post(self, request, *args, **kwargs):
  33. request.encoding = 'utf-8'
  34. request_dict = request.POST
  35. fileName = request.FILES.get('fileName', None)
  36. return self.validate(fileName, request_dict, request)
  37. def validate(self, fileName, request_dict, request):
  38. token = request_dict.get('token', None)
  39. area = request_dict.get('area', None)
  40. isReset = request_dict.get('isReset', 0)
  41. content = request_dict.get('fileName', None)
  42. print('content')
  43. print(content)
  44. content = base64.b64decode(content).decode().strip()
  45. content = content[3:(len(content) - 3)]
  46. uids = content.split('\n')
  47. # print(uids)
  48. # print(len(uids))
  49. response = ResponseObject()
  50. tko = TokenObject(token)
  51. if tko.code != 0:
  52. return response.json(tko.code)
  53. # if not fileName and not area:
  54. # return response.json(444, 'fileName,area')
  55. data = {}
  56. duplicate = []
  57. for line in uids:
  58. if len(line) < 20:
  59. continue
  60. if data.__contains__(line):
  61. duplicate.append(line)
  62. else:
  63. data[line] = ''
  64. if isReset == 0:
  65. return self.do_insert(data, duplicate, area, response, request, tko)
  66. else:
  67. return self.do_reset(data, response, area, request, tko)
  68. @transaction.atomic
  69. def do_insert(self, data, duplicate, area, response, request, token):
  70. bulk = []
  71. count = 0
  72. add_time = int(time.time())
  73. update_time = int(time.time())
  74. keys = data.keys()
  75. # 获取最新的mac
  76. mac = MacModel.objects.filter().values('id', 'value', 'is_active')[0]
  77. if not mac['is_active']:
  78. return response.json(175)
  79. # redisObject = RedisObject(db=3)
  80. key = ''
  81. tmpMac = mac['value']
  82. savePoint = None
  83. for item in keys:
  84. key = item.strip()
  85. # value = redisObject.get_data(key)
  86. # if value is False:
  87. # # redisObject.set_data(key, '1', 600)
  88. # else:
  89. # duplicate.append(key)
  90. # continue
  91. bulk.append(UIDModel(
  92. uid=item.strip(),
  93. uid_extra='',
  94. status=0,
  95. add_time=add_time,
  96. update_time=update_time,
  97. area=area,
  98. mac=mac['value']
  99. ))
  100. try:
  101. try:
  102. if (count % 5000) == 0:
  103. savePoint = transaction.savepoint()
  104. UIDModel.objects.bulk_create(bulk)
  105. bulk.clear()
  106. data = {
  107. 'value': mac['value'],
  108. 'is_active': tmpMac is not None
  109. }
  110. MacModel.objects.filter().update(**data)
  111. except Exception as e:
  112. # print('--------------------------error 5000')
  113. # print(repr(e))
  114. if savePoint:
  115. transaction.rollback(savePoint)
  116. djangoLogger = logging.getLogger('django')
  117. djangoLogger.exception(repr(e))
  118. return response.json(174, str(e))
  119. else:
  120. savePoint = None
  121. except Exception as e:
  122. # print('--------------------------error 5001')
  123. # print(repr(e))
  124. return response.json(174, str(e))
  125. count += 1
  126. tmpMac = CommonService.updateMac(mac['value'])
  127. if tmpMac is None:
  128. # 能分配的mac已用完
  129. break
  130. else:
  131. mac['value'] = tmpMac
  132. # 当bulk不足5000时,还有数据要插入
  133. try:
  134. try:
  135. savePoint = transaction.savepoint() # 事务保存点
  136. if len(bulk) > 0:
  137. UIDModel.objects.bulk_create(bulk)
  138. bulk.clear()
  139. except Exception as e:
  140. # print('--------------------------error')
  141. # print(repr(e))
  142. if savePoint:
  143. transaction.rollback(savePoint)
  144. return response.json(174)
  145. else:
  146. del data
  147. del bulk
  148. data = {
  149. 'value': mac['value'],
  150. 'is_active': tmpMac is not None
  151. }
  152. MacModel.objects.filter().update(**data)
  153. except Exception as e:
  154. # print('--------------------------error 1111')
  155. # print(repr(e))
  156. return response.json(174)
  157. # print('重复:')
  158. # print(duplicate)
  159. operation = self.formatOperation(operation='上传', quantity=int(count), area=int(area))
  160. print(operation)
  161. self.add_log(request, operation, token)
  162. if tmpMac is None:
  163. return response.json(175, {'last_uid': key})
  164. return response.json(0, {'count': count, 'duplicate_count': len(duplicate), 'data': duplicate})
  165. def do_reset(self, data, response, area, request, token):
  166. keys = data.keys()
  167. uids = []
  168. count = 0
  169. for key in keys:
  170. uids.append(key.strip())
  171. if len(uids) % 5000 == 0:
  172. count += self.do_update_uid_status(uids, area)
  173. uids.clear()
  174. if len(uids) > 0:
  175. count += self.do_update_uid_status(uids, area)
  176. uids.clear()
  177. operation = self.formatOperation('重置', int(count), int(area))
  178. self.add_log(request, operation, token)
  179. return response.json(0)
  180. def do_update_uid_status(self, uids, area):
  181. uid_qs = UIDModel.objects.filter(uid__in=uids, area=area, status=2)
  182. if uid_qs.exists():
  183. uid_ids = []
  184. for uid in uid_qs:
  185. if uid.status == 2:
  186. uid.status = 1
  187. uid_ids.append(uid.id)
  188. UIDModel.objects.bulk_update(uid_qs, fields=['status'])
  189. try:
  190. OrderUIDModel.objects.filter(uid__id__in=tuple(uid_ids)).delete()
  191. except Exception as e:
  192. print(e)
  193. return uid_qs.count()
  194. return 0
  195. def add_log(self, request, operation, token):
  196. ip = CommonService.get_ip_address(request)
  197. now_time = time.time()
  198. content = json.loads(json.dumps(request.POST))
  199. user_qs = UserModel.objects.filter(id=token.userID)
  200. if content.__contains__('fileName'):
  201. del content['fileName']
  202. log = {
  203. 'status': 200,
  204. 'content': json.dumps(content),
  205. 'ip': ip,
  206. 'time': now_time,
  207. 'url': 'upload',
  208. 'operation': operation,
  209. 'user': user_qs[0]
  210. }
  211. try:
  212. LogModel.objects.create(**log)
  213. except Exception as e:
  214. print('出错')
  215. print(repr(e))
  216. def formatOperation(self, operation, quantity, area):
  217. str = '{operation}{quantity}个{area}UID'
  218. if area == 0:
  219. return str.format(operation=operation, quantity=quantity, area='国内')
  220. else:
  221. return str.format(operation=operation, quantity=quantity, area='国外')
  222. class DownloadUIDFileView(View):
  223. @method_decorator(csrf_exempt)
  224. def dispatch(self, request, *args, **kwargs):
  225. return super(DownloadUIDFileView, self).dispatch(request, *args, **kwargs)
  226. def get(self, request, *args, **kwargs):
  227. request.encoding = 'utf-8'
  228. request_dict = request.GET
  229. return self.validate(request_dict)
  230. def post(self, request, *args, **kwargs):
  231. request.encoding = 'utf-8'
  232. request_dict = request.POST
  233. return self.validate(request_dict)
  234. def validate(self, request_dict):
  235. token = request_dict.get('token', None)
  236. area = request_dict.get('area', None)
  237. quantity = int(request_dict.get('quantity', None))
  238. fileType = request_dict.get('fileType', None)
  239. order_number = request_dict.get('order_number', None)
  240. board = request_dict.get('board', None)
  241. plan = request_dict.get('plan', None)
  242. checksum = request_dict.get('checksum', None)
  243. ic_model = request_dict.get('ic_model', None)
  244. order_quantity = request_dict.get('order_quantity', None)
  245. response = ResponseObject()
  246. # print(area)
  247. # print(quantity)
  248. token = TokenObject(token)
  249. if token.code != 0:
  250. return response.json(token.code)
  251. if not area or not order_number or not board or not plan or not checksum or not ic_model or not order_quantity or not fileType:
  252. return response.json(444)
  253. area = int(area)
  254. if area >= 0 and quantity > 0:
  255. # 保存订单信息
  256. now_time = int(time.time())
  257. order = {
  258. 'order_number': order_number,
  259. 'board': board,
  260. 'plan': plan,
  261. 'checksum': checksum,
  262. 'ic_model': ic_model,
  263. 'quantity': order_quantity,
  264. 'add_time': now_time
  265. }
  266. tmp = OrderTaskModel.objects.create(**order)
  267. print(tmp)
  268. order = OrderTaskModel.objects.filter(order_number=order_number).order_by('-add_time')[0]
  269. uid_qs = UserUIDModel.objects.filter(user__id=token.userID, uid__status=1, uid__area=area)
  270. uid_values = uid_qs.values('uid__id', 'uid__uid', 'uid__mac', 'uid__uid_extra', 'uid__add_time', 'uid__update_time', 'uid__area')
  271. count = uid_values.count()
  272. if count < quantity:
  273. return response.json(444, '设备UID不足')
  274. if uid_values.exists():
  275. uid_values = uid_values[0:quantity]
  276. uid_qs = uid_qs[0: quantity]
  277. if fileType == 'txt':
  278. # return self.download_txt(uid_values, uid_qs, order)
  279. # return self.download_excel(uid_values, order)
  280. return self.download_zip(uid_values, order)
  281. elif fileType == 'excel':
  282. return self.download_excel(uid_values, order)
  283. else:
  284. return response.json(444, 'fileType')
  285. else:
  286. return response.json(444, '111')
  287. else:
  288. return response.json(444, '222')
  289. def download_txt(self, uid_values, uid_qs, order):
  290. updates = []
  291. updates_uid = []
  292. content = ''
  293. now_time = int(time.time())
  294. for i in range(len(uid_values)):
  295. # print(item)
  296. item = uid_values[i]
  297. mac: str = item['uid__mac']
  298. index = mac.rfind(':')
  299. tmp = mac[0:index] + '\t' + mac[index:]
  300. content += tmp + '\t'
  301. content += item['uid__uid'].strip()
  302. content += '\r\n'
  303. uidModel = UIDModel(
  304. id=item['uid__id'],
  305. uid=item['uid__uid'],
  306. mac=item['uid__mac'],
  307. uid_extra=item['uid__uid_extra'],
  308. status=2,
  309. add_time=item['uid__add_time'],
  310. update_time=now_time,
  311. area=item['uid__area']
  312. )
  313. updates.append(uidModel)
  314. order_uid = OrderUIDModel(uid=uidModel, order=order, add_time=now_time, update_time=now_time)
  315. updates_uid.append(order_uid)
  316. if len(updates) % 5000 == 0:
  317. UIDModel.objects.bulk_update(updates, fields=["status"])
  318. OrderUIDModel.objects.bulk_create(updates_uid)
  319. updates.clear()
  320. updates_uid.clear()
  321. # print(item['uid__uid'])
  322. if len(updates) > 0:
  323. UIDModel.objects.bulk_update(updates, fields=["status"])
  324. OrderUIDModel.objects.bulk_create(updates_uid)
  325. updates.clear()
  326. updates_uid.clear()
  327. del updates
  328. del updates_uid
  329. content = content[0:len(content) - 1]
  330. response = StreamingHttpResponse(content)
  331. response['Content-Type'] = 'application/octet-stream'
  332. response['Content-Disposition'] = 'attachment;filename=UID'+time.strftime('-%Y-%m-%d-%H-%M-%S', time.localtime()) + '.txt'
  333. return response
  334. def download_excel(self, uid_qs, order):
  335. response = HttpResponse(content_type='application/vnd.ms-excel')
  336. response['Content-Disposition'] = 'attachment; filename=UID' + time.strftime('-%Y-%m-%d-%H-%M-%S',
  337. time.localtime()) + '.xls'
  338. workbook = xlwt.Workbook(encoding='utf-8')
  339. sheet1 = workbook.add_sheet('UID')
  340. # row1 = [u'设备UID']
  341. # for i in range(0, len(row1)):
  342. # sheet1.write(0, i, row1[i])
  343. num = 1
  344. updates = []
  345. updates_uid = []
  346. now_time = int(time.time())
  347. for item in uid_qs:
  348. uid = item['uid__uid']
  349. mac = item['uid__mac']
  350. index = mac.rfind(':')
  351. sheet1.write(num, 0, mac[0:index])
  352. sheet1.write(num, 1, mac[index:])
  353. sheet1.write(num, 2, uid)
  354. num += 1
  355. uidModel = UIDModel(
  356. id=item['uid__id'],
  357. uid=item['uid__uid'],
  358. mac=item['uid__mac'],
  359. uid_extra=item['uid__uid_extra'],
  360. status=2,
  361. add_time=item['uid__add_time'],
  362. update_time=now_time,
  363. area=item['uid__area']
  364. )
  365. updates.append(uidModel)
  366. order_uid = OrderUIDModel(uid=uidModel, order=order, add_time=now_time, update_time=now_time)
  367. updates_uid.append(order_uid)
  368. if len(updates) % 5000 == 0:
  369. UIDModel.objects.bulk_update(updates, fields=["status"])
  370. OrderUIDModel.objects.bulk_create(updates_uid)
  371. updates.clear()
  372. updates_uid.clear()
  373. # print(item['uid__uid'])
  374. if len(updates) > 0:
  375. UIDModel.objects.bulk_update(updates, fields=["status"])
  376. OrderUIDModel.objects.bulk_create(updates_uid)
  377. updates.clear()
  378. updates_uid.clear()
  379. UIDModel.objects.bulk_update(updates, fields=["status"])
  380. workbook.save(response)
  381. return response
  382. def download_zip(self, uid_qs, order):
  383. dir_name = 'static/' + time.strftime('%Y_%m_%d_%H_%M_%S', time.localtime())
  384. path = '/'.join((BASE_DIR, dir_name)).replace('\\', '/') + '/'
  385. if not os.path.exists(path):
  386. os.mkdir(path)
  387. filename = time.strftime('_%Y_%m_%d_%H_%M_%S', time.localtime())
  388. txt_filename = 'UID' + filename + '.txt'
  389. excel_filename = 'UID' + filename + '.xls'
  390. txt_file = open(path + txt_filename, 'w+')
  391. workbook = xlwt.Workbook(encoding='utf-8')
  392. sheet1 = workbook.add_sheet('UID')
  393. num = 1
  394. updates = []
  395. updates_uid = []
  396. content = ''
  397. now_time = int(time.time())
  398. for i in range(len(uid_qs)):
  399. # print(item)
  400. item = uid_qs[i]
  401. uid = item['uid__uid']
  402. mac = item['uid__mac']
  403. index = mac.rfind(':')
  404. tmp = mac[0:index] + '\t' + mac[index:]
  405. content += tmp + '\t'
  406. content += item['uid__uid'].strip()
  407. content += '\r\n'
  408. sheet1.write(num, 0, mac[0:index])
  409. sheet1.write(num, 1, mac[index:])
  410. sheet1.write(num, 2, uid)
  411. num += 1
  412. uidModel = UIDModel(
  413. id=item['uid__id'],
  414. uid=item['uid__uid'],
  415. mac=item['uid__mac'],
  416. uid_extra=item['uid__uid_extra'],
  417. status=2,
  418. add_time=item['uid__add_time'],
  419. update_time=now_time,
  420. area=item['uid__area']
  421. )
  422. updates.append(uidModel)
  423. order_uid = OrderUIDModel(uid=uidModel, order=order, add_time=now_time, update_time=now_time)
  424. updates_uid.append(order_uid)
  425. if len(updates) % 5000 == 0:
  426. UIDModel.objects.bulk_update(updates, fields=["status"])
  427. OrderUIDModel.objects.bulk_create(updates_uid)
  428. updates.clear()
  429. updates_uid.clear()
  430. # print(item['uid__uid'])
  431. if len(updates) > 0:
  432. UIDModel.objects.bulk_update(updates, fields=["status"])
  433. OrderUIDModel.objects.bulk_create(updates_uid)
  434. updates.clear()
  435. updates_uid.clear()
  436. del updates
  437. del updates_uid
  438. content = content[0:len(content) - 1]
  439. txt_file.write(content)
  440. txt_file.close()
  441. workbook.save(path + excel_filename)
  442. zip_name = path[0:path.rfind('/')] + '.zip'
  443. return self.get_zip(path, os.listdir(path), zip_name)
  444. def get_zip(self, path, files, zip_name):
  445. zp = zipfile.ZipFile(zip_name, 'w', zipfile.ZIP_DEFLATED)
  446. for file in files:
  447. zp.write(filename=(path + file), arcname=str(file))
  448. zp.close()
  449. response = StreamingHttpResponse(open(zip_name, 'rb'))
  450. response['content_type'] = "application/octet-stream"
  451. response['Content-Disposition'] = 'attachment; filename=UID' + time.strftime('_%Y_%m_%d_%H_%M_%S',
  452. time.localtime()) + '.zip'
  453. return response
  454. class DownloadSerialNumberFileView(View):
  455. @method_decorator(csrf_exempt)
  456. def dispatch(self, request, *args, **kwargs):
  457. return super(DownloadSerialNumberFileView, self).dispatch(request, *args, **kwargs)
  458. def get(self, request, *args, **kwargs):
  459. request.encoding = 'utf-8'
  460. request_dict = request.GET
  461. return self.validate(request_dict)
  462. def post(self, request, *args, **kwargs):
  463. request.encoding = 'utf-8'
  464. request_dict = request.POST
  465. return self.validate(request_dict)
  466. def validate(self, request_dict):
  467. token = request_dict.get('token', None)
  468. area = request_dict.get('area', None)
  469. quantity = int(request_dict.get('quantity', None))
  470. fileType = request_dict.get('fileType', None)
  471. order_number = request_dict.get('order_number', None)
  472. board = request_dict.get('board', None)
  473. plan = request_dict.get('plan', None)
  474. checksum = request_dict.get('checksum', None)
  475. ic_model = request_dict.get('ic_model', None)
  476. order_quantity = request_dict.get('order_quantity', None)
  477. response = ResponseObject()
  478. # print(area)
  479. # print(quantity)
  480. token = TokenObject(token)
  481. if token.code != 0:
  482. return response.json(token.code)
  483. if not area or not order_number or not board or not plan or not checksum or not ic_model or not order_quantity or not fileType:
  484. return response.json(444)
  485. area = int(area)
  486. if area >= 0 and quantity > 0:
  487. # 保存订单信息
  488. now_time = int(time.time())
  489. order = {
  490. 'order_number': order_number,
  491. 'board': board,
  492. 'plan': plan,
  493. 'checksum': checksum,
  494. 'ic_model': ic_model,
  495. 'quantity': order_quantity,
  496. 'add_time': now_time
  497. }
  498. tmp = OrderTaskModel.objects.create(**order)
  499. print(tmp)
  500. order = OrderTaskModel.objects.filter(order_number=order_number).order_by('-add_time')[0]
  501. sn_qs = SerialNumberModel.objects.filter(use_status=0, p2p=area)
  502. count = sn_qs.count()
  503. if count < quantity:
  504. return response.json(444, '序列号不足')
  505. if sn_qs.exists():
  506. sn_qs = sn_qs[0: quantity]
  507. if fileType == 'txt':
  508. # return self.download_txt(uid_values, uid_qs, order)
  509. # return self.download_excel(uid_values, order)
  510. return self.download_zip(sn_qs, order)
  511. else:
  512. return response.json(444, 'fileType')
  513. else:
  514. return response.json(444, '111')
  515. else:
  516. return response.json(444, '222')
  517. def download_zip(self, sn_qs, order):
  518. dir_name = 'static/' + time.strftime('%Y_%m_%d_%H_%M_%S', time.localtime())
  519. path = '/'.join((BASE_DIR, dir_name)).replace('\\', '/') + '/'
  520. if not os.path.exists(path):
  521. os.mkdir(path)
  522. filename = time.strftime('_%Y_%m_%d_%H_%M_%S', time.localtime())
  523. txt_filename = 'SerialNumber' + filename + '.txt'
  524. excel_filename = 'SerialNumber' + filename + '.xls'
  525. txt_file = open(path + txt_filename, 'w+')
  526. workbook = xlwt.Workbook(encoding='utf-8')
  527. sheet1 = workbook.add_sheet('SerialNumber')
  528. num = 0
  529. updates = []
  530. updates_serial_number = []
  531. content = ''
  532. now_time = int(time.time())
  533. for i in range(len(sn_qs)):
  534. # print(item)
  535. item = sn_qs[i]
  536. serial_number = item.serial_number
  537. sheet1.write(num, 0, serial_number)
  538. num += 1
  539. content += serial_number.strip()
  540. content += '\n'
  541. serialNumberModel = SerialNumberModel(
  542. id=item.id,
  543. serial_number = item.serial_number,
  544. status=item.status,
  545. use_status=1,
  546. p2p=item.p2p,
  547. add_time=item.add_time
  548. )
  549. updates.append(serialNumberModel)
  550. order_serial_number = OrderSerialNumberModel(serial_number=item, order=order, add_time=now_time, update_time=now_time)
  551. updates_serial_number.append(order_serial_number)
  552. if len(updates) % 5000 == 0:
  553. bulk_update(updates)
  554. OrderSerialNumberModel.objects.bulk_create(updates_serial_number)
  555. updates.clear()
  556. updates_serial_number.clear()
  557. # print(item['uid__uid'])
  558. if len(updates) > 0:
  559. bulk_update(updates)
  560. OrderSerialNumberModel.objects.bulk_create(updates_serial_number)
  561. updates.clear()
  562. updates_serial_number.clear()
  563. del updates
  564. del updates_serial_number
  565. content = content[0:len(content) - 1]
  566. txt_file.write(content)
  567. txt_file.close()
  568. workbook.save(path + excel_filename)
  569. zip_name = path[0:path.rfind('/')] + '.zip'
  570. return self.get_zip(path, os.listdir(path), zip_name)
  571. def get_zip(self, path, files, zip_name):
  572. zp = zipfile.ZipFile(zip_name, 'w', zipfile.ZIP_DEFLATED)
  573. for file in files:
  574. zp.write(filename=(path + file), arcname=str(file))
  575. zp.close()
  576. response = StreamingHttpResponse(open(zip_name, 'rb'))
  577. response['content_type'] = "application/octet-stream"
  578. response['Content-Disposition'] = 'attachment; filename=SerialNumber' + time.strftime('_%Y_%m_%d_%H_%M_%S',
  579. time.localtime()) + '.zip'
  580. return response