From 920ba56403d9239ea3b90e36e428b83d79d11e6d Mon Sep 17 00:00:00 2001 From: SimplifiedPrivacy Date: Thu, 5 Nov 2026 11:46:23 -0500 Subject: [PATCH] Profile Controller now routes to the Repo and Profile Object Creator service --- core/controllers/ProfileController.py | 72 ++----------------- .../controllers/profile_state/data_filters.py | 7 +- core/models/orm_calls/location_calls.py | 6 +- core/services/profiles/create_objects.py | 52 ++++++++------ 4 files changed, 45 insertions(+), 92 deletions(-) diff --git a/core/controllers/ProfileController.py b/core/controllers/ProfileController.py index d44552e..5fe5217 100644 --- a/core/controllers/ProfileController.py +++ b/core/controllers/ProfileController.py @@ -1,7 +1,7 @@ from core.services.profiles.create_objects import create_profile_object from core.services.profiles.ProfileRepository import ProfileRepository -from core.models.orm_calls.application_version_calls import get_application_by_id -from core.models.orm_calls.location_calls import get_profile_location_data +# from core.models.orm_calls.application_version_calls import get_application_by_id +# from core.models.orm_calls.location_calls import get_profile_location_data # from core.services.subscriptions import subscriptions from core.models.Result import Result, ResultError from core.errors.logger import logger @@ -60,22 +60,11 @@ class ProfileController: if not potential_data or not isinstance(potential_data, dict): return Result(valid=False, error_type=ResultError.INVALID_INPUT, message=f"The data to create the profile is not structured properly. It's type {type(potential_data)}") - # profile_type = potential_data.get('Type', None) - # protocol = potential_data.get('Protocol', None).value - # location_id = potential_data.get('Location', None) - # location = get_profile_location_data(location_id) - # print(f"location is {type(location)}") - # name = potential_data.get('Name', None) - - # if not profile_type or not isinstance(profile_type, ProfileType): - # return Result(valid=False, error_type=ResultError.INVALID_INPUT, message="The data to create the profile is not structured properly. It's missing the type") - - # logger.info("Passed initial data screen.") - # Get next id from the Repo, which confirms with what's on disk: profile_id = ProfileController._repo.get_next_id() logger.info(f"New profile id is {profile_id}") + # Create the object: created_object = create_profile_object(profile_id=profile_id, potential_data=potential_data) if not created_object.valid: @@ -83,59 +72,10 @@ class ProfileController: profile = created_object.data - - # if profile_type == ProfileType.SESSION: - # # SESSION ONLY PRE-REQS - # resolution = potential_data.get('Resolution', None) - # app_id = potential_data.get('Browser', None) - # if resolution and app_id: - # logger.info(f"Creating a session profile with {resolution} and {app_id}") - # else: - # error_msg = "Missing the resolution or app version" - # logger.error(error_msg) - # return Result(valid=False, error_type=ResultError.INVALID_INPUT, message=error_msg) - - # # Lookup App: - # application_version = get_application_by_id(app_id) - # if not application_version: - # error_msg = "Failed to find the app by id" - # logger.error(error_msg) - # return Result(valid=False, error_type=ResultError.INVALID_INPUT, message=error_msg) - - # logger.info(f"application_version is {application_version}") - # connection = SessionConnection(protocol) - - # profile = SessionProfile( - # id=profile_id, - # name=name, - # subscription=None, - # type=ProfileType.SESSION, - # location=location, - # resolution=resolution, - # application_version=application_version, - # connection=connection - # ) - - # elif profile_type == ProfileType.SYSTEM: - # connection = SystemConnection(protocol) - # profile = SystemProfile( - # id=profile_id, - # name=name, - # type=ProfileType.SYSTEM, - # subscription=None, - # location=location, - # connection=connection - # ) - - # else: - # error_msg = f"Invalid profile type of {profile_type} used to create a profile." - # logger.error(error_msg) - # return Result(valid=False, error_type=ResultError.INVALID_INPUT, message=error_msg) - - # Save and sync with the repo. - print(f"profile type is {type(profile)}") + # Save the object and sync with the repo. ProfileController._save_and_sync(profile, 'created', profile_observer) - print("saved it.") + + # Update UI & Return: Notifications.emit(EventType.PROFILES_CHANGED) return Result(valid=True) diff --git a/core/controllers/profile_state/data_filters.py b/core/controllers/profile_state/data_filters.py index 67ef36d..f88ac51 100644 --- a/core/controllers/profile_state/data_filters.py +++ b/core/controllers/profile_state/data_filters.py @@ -27,10 +27,11 @@ def get_supported_protocols_for_location(location_codes: str, profile_type: str logger.error(error_msg) return Result(valid=False, error_type=ResultError.INVALID_INPUT, message=error_msg) - country, city = location_codes.split("_") + country_code, city_code = location_codes.split("_") - # this IS doing sql: - location = get_profile_location_data(country_code=country, city_code=city) + # this is doing sql: + logger.info(f"country_code is {country_code} and city is {city_code}") + location = get_profile_location_data(country_code=country_code, city_code=city_code) if not location: error_msg = f"This is a developer error. No location data found for {location_codes}" diff --git a/core/models/orm_calls/location_calls.py b/core/models/orm_calls/location_calls.py index 996d577..e61ec58 100644 --- a/core/models/orm_calls/location_calls.py +++ b/core/models/orm_calls/location_calls.py @@ -22,6 +22,9 @@ def execute_location_sql(country_code: str, city_code: str, session: Session) -> def get_profile_location_data(country_code: str, city_code: str) -> Location: + + print(f"country_code is {country_code}") + location_object = execute_location_sql(country_code, city_code) if location_object.valid: return location_object.data @@ -68,7 +71,8 @@ def execute_location_by_id(location_id: int, session: Session) -> DatabaseOperat ).scalar_one_or_none() return location_object -def get_profile_location_data(location_id: int) -> Location: + +def get_profile_location_data_by_id(location_id: int) -> Location: location_object = execute_location_by_id(location_id) if location_object.valid: return location_object.data diff --git a/core/services/profiles/create_objects.py b/core/services/profiles/create_objects.py index 1e2a90c..70b5c22 100644 --- a/core/services/profiles/create_objects.py +++ b/core/services/profiles/create_objects.py @@ -1,7 +1,7 @@ from core.models.orm_models.Location import Location from core.models.enum_models import ProtocolChoice from core.models.orm_calls.application_version_calls import get_application_by_id -from core.models.orm_calls.location_calls import get_profile_location_data +from core.models.orm_calls.location_calls import get_profile_location_data_by_id from core.models.system.SystemConnection import SystemConnection from core.models.session.SessionConnection import SessionConnection from core.models.session.SessionProfile import SessionProfile @@ -38,7 +38,7 @@ def create_session_object( logger.error(error_msg) return Result(valid=False, error_type=ResultError.INVALID_INPUT, message=error_msg) - logger.info(f"application_version is {application_version}") + # logger.info(f"application_version is {application_version}") connection = SessionConnection(protocol.value) profile = SessionProfile( @@ -54,6 +54,28 @@ def create_session_object( return Result(valid=True, data=profile) +@type_checker +def create_system_object( + profile_id: int, + potential_data: dict, + protocol: ProtocolChoice, + location: Location, + name: str +) -> Result: + """Return a SessionProfile object""" + connection = SystemConnection(protocol.value) + profile = SystemProfile( + id=profile_id, + name=name, + type=ProfileType.SYSTEM, + subscription=None, + location=location, + connection=connection + ) + return Result(valid=True, data=profile) + + + def create_profile_object(profile_id: int, potential_data: dict) -> Result: """Creates and returns a literal profile object. (but not save to disk.)""" @@ -63,8 +85,7 @@ def create_profile_object(profile_id: int, potential_data: dict) -> Result: profile_type = potential_data.get('Type', None) protocol = potential_data.get('Protocol', None) location_id = potential_data.get('Location', None) - location = get_profile_location_data(location_id) - print(f"location is {type(location)}") + location = get_profile_location_data_by_id(location_id) name = potential_data.get('Name', None) if not profile_type or not isinstance(profile_type, ProfileType): @@ -76,31 +97,18 @@ def create_profile_object(profile_id: int, potential_data: dict) -> Result: # SESSION #--------------------------- if profile_type == ProfileType.SESSION: - created = create_session_object(profile_id=profile_id, potential_data=potential_data, protocol=protocol, location=location, name=name) - if not created.valid: - error_msg = "Failed to create Session Profile" - logger.error(error_msg) - return created.wrap(error_msg) - profile = created.data + return create_session_object(profile_id=profile_id, potential_data=potential_data, protocol=protocol, location=location, name=name) #--------------------------- # SYSTEM #--------------------------- elif profile_type == ProfileType.SYSTEM: - connection = SystemConnection(protocol.value) - profile = SystemProfile( - id=profile_id, - name=name, - type=ProfileType.SYSTEM, - subscription=None, - location=location, - connection=connection - ) + return create_system_object(profile_id=profile_id, potential_data=potential_data, protocol=protocol, location=location, name=name) + #--------------------------- + # OTHER + #--------------------------- else: error_msg = f"Invalid profile type of {profile_type} used to create a profile." logger.error(error_msg) return Result(valid=False, error_type=ResultError.INVALID_INPUT, message=error_msg) - - logger.info("Created the profile object.") - return Result(valid=True, data=profile) \ No newline at end of file