Procházet zdrojové kódy

添加zip包下载功能

tanghongbin před 5 roky
rodič
revize
d03be011d4
3 změnil soubory, kde provedl 134 přidání a 17 odebrání
  1. 3 1
      AnsjerUIDManage/settings.py
  2. 130 16
      Controller/FileController.py
  3. 1 0
      Model/models.py

+ 3 - 1
AnsjerUIDManage/settings.py

@@ -13,13 +13,15 @@ https://docs.djangoproject.com/en/3.0/ref/settings/
 import os
 
 # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
-BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
+# BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
 
 
 # Quick-start development settings - unsuitable for production
 # See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/
 
 # SECURITY WARNING: keep the secret key used in production secret!
+from AnsjerUIDManage.config import BASE_DIR
+
 SECRET_KEY = 'k#i*q_^=nhr^63ytqenfg!ecqizq60*6_x@sp_6+%u^b24%+fj'
 
 # SECURITY WARNING: don't run with debug turned on in production!

+ 130 - 16
Controller/FileController.py

@@ -2,7 +2,10 @@
 # -*- coding: utf-8 -*-
 import base64
 import json
+import os
 import time
+import zipfile
+
 import xlwt
 
 from django.http import StreamingHttpResponse, HttpResponse, QueryDict
@@ -11,6 +14,7 @@ from django.views import View
 from django.views.decorators.csrf import csrf_exempt
 from django.db import transaction
 
+from AnsjerUIDManage.config import BASE_DIR
 from Model.models import UIDModel, UserUIDModel, UserModel, LogModel, MacModel, OrderTaskModel, OrderUIDModel
 from Object.RedisObject import RedisObject
 from Object.TokenObject import TokenObject
@@ -373,32 +377,142 @@ class DownloadUIDFileView(View):
         response['Content-Disposition'] = 'attachment;filename=UID'+time.strftime('-%Y-%m-%d-%H-%M-%S', time.localtime()) + '.txt'
         return response
 
-    def download_excel(self, uid_qs):
+    def download_excel(self, uid_qs, order):
         response = HttpResponse(content_type='application/vnd.ms-excel')
-        response['Content-Disposition'] = 'attachment; filename=UID' + time.strftime('-%Y-%m-%d-%H-%M-%S', time.localtime()) + '.xls'
+        response['Content-Disposition'] = 'attachment; filename=UID' + time.strftime('-%Y-%m-%d-%H-%M-%S',
+                                                                                     time.localtime()) + '.xls'
         workbook = xlwt.Workbook(encoding='utf-8')
         sheet1 = workbook.add_sheet('UID')
-        row1 = [u'设备UID']
-        for i in range(0, len(row1)):
-            sheet1.write(0, i, row1[i])
+        # row1 = [u'设备UID']
+        # for i in range(0, len(row1)):
+        #     sheet1.write(0, i, row1[i])
 
         num = 1
         updates = []
+        updates_uid = []
+        now_time = int(time.time())
         for item in uid_qs:
-            uid = item['uid']
-            sheet1.write(num, 0, uid)
+            uid = item['uid__uid']
+            mac = item['uid__mac']
+            index = mac.rfind(':')
+            sheet1.write(num, 0, mac[0:index])
+            sheet1.write(num, 1, mac[index:])
+            sheet1.write(num, 2, uid)
             num += 1
-            updates.append(UIDModel(
-                id=item['id'],
-                uid=item['uid'],
-                uid_extra=item['uid_extra'],
-                status=1,
-                add_time=item['add_time'],
-                update_time=item['update_time'],
-                area=item['area']
-            ))
+            uidModel = UIDModel(
+                id=item['uid__id'],
+                uid=item['uid__uid'],
+                mac=item['uid__mac'],
+                uid_extra=item['uid__uid_extra'],
+                status=2,
+                add_time=item['uid__add_time'],
+                update_time=now_time,
+                area=item['uid__area']
+            )
+            updates.append(uidModel)
+
+            order_uid = OrderUIDModel(uid=uidModel, order=order, add_time=now_time, update_time=now_time)
+            updates_uid.append(order_uid)
+
+            if len(updates) % 5000 == 0:
+                UIDModel.objects.bulk_update(updates, fields=["status"])
+                OrderUIDModel.objects.bulk_create(updates_uid)
+                updates.clear()
+                updates_uid.clear()
+            # print(item['uid__uid'])
+
+        if len(updates) > 0:
+            UIDModel.objects.bulk_update(updates, fields=["status"])
+            OrderUIDModel.objects.bulk_create(updates_uid)
+            updates.clear()
+            updates_uid.clear()
 
         UIDModel.objects.bulk_update(updates, fields=["status"])
         workbook.save(response)
         return response
 
