EquipmentManager.py 14 KB

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