default_data_insert.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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('UserInfo', unique_dict)
  39. # --- check ---
  40. if not mdb_user:
  41. update_dict = {
  42. 'password': generate_password_hash(user.get('password')),
  43. 'role_type': role_info.get('role_type'),
  44. 'create_at': methods.now_ts(),
  45. }
  46. uid = Global.mdb.update_one('UserInfo', unique_dict, update_dict)
  47. d1['user'].append(uid)
  48. else:
  49. uid = str(mdb_user.get('_id'))
  50. d1['user'].append(uid)
  51. methods.debug_log('default_data_insert.default_data_insert.60', f"#d1: {d1}")
  52. # --- check ---
  53. # unique_dict = {'name': 'CameraConfig'}
  54. # item = Global.mdb.get_one('GlobalVariable', unique_dict)
  55. # data = item.get('args', {})
  56. # data['camera_rtsp'] = config.get('GlobalVariable', {}).get('CameraConfig', {}).get('camera_rtsp', '')
  57. # data['camera_ipv4'] = config.get('GlobalVariable', {}).get('CameraConfig', {}).get('camera_ipv4', '')
  58. # data['camera_user'] = config.get('GlobalVariable', {}).get('CameraConfig', {}).get('camera_user', '')
  59. # data['camera_pass'] = config.get('GlobalVariable', {}).get('CameraConfig', {}).get('camera_pass', '')
  60. # update_dict = {'args': data}
  61. # Global.mdb.update_one('GlobalVariable', unique_dict, update_dict)
  62. # methods.debug_log('default_data_insert.default_data_insert', f"m-68: {data}")