+    def download_zip(self, uid_qs, order):
+        dir_name = 'static/' + time.strftime('%Y_%m_%d_%H_%M_%S', time.localtime())
+        path = '/'.join((BASE_DIR, dir_name)).replace('\\', '/') + '/'
+        if not os.path.exists(path):
+            os.mkdir(path)
+
+        filename = time.strftime('_%Y_%m_%d_%H_%M_%S', time.localtime())
+        txt_filename = 'UID' + filename + '.txt'
+        excel_filename = 'UID' + filename + '.xls'
+
+        txt_file = open(path + txt_filename, 'w+')
+        workbook = xlwt.Workbook(encoding='utf-8')
+        sheet1 = workbook.add_sheet('UID')
+        num = 1
+
+        updates = []
+        updates_uid = []
+        content = ''
+        now_time = int(time.time())
+        for i in range(len(uid_qs)):
+            # print(item)
+            item = uid_qs[i]
+            uid = item['uid__uid']
+            mac = item['uid__mac']
+            index = mac.rfind(':')
+            tmp = mac[0:index] + ' ' + mac[index:]
+            content += tmp + ' '
+            content += item['uid__uid']
+            content += '\n'
+
+            sheet1.write(num, 0, mac[0:index])
+            sheet1.write(num, 1, mac[index:])
+            sheet1.write(num, 2, uid)
+            num += 1
+
+            uidModel = UIDModel(
+                id=item['uid__id'],
+                uid=item['uid__uid'],
+                mac=item['uid__mac'],
+                uid_extra=item['uid__uid_extra'],
+                status=2,
+                add_time=item['uid__add_time'],
+                update_time=now_time,
+                area=item['uid__area']
+            )
+            updates.append(uidModel)
+
+            order_uid = OrderUIDModel(uid=uidModel, order=order, add_time=now_time, update_time=now_time)
+            updates_uid.append(order_uid)
+
+            if len(updates) % 5000 == 0:
+                UIDModel.objects.bulk_update(updates, fields=["status"])
+                OrderUIDModel.objects.bulk_create(updates_uid)
+                updates.clear()
+                updates_uid.clear()
+            # print(item['uid__uid'])
+
+        if len(updates) > 0:
+            UIDModel.objects.bulk_update(updates, fields=["status"])
+            OrderUIDModel.objects.bulk_create(updates_uid)
+            updates.clear()
+            updates_uid.clear()
+
+        del updates
+        del updates_uid
+        content = content[0:len(content) - 1]
+        txt_file.write(content)
+        txt_file.close()
+
+        workbook.save(path + excel_filename)
+
+        zip_name = path[0:path.rfind('/')] + '.zip'
+        return self.get_zip(path, os.listdir(path), zip_name)
+
+    def get_zip(self, path, files, zip_name):
+        zp = zipfile.ZipFile(zip_name, 'w', zipfile.ZIP_DEFLATED)
+        for file in files:
+            zp.write(filename=(path + file), arcname=str(file))
+        zp.close()
+
+        response = StreamingHttpResponse(open(zip_name, 'rb'))
+        response['content_type'] = "application/octet-stream"
+        response['Content-Disposition'] = 'attachment; filename=UID' + time.strftime('_%Y_%m_%d_%H_%M_%S',
+                                                                                     time.localtime()) + '.zip'
+        return response

+ 1 - 0
Model/models.py

@@ -110,6 +110,7 @@ class OrderTaskModel(models.Model):
     checksum = models.CharField(max_length=32, verbose_name='校验和')
     ic_model = models.CharField(max_length=32, verbose_name='ic型号')
     quantity = models.IntegerField(default=0, verbose_name='通知单上的生成数量')
+    add_time = models.IntegerField(default=0, verbose_name='添加时间')
 
     class Meta:
         db_table = 'order_task'