new pass at properly handling related report models
This commit is contained in:
@@ -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]
|
||||
session.add(new_domain)
|
||||
new_paths = list()
|
||||
for path in paths:
|
||||
new_path = Path(path=path)
|
||||
if user:
|
||||
for path in new_domain.paths:
|
||||
new_report = Report(path_id=path.id, user_id=user.id)
|
||||
session.add(new_report)
|
||||
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)
|
||||
|
||||
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()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user