Skip to content

Commit

Permalink
Add type hints, add ability to convert model instance to a dict
Browse files Browse the repository at this point in the history
  • Loading branch information
le717 committed Jun 8, 2024
1 parent 5597275 commit 7b1dfc4
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 11 deletions.
6 changes: 4 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"request": "launch",
"module": "flask",
"console": "internalConsole",
"jinja": false,
"jinja": true,
"justMyCode": true,
"env": {
"FLASK_APP": "wsgi.py",
Expand All @@ -23,7 +23,9 @@
},
"args": [
"run",
"--no-debugger"
"--no-debugger",
"--no-reload"

]
},
{
Expand Down
13 changes: 9 additions & 4 deletions src/core/database/schema.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
# 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()


__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)
Expand All @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions src/views/linkrot.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Any

from flask.views import MethodView

from src.blueprints import linkrot
Expand All @@ -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()
Expand 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"]))
6 changes: 3 additions & 3 deletions src/views/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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"]))
Expand All @@ -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"]

Expand Down

0 comments on commit 7b1dfc4

Please sign in to comment.