ModelService.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. import logging
  2. import time
  3. import requests
  4. from Ansjer.config import BASE_DIR
  5. from Model.models import *
  6. import json
  7. from django.db.models import Q
  8. # 针对模型封装的复用性代码
  9. class ModelService:
  10. # UID Manage检测权限
  11. @staticmethod
  12. def check_perm_uid_manage(userID, permID):
  13. try:
  14. user_qs = UserModel.objects.filter(id=userID)
  15. if user_qs.exists():
  16. user = user_qs[0]
  17. if int(user.permission) != 0:
  18. return False
  19. else:
  20. return True
  21. else:
  22. return False
  23. except Exception as e:
  24. print(repr(e))
  25. # 获取当前用户角色名
  26. @staticmethod
  27. def getRole(rid):
  28. return Role.objects.get(rid=rid).roleName
  29. # 获取用户所有权限
  30. @staticmethod
  31. def own_permission(userID):
  32. permission = Device_User.objects.get(userID=userID).role.values_list('permission', flat=True)
  33. if permission:
  34. return list(permission)
  35. return []
  36. # 获取用户角色相关
  37. @staticmethod
  38. def own_role(userID):
  39. try:
  40. role_qs = Device_User.objects.get(userID=userID).role.values('rid', 'roleName')
  41. if role_qs.exists():
  42. return {'rid': role_qs[0]['rid'], 'roleName': role_qs[0]['roleName']}
  43. except Exception as e:
  44. pass
  45. return {'rid': '', 'roleName': ''}
  46. # 检测权限有无
  47. @staticmethod
  48. def check_perm(userID, permID):
  49. try:
  50. perm_list = Device_User.objects.get(userID=userID).role.values_list('permission', flat=True)
  51. if perm_list:
  52. if permID in perm_list:
  53. return True
  54. except Exception as e:
  55. return False
  56. return False
  57. # 根据设备主键ID判断是否拥有该设备
  58. @staticmethod
  59. def check_user_own_device(userID, deviceID):
  60. try:
  61. dvqs = Device_Info.objects.filter(userID_id=userID).values_list('id', flat=True)
  62. if dvqs:
  63. if deviceID in dvqs:
  64. return True
  65. except Exception as e:
  66. return False
  67. return False
  68. # 根据设设备唯一名称UID判断是否拥有该设备
  69. @staticmethod
  70. def check_own_device(userID, UID):
  71. dvqs = Device_Info.objects.filter(userID_id=userID, UID=UID)
  72. if dvqs.exists():
  73. return True
  74. return False
  75. # 根据userID获取用户名
  76. @staticmethod
  77. def get_user_name(userID):
  78. try:
  79. if userID:
  80. device_user = Device_User.objects.get(userID=userID)
  81. return device_user.username
  82. else:
  83. return ''
  84. except Exception as e:
  85. return ''
  86. @staticmethod
  87. def get_user_mark(userID):
  88. if userID:
  89. qs = Device_User.objects.filter(userID=userID).values('username', 'userEmail', 'phone')
  90. if qs[0]['username']:
  91. return qs[0]['username']
  92. elif qs[0]['userEmail']:
  93. return qs[0]['userEmail']
  94. elif qs[0]['phone']:
  95. return qs[0]['phone']
  96. else:
  97. return ''
  98. else:
  99. return ''
  100. # 根据username获取userID
  101. @staticmethod
  102. def get_userID_byname(username):
  103. try:
  104. device_user = Device_User.objects.get(Q(username=username) | Q(userEmail=username) | Q(phone=username))
  105. except Exception as e:
  106. return None
  107. else:
  108. return device_user.userID
  109. # 访问日志添加
  110. @staticmethod
  111. def addAccessLog(data):
  112. try:
  113. access_log = Access_Log.objects.create(**data)
  114. except Exception as e:
  115. return False
  116. else:
  117. return True
  118. # 访问日志批量添加
  119. @staticmethod
  120. def add_batch_log(data_list):
  121. try:
  122. if data_list:
  123. querysetlist = []
  124. for i in data_list:
  125. data = json.loads(i.decode('utf-8'))
  126. querysetlist.append(Access_Log(**data))
  127. Access_Log.objects.bulk_create(querysetlist)
  128. else:
  129. return
  130. except Exception as e:
  131. print('ggga')
  132. print(repr(e))
  133. return False
  134. else:
  135. return True
  136. # 通过用户名获取userIDLIST
  137. @staticmethod
  138. def get_user_list_by_username(username):
  139. userID_list = Device_User.objects.filter(Q(username=username) | Q(userEmail=username) | Q(phone=username)). \
  140. values_list('userID', flat=True)
  141. return userID_list
  142. @staticmethod
  143. def del_eq_info(userID, uid):
  144. notify_alexa_delete(userID, uid)
  145. ei_qs = Equipment_Info.objects.filter(userID_id=userID, devUid=uid)
  146. ei_qs.delete()
  147. # ei_count = ei_qs.count()
  148. # while (ei_count > 1000):
  149. # ei_qs[0:1000].delete()
  150. # 根据设备主键ID判断是否拥有该设备
  151. # 获取绑定用户设备列表
  152. @staticmethod
  153. def get_uid_list(userID):
  154. uid_list = Device_Info.objects.filter(userID_id=userID).values_list('UID', flat=True)
  155. return list(uid_list)
  156. @staticmethod
  157. def notify_alexa_add(uid, userID, nickname, encrypt_pwd):
  158. url = 'https://www.zositech.xyz/deviceStatus/addOrUpdate'
  159. data = {
  160. 'UID': uid,
  161. 'userID': userID,
  162. 'uid_nick': nickname,
  163. 'password': encrypt_pwd,
  164. }
  165. try:
  166. res = requests.post(url, data=data, timeout=5)
  167. except Exception as e:
  168. print(repr(e))
  169. @staticmethod
  170. def add_log(ip, userID, operation):
  171. file_path = '/'.join((BASE_DIR, 'static/delete_device.log'))
  172. file = open(file_path, 'a+')
  173. file.write(ip + "; username:" + userID + "; time:" + time.strftime(
  174. "%Y-%m-%d %H:%M:%S", time.localtime()) + "; " + operation)
  175. file.write('\n')
  176. file.flush()
  177. file.close()
  178. @staticmethod
  179. def update_log(ip, userID, operation, content, id):
  180. content['xid'] = id
  181. file_path = '/'.join((BASE_DIR, 'static/update_device.log'))
  182. file = open(file_path, 'a+')
  183. file.write(ip + "; username:" + userID + "; time:" + time.strftime(
  184. "%Y-%m-%d %H:%M:%S", time.localtime()) + "; content:" + json.dumps(content) + "; " + operation)
  185. file.write('\n')
  186. file.flush()
  187. file.close()
  188. @staticmethod
  189. def delete_log(ip, userID, operation, UID):
  190. file_path = '/'.join((BASE_DIR, 'static/delete_device.log'))
  191. file = open(file_path, 'a+')
  192. file.write(ip + "; username:" + userID + "; time:" + time.strftime(
  193. "%Y-%m-%d %H:%M:%S", time.localtime()) + "; " + operation + "; uid:" + UID)
  194. file.write('\n')
  195. file.flush()
  196. file.close()
  197. @staticmethod
  198. def add_ip_log(ip, info):
  199. file_path = '/'.join((BASE_DIR, 'static/get_timezone.log'))
  200. file = open(file_path, 'a+')
  201. file.write(ip + "; info:" + str(info) + "; time:" + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
  202. file.write('\n')
  203. file.flush()
  204. file.close()
  205. @staticmethod
  206. def add_tmp_log(info):
  207. file_path = '/'.join((BASE_DIR, 'static/tmp_test.log'))
  208. file = open(file_path, 'a+')
  209. file.write("info:" + str(info))
  210. file.write('\n')
  211. file.flush()
  212. file.close()
  213. @staticmethod
  214. def app_log_log(userID, UID):
  215. file_path = '/'.join((BASE_DIR, 'static/app_log.log'))
  216. file = open(file_path, 'a+')
  217. file.write("username:" + userID + "; time:" + time.strftime(
  218. "%Y-%m-%d %H:%M:%S", time.localtime()) + "; " + "; uid:" + UID)
  219. file.write('\n')
  220. file.flush()
  221. file.close()
  222. def notify_alexa_delete(userID, UID):
  223. url = 'https://www.zositech.xyz/deviceStatus/delete'
  224. data = {
  225. 'userID': userID,
  226. 'UID': UID
  227. }
  228. try:
  229. requests.post(url=url, data=data, timeout=5)
  230. except Exception as e:
  231. print(repr(e))