Profile Controller now routes to the Repo and Profile Object Creator service

This commit is contained in:
SimplifiedPrivacy 2026-11-05 11:46:23 -05:00
parent 02cc80b152
commit 920ba56403
4 changed files with 45 additions and 92 deletions

View file

@ -1,7 +1,7 @@
from core.services.profiles.create_objects import create_profile_object from core.services.profiles.create_objects import create_profile_object
from core.services.profiles.ProfileRepository import ProfileRepository 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.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
# from core.services.subscriptions import subscriptions # from core.services.subscriptions import subscriptions
from core.models.Result import Result, ResultError from core.models.Result import Result, ResultError
from core.errors.logger import logger from core.errors.logger import logger
@ -60,22 +60,11 @@ class ProfileController:
if not potential_data or not isinstance(potential_data, dict): 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)}") 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: # Get next id from the Repo, which confirms with what's on disk:
profile_id = ProfileController._repo.get_next_id() profile_id = ProfileController._repo.get_next_id()
logger.info(f"New profile id is {profile_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) created_object = create_profile_object(profile_id=profile_id, potential_data=potential_data)
if not created_object.valid: if not created_object.valid:
@ -83,59 +72,10 @@ class ProfileController:
profile = created_object.data profile = created_object.data
# Save the object and sync with the repo.
# 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)}")
ProfileController._save_and_sync(profile, 'created', profile_observer) ProfileController._save_and_sync(profile, 'created', profile_observer)
print("saved it.")
# Update UI & Return:
Notifications.emit(EventType.PROFILES_CHANGED) Notifications.emit(EventType.PROFILES_CHANGED)
return Result(valid=True) return Result(valid=True)

View file

@ -27,10 +27,11 @@ def get_supported_protocols_for_location(location_codes: str, profile_type: str
logger.error(error_msg) logger.error(error_msg)
return Result(valid=False, error_type=ResultError.INVALID_INPUT, message=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: # this is doing sql:
location = get_profile_location_data(country_code=country, city_code=city) 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: if not location:
error_msg = f"This is a developer error. No location data found for {location_codes}" error_msg = f"This is a developer error. No location data found for {location_codes}"

View file

@ -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: 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) location_object = execute_location_sql(country_code, city_code)
if location_object.valid: if location_object.valid:
return location_object.data return location_object.data
@ -68,7 +71,8 @@ def execute_location_by_id(location_id: int, session: Session) -> DatabaseOperat
).scalar_one_or_none() ).scalar_one_or_none()
return location_object 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) location_object = execute_location_by_id(location_id)
if location_object.valid: if location_object.valid:
return location_object.data return location_object.data

View file

@ -1,7 +1,7 @@
from core.models.orm_models.Location import Location from core.models.orm_models.Location import Location
from core.models.enum_models import ProtocolChoice 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.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.system.SystemConnection import SystemConnection
from core.models.session.SessionConnection import SessionConnection from core.models.session.SessionConnection import SessionConnection
from core.models.session.SessionProfile import SessionProfile from core.models.session.SessionProfile import SessionProfile
@ -38,7 +38,7 @@ def create_session_object(
logger.error(error_msg) logger.error(error_msg)
return Result(valid=False, error_type=ResultError.INVALID_INPUT, message=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) connection = SessionConnection(protocol.value)
profile = SessionProfile( profile = SessionProfile(
@ -54,6 +54,28 @@ def create_session_object(
return Result(valid=True, data=profile) 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: def create_profile_object(profile_id: int, potential_data: dict) -> Result:
"""Creates and returns a literal profile object. (but not save to disk.)""" """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) profile_type = potential_data.get('Type', None)
protocol = potential_data.get('Protocol', None) protocol = potential_data.get('Protocol', None)
location_id = potential_data.get('Location', None) location_id = potential_data.get('Location', None)
location = get_profile_location_data(location_id) location = get_profile_location_data_by_id(location_id)
print(f"location is {type(location)}")
name = potential_data.get('Name', None) name = potential_data.get('Name', None)
if not profile_type or not isinstance(profile_type, ProfileType): 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 # SESSION
#--------------------------- #---------------------------
if profile_type == ProfileType.SESSION: if profile_type == ProfileType.SESSION:
created = create_session_object(profile_id=profile_id, potential_data=potential_data, protocol=protocol, location=location, name=name) return 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
#--------------------------- #---------------------------
# SYSTEM # SYSTEM
#--------------------------- #---------------------------
elif profile_type == ProfileType.SYSTEM: elif profile_type == ProfileType.SYSTEM:
connection = SystemConnection(protocol.value) return create_system_object(profile_id=profile_id, potential_data=potential_data, protocol=protocol, location=location, name=name)
profile = SystemProfile(
id=profile_id,
name=name,
type=ProfileType.SYSTEM,
subscription=None,
location=location,
connection=connection
)
#---------------------------
# OTHER
#---------------------------
else: else:
error_msg = f"Invalid profile type of {profile_type} used to create a profile." error_msg = f"Invalid profile type of {profile_type} used to create a profile."
logger.error(error_msg) logger.error(error_msg)
return Result(valid=False, error_type=ResultError.INVALID_INPUT, message=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)