default_data_insert.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 UserRole&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 ---
  55. # unique_dict = {'name': 'CameraConfig'}
  56. # item = Global.mdb.get_one('GlobalVariable', unique_dict)
  57. # data = item.get('args', {})
  58. # data['camera_rtsp'] = config.get('GlobalVariable', {}).get('CameraConfig', {}).get('camera_rtsp', '')
  59. # data['camera_ipv4'] = config.get('GlobalVariable', {}).get('CameraConfig', {}).get('camera_ipv4', '')
  60. # data['camera_user'] = config.get('GlobalVariable', {}).get('CameraConfig', {}).get('camera_user', '')
  61. # data['camera_pass'] = config.get('GlobalVariable', {}).get('CameraConfig', {}).get('camera_pass', '')
  62. # update_dict = {'args': data}
  63. # Global.mdb.update_one('GlobalVariable', unique_dict, update_dict)
  64. # methods.debug_log('default_data_insert.default_data_insert', f"m-68: {data}")