From 7b1dfc424e0db12b010757908cc95d5bf5af40fc Mon Sep 17 00:00:00 2001 From: Caleb Ely Date: Sat, 8 Jun 2024 00:04:52 -0400 Subject: [PATCH] Add type hints, add ability to convert model instance to a dict --- .vscode/launch.json | 6 ++++-- src/core/database/schema.py | 13 +++++++++---- src/views/linkrot.py | 5 +++-- src/views/root.py | 6 +++--- 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 414a430..85b2021 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,7 +10,7 @@ "request": "launch", "module": "flask", "console": "internalConsole", - "jinja": false, + "jinja": true, "justMyCode": true, "env": { "FLASK_APP": "wsgi.py", @@ -23,7 +23,9 @@ }, "args": [ "run", - "--no-debugger" + "--no-debugger", + "--no-reload" + ] }, { diff --git a/src/core/database/schema.py b/src/core/database/schema.py index 586971d..d022774 100644 --- a/src/core/database/schema.py +++ b/src/core/database/schema.py @@ -1,7 +1,6 @@ # coding: utf-8 from flask_sqlalchemy import SQLAlchemy -from sqlalchemy import Column, DateTime, Integer, String - +from sqlalchemy import Column, DateTime, Integer, String, inspect db = SQLAlchemy() @@ -9,7 +8,13 @@ __all__ = ["WebLink"] -class WebLink(db.Model): +class HelperMethods: + def as_dict(self) -> dict: + """Return a model as a dictionary.""" + return {c.key: getattr(self, c.key) for c in inspect(self).mapper.column_attrs} + + +class WebLink(HelperMethods, db.Model): __tablename__ = "weblinks" id = Column(String, primary_key=True) @@ -21,7 +26,7 @@ class WebLink(db.Model): is_web_archive = Column(Integer, nullable=False, server_default="0") -class RottedLinks(db.Model): +class RottedLinks(HelperMethods, db.Model): __tablename__ = "rotted_links" id = Column(String, primary_key=True) diff --git a/src/views/linkrot.py b/src/views/linkrot.py index 2b9e3f1..7db53a8 100644 --- a/src/views/linkrot.py +++ b/src/views/linkrot.py @@ -1,4 +1,5 @@ from typing import Any + from flask.views import MethodView from src.blueprints import linkrot @@ -10,7 +11,7 @@ class LinkRotCheck(MethodView): @linkrot.arguments(models.AuthKey, location="query", as_kwargs=True) @linkrot.response(200, models.RotResult(many=True)) - def post(self, **kwargs: Any): + def post(self, **kwargs: Any) -> list[models.RotResult]: """Check all links in the ring for link rot.""" del kwargs["auth_key"] return db.check_all() @@ -21,7 +22,7 @@ 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) - def post(self, **kwargs: Any): + def post(self, **kwargs: Any) -> models.RotResult: """Check a single link in the ring for link rot.""" del kwargs["auth_key"] return db.check_one(str(kwargs["id"])) diff --git a/src/views/root.py b/src/views/root.py index 90f90aa..58b52a2 100644 --- a/src/views/root.py +++ b/src/views/root.py @@ -11,7 +11,7 @@ @root.route("/") class WebRing(MethodView): @root.response(200, models.WebLink(many=True)) - def get(self): + def get(self) -> list[models.WebLink]: """Fetch webring items. This will return rotted links in the result set. @@ -32,7 +32,7 @@ class WebRing(MethodView): @root.arguments(models.AuthKey, location="query", as_kwargs=True) @root.arguments(models.WebLinkId, location="path", as_kwargs=True) @root.response(204, models.Empty) - def delete(self, **kwargs: Any): + def delete(self, **kwargs: Any) -> None: """Delete a webring item.""" del kwargs["auth_key"] db.delete(str(kwargs["id"])) @@ -42,7 +42,7 @@ def delete(self, **kwargs: Any): @root.arguments(models.WebLinkUpdate, location="json", as_kwargs=True) @root.response(204, models.Empty) @root.alt_response(400, schema=models.HttpError) - def patch(self, **kwargs: Any): + def patch(self, **kwargs: Any) -> None: """Update a webring item.""" del kwargs["auth_key"]