|
@@ -1,9 +1,14 @@
|
|
|
#!/usr/bin/env python3
|
|
|
# -*- coding: utf-8 -*-
|
|
|
+import os
|
|
|
+import re
|
|
|
import time
|
|
|
|
|
|
+from django.db import transaction
|
|
|
from django.views import View
|
|
|
+from django.views.decorators.csrf import csrf_exempt
|
|
|
|
|
|
+from Ansjer.config import BASE_DIR
|
|
|
from Model.models import RegionModel, CompanyModel, VPGModel, UIDModel
|
|
|
from Object.uidManageResponseObject import uidManageResponseObject
|
|
|
from Object.TokenObject import TokenObject
|
|
@@ -39,6 +44,8 @@ class VPGView(View):
|
|
|
return self.do_delete(token.userID, request_dict, response)
|
|
|
elif operation == 'list':
|
|
|
return self.do_list(token.userID, request_dict, response)
|
|
|
+ elif operation == 'uid_list':
|
|
|
+ return self.do_uid_list(token.userID, request_dict, response)
|
|
|
else:
|
|
|
return response.json(404)
|
|
|
|
|
@@ -148,10 +155,10 @@ class VPGView(View):
|
|
|
|
|
|
count = vpg_qs.count()
|
|
|
vpg_qs = vpg_qs.values('id', 'name', 'region__name', 'region_id', 'company__name', 'add_time',
|
|
|
- 'update_time', 'company__secret')
|
|
|
- vpg_qs = vpg_qs[start: end]
|
|
|
- for vpg in vpg_qs:
|
|
|
- vpg['uid_count'] = UIDModel.objects.filter(vpg_id=vpg['id']).count()
|
|
|
+ 'update_time', 'company__secret', 'uid_count')
|
|
|
+ vpg_qs = vpg_qs[start:end]
|
|
|
+ # for vpg in vpg_qs:
|
|
|
+ # vpg['uid_count'] = UIDModel.objects.filter(vpg_id=vpg['id']).count()
|
|
|
|
|
|
res = {
|
|
|
'count': count,
|
|
@@ -161,3 +168,71 @@ class VPGView(View):
|
|
|
return response.json(0, res)
|
|
|
else:
|
|
|
return response.json(444)
|
|
|
+
|
|
|
+
|
|
|
+ def do_uid_list(self, userID, request_dict, response):
|
|
|
+ vpg_id = request_dict.get('vpg_id', None)
|
|
|
+ page = request_dict.get('page', None)
|
|
|
+ line = request_dict.get('limit', None)
|
|
|
+
|
|
|
+ if not vpg_id:
|
|
|
+ return response.json(444)
|
|
|
+ uid_qs = UIDModel.objects.filter(vpg_id=vpg_id).values('uid')
|
|
|
+ res = {
|
|
|
+ 'data': list(uid_qs),
|
|
|
+ }
|
|
|
+ return response.json(0, res)
|
|
|
+
|
|
|
+
|
|
|
+@csrf_exempt
|
|
|
+def do_upload_uid(request):
|
|
|
+ # 上传UID,需要request.FILES,单独提取出来
|
|
|
+ # perm = ModelService.check_perm_uid_manage(userID, 0)
|
|
|
+ # if not perm:
|
|
|
+ # return response.json(309)
|
|
|
+
|
|
|
+ request.encoding = 'utf-8'
|
|
|
+ response = uidManageResponseObject()
|
|
|
+ if request.method == "POST":
|
|
|
+ request_dict = request.POST
|
|
|
+ elif request.method == "GET":
|
|
|
+ request_dict = request.GET
|
|
|
+ else:
|
|
|
+ return response.json(444)
|
|
|
+ file = request.FILES.get('file', None)
|
|
|
+ vpg_id = request_dict.get('vpg_id', None)
|
|
|
+
|
|
|
+ bulk = []
|
|
|
+ add_time = update_time = int(time.time())
|
|
|
+ # path = '/'.join((BASE_DIR, 'static/uid')).replace('\\', '/') + '/'
|
|
|
+ # if not os.path.exists(path):
|
|
|
+ # os.makedirs(path)
|
|
|
+ # full_path = path + str(file)
|
|
|
+ # with open(full_path, 'wb+') as uid_file:
|
|
|
+ try:
|
|
|
+ for chunk in file.chunks():
|
|
|
+ # str_chunk = str(chunk)
|
|
|
+ # print('str(chunk):', str_chunk)
|
|
|
+ # str_chunk = re.findall("b\'(.*)\'", str_chunk)[0]
|
|
|
+ # str_chunk = str_chunk.split('\\r\\n')
|
|
|
+ # print('str(chunk):', str_chunk)
|
|
|
+ uid_list = re.findall("b\'(.*)\'", str(chunk))[0].split('\\r\\n')
|
|
|
+ for uid in uid_list:
|
|
|
+ bulk.append(UIDModel(
|
|
|
+ uid=uid,
|
|
|
+ mac='74:19:F8:DC:CA:C9', # mac['value']
|
|
|
+ uid_extra='',
|
|
|
+ status=0,
|
|
|
+ add_time=add_time,
|
|
|
+ update_time=update_time,
|
|
|
+ area=0, # 关联vgp表已有区域信息,可以考虑去掉
|
|
|
+ vpg_id=vpg_id
|
|
|
+ ))
|
|
|
+ with transaction.atomic():
|
|
|
+ UIDModel.objects.bulk_create(bulk) # 批量写入uid数据
|
|
|
+ uid_count = UIDModel.objects.filter(vpg_id=vpg_id).count() # 获取族群下uid的数量
|
|
|
+ VPGModel.objects.filter(id=vpg_id).update(uid_count=uid_count) # 更新vgp表的uid_count
|
|
|
+ return response.json(0)
|
|
|
+ except Exception as e:
|
|
|
+ print(e)
|
|
|
+ return response.json(500, repr(e))
|