VoicePromptController.py 16 KB

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