VoicePromptController.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. import time
  4. import oss2
  5. from django.views import View
  6. from Ansjer.config import OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET
  7. from Model.models import VoicePromptModel, UidChannelSetModel, Device_Info
  8. from Object.ResponseObject import ResponseObject
  9. from Object.TokenObject import TokenObject
  10. from Service.CommonService import CommonService
  11. from Service.ModelService import ModelService
  12. class VoicePromptView(View):
  13. def get(self, request, *args, **kwargs):
  14. request.encoding = 'utf-8'
  15. request_dict = request.GET
  16. operation = kwargs.get('operation', None)
  17. return self.validate(request_dict, operation)
  18. def post(self, request, *args, **kwargs):
  19. request.encoding = 'utf-8'
  20. request_dict = request.POST
  21. operation = kwargs.get('operation', None)
  22. return self.validate(request_dict, operation)
  23. def validate(self, request_dict, operation):
  24. token = request_dict.get('token', None)
  25. lang = request_dict.get('lang', None)
  26. response = ResponseObject(lang=lang)
  27. token = TokenObject(token)
  28. if token.code != 0:
  29. return response.json(token.code)
  30. if operation == 'getUploadUrl':
  31. return self.get_upload_url(request_dict, response)
  32. elif operation == 'add':
  33. return self.do_add(request_dict, response)
  34. elif operation == 'delete':
  35. return self.do_delete(token.userID, request_dict, response)
  36. elif operation == 'query':
  37. return self.do_query(request_dict, response)
  38. elif operation == 'update':
  39. return self.do_update(token.userID, request_dict, response)
  40. elif operation == 'adminAdd':
  41. return self.do_admin_add(token.userID, request_dict, response)
  42. elif operation == 'adminQuery':
  43. return self.do_admin_query(token.userID, request_dict, response)
  44. elif operation == 'adminUpdate':
  45. return self.do_admin_update(token.userID, request_dict, response)
  46. elif operation == 'adminDelete':
  47. return self.do_admin_delete(token.userID, request_dict, response)
  48. else:
  49. return response.json(404)
  50. def get_upload_url(self, request_dict, response):
  51. upload_type = request_dict.get('upload_type', None)
  52. uid = request_dict.get('uid', None)
  53. channel = request_dict.get('channel', None)
  54. if upload_type and uid and channel:
  55. count = VoicePromptModel.objects.filter(uid=uid, channel=channel).count()
  56. if count >= 3:
  57. return response.json(201)
  58. auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
  59. bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'ansjer-static-resources')
  60. name = CommonService.createOrderID()
  61. filename = str(name) + '.' + upload_type
  62. obj = 'voice_prompt/{uid}/{channel}/'.format(uid=uid, channel=channel) + filename
  63. url = bucket.sign_url('PUT', obj, 7200)
  64. return response.json(0, {'put_url': url, 'filename': filename})
  65. else:
  66. return response.json(444)
  67. def do_add(self, request_dict, response):
  68. filename = request_dict.get('filename', None)
  69. title = request_dict.get('title', None)
  70. type = request_dict.get('type', None)
  71. lang = request_dict.get('lang', '')
  72. uid = request_dict.get('uid', None)
  73. channel = request_dict.get('channel', None)
  74. if filename and title and type and uid and channel:
  75. voice_prompt = VoicePromptModel()
  76. voice_prompt.filename = filename
  77. voice_prompt.title = title
  78. voice_prompt.type = type
  79. voice_prompt.language = lang
  80. voice_prompt.classification = 1
  81. voice_prompt.uid = uid
  82. voice_prompt.channel = channel
  83. voice_prompt.add_time = int(time.time())
  84. voice_prompt.save()
  85. return response.json(0)
  86. else:
  87. return response.json(444)
  88. def do_update(self, userID, request_dict, response):
  89. id = request_dict.get('id', None)
  90. title = request_dict.get('title', None)
  91. if id and title:
  92. voice_qs = VoicePromptModel.objects.filter(id=id)
  93. if voice_qs.exists():
  94. uid = voice_qs[0].uid
  95. device_qs = Device_Info.objects.filter(UID=uid, userID=userID)
  96. if device_qs.exists():
  97. voice_qs.update(title=title)
  98. return response.json(0)
  99. else:
  100. return response.json(404)
  101. else:
  102. return response.json(173)
  103. else:
  104. return response.json(444)
  105. def do_delete(self, userID, request_dict, response):
  106. id = request_dict.get('id', None)
  107. if id:
  108. voice_qs = VoicePromptModel.objects.filter(id=id)
  109. if voice_qs.exists():
  110. uid = voice_qs[0].uid
  111. device_qs = Device_Info.objects.filter(UID=uid, userID=userID)
  112. if device_qs.exists():
  113. voice_qs.delete()
  114. auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
  115. bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'ansjer-static-resources')
  116. obj = 'voice_prompt/{uid}/{channel}/'.format(uid=uid, channel=voice_qs[0].channel) + voice_qs[0].filename
  117. bucket.delete_object(obj)
  118. return response.json(0)
  119. else:
  120. return response.json(404)
  121. else:
  122. return response.json(444)
  123. def do_query(self, request_dict, response):
  124. lang = request_dict.get('lang', None)
  125. uid = request_dict.get('uid', None)
  126. channel = request_dict.get('channel', None)
  127. if uid and channel and lang:
  128. voice_qs = VoicePromptModel.objects.filter(uid=uid, channel=channel, classification=1)
  129. system_qs = VoicePromptModel.objects.filter(classification=0, language=lang, status=1)
  130. channel_qs = UidChannelSetModel.objects.filter(uid__uid=uid, channel=channel)
  131. res = {
  132. 'enter_voice': {},
  133. 'leave_voice': {},
  134. 'voice_status': 0,
  135. 'voice_mute': 0
  136. }
  137. if channel_qs.exists():
  138. channel_qs = channel_qs.values('voice_prompt_enter', 'voice_prompt_leave', 'voice_prompt_status',
  139. 'voice_prompt_intelligent_mute', 'voice_start_x', 'voice_start_y',
  140. 'voice_end_x', 'voice_end_y', 'voice_start_time', 'voice_end_time')
  141. print(channel_qs)
  142. res['enter_voice'] = channel_qs[0]['voice_prompt_enter']
  143. res['leave_voice'] = channel_qs[0]['voice_prompt_leave']
  144. res['voice_status'] = channel_qs[0]['voice_prompt_status']
  145. res['voice_mute'] = channel_qs[0]['voice_prompt_intelligent_mute']
  146. res['start_x'] = channel_qs[0]['voice_start_x']
  147. res['start_y'] = channel_qs[0]['voice_start_y']
  148. res['end_x'] = channel_qs[0]['voice_end_x']
  149. res['end_y'] = channel_qs[0]['voice_end_y']
  150. res['start_time'] = channel_qs[0]['voice_start_time']
  151. res['end_time'] = channel_qs[0]['voice_end_time']
  152. enter_systems = []
  153. leave_systems = []
  154. enter_customs = []
  155. leave_customs = []
  156. res['system'] = {}
  157. res['custom'] = {}
  158. auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
  159. bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'ansjer-static-resources')
  160. if system_qs.exists():
  161. system_qs = system_qs.values('id', 'title', 'filename', 'type')
  162. for system in system_qs:
  163. filename = system['filename']
  164. obj = 'voice_prompt/system/' + filename
  165. url = bucket.sign_url('GET', obj, 3600)
  166. system['url'] = url
  167. del system['filename']
  168. if system['type'] == 1:
  169. enter_systems.append(system)
  170. if res['enter_voice'] == system['id']:
  171. res['enter_voice'] = system
  172. elif system['type'] == 2:
  173. leave_systems.append(system)
  174. if res['leave_voice'] == system['id']:
  175. res['leave_voice'] = system
  176. if voice_qs.exists():
  177. voice_qs = voice_qs.values('id', 'title', 'filename', 'type')
  178. for voice in voice_qs:
  179. filename = voice['filename']
  180. obj = 'voice_prompt/' + uid + '/' + channel + '/' + filename
  181. url = bucket.sign_url('GET', obj, 3600)
  182. voice['url'] = url
  183. del voice['filename']
  184. if voice['type'] == 1:
  185. enter_customs.append(voice)
  186. if res['enter_voice'] == voice['id']:
  187. res['enter_voice'] = voice
  188. elif voice['type'] == 2:
  189. leave_customs.append(voice)
  190. if res['leave_voice'] == voice['id']:
  191. res['leave_voice'] = voice
  192. res['system']['enter'] = enter_systems
  193. res['system']['leave'] = leave_systems
  194. res['custom']['enter'] = enter_customs
  195. res['custom']['leave'] = leave_customs
  196. return response.json(0, res)
  197. else:
  198. return response.json(444)
  199. def do_admin_add(self, userID, request_dict, response):
  200. own_perm = ModelService.check_perm(userID, 10)
  201. if not own_perm:
  202. return response.json(404)
  203. filename = request_dict.get('filename', None)
  204. title = request_dict.get('title', None)
  205. type = request_dict.get('type', None)
  206. lang = request_dict.get('lang', '')
  207. if filename and title and type:
  208. voice_prompt = VoicePromptModel()
  209. voice_prompt.filename = filename
  210. voice_prompt.title = title
  211. voice_prompt.type = type
  212. voice_prompt.language = lang
  213. voice_prompt.classification = 0
  214. voice_prompt.add_time = int(time.time())
  215. voice_prompt.status = 0
  216. voice_prompt.save()
  217. return response.json(0)
  218. else:
  219. return response.json(444)
  220. def do_admin_query(self, userID, request_dict, response):
  221. # own_perm = ModelService.check_perm(userID, 10)
  222. # if not own_perm:
  223. # return response.json(404)
  224. type = request_dict.get('type', 1)
  225. page = request_dict.get('page', None)
  226. line = request_dict.get('line', None)
  227. if page is None or line is None:
  228. return response.json(444)
  229. voice_qs = VoicePromptModel.objects.filter(classification=0, type=type)
  230. res = {
  231. 'count': 0
  232. }
  233. if voice_qs.exists():
  234. page = int(page)
  235. line = int(line)
  236. start = (page - 1) * line
  237. end = start + line
  238. count = voice_qs.count()
  239. res['count'] = count
  240. voice_qs = voice_qs.values('id', 'title', 'type', 'language', 'status', 'filename')[start:end]
  241. auth = oss2.Auth(OSS_STS_ACCESS_KEY, OSS_STS_ACCESS_SECRET)
  242. bucket = oss2.Bucket(auth, 'oss-cn-shenzhen.aliyuncs.com', 'ansjer-static-resources')
  243. for item in voice_qs:
  244. filename = item['filename']
  245. obj = 'voice_prompt/system/' + filename
  246. url = bucket.sign_url('GET', obj, 3600)
  247. item['url'] = url
  248. del item['filename']
  249. res['data'] = list(voice_qs)
  250. return response.json(0, res)
  251. else:
  252. res['data'] = []
  253. return response.json(0, res)
  254. def do_admin_update(self, userID, request_dict, response):
  255. # own_perm = ModelService.check_perm(userID, 10)
  256. # if not own_perm:
  257. # return response.json(404)
  258. id = request_dict.get('id', None)
  259. status = request_dict.get('status', None)
  260. title = request_dict.get('title', None)
  261. if id and status and title:
  262. VoicePromptModel.objects.filter(id=id, classification=0).update(status=status, title=title)
  263. return response.json(0)
  264. else:
  265. return response.json(444)
  266. def do_admin_delete(self, userID, request_dict, response):
  267. # own_perm = ModelService.check_perm(userID, 10)
  268. # if not own_perm:
  269. # return response.json(404)
  270. id = request_dict.get('id', None)
  271. if id:
  272. VoicePromptModel.objects.filter(id=id, classification=0).delete()
  273. return response.json(0)
  274. else:
  275. return response.json(444)