insert slop page endpoint
This commit is contained in:
@@ -10,3 +10,37 @@ def select_slop(urls: list[ParseResult], engine: Engine) -> Iterable[Domain]:
|
|||||||
with Session(engine) as session:
|
with Session(engine) as session:
|
||||||
rows = session.scalars(query).all()
|
rows = session.scalars(query).all()
|
||||||
return rows
|
return rows
|
||||||
|
|
||||||
|
def insert_slop(urls: list[ParseResult], engine: Engine):
|
||||||
|
domain_dict: dict[str. set[str]] = dict()
|
||||||
|
for url in urls:
|
||||||
|
if not domain_dict.get(url[1]):
|
||||||
|
domain_dict[url[1]] = set()
|
||||||
|
|
||||||
|
if url.path:
|
||||||
|
domain_dict[url[1]].add(url.path)
|
||||||
|
|
||||||
|
# get existing domains
|
||||||
|
query = select(Domain).where(Domain.domain_name.in_(domain_dict.keys()))
|
||||||
|
|
||||||
|
existing_dict: dict[str, Domain] = dict()
|
||||||
|
with Session(engine) as session:
|
||||||
|
existing_domains = session.scalars(query).all()
|
||||||
|
for domain in existing_domains:
|
||||||
|
existing_dict[domain.domain_name] = domain
|
||||||
|
|
||||||
|
for domain, paths in domain_dict.items():
|
||||||
|
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)
|
||||||
|
|
||||||
|
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))
|
||||||
|
|
||||||
|
session.commit()
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ class Path(SQLModel, table=True):
|
|||||||
id: int | None = Field(default=None, primary_key=True)
|
id: int | None = Field(default=None, primary_key=True)
|
||||||
path: str
|
path: str
|
||||||
|
|
||||||
domain_id: int = Field(foreign_key="domain.id")
|
domain_id: int | None = Field(foreign_key="domain.id")
|
||||||
domain: Domain = Relationship(back_populates="paths")
|
domain: Domain = Relationship(back_populates="paths")
|
||||||
|
|
||||||
class User(SQLModel, table=True):
|
class User(SQLModel, table=True):
|
||||||
|
|||||||
@@ -7,12 +7,12 @@
|
|||||||
- get reports for given domains/pages
|
- get reports for given domains/pages
|
||||||
- post report
|
- post report
|
||||||
"""
|
"""
|
||||||
|
import uvicorn
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
from sqlalchemy import create_engine
|
from sqlalchemy import create_engine
|
||||||
from slopserver.models import Domain, Path, User
|
from slopserver.models import Domain, Path, User
|
||||||
from slopserver.models import SlopReport
|
from slopserver.models import SlopReport
|
||||||
from slopserver.db import select_slop
|
from slopserver.db import select_slop, insert_slop
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
|
|
||||||
@@ -20,9 +20,12 @@ temp_engine = create_engine("postgresql+psycopg://slop-farmer@192.168.1.163/slop
|
|||||||
|
|
||||||
@app.post("/report/")
|
@app.post("/report/")
|
||||||
async def report_slop(report: SlopReport):
|
async def report_slop(report: SlopReport):
|
||||||
pass
|
insert_slop(report.slop_urls, temp_engine)
|
||||||
|
|
||||||
@app.post("/check/")
|
@app.post("/check/")
|
||||||
async def check_slop(check: SlopReport):
|
async def check_slop(check: SlopReport):
|
||||||
slop_results = select_slop(check.slop_urls, temp_engine)
|
slop_results = select_slop(check.slop_urls, temp_engine)
|
||||||
return slop_results
|
return slop_results
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
uvicorn.run(app, host="0.0.0.0", port=8000)
|
||||||
Reference in New Issue
Block a user