|
| 1 | +from src.config import db, app |
| 2 | +from src.models.user_card_settings import UserCardSettings |
| 3 | +from src.models.dashboard_netowrk import DashboardNetworkSettings |
| 4 | +from src.models.user_dashboard_settings import UserDashboardSettings |
| 5 | +from src.models.feature_toggle_settings import FeatureToggleSettings |
| 6 | +from src.models.application_general_settings import ApplicationGeneralSettings |
| 7 | +from src.models.smtp_configuration import SMTPSettings |
| 8 | +from src.models.network_speed_test_result import NetworkSpeedTestResult |
| 9 | +from src.models.system_information import SystemInformation |
| 10 | +from src.models.user_profile import UserProfile |
| 11 | +from flask_login import current_user |
| 12 | +from werkzeug.security import generate_password_hash |
| 13 | +import json |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +with app.app_context(): |
| 18 | + print("Creating tables") |
| 19 | + db.create_all() |
| 20 | + |
| 21 | + # initialize default dashboard user_dashboard_settings for users |
| 22 | + users = UserProfile.query.all() |
| 23 | + for user in users: |
| 24 | + if not user.dashboard_settings: |
| 25 | + db.session.add(UserDashboardSettings(user_id=user.id)) |
| 26 | + db.session.add(UserCardSettings(user_id=user.id)) |
| 27 | + db.session.add(FeatureToggleSettings(user_id=user.id)) |
| 28 | + db.session.commit() |
| 29 | + |
| 30 | + pre_defined_users_json = "src/assets/predefine_user.json" |
| 31 | + with open(pre_defined_users_json, "r") as file: |
| 32 | + pre_defined_users = json.load(file) |
| 33 | + for user in pre_defined_users: |
| 34 | + if not UserProfile.query.filter_by(user_level=user["user_level"]).first(): |
| 35 | + hashed_password = generate_password_hash(user["password"]) |
| 36 | + user = UserProfile( |
| 37 | + username=user["username"], |
| 38 | + email=user["email"], |
| 39 | + password=hashed_password, |
| 40 | + user_level=user["user_level"], |
| 41 | + receive_email_alerts=user["receive_email_alerts"], |
| 42 | + profession=user["profession"], |
| 43 | + ) |
| 44 | + |
| 45 | + db.session.add(user) |
| 46 | + db.session.commit() |
| 47 | + |
| 48 | + # Initialize default user_dashboard_settings |
| 49 | + general_settings = ApplicationGeneralSettings.query.first() |
| 50 | + if not general_settings: |
| 51 | + db.session.add(ApplicationGeneralSettings()) |
| 52 | + db.session.commit() |
| 53 | + |
| 54 | + |
| 55 | +# ibject for all templates |
| 56 | +@app.context_processor |
| 57 | +def inject_settings(): |
| 58 | + if current_user.is_anonymous: |
| 59 | + return dict(user_dashboard_settings=None, card_settings=None) |
| 60 | + general_settings = ApplicationGeneralSettings.query.first() |
| 61 | + card_settings = UserCardSettings.query.filter_by(user_id=current_user.id).first() |
| 62 | + user_dashboard_settings = UserDashboardSettings.query.filter_by( |
| 63 | + user_id=current_user.id |
| 64 | + ).first() # Retrieve user-specific user_dashboard_settings from DB |
| 65 | + feature_toggles_settings = FeatureToggleSettings.query.filter_by( |
| 66 | + user_id=current_user.id |
| 67 | + ).first() |
| 68 | + all_settings = dict( |
| 69 | + user_dashboard_settings=user_dashboard_settings, |
| 70 | + general_settings=general_settings, |
| 71 | + card_settings=card_settings, |
| 72 | + feature_toggles_settings=feature_toggles_settings, |
| 73 | + ) |
| 74 | + return all_settings |
0 commit comments