From e420a311275f04a447b19ce6b1d508542ad1d925 Mon Sep 17 00:00:00 2001 From: Jack Case Date: Wed, 29 Oct 2025 14:33:02 +0000 Subject: [PATCH] new pass at properly handling related report models --- slopserver/db.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/slopserver/db.py b/slopserver/db.py index 65a7573..093c751 100644 --- a/slopserver/db.py +++ b/slopserver/db.py @@ -34,27 +34,31 @@ def insert_slop(urls: list[ParseResult], engine: Engine, user: User | None = Non if not domain in existing_dict: # create a new domain object and paths new_domain = Domain(domain_name=domain, paths=list()) - new_domain.paths = [Path(path=path) for path in paths] + new_paths = list() + for path in paths: + new_path = Path(path=path) + if user: + new_path.reports = list().append(Report(path=new_path, user=user, timestamp=datetime.now())) + new_paths.append(new_path) + new_domain.paths = new_paths session.add(new_domain) - if user: - for path in new_domain.paths: - new_report = Report(path_id=path.id, user_id=user.id) - session.add(new_report) else: existing_domain = existing_dict[domain] - existing_paths = set((path.path for path in existing_domain.paths)) + existing_paths = dict({path.path: path for path in existing_domain.paths}) for path in paths: if not path in existing_paths: new_path = Path(path=path) + if user: + new_path.reports = list().append(Report(path=new_path, user=user, timestamp=datetime.now())) existing_domain.paths.append(new_path) session.add(new_path) - session.flush([new_path]) - session.refresh(new_path) + + else: + # domain and path exist, append to the path's reports if user: - new_report = Report( - path_id=new_path.id, user_id=user.id, timestamp=datetime.now()) - session.add(new_report) + existing_path = existing_paths.get(path) + existing_path.reports.append(Report(path=new_path, user=user, timestamp=datetime.now())) session.commit()