DeviceManagementController.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import json
  4. import time
  5. from django.db import transaction
  6. from Object.ResponseObject import ResponseObject
  7. from Object.TokenObject import TokenObject
  8. from django.views.generic.base import View
  9. from Service.ModelService import ModelService
  10. from Service.CommonService import CommonService
  11. from Model.models import Device_Info, UidSetModel, LogModel, UID_Bucket, Unused_Uid_Meal, Order_Model, StsCrdModel, \
  12. VodHlsModel, ExperienceContextModel, DeviceTypeModel
  13. class DeviceManagement(View):
  14. def get(self, request, *args, **kwargs):
  15. request.encoding = 'utf-8'
  16. operation = kwargs.get('operation')
  17. return self.validation(request.GET, request, operation)
  18. def post(self, request, *args, **kwargs):
  19. request.encoding = 'utf-8'
  20. operation = kwargs.get('operation')
  21. return self.validation(request.POST, request, operation)
  22. def validation(self, request_dict, request, operation):
  23. language = request_dict.get('language', 'en')
  24. response = ResponseObject(language, 'pc')
  25. tko = TokenObject(
  26. request.META.get('HTTP_AUTHORIZATION'),
  27. returntpye='pc')
  28. if tko.code != 0:
  29. return response.json(tko.code)
  30. response.lang = tko.lang
  31. userID = tko.userID
  32. if not userID:
  33. return response.json(444, 'userID')
  34. if operation == 'getDeviceInfoList':
  35. return self.getDeviceInfoList(request_dict, response)
  36. elif operation == 'deleteDevice':
  37. return self.deleteDevice(request_dict, response)
  38. elif operation == 'resetVod':
  39. return self.resetVod(request, request_dict, response)
  40. elif operation == 'resetPrimaryUser':
  41. return self.resetPrimaryUser(request, request_dict, response)
  42. else:
  43. return response.json(444, 'operation')
  44. # 获取设备信息数据
  45. def getDeviceInfoList(self, request_dict, response):
  46. pageNo = request_dict.get('pageNo', None)
  47. pageSize = request_dict.get('pageSize', None)
  48. UID = request_dict.get('UID', None)
  49. if not all([pageNo, pageSize]):
  50. return response.json(444)
  51. page = int(pageNo)
  52. line = int(pageSize)
  53. try:
  54. device_info_qs = Device_Info.objects.all()
  55. if UID:
  56. device_info_qs = device_info_qs.filter(UID__contains=UID)
  57. if not device_info_qs.exists():
  58. return response.json(0, {'list': {}, 'total': 0})
  59. count = device_info_qs.count()
  60. device_info_qs = device_info_qs[(page - 1) * line:page * line]
  61. device_info_list = CommonService.qs_to_dict(device_info_qs)
  62. for k, v in enumerate(device_info_list["datas"]):
  63. for device_info in device_info_qs:
  64. if v['pk'] == device_info.id:
  65. # 设备的用户名和主用户
  66. username = ModelService.get_user_name(device_info_list["datas"][k]['fields']['userID'])
  67. device_info_list["datas"][k]['fields']['username'] = username
  68. device_info_list["datas"][k]['fields']['vodPrimaryMaster'] = device_info_list["datas"][k]['fields']['vodPrimaryMaster']
  69. # 设备类型,是否支持Alexa和ip
  70. type = device_info_list["datas"][k]['fields']['Type']
  71. device_type_qs = DeviceTypeModel.objects.filter(type=type).values('name')
  72. if device_type_qs.exists():
  73. device_info_list["datas"][k]['fields']['Type'] = device_type_qs[0]['name']
  74. uid_set_qs = UidSetModel.objects.filter(uid=device_info_list["datas"][k]['fields']['UID']).values('is_alexa', 'ip')
  75. if uid_set_qs.exists():
  76. isAlexa = '是' if uid_set_qs[0]['is_alexa'] else '否'
  77. device_info_list["datas"][k]['fields']['isAlexa'] = isAlexa
  78. device_info_list["datas"][k]['fields']['ip'] = uid_set_qs[0]['ip']
  79. return response.json(0, {'list': device_info_list, 'total': count})
  80. except Exception as e:
  81. print(e)
  82. return response.json(500, repr(e))
  83. # 根据id删除设备
  84. def deleteDevice(self, request_dict, response):
  85. deviceID = request_dict.get('deviceID', None)
  86. if not deviceID:
  87. return response.json(444)
  88. try:
  89. Device_Info.objects.filter(id=deviceID).delete()
  90. return response.json(0)
  91. except Exception as e:
  92. print(e)
  93. return response.json(500, repr(e))
  94. # 重置设备主用户
  95. def resetPrimaryUser(self, request, request_dict, response):
  96. uid = request_dict.get('uid', None)
  97. if not uid:
  98. return response.json(404)
  99. try:
  100. # 记录操作日志
  101. ip = CommonService.get_ip_address(request)
  102. content = json.loads(json.dumps(request_dict))
  103. log = {
  104. 'ip': ip,
  105. 'user_id': 2,
  106. 'status': 200,
  107. 'time': int(time.time()),
  108. 'url': 'deviceManagement/resetPrimaryUser',
  109. 'content': json.dumps(content),
  110. 'operation': '{}重置设备主用户'.format(uid),
  111. }
  112. LogModel.objects.create(**log)
  113. Device_Info.objects.filter(UID=uid).update(vodPrimaryUserID='', vodPrimaryMaster='')
  114. return response.json(0)
  115. except Exception as e:
  116. return response.json(500, repr(e))
  117. # 重置设备云存
  118. def resetVod(self, request, request_dict, response):
  119. uid = request_dict.get('uid', None)
  120. if not uid:
  121. return response.json(404)
  122. try:
  123. # 记录操作日志
  124. ip = CommonService.get_ip_address(request)
  125. content = json.loads(json.dumps(request_dict))
  126. log = {
  127. 'ip': ip,
  128. 'user_id': 2,
  129. 'status': 200,
  130. 'time': int(time.time()),
  131. 'url': 'deviceManagement/resetVod',
  132. 'content': json.dumps(content),
  133. 'operation': '{}重置设备云存'.format(uid),
  134. }
  135. with transaction.atomic():
  136. LogModel.objects.create(**log)
  137. # 删除和更新设备云存相关数据
  138. UID_Bucket.objects.filter(uid=uid).delete()
  139. Unused_Uid_Meal.objects.filter(uid=uid).delete()
  140. Order_Model.objects.filter(UID=uid).delete()
  141. StsCrdModel.objects.filter(uid=uid).delete()
  142. VodHlsModel.objects.filter(uid=uid).delete()
  143. ExperienceContextModel.objects.filter(uid=uid).delete()
  144. Device_Info.objects.filter(UID=uid).update(vodPrimaryUserID='', vodPrimaryMaster='')
  145. return response.json(0)
  146. except Exception as e:
  147. print(e)
  148. return response.json(500, repr(e))