associate reports with users
this will definitely need some more work.
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
from collections.abc import Iterable
|
||||
from datetime import datetime
|
||||
from urllib.parse import ParseResult
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.engine import Engine
|
||||
from sqlalchemy.orm import Session
|
||||
from slopserver.models import Domain, Path, User
|
||||
from slopserver.models import Domain, Path, User, Report
|
||||
|
||||
def select_slop(urls: list[ParseResult], engine: Engine) -> Iterable[Domain]:
|
||||
query = select(Domain).where(Domain.domain_name.in_(url[1] for url in urls))
|
||||
@@ -11,7 +12,7 @@ def select_slop(urls: list[ParseResult], engine: Engine) -> Iterable[Domain]:
|
||||
rows = session.scalars(query).all()
|
||||
return rows
|
||||
|
||||
def insert_slop(urls: list[ParseResult], engine: Engine):
|
||||
def insert_slop(urls: list[ParseResult], engine: Engine, user: User | None = None):
|
||||
domain_dict: dict[str. set[str]] = dict()
|
||||
for url in urls:
|
||||
if not domain_dict.get(url[1]):
|
||||
@@ -35,13 +36,25 @@ def insert_slop(urls: list[ParseResult], engine: Engine):
|
||||
new_domain = Domain(domain_name=domain, paths=list())
|
||||
new_domain.paths = [Path(path=path) for path in 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))
|
||||
for path in paths:
|
||||
if not path in existing_paths:
|
||||
existing_domain.paths.append(Path(path=path))
|
||||
new_path = Path(path=path)
|
||||
existing_domain.paths.append(new_path)
|
||||
session.add(new_path)
|
||||
session.flush([new_path])
|
||||
session.refresh(new_path)
|
||||
if user:
|
||||
new_report = Report(
|
||||
path_id=new_path.id, user_id=user.id, timestamp=datetime.now())
|
||||
session.add(new_report)
|
||||
|
||||
session.commit()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user