EquipmentManager.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. # 查询用户设备
  11. @csrf_exempt
  12. def queryUserEquipmentInterface(request):
  13. request.encoding = 'utf-8'
  14. response = ResponseObject()
  15. if request.method == 'POST':
  16. request_dict = request.POST
  17. elif request.method == 'GET':
  18. request_dict = request.GET
  19. else:
  20. return response.json(444)
  21. token = request_dict.get('token', None)
  22. tko = TokenObject(token)
  23. response.lang = tko.lang
  24. if tko.code != 0:
  25. return response.json(tko.code)
  26. userID = tko.userID
  27. if not userID:
  28. return response.json(104)
  29. dvqs = Device_Info.objects.filter(userID_id=userID)
  30. dvdict = CommonService.qs_to_dict(dvqs)
  31. uid_list = Device_Info.objects.filter(userID_id=userID, isShare=False). \
  32. values_list('UID', flat=True)
  33. ubqs = UID_Bucket.objects.filter(uid__in=uid_list). \
  34. values('bucket__content', 'status', 'channel', 'endTime', 'uid')
  35. res = []
  36. for p in dvdict['datas']:
  37. p['fields']['vod'] = []
  38. for dm in ubqs:
  39. if p['fields']['UID'] == dm['uid']:
  40. p['fields']['vod'].append(dm)
  41. p['fields']['primaryUserID'] = ''
  42. res.append(p)
  43. return response.json(0, {'datas': res})
  44. @csrf_exempt
  45. def addNewUserEquipmentInterface(request):
  46. request.encoding = 'utf-8'
  47. response = ResponseObject()
  48. if request.method == 'POST':
  49. request_dict = request.POST
  50. elif request.method == 'GET':
  51. request_dict = request.GET
  52. else:
  53. return response.json(444)
  54. token = request_dict.get('token', None)
  55. deviceContent = request_dict.get('content', None)
  56. if not deviceContent:
  57. return response.json(444, 'content')
  58. tko = TokenObject(token)
  59. response.lang = tko.lang
  60. if tko.code != 0:
  61. return response.json(tko.code)
  62. userID = tko.userID
  63. userIDValid = Device_User.objects.filter(userID=userID)
  64. if userIDValid:
  65. try:
  66. try:
  67. deviceData = json.loads(deviceContent)
  68. except Exception as e:
  69. return response.json(444, repr(e))
  70. else:
  71. UID = deviceData.get('UID', None)
  72. if not UID:
  73. return response.json(444, 'content')
  74. dValid = Device_Info.objects.filter(userID=userID, UID=UID)
  75. if dValid:
  76. return response.json(174)
  77. else:
  78. UID = deviceData.get('UID', '')
  79. re_uid = re.compile(r'^[A-Za-z0-9]{20}$')
  80. if re_uid.match(UID):
  81. is_bind = Device_Info.objects.filter(UID=UID, isShare=False)
  82. # 判断是否有已绑定用户
  83. if not is_bind.exists():
  84. userDevice = Device_Info(id=CommonService.getUserID(getUser=False), userID_id=userID,
  85. **deviceData)
  86. userDevice.save()
  87. else:
  88. # 分享添加
  89. if 'isShare' in deviceData:
  90. deviceData['isShare'] = True
  91. else:
  92. deviceData['isShare'] = True
  93. bind_userID = is_bind[0].userID_id
  94. userDevice = Device_Info(id=CommonService.getUserID(getUser=False),
  95. userID_id=userID,
  96. primaryUserID=bind_userID,
  97. primaryMaster=ModelService.get_user_name(bind_userID),
  98. **deviceData)
  99. userDevice.save()
  100. else:
  101. return response.json(444, 'UID')
  102. except Exception as e:
  103. errorInfo = traceback.format_exc()
  104. print('添加设备错误: %s ' % errorInfo)
  105. return response.json(178, repr(e))
  106. else:
  107. sqlDict = CommonService.qs_to_dict([userDevice])
  108. return response.json(0, sqlDict)
  109. else:
  110. return response.json(104)
  111. @csrf_exempt
  112. def delUserEquipmentInterface(request):
  113. '''
  114. 删除用户设备
  115. :param request:
  116. :return:
  117. '''
  118. response = ResponseObject()
  119. request.encoding = 'utf-8'
  120. if request.method == 'POST':
  121. request_dict = request.POST
  122. elif request.method == 'GET':
  123. request_dict = request.GET
  124. else:
  125. return response.json(444)
  126. token = request_dict.get('token', None)
  127. id = request_dict.get('id', None)
  128. if not id:
  129. return response.json(444, 'id')
  130. tko = TokenObject(token)
  131. response.lang = tko.lang
  132. if tko.code != 0:
  133. return response.json(tko.code)
  134. userID = tko.userID
  135. # 主用户删除设备全部删除
  136. try:
  137. Device_Info.objects.filter(userID_id=userID, id=id).delete()
  138. except Exception as e:
  139. errorInfo = traceback.format_exc()
  140. print('删除数据库记录错误: %s' % errorInfo)
  141. return response.json(176, repr(e))
  142. else:
  143. return response.json(0)
  144. @csrf_exempt
  145. def deleteInterface(request):
  146. '''
  147. 删除用户设备
  148. :param request:
  149. :return:
  150. '''
  151. response = ResponseObject()
  152. request.encoding = 'utf-8'
  153. if request.method == 'POST':
  154. request_dict = request.POST
  155. elif request.method == 'GET':
  156. request_dict = request.GET
  157. else:
  158. return response.json(444)
  159. token = request_dict.get('token', None)
  160. id = request_dict.get('id', None)
  161. if not id:
  162. return response.json(444, 'id')
  163. tko = TokenObject(token)
  164. response.lang = tko.lang
  165. if tko.code != 0:
  166. return response.json(tko.code)
  167. userID = tko.userID
  168. # 主用户删除设备全部删除
  169. try:
  170. dv_qs = Device_Info.objects.filter(userID_id=userID, id=id)
  171. uid = dv_qs[0].UID
  172. if dv_qs[0].isShare:
  173. dv_qs.delete()
  174. else:
  175. # 主用户删除设备
  176. dv_qs.delete()
  177. # 分享获得用户假删除
  178. ud_dv_qs = Device_Info.objects.filter(UID=uid)
  179. if ud_dv_qs.exists():
  180. ud_dv_qs.update(isExist=0)
  181. except Exception as e:
  182. errorInfo = traceback.format_exc()
  183. print('删除数据库记录错误: %s' % errorInfo)
  184. return response.json(176, repr(e))
  185. else:
  186. return response.json(0)
  187. @csrf_exempt
  188. def modifyUserEquipmentInterface(request):
  189. '''
  190. 修改用户设备
  191. :param request:
  192. :return:
  193. '''
  194. response = ResponseObject()
  195. if request.method == 'POST':
  196. request.encoding = 'utf-8'
  197. request_dict = request.POST
  198. elif request.method == 'GET':
  199. request.encoding = 'utf-8'
  200. request_dict = request.GET
  201. else:
  202. return response.json(444)
  203. token = request_dict.get('token', None)
  204. deviceContent = request_dict.get('content', None)
  205. id = request_dict.get('id', None)
  206. if not deviceContent or not id:
  207. return response.json(444, 'content,id')
  208. tko = TokenObject(token)
  209. response.lang = tko.lang
  210. if tko.code != 0:
  211. return response.json(tko.code)
  212. userID = tko.userID
  213. if userID is None:
  214. return response.json(309)
  215. try:
  216. deviceData = json.loads(deviceContent)
  217. Device_Info.objects.filter(userID_id=userID, id=id).update(**deviceData)
  218. except Exception as e:
  219. return response.json(177, repr(e))
  220. else:
  221. qs = Device_Info.objects.filter(userID_id=userID, id=id)
  222. res = CommonService.qs_to_dict(qs)
  223. return response.json(0, res)
  224. @csrf_exempt
  225. def showAllUserEquipmentInterface(request, *callback_args, **callback_kwargs):
  226. request.encoding = 'utf-8'
  227. response = ResponseObject()
  228. if request.method == 'POST':
  229. fieldDict = request.POST
  230. elif request.method == 'GET':
  231. fieldDict = request.GET
  232. else:
  233. return response.json(444)
  234. token = request.POST.get('token', None)
  235. type = request.POST.get('type', None)
  236. tko = TokenObject(token)
  237. response.lang = tko.lang
  238. if tko.code != 0:
  239. return response.json(tko.code)
  240. userID = tko.userID
  241. if not userID:
  242. return response.json(104)
  243. own_perm = ModelService.check_perm(userID=userID, permID=30)
  244. if not own_perm:
  245. return response.json(404)
  246. if type == 'PC':
  247. page = int(fieldDict['page'])
  248. line = int(fieldDict['line'])
  249. qs = Device_Info.objects.all()
  250. count = qs.count()
  251. res = qs[(page - 1) * line:page * line]
  252. sqlDict = CommonService.qs_to_dict(query_set=res)
  253. sqlDict['count'] = count
  254. return response.json(0, sqlDict)
  255. else:
  256. qs = Device_Info.objects.all()
  257. res = CommonService.qs_to_dict(qs)
  258. return response.json(0, res)
  259. @csrf_exempt
  260. def findEquipmentInfoInterface(request, *callback_args, **callback_kwargs):
  261. request.encoding = 'utf-8'
  262. response = ResponseObject()
  263. if request.method == 'GET':
  264. fieldDict = request.GET
  265. elif request.method == 'POST':
  266. fieldDict = request.POST
  267. else:
  268. return response.json(444)
  269. deviceContent = fieldDict.get('content', None)
  270. token = fieldDict.get('token', None)
  271. tko = TokenObject(token)
  272. response.lang = tko.lang
  273. if tko.code != 0:
  274. return response.json(tko.code)
  275. if deviceContent:
  276. try:
  277. searchCondition = json.loads(deviceContent)
  278. except Exception as e:
  279. print(repr(e))
  280. return response.json(10, repr(e))
  281. else:
  282. kwargs = CommonService.get_kwargs(data=searchCondition)
  283. qs = Device_Info.objects.filter(**kwargs)
  284. page = int(fieldDict['page'])
  285. line = int(fieldDict['line'])
  286. count = qs.count()
  287. res = qs[(page - 1) * line:page * line]
  288. send_dict = CommonService.qs_to_dict(query_set=res)
  289. for k, v in enumerate(send_dict["datas"]):
  290. for val in res:
  291. if v['pk'] == val.id:
  292. username = ModelService.get_user_name(send_dict["datas"][k]['fields']['userID'])
  293. send_dict["datas"][k]['fields']['username'] = username
  294. primary = ModelService.get_user_name(send_dict["datas"][k]['fields']['primaryUserID'])
  295. send_dict["datas"][k]['fields']['primaryusername'] = primary
  296. send_dict['count'] = count
  297. return response.json(0, send_dict)
  298. else:
  299. qs = Device_Info.objects.all()
  300. page = int(fieldDict['page'])
  301. line = int(fieldDict['line'])
  302. count = qs.count()
  303. res = qs[(page - 1) * line:page * line]
  304. send_dict = CommonService.qs_to_dict(query_set=res)
  305. for k, v in enumerate(send_dict["datas"]):
  306. for val in res:
  307. if v['pk'] == val.id:
  308. username = ModelService.get_user_name(send_dict["datas"][k]['fields']['userID'])
  309. send_dict["datas"][k]['fields']['username'] = username
  310. primary = ModelService.get_user_name(send_dict["datas"][k]['fields']['primaryUserID'])
  311. send_dict["datas"][k]['fields']['primaryusername'] = primary
  312. send_dict['count'] = count
  313. return response.json(0, send_dict)