AlgorithmShopController.py 7.7 KB

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