ModelService.py 3.3 KB

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