EquipmentManager.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. from django.views.decorators.csrf import csrf_exempt
  2. import traceback
  3. from Service.ModelService import ModelService
  4. from Model.models import Device_User, Device_Info, Device_Meal, UID_Bucket
  5. from Service.CommonService import CommonService
  6. import simplejson as json
  7. from Object.TokenObject import TokenObject
  8. from Object.ResponseObject import ResponseObject
  9. import re
  10. def addNewUserEquipment(userID, deviceContent, response):
  11. userIDValid = Device_User.objects.filter(userID=userID)
  12. if userIDValid:
  13. try:
  14. try:
  15. deviceData = json.loads(deviceContent)
  16. except Exception as e:
  17. return response.json(803, repr(e))
  18. else:
  19. UID = deviceData.get('UID', None)
  20. if UID != None:
  21. dValid = Device_Info.objects.filter(userID=userID, UID=UID)
  22. if dValid:
  23. return response.json(174)
  24. else:
  25. # 判断是否有同样昵称存在
  26. # if 'NickName' in deviceData:
  27. # nValid = Device_Info.objects.filter(userID=userID, NickName=deviceData['NickName'])
  28. # if nValid.exists():
  29. # return response.json(179)
  30. UID = deviceData.get('UID', '')
  31. re_uid = re.compile(r'^[A-Za-z0-9]{20}$')
  32. if re_uid.match(UID):
  33. # is_bind = Device_Info.objects.filter(UID=UID, isShare=False)
  34. is_bind = ''
  35. # 判断是否有已绑定用户
  36. if is_bind:
  37. # return response.json(175)
  38. # 判断用户密码是否正确
  39. # dev_user = deviceData.get('View_Account', '')
  40. # dev_pass = deviceData.get('View_Password', '')
  41. # if is_bind[0].View_Account == dev_user and is_bind[0].View_Password == dev_pass:
  42. deviceData['NickName'] = is_bind[0].NickName
  43. if 'isShare' in deviceData:
  44. deviceData['isShare'] = True
  45. userDevice = Device_Info(id=CommonService.getUserID(getUser=False),
  46. primaryUserID=is_bind[0].userID_id, userID_id=userID,
  47. **deviceData)
  48. userDevice.save()
  49. # else:
  50. # return response.json(111)
  51. else:
  52. userDevice = Device_Info(id=CommonService.getUserID(getUser=False), userID_id=userID,
  53. **deviceData)
  54. userDevice.save()
  55. else:
  56. return response.json(180)
  57. else:
  58. return response.json(806)
  59. except Exception as e:
  60. errorInfo = traceback.format_exc()
  61. print('添加设备错误: %s ' % errorInfo)
  62. return response.json(178, repr(e))
  63. else:
  64. sqlDict = CommonService.qs_to_dict([userDevice])
  65. return response.json(0, sqlDict)
  66. else:
  67. return response.json(113)
  68. def delUserEquipment(userID, id, response):
  69. try:
  70. deviceValid = Device_Info.objects.filter(userID_id=userID, id=id)
  71. except Exception as e:
  72. errorInfo = traceback.format_exc()
  73. print('查询数据库错误: %s' % errorInfo)
  74. return response.json(500, repr(e))
  75. else:
  76. if deviceValid:
  77. try:
  78. Device_Info.objects.filter(userID_id=userID, id=id).delete()
  79. except Exception as e:
  80. errorInfo = traceback.format_exc()
  81. print('删除数据库记录错误: %s' % errorInfo)
  82. return response.json(176, repr(e))
  83. else:
  84. return response.json(0)
  85. else:
  86. UserValid = Device_User.objects.filter(userID=userID)
  87. if UserValid.exists():
  88. return response.json(172)
  89. else:
  90. return response.json(113)
  91. def modifyUserEquipment(userID, deviceContent, id, response):
  92. try:
  93. deviceValid = Device_Info.objects.filter(userID_id=userID, id=id)
  94. except Exception as e:
  95. errorInfo = traceback.format_exc()
  96. print('查询数据库错误: %s' % errorInfo)
  97. return response.json(500, repr(e))
  98. else:
  99. if deviceValid:
  100. deviceData = json.loads(deviceContent)
  101. try:
  102. Device_Info.objects.filter(userID_id=userID, id=id).update(**deviceData)
  103. except Exception as e:
  104. errorInfo = traceback.format_exc()
  105. print('修改设备信息错误: %s ' % errorInfo)
  106. return response.json(177, repr(e))
  107. else:
  108. qs = Device_Info.objects.filter(userID_id=userID, id=id)
  109. res = CommonService.qs_to_dict(qs)
  110. return response.json(0, res)
  111. else:
  112. UserValid = Device_User.objects.filter(userID=userID)
  113. if UserValid.exists():
  114. return response.json(172)
  115. else:
  116. return response.json(113)
  117. def showAllUserEquipment(userID, response):
  118. try:
  119. userValid = Device_User.objects.filter(userID=userID).order_by('-data_joined')
  120. except Exception as e:
  121. errorInfo = traceback.format_exc()
  122. print('查询数据库错误: %s' % errorInfo)
  123. return response.json(500, repr(e))
  124. else:
  125. if not userValid.exists():
  126. return response.json(113)
  127. own_perm = ModelService.check_permission(userID=userID, permID=30)
  128. # if userValid[0].is_superuser != 100 and userValid[0].is_superuser != 1:
  129. if own_perm is not True:
  130. return response.json(612)
  131. qs = Device_Info.objects.all()
  132. res = CommonService.qs_to_dict(qs)
  133. return response.json(0, res)
  134. def showAllUserEquipmentPC(userID, fieldDict, response):
  135. try:
  136. user_valid = Device_User.objects.filter(userID=userID).order_by('-data_joined')
  137. except Exception as e:
  138. errorInfo = traceback.format_exc()
  139. print('查询数据库错误: %s' % errorInfo)
  140. return response.json(500, repr(e))
  141. else:
  142. if not user_valid:
  143. return response.json(113)
  144. own_permission = ModelService.check_permission(userID=userID, permID=30)
  145. if not own_permission:
  146. return response.json(612)
  147. page = int(fieldDict['page'])
  148. line = int(fieldDict['line'])
  149. device_info_query_set = Device_Info.objects.all()
  150. device_info_count = device_info_query_set.count()
  151. device_info_res = device_info_query_set[(page - 1) * line:page * line]
  152. sqlDict = CommonService.qs_to_dict(query_set=device_info_res)
  153. sqlDict['count'] = device_info_count
  154. return response.json(0, sqlDict)
  155. def findEquipmentInfo(content, type, fieldDict):
  156. if type == 1:
  157. Device_Info_QuerySet = Device_Info.objects.all()
  158. if type == 2:
  159. searchCondition = content
  160. kwargs = CommonService.get_kwargs(data=searchCondition)
  161. Device_Info_QuerySet = Device_Info.objects.filter(**kwargs)
  162. page = int(fieldDict['page'])
  163. line = int(fieldDict['line'])
  164. device_info_count = Device_Info_QuerySet.count()
  165. res = Device_Info_QuerySet[(page - 1) * line:page * line]
  166. send_dict = CommonService.qs_to_dict(query_set=res)
  167. for k, v in enumerate(send_dict["datas"]):
  168. for val in res:
  169. if v['pk'] == val.id:
  170. username = ModelService.get_user_name(send_dict["datas"][k]['fields']['userID'])
  171. send_dict["datas"][k]['fields']['username'] = username
  172. primary = ModelService.get_user_name(send_dict["datas"][k]['fields']['primaryUserID'])
  173. send_dict["datas"][k]['fields']['primaryusername'] = primary
  174. send_dict['count'] = device_info_count
  175. return send_dict
  176. @csrf_exempt
  177. def queryUserEquipmentInterface(request, *callback_args,
  178. **callback_kwargs):
  179. '''
  180. 查询用户设备
  181. :param request:
  182. :param callback_args:
  183. :param callback_kwargs:
  184. :return:
  185. '''
  186. response = ResponseObject()
  187. if request.method == 'POST':
  188. request.encoding = 'utf-8'
  189. token = request.POST.get('token', None)
  190. elif request.method == 'GET':
  191. request.encoding = 'gb2312'
  192. token = request.GET.get('token', None)
  193. else:
  194. return response.json(801)
  195. tko = TokenObject(token)
  196. tko.valid()
  197. response.lang = tko.lang
  198. if tko.code != 0:
  199. return response.json(tko.code)
  200. userID = tko.userID
  201. # if userID is not None:
  202. # deviceValid = Device_Info.objects.filter(userID_id=userID)
  203. # # print(deviceValid)
  204. # res = CommonService.qs_to_dict(deviceValid)
  205. # datas = res['datas']
  206. # uid_list = []
  207. # for q in datas:
  208. # uid_list.append(q['fields']['UID'])
  209. # dmqs = Device_Meal.objects.filter(uid__in=uid_list).\
  210. # values('rank', 'status', 'channel', 'end_time', 'rank__title','uid')
  211. # dmlt = CommonService.qs_to_list(dmqs)
  212. # # print(uid_list)
  213. # res = []
  214. # for p in datas:
  215. # p['fields']['vod'] = []
  216. # for dm in dmlt:
  217. # if p['fields']['UID'] == dm['uid']:
  218. # p['fields']['vod'].append(dm)
  219. # res.append(p)
  220. # print(res)
  221. # return response.json(0, {'datas':res})
  222. if userID is not None:
  223. deviceValid = Device_Info.objects.filter(userID_id=userID)
  224. # print(deviceValid)
  225. res = CommonService.qs_to_dict(deviceValid)
  226. datas = res['datas']
  227. uid_list = []
  228. for q in datas:
  229. uid_list.append(q['fields']['UID'])
  230. ubqs = UID_Bucket.objects.filter(uid__in=uid_list). \
  231. values('bucket__content', 'status', 'channel', 'endTime', 'uid')
  232. ubql = CommonService.qs_to_list(ubqs)
  233. # print(uid_list)
  234. res = []
  235. for p in datas:
  236. p['fields']['vod'] = []
  237. for dm in ubql:
  238. if p['fields']['UID'] == dm['uid']:
  239. p['fields']['vod'].append(dm)
  240. res.append(p)
  241. print(res)
  242. return response.json(0, {'datas': res})
  243. else:
  244. return response.json(309)
  245. @csrf_exempt
  246. def addNewUserEquipmentInterface(request, *callback_args,
  247. **callback_kwargs):
  248. response = ResponseObject()
  249. if request.method == 'POST':
  250. request.encoding = 'utf-8'
  251. request_dict = request.POST
  252. elif request.method == 'GET':
  253. request.encoding = 'utf-8'
  254. request_dict = request.GET
  255. else:
  256. return response.json(801)
  257. token = request_dict.get('token', None)
  258. deviceContent = request_dict.get('content', None)
  259. if token is not None and deviceContent is not None:
  260. tko = TokenObject(token)
  261. tko.valid()
  262. response.lang = tko.lang
  263. if tko.code == 0:
  264. userID = tko.userID
  265. if userID is not None:
  266. return addNewUserEquipment(userID, deviceContent, response)
  267. else:
  268. return response.json(309)
  269. else:
  270. return response.json(tko.code)
  271. else:
  272. return response.json(444, 'token,content')
  273. @csrf_exempt
  274. def delUserEquipmentInterface(request, *callback_args,
  275. **callback_kwargs):
  276. '''
  277. 删除用户设备
  278. :param request:
  279. :param callback_args: 表示任何多个无名参数,tuple类型
  280. :param callback_kwargs: 表示关键字参数,dict类型
  281. :return:
  282. '''
  283. response = ResponseObject()
  284. if request.method == 'POST':
  285. request.encoding = 'utf-8'
  286. request_dict = request.POST
  287. elif request.method == 'GET':
  288. request.encoding = 'gb2312'
  289. request_dict = request.GET
  290. else:
  291. return response.json(801)
  292. token = request_dict.get('token', None)
  293. id = request_dict.get('id', None)
  294. if token is not None and id is not None:
  295. tko = TokenObject(token)
  296. tko.valid()
  297. response.lang = tko.lang
  298. if tko.code == 0:
  299. userID = tko.userID
  300. if userID is not None:
  301. return delUserEquipment(userID, id, response)
  302. else:
  303. return response.json(309)
  304. else:
  305. return response.json(tko.code)
  306. else:
  307. return response.json(800)
  308. @csrf_exempt
  309. def modifyUserEquipmentInterface(request, *callback_args,
  310. **callback_kwargs):
  311. '''
  312. 修改用户设备
  313. :param request:
  314. :param callback_args:
  315. :param callback_kwargs:
  316. :return:
  317. '''
  318. response = ResponseObject()
  319. if request.method == 'POST':
  320. request.encoding = 'utf-8'
  321. request_dict = request.POST
  322. elif request.method == 'GET':
  323. request.encoding = 'gb2312'
  324. request_dict = request.GET
  325. token = request_dict.get('token', None)
  326. deviceContent = request_dict.get('content', None)
  327. id = request_dict.get('id', None)
  328. if token is not None and deviceContent is not None and id is not None:
  329. tko = TokenObject(token)
  330. tko.valid()
  331. response.lang = tko.lang
  332. if tko.code == 0:
  333. userID = tko.userID
  334. if userID is not None:
  335. return modifyUserEquipment(userID, deviceContent, id, response)
  336. else:
  337. return response.json(309)
  338. else:
  339. return response.json(tko.code)
  340. else:
  341. return response.json(444, 'token,content,id')
  342. @csrf_exempt
  343. def showAllUserEquipmentInterface(request, *callback_args, **callback_kwargs):
  344. if request.method == 'POST':
  345. request.encoding = 'utf-8'
  346. fieldDict = request.POST
  347. if request.method == 'GET':
  348. request.encoding = 'utf-8'
  349. fieldDict = request.GET
  350. token = request.POST.get('token', None)
  351. type = request.POST.get('type', None)
  352. response = ResponseObject()
  353. if token != None:
  354. tko = TokenObject(token)
  355. tko.valid()
  356. response.lang = tko.lang
  357. if tko.code == 0:
  358. userID = tko.userID
  359. if userID:
  360. if type == 'PC':
  361. return showAllUserEquipmentPC(userID, fieldDict, response)
  362. else:
  363. return showAllUserEquipment(userID, response)
  364. else:
  365. return response.json(309)
  366. else:
  367. return response.json(tko.code)
  368. else:
  369. return response.json(800)
  370. @csrf_exempt
  371. def findEquipmentInfoInterface(request, *callback_args, **callback_kwargs):
  372. if request.method == 'GET':
  373. request.encoding = 'gb2312'
  374. fieldDict = request.GET
  375. if request.method == 'POST':
  376. request.encoding = 'utf-8'
  377. fieldDict = request.POST
  378. deviceContent = fieldDict.get('content', None)
  379. token = fieldDict.get('token', None)
  380. response = ResponseObject()
  381. if token != None:
  382. tko = TokenObject(token)
  383. tko.valid()
  384. response.lang = tko.lang
  385. if tko.code == 0:
  386. if deviceContent:
  387. try:
  388. deviceContent = json.loads(deviceContent)
  389. except Exception as e:
  390. print(repr(e))
  391. return response.json(10, repr(e))
  392. else:
  393. resultDict = findEquipmentInfo(content=deviceContent, type=2, fieldDict=fieldDict)
  394. else:
  395. resultDict = findEquipmentInfo(content='', type=1, fieldDict=fieldDict)
  396. return response.json(0, resultDict)
  397. else:
  398. return response.json(tko.code)
  399. else:
  400. return response.json(444, 'token')