ModelService.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. from Model.models import *
  2. from Service.CommonService import CommonService
  3. # 针对模型封装的复用性代码
  4. class ModelService:
  5. # 获取当前用户角色名
  6. @staticmethod
  7. def getRole(rid):
  8. Roles = Role.objects.get(rid=rid)
  9. return Roles.roleName
  10. @staticmethod
  11. def getValidateMember(userID):
  12. device_user_query_set = Device_User.objects.get(userID=userID)
  13. role_query_set = device_user_query_set.role.all()
  14. role_dict = CommonService.qs_to_dict(role_query_set)
  15. permission = role_dict["datas"][0]["fields"]["permission"]
  16. if len(permission):
  17. if 92 in permission:
  18. return True
  19. return False
  20. # 获取用户所有权限
  21. @staticmethod
  22. def own_permission(userID):
  23. device_user_query_set = Device_User.objects.get(userID=userID)
  24. role_query_set = device_user_query_set.role.all()
  25. if role_query_set.exists():
  26. role_dict = CommonService.qs_to_dict(role_query_set)
  27. permission = role_dict["datas"][0]["fields"]["permission"]
  28. if len(permission):
  29. return permission
  30. return []
  31. # 获取用户角色相关
  32. @staticmethod
  33. def own_role(userID):
  34. device_user_query_set = Device_User.objects.get(userID=userID)
  35. role_query_set = device_user_query_set.role.all()
  36. if role_query_set.exists():
  37. return {'rid': role_query_set[0].rid, 'roleName': role_query_set[0].roleName}
  38. return {'rid': '', 'roleName': ''}
  39. # 检测权限有无
  40. @staticmethod
  41. def check_permission(userID, permID):
  42. try:
  43. device_user_query_set = Device_User.objects.get(userID=userID)
  44. if device_user_query_set:
  45. role_query_set = device_user_query_set.role.all()
  46. if role_query_set:
  47. role_dict = CommonService.qs_to_dict(role_query_set)
  48. permission = role_dict["datas"][0]["fields"]["permission"]
  49. print(permission)
  50. if len(permission) > 0:
  51. if permID in permission:
  52. return True
  53. except Exception as e:
  54. return False
  55. return False
  56. # 根据设备主键ID判断是否拥有该设备
  57. @staticmethod
  58. def check_user_own_device(userID, deviceID):
  59. try:
  60. device_user = Device_User.objects.get(userID=userID)
  61. device_info_queryset = device_user.device_info_set.all().values('id')
  62. id_list = []
  63. for id_dict in list(device_info_queryset):
  64. id_list.append(id_dict['id'])
  65. if deviceID in id_list:
  66. return True
  67. except Exception as e:
  68. return False
  69. return False
  70. # 根据设设备唯一名称UID判断是否拥有该设备
  71. @staticmethod
  72. def check_own_device(userID, UID):
  73. device_info_queryset = Device_Info.objects.filter(userID_id=userID, UID=UID)
  74. if device_info_queryset.exists():
  75. return True
  76. return False
  77. # 根据userID获取用户名
  78. @staticmethod
  79. def get_user_name(userID):
  80. try:
  81. device_user = Device_User.objects.get(userID=userID)
  82. except Exception as e:
  83. return None
  84. else:
  85. return device_user.username
  86. # 根据username获取userID
  87. @staticmethod
  88. def get_userID_byname(username):
  89. try:
  90. device_user = Device_User.objects.get(username=username)
  91. except Exception as e:
  92. return None
  93. else:
  94. return device_user.userID
  95. # 访问日志添加
  96. @staticmethod
  97. def addAccessLog(data):
  98. try:
  99. access_log = Access_Log.objects.create(**data)
  100. except Exception as e:
  101. return False
  102. else:
  103. if access_log.id:
  104. return True
  105. else:
  106. return False
  107. # 判断设备账户信息是否正确
  108. @staticmethod
  109. def device_auth(UID, View_Account, View_Password, ChannelIndex):
  110. device_info_queryset = Device_Info.objects.filter(UID=UID, View_Account=View_Account,
  111. View_Password=View_Password, ChannelIndex=ChannelIndex)
  112. if device_info_queryset.exists():
  113. return True
  114. return False