VPGController.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import json
  4. import os
  5. import re
  6. import time
  7. from django.db import transaction
  8. from django.views import View
  9. from django.views.decorators.csrf import csrf_exempt
  10. from Ansjer.config import BASE_DIR
  11. from Model.models import RegionModel, CompanyModel, VPGModel, UIDModel, MacModel, UIDCompanySerialModel, LogModel
  12. from Object.uidManageResponseObject import uidManageResponseObject
  13. from Object.TokenObject import TokenObject
  14. from Service.CommonService import CommonService
  15. from Service.ModelService import ModelService
  16. class VPGView(View):
  17. def get(self, request, *args, **kwargs):
  18. request.encoding = 'utf-8'
  19. operation = kwargs.get('operation', None)
  20. request_dict = request.GET
  21. return self.validate(request_dict, operation)
  22. def post(self, request, *args, **kwargs):
  23. request.encoding = 'utf-8'
  24. operation = kwargs.get('operation', None)
  25. request_dict = request.POST
  26. return self.validate(request_dict, operation)
  27. def validate(self, request_dict, operation):
  28. token = TokenObject(request_dict.get('token', None))
  29. response = uidManageResponseObject()
  30. if token.code != 0:
  31. return response.json(token.code)
  32. if operation == 'add':
  33. return self.do_add(token.userID, request_dict, response)
  34. elif operation == 'update':
  35. return self.do_update(token.userID, request_dict, response)
  36. elif operation == 'delete':
  37. return self.do_delete(token.userID, request_dict, response)
  38. elif operation == 'list':
  39. return self.do_list(token.userID, request_dict, response)
  40. elif operation == 'uid_list':
  41. return self.do_uid_list(token.userID, request_dict, response)
  42. else:
  43. return response.json(404)
  44. def do_add(self, userID, request_dict, response):
  45. # perm = ModelService.check_perm_uid_manage(userID, 0)
  46. # if not perm:
  47. # return response.json(309)
  48. name = request_dict.get('vpg', None)
  49. region_id = request_dict.get('region_id', None)
  50. company_id = request_dict.get('company_id', None)
  51. if name and region_id and company_id:
  52. region_qs = RegionModel.objects.filter(id=region_id)
  53. if not region_qs.exists():
  54. return response.json(374)
  55. company_qs = CompanyModel.objects.filter(secret=company_id)
  56. if not company_qs.exists():
  57. return response.json(373)
  58. company = company_qs[0]
  59. now_time = int(time.time())
  60. vpgModel = VPGModel(name=name, region_id=region_id, company_id=company.id, add_time=now_time, update_time=now_time)
  61. vpgModel.save()
  62. return response.json(0)
  63. else:
  64. return response.json(444)
  65. def do_update(self, userID, request_dict, response):
  66. # perm = ModelService.check_perm_uid_manage(userID, 0)
  67. # if not perm:
  68. # return response.json(309)
  69. id = request_dict.get('id', None)
  70. name = request_dict.get('vpg', None)
  71. region_id = request_dict.get('region_id', None)
  72. company_id = request_dict.get('company_id', None)
  73. if id:
  74. vpg_qs = VPGModel.objects.filter(id=id)
  75. if vpg_qs.exists():
  76. now_time = int(time.time())
  77. update = {
  78. 'update_time': now_time
  79. }
  80. if name:
  81. update['name'] = name
  82. if region_id:
  83. update['region_id'] = region_id
  84. if company_id:
  85. update['company_id'] = company_id
  86. vpg_qs.update(**update)
  87. return response.json(0)
  88. else:
  89. return response.json(173)
  90. else:
  91. return response.json(444)
  92. def do_delete(self, userID, request_dict, response):
  93. # perm = ModelService.check_perm_uid_manage(userID, 0)
  94. # if not perm:
  95. # return response.json(309)
  96. id = request_dict.get('id', None)
  97. if id:
  98. vpg_qs = VPGModel.objects.filter(id=id)
  99. if vpg_qs.exists():
  100. vpg_qs.delete()
  101. return response.json(0)
  102. else:
  103. return response.json(173)
  104. else:
  105. return response.json(444)
  106. def do_list(self, userID, request_dict, response):
  107. # perm = ModelService.check_perm_uid_manage(userID, 0)
  108. # if not perm:
  109. # return response.json(309)
  110. company_id = request_dict.get('company_id', None)
  111. region_id = request_dict.get('region_id', None)
  112. page = request_dict.get('page', None)
  113. line = request_dict.get('limit', None)
  114. if page and line:
  115. page = int(page)
  116. line = int(line)
  117. start = (page - 1) * line
  118. end = start + line
  119. vpg_qs = VPGModel.objects.filter()
  120. if company_id:
  121. vpg_qs.filter(company_id=company_id)
  122. if region_id:
  123. vpg_qs.filter(region_id=region_id)
  124. count = vpg_qs.count()
  125. vpg_qs = vpg_qs.values('id', 'name', 'region__name', 'region_id', 'company__name', 'add_time',
  126. 'update_time', 'company__secret', 'uid_count')
  127. vpg_qs = vpg_qs[start:end]
  128. # for vpg in vpg_qs:
  129. # vpg['uid_count'] = UIDModel.objects.filter(vpg_id=vpg['id']).count()
  130. res = {
  131. 'count': count,
  132. 'data': list(vpg_qs),
  133. }
  134. return response.json(0, res)
  135. else:
  136. return response.json(444)
  137. def do_uid_list(self, userID, request_dict, response):
  138. vpg_id = request_dict.get('vpg_id', None)
  139. page = int(request_dict.get('page', None))
  140. line = int(request_dict.get('limit', None))
  141. if not vpg_id:
  142. return response.json(444)
  143. start = (page - 1) * line
  144. end = start + line
  145. uid_qs = UIDModel.objects.filter(vpg_id=vpg_id).values('uid', 'p2p_type', 'platform', 'init_string', 'init_string_app')
  146. count = VPGModel.objects.get(id=vpg_id).uid_count # 从vpg表获取uid总数
  147. uid_qs = uid_qs[start:end] # 显示条数
  148. res = {
  149. 'count': count,
  150. 'data': list(uid_qs),
  151. }
  152. return response.json(0, res)
  153. @csrf_exempt
  154. def do_upload_uid(request):
  155. # 上传UID,需要request.FILES,单独提取出来
  156. request.encoding = 'utf-8'
  157. response = uidManageResponseObject()
  158. if request.method == "POST":
  159. request_dict = request.POST
  160. elif request.method == "GET":
  161. request_dict = request.GET
  162. else:
  163. return response.json(444)
  164. file = request.FILES.get('file', None)
  165. vpg_id = request_dict.get('vpg_id', None)
  166. p2p_type = request_dict.get('p2p_type', None)
  167. platform = request_dict.get('platform', '')
  168. init_string = request_dict.get('init_string', '')
  169. init_string_app = request_dict.get('init_string_app', '')
  170. if not all([vpg_id, p2p_type]):
  171. return response.json(444)
  172. try:
  173. bulk = []
  174. p2p_type = int(p2p_type)
  175. # 尚云必须输入平台和初始化字符
  176. if p2p_type == 1 and (not platform or not platform or not init_string_app):
  177. return response.json(444)
  178. p2p = '尚云' if p2p_type == 1 else 'tutk'
  179. area = 1 if vpg_id != '1' else 0 # vpg_id为'1':国内
  180. add_time = update_time = int(time.time())
  181. for chunk in file.chunks():
  182. uid_list = re.findall("b\'(.*)\'", str(chunk))[0].split('\\r\\n')
  183. for uid in uid_list:
  184. UID = UIDModel(
  185. mac='',
  186. uid_extra='',
  187. status=0,
  188. add_time=add_time,
  189. update_time=update_time,
  190. area=area, # 关联vgp表已有区域信息,可以考虑去掉
  191. vpg_id=vpg_id,
  192. p2p_type=p2p_type,
  193. platform=platform,
  194. init_string=init_string,
  195. init_string_app=init_string_app
  196. )
  197. # 尚云完整uid,eg.ACN-000005-FHCGR,VRWEDU -> ACN000005FHCGR,必须包含','
  198. if p2p == '尚云':
  199. if '-' in uid and ',' in uid:
  200. UID.full_uid_code = uid
  201. uid_split = uid.split('-')
  202. uid = uid_split[0] + uid_split[1] + uid_split[2].split(',')[0]
  203. else:
  204. return response.json(376)
  205. # tutk uid长度为14或20
  206. elif len(uid) != 14 and len(uid) != 20:
  207. return response.json(376)
  208. UID.uid = uid
  209. bulk.append(UID)
  210. ip = CommonService.get_ip_address(request)
  211. content = json.loads(json.dumps(request_dict))
  212. log = {
  213. 'ip': ip,
  214. 'user_id': 1,
  215. 'status': 200,
  216. 'time': add_time,
  217. 'url': 'vpgUid/uid',
  218. 'content': json.dumps(content),
  219. 'operation': '上传{}个{}uid到VPG ID {}'.format(len(uid_list), p2p, vpg_id),
  220. }
  221. with transaction.atomic():
  222. LogModel.objects.create(**log) # 记录操作日志
  223. UIDModel.objects.bulk_create(bulk) # 批量写入uid数据
  224. uid_count = UIDModel.objects.filter(vpg_id=vpg_id).count() # 获取族群下uid的数量
  225. VPGModel.objects.filter(id=vpg_id).update(uid_count=uid_count) # 更新vgp表的uid_count
  226. return response.json(0)
  227. except Exception as e:
  228. print(e)
  229. return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))