EquipmentManager.py 13 KB

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