AlgorithmShopController.py 8.5 KB

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