-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update to declarative model creation, fix new db creation script
- Loading branch information
Showing
6 changed files
with
69 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
#!/usr/bin/env bash | ||
# Run database migrations | ||
python ./scripts/make_database.py | ||
flask db migrate | ||
flask db seed | ||
rm .env | ||
|
||
# Start the application | ||
gunicorn --bind 0.0.0.0:80 --workers 2 --log-level error --access-logfile /app/log/access.log --error-logfile /app/log/error.log wsgi:app |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,56 @@ | ||
from datetime import datetime | ||
from typing import Any | ||
|
||
from flask_sqlalchemy import SQLAlchemy | ||
from sqlalchemy import Column, DateTime, Integer, String, inspect | ||
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column | ||
from sqlalchemy.types import DateTime, String | ||
|
||
__all__ = ["Base", "RottedLinks", "WebLink"] | ||
|
||
db = SQLAlchemy() | ||
|
||
# Set up the flask-sqlalchemy extension for "new-style" models | ||
class Base(DeclarativeBase): ... | ||
|
||
__all__ = ["WebLink"] | ||
|
||
db = SQLAlchemy(model_class=Base) | ||
|
||
|
||
class HelperMethods: | ||
def as_dict(self) -> dict: | ||
def as_dict(self) -> dict[str, Any]: | ||
"""Return a model as a dictionary.""" | ||
return {c.key: getattr(self, c.key) for c in inspect(self).mapper.column_attrs} | ||
|
||
def update_with(self, data: dict[str, Any]) -> None: | ||
"""Update a record with the given data.""" | ||
for k, v in data.items(): | ||
setattr(self, k, v) | ||
return None | ||
|
||
class WebLink(HelperMethods, db.Model): | ||
__tablename__ = "weblinks" | ||
|
||
id = Column(String, primary_key=True) | ||
title = Column(String, nullable=False) | ||
description = Column(String, nullable=False) | ||
url = Column(String, nullable=False) | ||
date_added = Column(DateTime, nullable=False) | ||
is_dead = Column(Integer, nullable=False, server_default="0") | ||
is_web_archive = Column(Integer, nullable=False, server_default="0") | ||
|
||
|
||
class RottedLinks(HelperMethods, db.Model): | ||
class WebLink(HelperMethods, Base): | ||
__tablename__ = "weblinks" | ||
__table_args__ = {"comment": "Store the webring entries."} | ||
|
||
id: Mapped[str] = mapped_column(String, primary_key=True) | ||
title: Mapped[str] = mapped_column(String) | ||
description: Mapped[str] = mapped_column(String) | ||
url: Mapped[str] = mapped_column( | ||
String, | ||
) | ||
date_added: Mapped[datetime] = Column( | ||
DateTime, | ||
nullable=False, | ||
default=datetime.now, | ||
onupdate=datetime.now, | ||
) | ||
is_dead: Mapped[bool] = mapped_column(default=False, server_default="0") | ||
is_web_archive: Mapped[bool] = mapped_column(default=False, server_default="0") | ||
|
||
|
||
class RottedLinks(HelperMethods, Base): | ||
__tablename__ = "rotted_links" | ||
__table_args__ = {"comment": "Log rotting webring entries."} | ||
|
||
id = Column(String, primary_key=True) | ||
times_failed = Column(Integer, nullable=False) | ||
id: Mapped[str] = mapped_column(String, primary_key=True) | ||
times_failed: Mapped[int] = mapped_column(Integer) |