AlgorithmShopController.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. # -*- encoding: utf-8 -*-
  2. """
  3. @File : AlgorithmShopController.py
  4. @Time : 2022/8/24 20:02
  5. @Author : stephen
  6. @Email : zhangdongming@asj6.wecom.work
  7. @Software: PyCharm
  8. """
  9. import logging
  10. import time
  11. from django.views.generic.base import View
  12. from Model.models import DeviceAlgorithmExplain, DeviceAlgorithmBanner, DeviceUidAlgorithmType
  13. from Object.ResponseObject import ResponseObject
  14. from Object.TokenObject import TokenObject
  15. logger = logging.getLogger('info')
  16. class AlgorithmShopView(View):
  17. def get(self, request, *args, **kwargs):
  18. request.encoding = 'utf-8'
  19. operation = kwargs.get('operation')
  20. return self.validation(request.GET, request, operation)
  21. def post(self, request, *args, **kwargs):
  22. request.encoding = 'utf-8'
  23. operation = kwargs.get('operation')
  24. return self.validation(request.POST, request, operation)
  25. def validation(self, request_dict, request, operation):
  26. token = TokenObject(request.META.get('HTTP_AUTHORIZATION'))
  27. lang = request_dict.get('lang', token.lang)
  28. response = ResponseObject(lang)
  29. if token.code != 0:
  30. return response.json(token.code)
  31. if operation == 'list':
  32. return self.algorithm_list(request_dict, response)
  33. elif operation == 'banner-list':
  34. return self.get_algorithm_banner(response)
  35. elif operation == 'uid-details':
  36. return self.get_algorithm_details(request_dict, response)
  37. elif operation == 'save':
  38. return self.algorithm_setting_save(request_dict, response)
  39. @classmethod
  40. def get_algorithm_banner(cls, response):
  41. """
  42. 获取算法小店banner
  43. """
  44. banner_qs = DeviceAlgorithmBanner.objects.all()
  45. banner_vs = banner_qs.order_by('sort') \
  46. .values('algorithm_type__type', 'algorithm_type__id', 'image_url')
  47. banner_list = []
  48. if not banner_vs.exists():
  49. return response.json(0, banner_list)
  50. for item in banner_vs:
  51. banner_list.append({
  52. 'typeId': item['algorithm_type__id'],
  53. 'type': item['algorithm_type__type'],
  54. 'imageUrl': item['image_url'],
  55. })
  56. return response.json(0, banner_list)
  57. @classmethod
  58. def algorithm_list(cls, request_dict, response):
  59. """
  60. 获取算法小店列表
  61. """
  62. try:
  63. lang = request_dict.get('lang', 'en')
  64. uid = request_dict.get('uid', None)
  65. algorithm_qs = DeviceAlgorithmExplain.objects.filter(lang=lang).order_by('algorithm_type__sort') \
  66. .values('algorithm_type__id', 'algorithm_type__type',
  67. 'algorithm_type__icon_url',
  68. 'title', 'subtitle', 'algorithm_type__image_url',
  69. 'algorithm_type__basic_function')
  70. algorithm_list = []
  71. if not algorithm_qs.exists():
  72. return response.json(0, algorithm_list)
  73. for item in algorithm_qs:
  74. setting = ''
  75. if uid:
  76. setting = cls.get_uid_algorithm_info(item['algorithm_type__id'], uid)
  77. setting = setting if setting else {'status': 0, 'function': {}}
  78. algorithm_list.append({
  79. 'typeId': item['algorithm_type__id'],
  80. 'type': item['algorithm_type__type'],
  81. 'iconUrl': item['algorithm_type__icon_url'],
  82. 'imageUrl': item['algorithm_type__image_url'],
  83. 'title': item['title'],
  84. 'subtitle': item['subtitle'],
  85. 'setting': setting,
  86. 'basicFunction': item['algorithm_type__basic_function']
  87. })
  88. return response.json(0, algorithm_list)
  89. except Exception as e:
  90. print('查询算法小店列表异常,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  91. return response.json(177, repr(e))
  92. @classmethod
  93. def get_algorithm_details(cls, request_dict, response):
  94. """
  95. 获取算法小店类型详情
  96. """
  97. try:
  98. lang = request_dict.get('lang', 'en')
  99. type_id = request_dict.get('typeId', None)
  100. if not type_id:
  101. return response.json(444, 'typeId not null')
  102. type_id = int(type_id)
  103. uid = request_dict.get('uid', None)
  104. explain_qs = DeviceAlgorithmExplain.objects.filter(lang=lang).filter(algorithm_type__id=type_id) \
  105. .values('algorithm_type__id', 'algorithm_type__type',
  106. 'algorithm_type__down_count',
  107. 'algorithm_type__details_img_url',
  108. 'algorithm_type__icon_url',
  109. 'title', 'subtitle', 'introduction',
  110. 'install_explain', 'risk_warning',
  111. 'algorithm_type__basic_function')
  112. if not explain_qs.exists():
  113. return response.json(0, {})
  114. item = explain_qs.first()
  115. algorithm_dict = {
  116. 'typeId': item['algorithm_type__id'],
  117. 'type': item['algorithm_type__type'],
  118. 'downCount': item['algorithm_type__down_count'],
  119. 'detailsImgUrl': item['algorithm_type__details_img_url'],
  120. 'iconUrl': item['algorithm_type__icon_url'],
  121. 'title': item['title'],
  122. 'subtitle': item['subtitle'],
  123. 'introduction': item['introduction'],
  124. 'installExplain': item['install_explain'],
  125. 'riskWarning': item['risk_warning'],
  126. 'basicFunction': item['algorithm_type__basic_function'],
  127. }
  128. if uid:
  129. setting = cls.get_uid_algorithm_info(item['algorithm_type__id'], uid)
  130. algorithm_dict['setting'] = setting if setting else {'status': 0, 'function': {}}
  131. return response.json(0, algorithm_dict)
  132. except Exception as e:
  133. print('查询算法详情异常,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  134. return response.json(177, repr(e))
  135. @staticmethod
  136. def get_uid_algorithm_info(type_id, uid):
  137. """
  138. 获取当前设备使用算法状态信息
  139. @param type_id: 算法类型ID
  140. @param uid: 设备唯一标识
  141. @return: dict
  142. """
  143. uid_algorithm_qs = DeviceUidAlgorithmType.objects.filter(algorithm_type_id=type_id, device_uid=uid) \
  144. .values('status', 'function')
  145. if not uid_algorithm_qs.exists():
  146. return None
  147. return uid_algorithm_qs.first()
  148. @classmethod
  149. def algorithm_setting_save(cls, request_dict, response):
  150. """
  151. 算法设置保存
  152. """
  153. try:
  154. type_id = request_dict.get('typeId', None)
  155. uid = request_dict.get('uid', None)
  156. status = request_dict.get('status', None)
  157. setting_json = request_dict.get('function')
  158. if not all([type_id, uid, status, setting_json]):
  159. return response.json(444)
  160. status = int(status)
  161. type_id = int(type_id)
  162. now_time = int(time.time())
  163. uid_algorithm_qs = DeviceUidAlgorithmType.objects.filter(algorithm_type_id=type_id, device_uid=uid)
  164. if not uid_algorithm_qs.exists():
  165. param = {'algorithm_type_id': int(type_id), 'uid': uid, 'function': setting_json,
  166. 'status': status, 'updated_time': now_time, 'created_time': now_time}
  167. DeviceUidAlgorithmType.objects.create(**param)
  168. return response.json(0)
  169. uid_algorithm_qs.update(status=status, function=setting_json, updated_time=now_time)
  170. return response.json(0)
  171. except Exception as e:
  172. print('保存算法设置异常,errLine:{}, errMsg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
  173. return response.json(177, repr(e))