Skip to content

Commit

Permalink
cleaning up request/response model structures
Browse files Browse the repository at this point in the history
  • Loading branch information
le717 committed Jun 20, 2024
1 parent 9859636 commit f13f9d9
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 29 deletions.
12 changes: 6 additions & 6 deletions src/core/models/WebLink.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
from marshmallow import Schema, fields


__all__ = ["WebLink", "WebLinkCreate", "WebLinkGet", "WebLinkId", "WebLinkUpdate"]
__all__ = ["Entry", "EntryId", "EntryCreate", "RingArgs", "EntryUpdate"]


class WebLink(Schema):
class Entry(Schema):
uuid = fields.UUID(data_key="id")
title = fields.String()
description = fields.String()
Expand All @@ -17,25 +17,25 @@ class WebLink(Schema):
is_web_archive = fields.Boolean()


class WebLinkCreate(Schema):
class EntryCreate(Schema):
title = fields.String(required=True)
description = fields.String(required=True)
url = fields.Url(required=True)


class WebLinkGet(Schema):
class RingArgs(Schema):
include_rotted = fields.Boolean(load_default=sys_vars.get_bool("FILTER_INCLUDE_ROTTED"))
include_web_archive = fields.Boolean(
load_default=sys_vars.get_bool("FILTER_INCLUDE_WEB_ARCHIVE")
)
exclude_origin = fields.Boolean(load_default=sys_vars.get_bool("FILTER_EXCLUDE_ORIGIN"))


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


class WebLinkUpdate(Schema):
class EntryUpdate(Schema):
title = fields.String()
description = fields.String()
url = fields.Url()
Expand Down
3 changes: 0 additions & 3 deletions src/core/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
from .AuthKey import * # noqa: F403
from .RotResult import * # noqa: F403
from .WebLink import * # noqa: F403
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
__all__ = ["RotResult"]


class Result(Schema):
class _Result(Schema):
times_failed = fields.Integer()
is_dead = fields.Boolean()
is_web_archive = fields.Boolean()
Expand All @@ -13,4 +13,4 @@ class Result(Schema):
class RotResult(Schema):
id = fields.UUID()
url = fields.Url()
result = fields.Nested(Result())
result = fields.Nested(_Result())
6 changes: 3 additions & 3 deletions src/views/embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from webargs.flaskparser import use_kwargs

from src.core.database import weblink as db
from src.core.models.WebLink import WebLink, WebLinkGet
from src.core.models.WebLink import Entry, RingArgs

from ..blueprints import route_embed

Expand All @@ -11,7 +11,7 @@


@route_embed.get("")
@use_kwargs(WebLinkGet().fields, location="query")
@use_kwargs(RingArgs().fields, location="query")
def embed(**kwargs) -> Response:
"""Get a small JavaScript file that automatically embeds the webring on your site.
Expand All @@ -25,7 +25,7 @@ def embed(**kwargs) -> Response:
# Get all current links in the webring, including dead links, excluding the current site,
# 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=["uuid", "url", "title", "description"]).dump(
all_links = Entry(only=["uuid", "url", "title", "description"]).dump(
db.get_all(**kwargs),
many=True,
)
Expand Down
13 changes: 6 additions & 7 deletions src/views/linkrot.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@

from src.blueprints import linkrot
from src.core import database as db
from src.core import models
from src.core.models import Generic
from src.core.models import Generic, WebLink, auth, rot_result


@linkrot.route("/")
class LinkRotCheck(MethodView):
@linkrot.arguments(models.AuthKey, location="query", as_kwargs=True)
@linkrot.response(200, models.RotResult(many=True))
@linkrot.arguments(auth.AuthKey, location="query", as_kwargs=True)
@linkrot.response(200, rot_result.RotResult(many=True))
@linkrot.alt_response(422, schema=Generic.HttpError)
def post(self, **kwargs: Any) -> list[db.linkrot.RotResult]:
"""Check all links in the ring for link rot."""
Expand All @@ -22,9 +21,9 @@ def post(self, **kwargs: Any) -> list[db.linkrot.RotResult]:

@linkrot.route("/<uuid:id>")
class LinkRotSingleCheck(MethodView):
@linkrot.arguments(models.AuthKey, location="query", as_kwargs=True)
@linkrot.arguments(models.WebLinkId, location="path", as_kwargs=True)
@linkrot.response(200, models.RotResult)
@linkrot.arguments(auth.AuthKey, location="query", as_kwargs=True)
@linkrot.arguments(WebLink.EntryId, location="path", as_kwargs=True)
@linkrot.response(200, rot_result.RotResult)
@linkrot.alt_response(404, schema=Generic.HttpError)
@linkrot.alt_response(422, schema=Generic.HttpError)
def post(self, **kwargs: Any) -> db.linkrot.RotResult | None:
Expand Down
16 changes: 8 additions & 8 deletions src/views/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@

from src.blueprints import root
from src.core import database as db
from src.core import models
from src.core.models import Generic
from src.core.models import WebLink as models


@root.route("/")
class WebRing(MethodView):
@root.arguments(models.WebLinkGet, location="query", as_kwargs=True)
@root.response(200, models.WebLink(many=True))
@root.arguments(models.RingArgs, location="query", as_kwargs=True)
@root.response(200, models.Entry(many=True))
def get(self, **kwargs: Any) -> Sequence[db.schema.WebLink]:
"""Fetch all entries.
Expand All @@ -28,8 +28,8 @@ def get(self, **kwargs: Any) -> Sequence[db.schema.WebLink]:
return db.weblink.get_all(**kwargs)

@root.arguments(models.AuthKey, location="query", as_kwargs=True)
@root.arguments(models.WebLinkCreate, location="json", as_kwargs=True)
@root.response(201, models.WebLinkId)
@root.arguments(models.EntryCreate, location="json", as_kwargs=True)
@root.response(201, models.EntryId)
def post(self, **kwargs: Any) -> dict[str, UUID]:
"""Create an entry."""
del kwargs["auth_key"]
Expand All @@ -39,7 +39,7 @@ def post(self, **kwargs: Any) -> dict[str, UUID]:
@root.route("/<uuid:id>")
class WebRingItem(MethodView):
@root.arguments(models.AuthKey, location="query", as_kwargs=True)
@root.arguments(models.WebLinkId, location="path", as_kwargs=True)
@root.arguments(models.EntryId, location="path", as_kwargs=True)
@root.response(204, Generic.Empty)
@root.alt_response(422, schema=Generic.HttpError)
def delete(self, **kwargs: Any) -> None:
Expand All @@ -48,8 +48,8 @@ def delete(self, **kwargs: Any) -> None:
db.weblink.delete(str(kwargs["id"]))

@root.arguments(models.AuthKey, location="query", as_kwargs=True)
@root.arguments(models.WebLinkId, location="path", as_kwargs=True)
@root.arguments(models.WebLinkUpdate, location="json", as_kwargs=True)
@root.arguments(models.EntryId, location="path", as_kwargs=True)
@root.arguments(models.EntryUpdate, location="json", as_kwargs=True)
@root.response(204, Generic.Empty)
@root.alt_response(400, schema=Generic.HttpError)
@root.alt_response(422, schema=Generic.HttpError)
Expand Down

0 comments on commit f13f9d9

Please sign in to comment.