default_data_insert.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. from werkzeug.security import generate_password_hash
  2. from hub import methods, Global
  3. def default_data_insert():
  4. # --- check ---
  5. from default_data import data
  6. if not data.get('enable'):
  7. return
  8. config = data.get('config')
  9. # --- check Role&User ---
  10. d1 = {
  11. 'role': [],
  12. 'user': [],
  13. }
  14. for item in config.get('Role&User'):
  15. # --- insert role_info ---
  16. role_info = item.get('role_info')
  17. unique_dict = {
  18. 'role_type': role_info.get('role_type'),
  19. }
  20. mdb_role = Global.mdb.get_one('UserRole', unique_dict)
  21. # --- check ---
  22. if not mdb_role:
  23. update_dict = {
  24. 'role_type': role_info.get('role_type'),
  25. 'role_name': role_info.get('role_name'),
  26. 'switch_list': role_info.get('switch_list'),
  27. 'create_at': methods.now_ts(),
  28. }
  29. rid = Global.mdb.update_one('UserRole', unique_dict, update_dict)
  30. d1['role'].append(rid)
  31. else:
  32. rid = str(mdb_role.get('_id'))
  33. d1['role'].append(rid)
  34. # --- insert users_info ---
  35. users_info = item.get('users_info')
  36. for user in users_info:
  37. unique_dict = {'username': user.get('username')}
  38. # mdb_user = Global.mdb.get_one('User', unique_dict)
  39. mdb_user = Global.mdb.get_one('UserInfo', unique_dict)
  40. # --- check ---
  41. if not mdb_user:
  42. update_dict = {
  43. 'password': generate_password_hash(user.get('password')),
  44. 'role_type': role_info.get('role_type'),
  45. 'create_at': methods.now_ts(),
  46. }
  47. # uid = Global.mdb.update_one('User', unique_dict, update_dict)
  48. uid = Global.mdb.update_one('UserInfo', unique_dict, update_dict)
  49. d1['user'].append(uid)
  50. else:
  51. uid = str(mdb_user.get('_id'))
  52. d1['user'].append(uid)
  53. methods.debug_log('default_data_insert.default_data_insert.60', f"#d1: {d1}")
  54. # --- check VehicleInfo ---
  55. for item in config.get('VehicleInfo'):
  56. unique_dict = {'address': item.get('address')}
  57. data = Global.mdb.get_one('VehicleInfo', unique_dict)
  58. if not data:
  59. update_dict = {
  60. 'name': item.get('name'),
  61. 'address': item.get('address'),
  62. 'state': 1,
  63. }
  64. uid = Global.mdb.update_one('VehicleInfo', unique_dict, update_dict)
  65. # --- check ---
  66. # unique_dict = {'name': 'CameraConfig'}
  67. # item = Global.mdb.get_one('GlobalVariable', unique_dict)
  68. # data = item.get('args', {})
  69. # data['camera_rtsp'] = config.get('GlobalVariable', {}).get('CameraConfig', {}).get('camera_rtsp', '')
  70. # data['camera_ipv4'] = config.get('GlobalVariable', {}).get('CameraConfig', {}).get('camera_ipv4', '')
  71. # data['camera_user'] = config.get('GlobalVariable', {}).get('CameraConfig', {}).get('camera_user', '')
  72. # data['camera_pass'] = config.get('GlobalVariable', {}).get('CameraConfig', {}).get('camera_pass', '')
  73. # update_dict = {'args': data}
  74. # Global.mdb.update_one('GlobalVariable', unique_dict, update_dict)
  75. # methods.debug_log('default_data_insert.default_data_insert', f"m-68: {data}")