UserBrandController.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. from django.views.generic.base import View
  2. from django.utils.decorators import method_decorator
  3. from django.views.decorators.csrf import csrf_exempt
  4. from Service.ModelService import ModelService
  5. from Model.models import User_Brand, Device_User
  6. from django.utils import timezone
  7. import traceback, time
  8. from Object.ResponseObject import ResponseObject
  9. from Object.TokenObject import TokenObject
  10. from Service.CommonService import CommonService
  11. '''
  12. http://192.168.136.45:8077/userbrandinfo?operation=query&token=test&page=1&line=10
  13. http://192.168.136.39:8000/userbrandinfo?operation=query&token=test&page=1&line=5
  14. http://192.168.136.39:8000/userbrandinfo?operation=queryAll&token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VySUQiOiIxNTQzOTA5MDUwNDEzMTM4MDAxMzgwMDAiLCJsYW5nIjoiY24iLCJ1c2VyIjoiMTM4MDAxMzgwMDEiLCJtX2NvZGUiOiIxMjM0MTMyNDMyMTQiLCJleHAiOjE1NTk4OTY4NTd9.nhK3VSghSGjyXKjel4woz7R_3bhjgqQDlX-ypYsklNU&page=1&line=5
  15. '''
  16. class UserBrandInfo(View):
  17. @method_decorator(csrf_exempt)
  18. def dispatch(self, *args, **kwargs):
  19. return super(UserBrandInfo, self).dispatch(*args, **kwargs)
  20. def get(self, request, *args, **kwargs):
  21. request.encoding = 'utf-8'
  22. self.clientIP = CommonService.get_ip_address(request)
  23. return self.validation(request_dict=request.GET)
  24. def post(self, request, *args, **kwargs):
  25. request.encoding = 'utf-8'
  26. self.clientIP = CommonService.get_ip_address(request)
  27. return self.validation(request_dict=request.POST)
  28. def validation(self, request_dict, *args, **kwargs):
  29. response = ResponseObject()
  30. token = request_dict.get('token', None)
  31. if token is not None:
  32. tko = TokenObject(token)
  33. response.lang = tko.lang
  34. if tko.code == 0:
  35. userID = tko.userID
  36. operation = request_dict.get('operation', None)
  37. if userID is not None:
  38. if operation == 'query':
  39. return self.query_info(request_dict, userID, response)
  40. elif operation == 'add':
  41. return self.add_info(request_dict, userID, response)
  42. elif operation == 'queryAll':
  43. return self.query_all_info(request_dict, userID, response)
  44. else:
  45. return response.json(444, '444')
  46. else:
  47. return response.json(309)
  48. else:
  49. return response.json(tko.code)
  50. else:
  51. return response.json(309)
  52. # 获取外网IP
  53. # http://192.168.136.39:8000/userbrandinfo?operation=add&token=test&deviceSupplier=12341234&deviceModel=deviceModel&osType=osType&osVersion=osVersion
  54. def add_info(self, request_dict, userID, response):
  55. deviceSupplier = request_dict.get('deviceSupplier', None)
  56. deviceModel = request_dict.get('deviceModel', None)
  57. osType = request_dict.get('osType', None)
  58. osVersion = request_dict.get('osVersion', None)
  59. print(self.clientIP)
  60. try:
  61. user_brand = User_Brand(
  62. userID=Device_User.objects.get(userID=userID),
  63. deviceSupplier=deviceSupplier,
  64. deviceModel=deviceModel,
  65. osType=osType,
  66. osVersion=osVersion,
  67. ip=self.clientIP,
  68. addTime=int(time.time())
  69. )
  70. user_brand.save()
  71. except Exception:
  72. errorInfo = traceback.format_exc()
  73. print(errorInfo)
  74. return response.json(424, {'details': errorInfo})
  75. else:
  76. print(type(user_brand.addTime))
  77. return response.json(0, {'id': user_brand.id})
  78. def query_info(self, request_dict, userID, response):
  79. page = int(request_dict.get('page', None))
  80. line = int(request_dict.get('line', None))
  81. param_flag = CommonService.get_param_flag(data=[page, line])
  82. if param_flag is True:
  83. user_brand_queryset = User_Brand.objects.filter(userID=userID).order_by('-id')
  84. if user_brand_queryset.exists():
  85. count = user_brand_queryset.count()
  86. res = user_brand_queryset[(page - 1) * line:page * line]
  87. send_json = CommonService.qs_to_dict(res)
  88. send_json['count'] = count
  89. return response.json(0, send_json)
  90. return response.json(0, {'datas': [], 'count': 0})
  91. else:
  92. return response.json(444)
  93. def query_all_info(self, request_dict, userID, response):
  94. page = int(request_dict.get('page', None))
  95. line = int(request_dict.get('line', None))
  96. order = request_dict.get('order', '-id')
  97. if order == '':
  98. order = '-id'
  99. param_flag = CommonService.get_param_flag(data=[page, line])
  100. if param_flag is True:
  101. check_perm = ModelService.check_perm(userID=userID, permID=30)
  102. if check_perm is True:
  103. user_brand_queryset = User_Brand.objects.all().order_by('userID').values_list('userID',
  104. flat=True).distinct()
  105. print(user_brand_queryset)
  106. addtime = []
  107. send_jsons = []
  108. counts = 0
  109. for i in user_brand_queryset:
  110. counts = counts + 1
  111. user_brand_querysetlast = User_Brand.objects.filter(userID=i).order_by(order)[:1]
  112. user_brand_querysetlast = CommonService.qs_to_dict(user_brand_querysetlast)
  113. addtime.append(user_brand_querysetlast["datas"][0]['fields']['addTime'])
  114. username = ModelService.get_user_name(
  115. userID=user_brand_querysetlast["datas"][0]['fields']['userID'])
  116. user_brand_querysetlast["datas"][0]['fields']['username'] = username
  117. send_jsons.append(user_brand_querysetlast["datas"][0])
  118. print(addtime)
  119. print(1111111111)
  120. user_brand_queryset = User_Brand.objects.filter(userID__in=user_brand_queryset,
  121. addTime__in=addtime).order_by(order)
  122. if user_brand_queryset.exists():
  123. count = user_brand_queryset.count()
  124. res = user_brand_queryset[(page - 1) * line:page * line]
  125. send_json = CommonService.qs_to_dict(res)
  126. for k, v in enumerate(send_json["datas"]):
  127. username = ModelService.get_user_name(userID=send_json["datas"][k]['fields']['userID'])
  128. send_json["datas"][k]['fields']['username'] = username
  129. send_json['count'] = count
  130. return response.json(0, {'datas': send_jsons, 'count': counts})
  131. return response.json(0, {'datas': [], 'count': 0})
  132. else:
  133. return response.json(404)
  134. else:
  135. return response.json(444)