Skip to content

Commit

Permalink
Add ability to filter result set when fetching ring root
Browse files Browse the repository at this point in the history
  • Loading branch information
le717 committed Jun 8, 2024
1 parent eba0523 commit b89ccc4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
12 changes: 7 additions & 5 deletions src/core/database/weblink.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
from typing import Any, Optional, OrderedDict

from markupsafe import Markup
from sqlalchemy import func

from src.core.database.schema import WebLink, db
from src.core.logger import logger

__all__ = ["create", "delete", "exists", "get", "get_all", "update"]


def create(data: OrderedDict) -> dict:
def create(data: OrderedDict) -> dict[str, uuid.UUID]:
"""Create a single weblink."""
entry_id = str(uuid.uuid4())
weblink = WebLink(
Expand Down Expand Up @@ -56,16 +57,17 @@ def get(uuid: str) -> Optional[WebLink]:
return db.session.execute(db.select(WebLink).filter_by(id=uuid)).scalars().first()


def get_all(with_rotted: bool = False, **kwargs: Any) -> list[WebLink]:
def get_all(include_rotted: bool = False, **kwargs: Any) -> list[WebLink]:
"""Get all weblinks."""
filters = []

# Filter out the site in the weblink we are on
# Filter out the site in the weblink we are on.
# Make sure we normalize the casing of the two URLs to better ensure we filter correctly
if origin := kwargs.get("http_origin"):
filters.append(WebLink.url != origin)
filters.append(func.lower(WebLink.url) != func.lower(origin))

# Remove all rotted links
if not with_rotted:
if not include_rotted:
filters.append(WebLink.is_dead != "1")
wbs = db.session.execute(db.select(WebLink).filter(*filters)).scalars().all()

Expand Down
10 changes: 7 additions & 3 deletions src/core/models/WebLink.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from marshmallow import fields, Schema
from marshmallow import Schema, fields


__all__ = ["WebLink", "WebLinkCreate", "WebLinkId", "WebLinkUpdate"]
__all__ = ["WebLink", "WebLinkCreate", "WebLinkGet", "WebLinkId", "WebLinkUpdate"]


class WebLink(Schema):
Expand All @@ -20,6 +19,11 @@ class WebLinkCreate(Schema):
url = fields.Url(required=True)


class WebLinkGet(Schema):
include_rotted = fields.Boolean(load_default=True)
exclude_origin = fields.Boolean(load_default=True)


class WebLinkId(Schema):
id = fields.UUID(required=True)

Expand Down
2 changes: 1 addition & 1 deletion src/views/embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def embed() -> Response:
# and convert them to plain dictionaries for including in the JavaScript file directly,
# which removes the need for a fetch request on the client
all_links = WebLink(only=["id", "url", "title", "description"]).dump(
db.get_all(with_rotted=True, http_origin=request.headers.get("ORIGIN")),
db.get_all(include_rotted=True, http_origin=request.headers.get("ORIGIN")),
many=True,
)

Expand Down
16 changes: 11 additions & 5 deletions src/views/root.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Any
from uuid import UUID

from flask import abort, request
from flask.views import MethodView
Expand All @@ -10,19 +11,24 @@

@root.route("/")
class WebRing(MethodView):
@root.arguments(models.WebLinkGet, location="query", as_kwargs=True)
@root.response(200, models.WebLink(many=True))
def get(self) -> list[models.WebLink]:
def get(self, **kwargs: Any) -> list[models.WebLink]:
"""Fetch webring items.
This will return rotted links in the result set.
Provide the appropriate query string arguments to
filter the result set as desired.
"""
# TODO: Add query param to exclude rotted params
return db.get_all(with_rotted=True, http_origin=request.headers.get("ORIGIN"))
# Remove the site making the request from the result set if told to
query_args = {}
if kwargs["exclude_origin"]:
query_args["http_origin"] = request.headers.get("ORIGIN")
return db.get_all(include_rotted=kwargs["include_rotted"], **query_args)

@root.arguments(models.AuthKey, location="query", as_kwargs=True)
@root.arguments(models.WebLinkCreate, location="json", as_kwargs=True)
@root.response(201, models.WebLinkId)
def post(self, **kwargs: Any):
def post(self, **kwargs: Any) -> dict[str, UUID]:
"""Create a webring item."""
del kwargs["auth_key"]
return db.create(kwargs)
Expand Down

0 comments on commit b89ccc4

Please sign in to comment.