ModelService.py 7.5 KB

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