UserBrandController.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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,json,urllib.request
  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.39:8000/userbrandinfo/queryByAdmin?token=test&page=1&line=10 全部记录
  27. http://192.168.136.39:8000/userbrandinfo/queryArea?token=test 市的区域统计
  28. http://192.168.136.39:8000/userbrandinfo/queryDeviceSupplier?token=test 品牌统计
  29. http://192.168.136.39:8000/userbrandinfo/queryAllByAdmin?token=test&page=1&line=10 全部记录中过滤查询每个用户的最新一条数据
  30. http://192.168.136.39:8000/userbrandinfo/queryAll?token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VySUQiOiIxNTQzOTA5MDUwNDEzMTM4MDAxMzgwMDAiLCJsYW5nIjoiY24iLCJ1c2VyIjoiMTM4MDAxMzgwMDEiLCJtX2NvZGUiOiIxMjM0MTMyNDMyMTQiLCJleHAiOjE1NTk4OTY4NTd9.nhK3VSghSGjyXKjel4woz7R_3bhjgqQDlX-ypYsklNU&page=1&line=5
  31. '''
  32. class UserBrandInfo(View):
  33. @method_decorator(csrf_exempt)
  34. def dispatch(self, *args, **kwargs):
  35. return super(UserBrandInfo, self).dispatch(*args, **kwargs)
  36. def get(self, request, *args, **kwargs):
  37. request.encoding = 'utf-8'
  38. operation = kwargs.get('operation')
  39. return self.validation(request.GET, request, operation)
  40. def post(self, request, *args, **kwargs):
  41. request.encoding = 'utf-8'
  42. operation = kwargs.get('operation')
  43. return self.validation(request.POST, request, operation)
  44. def validation(self, request_dict, request, operation):
  45. if operation is None:
  46. return response.json(444, 'error path')
  47. self.clientIP = CommonService.get_ip_address(request)
  48. response = ResponseObject()
  49. token = request_dict.get('token', None)
  50. if token is not None:
  51. tko = TokenObject(token)
  52. response.lang = tko.lang
  53. if tko.code == 0:
  54. userID = tko.userID
  55. if userID is not None:
  56. if operation == 'queryByAdmin':
  57. return self.query_info(request_dict, userID,response)
  58. elif operation == 'add':
  59. return self.add_info(request_dict, userID,response)
  60. elif operation == 'queryAllByAdmin':
  61. return self.query_all_info(request_dict, userID,response)
  62. elif operation == 'deleteByAdmin':
  63. return self.delete_by_admin(request_dict, userID,response)
  64. elif operation == 'queryDeviceSupplier':
  65. return self.query_deviceSupplier_info(request_dict, userID,response)
  66. elif operation == 'queryArea':
  67. return self.query_area_info(request_dict, userID,response)
  68. else:
  69. return response.json(444)
  70. else:
  71. return response.json(309)
  72. else:
  73. return response.json(tko.code)
  74. else:
  75. return response.json(309)
  76. # 获取外网IP
  77. # http://192.168.136.39:8000/userbrandinfo?operation=add&token=test&deviceSupplier=小米&deviceModel=HM NOTE 1TD&osType=WEB&osVersion=4.0.0
  78. def add_info(self, request_dict, userID,response):
  79. deviceSupplier = request_dict.get('deviceSupplier', None)
  80. deviceModel = request_dict.get('deviceModel', None)
  81. osType = request_dict.get('osType', None)
  82. osVersion = request_dict.get('osVersion', None)
  83. country = request_dict.get('country', None)
  84. province = request_dict.get('province', None)
  85. city = request_dict.get('city', None)
  86. area = request_dict.get('area', None)
  87. street = request_dict.get('street', None)
  88. longitude = request_dict.get('longitude', None)
  89. latitude = request_dict.get('latitude', None)
  90. appId = request_dict.get('appId', None)
  91. if area is None:
  92. area=''
  93. if street is None:
  94. street=''
  95. if longitude is None:
  96. longitude=''
  97. if latitude is None:
  98. latitude=''
  99. param_area = CommonService.get_param_flag(data=[country,province,city])
  100. must_fill_in = CommonService.get_param_flag(data=[appId,deviceSupplier,deviceModel,osType,osVersion])
  101. if must_fill_in is False:
  102. return response.json(444,'appId,deviceSupplier,deviceModel,osType,osVersion')
  103. if param_area is True:
  104. if '省' in province:
  105. province = province.replace('省','')
  106. if '市' in city:
  107. city = city.replace('市','')
  108. status = 1
  109. country = country
  110. province = province
  111. city = city
  112. area = area
  113. street = street
  114. longitude = longitude
  115. latitude = latitude
  116. else:
  117. status = 0
  118. print (self.clientIP)
  119. jsonData = CommonService.getIpIpInfo(ip=self.clientIP,lang='CN')
  120. country = jsonData['country_name']
  121. province = jsonData['region_name']
  122. city = jsonData['city_name']
  123. area = ''
  124. street = ''
  125. longitude = jsonData['longitude']
  126. latitude = jsonData['latitude']
  127. param_flag = CommonService.get_param_flag(data=[deviceSupplier, deviceModel,osType,osVersion])
  128. if param_flag is True:
  129. try:
  130. user_brand = User_Brand(
  131. userID=Device_User.objects.get(userID=userID),
  132. deviceSupplier=deviceSupplier,
  133. deviceModel=deviceModel,
  134. osType=osType,
  135. osVersion=osVersion,
  136. ip=self.clientIP,
  137. addTime = int(time.time()),
  138. status = status,
  139. country = country,
  140. province = province,
  141. city = city,
  142. appId=appId,
  143. area = area,
  144. street = street,
  145. longitude = longitude,
  146. latitude = latitude
  147. )
  148. user_brand.save()
  149. except Exception:
  150. errorInfo = traceback.format_exc()
  151. print(errorInfo)
  152. return response.json(424, {'details': errorInfo})
  153. else:
  154. print(type(user_brand.addTime))
  155. return response.json(0,{'id':user_brand.id})
  156. else:
  157. # 参数错误
  158. return response.json(444)
  159. def query_info(self, request_dict, userID,response):
  160. page = int(request_dict.get('page', None))
  161. line = int(request_dict.get('line', None))
  162. username = request_dict.get('username',None)
  163. param_flag = CommonService.get_param_flag(data=[page, line])
  164. if param_flag is True:
  165. check_perm = ModelService.check_perm(userID=userID,permID=30)
  166. if check_perm is True:
  167. if username is None or username is '':
  168. user_brand_queryset = User_Brand.objects.all().order_by( '-id')
  169. else:
  170. user_brand_queryset = User_Brand.objects.filter(userID__username=username).order_by('-id')
  171. if user_brand_queryset.exists():
  172. count = user_brand_queryset.count()
  173. res = user_brand_queryset[(page - 1) * line:page * line]
  174. send_json = CommonService.qs_to_dict(res)
  175. for k, v in enumerate(send_json["datas"]):
  176. username = ModelService.get_user_name(userID=send_json["datas"][k]['fields']['userID'])
  177. send_json["datas"][k]['fields']['username']=username
  178. send_json['count'] = count
  179. return response.json(0, send_json)
  180. return response.json(0, {'datas': [], 'count': 0})
  181. else:
  182. return response.json(404)
  183. else:
  184. return response.json(444)
  185. def delete_by_admin(self, request_dict, userID,response):
  186. id_list = request_dict.getlist('id', None)
  187. param_flag = CommonService.get_param_flag(data=[id_list])
  188. if param_flag is True:
  189. check_perm = ModelService.check_perm(userID=userID, permID=10)
  190. if check_perm is True:
  191. is_delete = User_Brand.objects.filter(id__in=id_list).delete()
  192. return response.json(0, {'delete_count': is_delete[0]})
  193. else:
  194. return response.json(404)
  195. else:
  196. return response.json(444)
  197. # 查询每个用户最新的一条记录统计信息
  198. def query_all_info(self, request_dict, userID,response):
  199. page = int(request_dict.get('page', None))
  200. line = int(request_dict.get('line', None))
  201. username = request_dict.get('username', None)
  202. param_flag = CommonService.get_param_flag(data=[page, line])
  203. if param_flag is True:
  204. check_perm = ModelService.check_perm(userID=userID,permID=30)
  205. if check_perm is True:
  206. if username is None or username is '':
  207. # 按照用户去重复查询
  208. # 分页
  209. user_brand_queryset = User_Brand.objects.all().order_by('userID').values_list('userID', flat=True).distinct()[(page - 1) * line:page * line]
  210. else:
  211. # 分页
  212. user_brand_queryset = User_Brand.objects.filter(userID__username=username).order_by('userID').values_list( 'userID',flat=True).distinct()[(page - 1) * line:page * line]
  213. send_jsons=[]
  214. counts=0
  215. for i in user_brand_queryset:
  216. counts=counts+1
  217. user_brand_querysetlast = User_Brand.objects.filter(userID=i).order_by('-addTime')[:1]
  218. user_brand_querysetlast = CommonService.qs_to_dict(user_brand_querysetlast)
  219. username = ModelService.get_user_name(userID=user_brand_querysetlast["datas"][0]['fields']['userID'])
  220. user_brand_querysetlast["datas"][0]['fields']['username']=username
  221. send_jsons.append(user_brand_querysetlast["datas"][0])
  222. # 按照加入的日期排序
  223. send_jsons = sorted(send_jsons, key=lambda x:x['fields']['addTime'], reverse=True)
  224. return response.json(0, {'datas': send_jsons, 'count': counts})
  225. else:
  226. return response.json(404)
  227. else:
  228. return response.json(444)
  229. # 品牌统计的接口
  230. def query_deviceSupplier_info(self, request_dict, userID,response):
  231. check_perm = ModelService.check_perm(userID=userID,permID=30)
  232. print ('---------------------------------------------------------')
  233. if check_perm is True:
  234. # 按照用户去重复查询
  235. user_brand_queryset = User_Brand.objects.all().order_by('userID').values_list('userID', flat=True).distinct()
  236. send_jsons=[]
  237. counts=0
  238. for i in user_brand_queryset:
  239. counts=counts+1
  240. user_brand_querysetlast = User_Brand.objects.filter(userID=i).order_by('-addTime')[:1]
  241. user_brand_querysetlast = CommonService.qs_to_dict(user_brand_querysetlast)
  242. username = ModelService.get_user_name(userID=user_brand_querysetlast["datas"][0]['fields']['userID'])
  243. user_brand_querysetlast["datas"][0]['fields']['username']=username
  244. send_jsons.append(user_brand_querysetlast["datas"][0])
  245. deviceSupplier=[]
  246. for k, v in enumerate(send_jsons):
  247. deviceSupplier.append(v['fields']['deviceSupplier'])
  248. # user_brand_queryset = User_Brand.objects.all().order_by('userID').values_list('userID', flat=True).distinct()
  249. # send_jsons=[]
  250. # counts=0
  251. # for i in user_brand_queryset:
  252. # counts=counts+1
  253. # user_brand_querysetlast = User_Brand.objects.filter(userID=i).order_by('-addTime')[:1]
  254. # user_brand_querysetlast = CommonService.qs_to_dict(user_brand_querysetlast)
  255. # username = ModelService.get_user_name(userID=user_brand_querysetlast["datas"][0]['fields']['userID'])
  256. # user_brand_querysetlast["datas"][0]['fields']['username']=username
  257. # send_jsons.append(user_brand_querysetlast["datas"][0])
  258. # deviceSupplier=[]
  259. # for k, v in enumerate(send_jsons):
  260. # deviceSupplier.append(v['fields']['deviceSupplier'])
  261. # deviceSupplier = Counter(deviceSupplier)
  262. # return response.json(0, {'datas': deviceSupplier,'counts':counts})
  263. ub_qs = User_Brand.objects.filter().values('userID','deviceSupplier').order_by('userID').order_by('userID').distinct()
  264. count = ub_qs.count()
  265. print (ub_qs)
  266. print ('---------------------------------------------------------')
  267. deviceSupplier = []
  268. ub_ql = CommonService.qs_to_list(ub_qs)
  269. for v in ub_qs:
  270. deviceSupplier.append(v['deviceSupplier'])
  271. print (v)
  272. deviceSupplier = Counter(deviceSupplier)
  273. return response.json(0, {'datas': deviceSupplier,'counts':counts})
  274. print(deviceSupplier)
  275. print ('---------------------------------------------------------')
  276. return response.json(0, {'datas': deviceSupplier, 'counts': count})
  277. else:
  278. return response.json(404)
  279. # 区域统计的接口
  280. def query_area_info(self, request_dict, userID,response):
  281. check_perm = ModelService.check_perm(userID=userID,permID=30)
  282. district = request_dict.get('district', None)
  283. if check_perm is True:
  284. # 按照用户去重复查询
  285. user_brand_queryset = User_Brand.objects.all().order_by('userID').values_list('userID', flat=True).distinct()
  286. send_jsons=[]
  287. counts=0
  288. for i in user_brand_queryset:
  289. counts=counts+1
  290. user_brand_querysetlast = User_Brand.objects.filter(userID=i).order_by('-addTime')[:1]
  291. user_brand_querysetlast = CommonService.qs_to_dict(user_brand_querysetlast)
  292. username = ModelService.get_user_name(userID=user_brand_querysetlast["datas"][0]['fields']['userID'])
  293. user_brand_querysetlast["datas"][0]['fields']['username']=username
  294. send_jsons.append(user_brand_querysetlast["datas"][0])
  295. deviceSupplier=[]
  296. for k, v in enumerate(send_jsons):
  297. deviceSupplier.append(v['fields'][district])
  298. deviceSupplier = Counter(deviceSupplier)
  299. return response.json(0, {'datas': deviceSupplier,'counts':counts})
  300. else:
  301. return response.json(404)