ModelService.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. from Model.models import *
  2. from Service.CommonService import CommonService
  3. import json
  4. from django.db.models import Q
  5. # 针对模型封装的复用性代码
  6. class ModelService:
  7. # 获取当前用户角色名
  8. @staticmethod
  9. def getRole(rid):
  10. return Role.objects.get(rid=rid).roleName
  11. # 获取用户所有权限
  12. @staticmethod
  13. def own_permission(userID):
  14. permission = Device_User.objects.get(userID=userID).role.values_list('permission', flat=True)
  15. if permission:
  16. return list(permission)
  17. return []
  18. # 获取用户角色相关
  19. @staticmethod
  20. def own_role(userID):
  21. try:
  22. role_qs = Device_User.objects.get(userID=userID).role.values('rid', 'roleName')
  23. if role_qs.exists():
  24. return {'rid': role_qs[0]['rid'], 'roleName': role_qs[0]['roleName']}
  25. except Exception as e:
  26. pass
  27. return {'rid': '', 'roleName': ''}
  28. # 检测权限有无
  29. @staticmethod
  30. def check_perm(userID, permID):
  31. try:
  32. perm_list = Device_User.objects.get(userID=userID).role.values_list('permission', flat=True)
  33. if perm_list:
  34. if permID in perm_list:
  35. return True
  36. except Exception as e:
  37. return False
  38. return False
  39. # 根据设备主键ID判断是否拥有该设备
  40. @staticmethod
  41. def check_user_own_device(userID, deviceID):
  42. try:
  43. dvqs = Device_Info.objects.filter(userID_id=userID).values_list('id', flat=True)
  44. if dvqs:
  45. if deviceID in dvqs:
  46. return True
  47. except Exception as e:
  48. return False
  49. return False
  50. # 根据设设备唯一名称UID判断是否拥有该设备
  51. @staticmethod
  52. def check_own_device(userID, UID):
  53. dvqs = Device_Info.objects.filter(userID_id=userID, UID=UID)
  54. if dvqs.exists():
  55. return True
  56. return False
  57. # 根据userID获取用户名
  58. @staticmethod
  59. def get_user_name(userID):
  60. try:
  61. if userID:
  62. device_user = Device_User.objects.get(userID=userID)
  63. return device_user.username
  64. else:
  65. return ''
  66. except Exception as e:
  67. return ''
  68. # 根据username获取userID
  69. @staticmethod
  70. def get_userID_byname(username):
  71. try:
  72. device_user = Device_User.objects.get(Q(username=username) | Q(userEmail=username) | Q(phone=username))
  73. except Exception as e:
  74. return None
  75. else:
  76. return device_user.userID
  77. # 访问日志添加
  78. @staticmethod
  79. def addAccessLog(data):
  80. try:
  81. access_log = Access_Log.objects.create(**data)
  82. except Exception as e:
  83. return False
  84. else:
  85. return True
  86. # 访问日志批量添加
  87. @staticmethod
  88. def add_batch_log(data_list):
  89. try:
  90. if data_list:
  91. querysetlist = []
  92. for i in data_list:
  93. data = json.loads(i.decode('utf-8'))
  94. querysetlist.append(Access_Log(**data))
  95. Access_Log.objects.bulk_create(querysetlist)
  96. else:
  97. return
  98. except Exception as e:
  99. print('ggga')
  100. print(repr(e))
  101. return False
  102. else:
  103. return True
  104. # 通过用户名获取userIDLIST
  105. @staticmethod
  106. def get_user_list_by_username(username):
  107. userID_list = Device_User.objects.filter(Q(username=username) | Q(userEmail=username) | Q(phone=username)). \
  108. values_list('userID', flat=True)
  109. return userID_list