UserBrandController.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. @Copyright (C) ansjer cop Video Technology Co.,Ltd.All rights reserved.
  5. @AUTHOR: ASJRD019
  6. @NAME: AnsjerFormal
  7. @software: PyCharm
  8. @DATE: 2019/5/9 11:50
  9. @Version: python3.6
  10. @MODIFY DECORD:ansjer dev
  11. @file: AliPayObject.py
  12. @Contact: pzb3076@163.com
  13. """
  14. from django.views.generic.base import View
  15. from django.utils.decorators import method_decorator
  16. from django.views.decorators.csrf import csrf_exempt
  17. from Service.ModelService import ModelService
  18. from Model.models import User_Brand,Device_User
  19. from django.utils import timezone
  20. import traceback,time
  21. from Object.ResponseObject import ResponseObject
  22. from Object.TokenObject import TokenObject
  23. from Service.CommonService import CommonService
  24. from collections import Counter
  25. '''
  26. http://192.168.136.45:8077/userbrandinfo?operation=query&token=test&page=1&line=10
  27. http://192.168.136.39:8000/userbrandinfo?operation=query&token=test&page=1&line=5
  28. http://192.168.136.39:8000/userbrandinfo?operation=queryAll&token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VySUQiOiIxNTQzOTA5MDUwNDEzMTM4MDAxMzgwMDAiLCJsYW5nIjoiY24iLCJ1c2VyIjoiMTM4MDAxMzgwMDEiLCJtX2NvZGUiOiIxMjM0MTMyNDMyMTQiLCJleHAiOjE1NTk4OTY4NTd9.nhK3VSghSGjyXKjel4woz7R_3bhjgqQDlX-ypYsklNU&page=1&line=5
  29. '''
  30. class UserBrandInfo(View):
  31. @method_decorator(csrf_exempt)
  32. def dispatch(self, *args, **kwargs):
  33. return super(UserBrandInfo, self).dispatch(*args, **kwargs)
  34. def get(self, request, *args, **kwargs):
  35. request.encoding = 'utf-8'
  36. self.clientIP = CommonService.get_ip_address(request)
  37. return self.validation(request_dict=request.GET)
  38. def post(self, request, *args, **kwargs):
  39. request.encoding = 'utf-8'
  40. self.clientIP = CommonService.get_ip_address(request)
  41. return self.validation(request_dict=request.POST)
  42. def validation(self, request_dict, *args, **kwargs):
  43. response = ResponseObject()
  44. token = request_dict.get('token', None)
  45. if token is not None:
  46. tko = TokenObject(token)
  47. response.lang = tko.lang
  48. if tko.code == 0:
  49. userID = tko.userID
  50. operation = request_dict.get('operation', None)
  51. if userID is not None:
  52. if operation == 'query':
  53. return self.query_info(request_dict, userID,response)
  54. elif operation == 'add':
  55. return self.add_info(request_dict, userID,response)
  56. elif operation == 'queryAll':
  57. return self.query_all_info(request_dict, userID,response)
  58. elif operation == 'queryDeviceSupplier':
  59. return self.query_deviceSupplier_info(request_dict, userID,response)
  60. else:
  61. return response.json(444,'444')
  62. else:
  63. return response.json(309)
  64. else:
  65. return response.json(tko.code)
  66. else:
  67. return response.json(309)
  68. # 获取外网IP
  69. # http://192.168.136.39:8000/userbrandinfo?operation=add&token=test&deviceSupplier=12341234&deviceModel=deviceModel&osType=osType&osVersion=osVersion
  70. def add_info(self, request_dict, userID,response):
  71. deviceSupplier = request_dict.get('deviceSupplier', None)
  72. deviceModel = request_dict.get('deviceModel', None)
  73. osType = request_dict.get('osType', None)
  74. osVersion = request_dict.get('osVersion', None)
  75. print(self.clientIP)
  76. try:
  77. user_brand = User_Brand(
  78. userID=Device_User.objects.get(userID=userID),
  79. deviceSupplier=deviceSupplier,
  80. deviceModel=deviceModel,
  81. osType=osType,
  82. osVersion=osVersion,
  83. ip=self.clientIP,
  84. addTime = int(time.time())
  85. )
  86. user_brand.save()
  87. except Exception:
  88. errorInfo = traceback.format_exc()
  89. print(errorInfo)
  90. return response.json(424, {'details': errorInfo})
  91. else:
  92. print(type(user_brand.addTime))
  93. return response.json(0,{'id':user_brand.id})
  94. def query_info(self, request_dict, userID,response):
  95. page = int(request_dict.get('page', None))
  96. line = int(request_dict.get('line', None))
  97. param_flag = CommonService.get_param_flag(data=[page, line])
  98. if param_flag is True:
  99. user_brand_queryset = User_Brand.objects.filter(userID=userID).order_by('-id')
  100. if user_brand_queryset.exists():
  101. count = user_brand_queryset.count()
  102. res = user_brand_queryset[(page - 1) * line:page * line]
  103. send_json = CommonService.qs_to_dict(res)
  104. send_json['count'] = count
  105. return response.json(0, send_json)
  106. return response.json(0, {'datas': [], 'count': 0})
  107. else:
  108. return response.json(444)
  109. def query_all_info(self, request_dict, userID,response):
  110. page = int(request_dict.get('page', None))
  111. line = int(request_dict.get('line', None))
  112. param_flag = CommonService.get_param_flag(data=[page, line])
  113. if param_flag is True:
  114. check_perm = ModelService.check_perm(userID=userID,permID=30)
  115. if check_perm is True:
  116. # 按照用户去重复查询
  117. user_brand_queryset = User_Brand.objects.all().order_by('userID').values_list('userID', flat=True).distinct()
  118. print (user_brand_queryset)
  119. send_jsons=[]
  120. counts=0
  121. for i in user_brand_queryset:
  122. counts=counts+1
  123. user_brand_querysetlast = User_Brand.objects.filter(userID=i).order_by('-addTime')[:1]
  124. user_brand_querysetlast = CommonService.qs_to_dict(user_brand_querysetlast)
  125. username = ModelService.get_user_name(userID=user_brand_querysetlast["datas"][0]['fields']['userID'])
  126. user_brand_querysetlast["datas"][0]['fields']['username']=username
  127. send_jsons.append(user_brand_querysetlast["datas"][0])
  128. # 按照加入的日期排序
  129. send_jsons = sorted(send_jsons, key=lambda x:x['fields']['addTime'], reverse=True)
  130. # 分页
  131. send_jsons = send_jsons[(page - 1) * line:page * line]
  132. return response.json(0, {'datas': send_jsons, 'count': counts})
  133. else:
  134. return response.json(404)
  135. else:
  136. return response.json(444)
  137. def query_deviceSupplier_info(self, request_dict, userID,response):
  138. check_perm = ModelService.check_perm(userID=userID,permID=30)
  139. if check_perm is True:
  140. # 按照用户去重复查询
  141. user_brand_queryset = User_Brand.objects.all().order_by('userID').values_list('userID', flat=True).distinct()
  142. print (user_brand_queryset)
  143. send_jsons=[]
  144. counts=0
  145. for i in user_brand_queryset:
  146. counts=counts+1
  147. user_brand_querysetlast = User_Brand.objects.filter(userID=i).order_by('-addTime')[:1]
  148. user_brand_querysetlast = CommonService.qs_to_dict(user_brand_querysetlast)
  149. username = ModelService.get_user_name(userID=user_brand_querysetlast["datas"][0]['fields']['userID'])
  150. user_brand_querysetlast["datas"][0]['fields']['username']=username
  151. send_jsons.append(user_brand_querysetlast["datas"][0])
  152. deviceSupplier=[]
  153. for k, v in enumerate(send_jsons):
  154. deviceSupplier.append(v['fields']['deviceSupplier'])
  155. deviceSupplier = Counter(deviceSupplier)
  156. return response.json(0, {'datas': deviceSupplier})
  157. else:
  158. return response.json(404)