EquipmentManager.py 14 KB

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