Skip to content

Commit

Permalink
Merge pull request #254 from dwreeves/oct2024-changes
Browse files Browse the repository at this point in the history
Oct2024 changes
  • Loading branch information
dwreeves authored Nov 20, 2024
2 parents 34e417e + 013aa82 commit 17af373
Show file tree
Hide file tree
Showing 24 changed files with 296 additions and 343 deletions.
16 changes: 16 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
FLASK_ENV=development
FLASK_APP="app.main:create_app"

HOBOLINK_USERNAME=replace_me
HOBOLINK_PASSWORD=replace_me
HOBOLINK_TOKEN=replace_me

BASIC_AUTH_USERNAME=admin
BASIC_AUTH_PASSWORD=password

MAPBOX_ACCESS_TOKEN=replace_me

SENTRY_DSN=replace_me
SENTRY_ENVIRONMENT=replace_me

USE_MOCK_DATA=false
2 changes: 1 addition & 1 deletion .flaskenv
Original file line number Diff line number Diff line change
@@ -1 +1 @@
FLASK_APP="app.main:create_app"
FLASK_APP=app.main:create_app
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ docs/site/
pgadmin4/
mkdocs_env/
latest.dump
server.crt
server.key

# Created by https://www.toptal.com/developers/gitignore/api/python,pycharm,windows,osx,visualstudiocode
# Edit at https://www.toptal.com/developers/gitignore?templates=python,pycharm,windows,osx,visualstudiocode
Expand Down
11 changes: 11 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,14 @@ repos:
rev: v2.12.0
hooks:
- id: hadolint-docker

- repo: https://github.com/rhysd/actionlint
rev: v1.6.27
hooks:
- id: actionlint-docker

- repo: https://github.com/koalaman/shellcheck-precommit
rev: v0.10.0
hooks:
- id: shellcheck
files: ^(.*\.sh|run)$
2 changes: 1 addition & 1 deletion .python-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.12.0
3.12
16 changes: 8 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
FROM python:3.12

ADD --chmod=755 https://astral.sh/uv/install.sh /install.sh
RUN /install.sh && rm /install.sh
FROM python:3.12-slim
COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/

WORKDIR /app
COPY requirements.txt ./requirements.txt
ENV UV_PROJECT_ENVIRONMENT=/usr/local

RUN /root/.cargo/bin/uv pip install --system --no-cache -r requirements.txt
RUN --mount=type=cache,target=/root/.cache/uv \
--mount=type=bind,source=requirements.txt,target=requirements.txt \
uv pip install -r requirements.txt --compile-bytecode --system

COPY ./ .
COPY . /app

EXPOSE 80
EXPOSE 80 443

CMD ["bash", "-c", "flask db migrate && gunicorn -c gunicorn_conf.py app.main:create_app\\(\\)"]
80 changes: 39 additions & 41 deletions alembic/versions/rev001.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
"""

import os

import sqlalchemy as sa
from sqlalchemy import schema
from sqlalchemy.engine.reflection import Inspector

from alembic import op
from app.config import QUERIES_DIR
Expand All @@ -26,8 +27,6 @@

def upgrade():
conn = op.get_bind()
inspector = Inspector.from_engine(conn)
tables = inspector.get_table_names()

# skip the following tables:
# - hobolink
Expand All @@ -36,44 +35,43 @@ def upgrade():
# - model_outputs
# These are rewritten each time; their data doesn't need to be persisted.

if "boathouses" not in tables:
op.execute(schema.CreateSequence(schema.Sequence("boathouses_id_seq")))
op.create_table(
"boathouses",
sa.Column(
"id",
sa.Integer(),
autoincrement=True,
nullable=False,
server_default=sa.text("nextval('boathouses_id_seq'::regclass)"),
),
sa.Column("boathouse", sa.String(length=255), nullable=False),
sa.Column("reach", sa.Integer(), nullable=True),
sa.Column("latitude", sa.Numeric(), nullable=True),
sa.Column("longitude", sa.Numeric(), nullable=True),
sa.Column("overridden", sa.Boolean(), nullable=True),
sa.Column("reason", sa.String(length=255), nullable=True),
sa.PrimaryKeyConstraint("boathouse"),
)
with open(QUERIES_DIR + "/override_event_triggers_v1.sql", "r") as f:
sql = sa.text(f.read())
conn.execute(sql)
if "live_website_options" not in tables:
op.create_table(
"live_website_options",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("flagging_message", sa.Text(), nullable=True),
sa.Column("boating_season", sa.Boolean(), nullable=False),
sa.PrimaryKeyConstraint("id"),
)
if "override_history" not in tables:
op.create_table(
"override_history",
sa.Column("time", sa.TIMESTAMP(), nullable=True),
sa.Column("boathouse", sa.TEXT(), nullable=True),
sa.Column("overridden", sa.BOOLEAN(), nullable=True),
sa.Column("reason", sa.TEXT(), nullable=True),
)
op.execute(schema.CreateSequence(schema.Sequence("boathouses_id_seq")))
op.create_table(
"boathouses",
sa.Column(
"id",
sa.Integer(),
autoincrement=True,
nullable=False,
server_default=sa.text("nextval('boathouses_id_seq'::regclass)"),
),
sa.Column("boathouse", sa.String(length=255), nullable=False),
sa.Column("reach", sa.Integer(), nullable=True),
sa.Column("latitude", sa.Numeric(), nullable=True),
sa.Column("longitude", sa.Numeric(), nullable=True),
sa.Column("overridden", sa.Boolean(), nullable=True),
sa.Column("reason", sa.String(length=255), nullable=True),
sa.PrimaryKeyConstraint("boathouse"),
)

with open(os.path.join(QUERIES_DIR, "override_event_triggers_v1.sql"), "r") as f:
sql = sa.text(f.read())
conn.execute(sql)

op.create_table(
"live_website_options",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("flagging_message", sa.Text(), nullable=True),
sa.Column("boating_season", sa.Boolean(), nullable=False),
sa.PrimaryKeyConstraint("id"),
)
op.create_table(
"override_history",
sa.Column("time", sa.TIMESTAMP(), nullable=True),
sa.Column("boathouse", sa.TEXT(), nullable=True),
sa.Column("overridden", sa.BOOLEAN(), nullable=True),
sa.Column("reason", sa.TEXT(), nullable=True),
)


def downgrade():
Expand Down
13 changes: 12 additions & 1 deletion alembic/versions/rev002.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"""

import os

import sqlalchemy as sa
from sqlalchemy.engine.reflection import Inspector

Expand Down Expand Up @@ -77,7 +79,16 @@ def upgrade():
op.create_unique_constraint(None, "boathouses", ["name"])
op.create_foreign_key(None, "boathouses", "reach", ["reach_id"], ["id"])
op.rename_table("boathouses", "boathouse")
with open(QUERIES_DIR + "/override_event_triggers_v2.sql", "r") as f:
with open(os.path.join(QUERIES_DIR, "override_event_triggers_v2.sql"), "r") as f:
sql = sa.text(f.read())
conn.execute(sql)
with open(os.path.join(QUERIES_DIR, "define_reach.sql"), "r") as f:
sql = sa.text(f.read())
conn.execute(sql)
with open(os.path.join(QUERIES_DIR, "define_boathouse.sql"), "r") as f:
sql = sa.text(f.read())
conn.execute(sql)
with open(os.path.join(QUERIES_DIR, "define_default_options.sql"), "r") as f:
sql = sa.text(f.read())
conn.execute(sql)

Expand Down
3 changes: 1 addition & 2 deletions app/admin/views/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ class LogoutView(BaseView):
@expose("/")
def index(self):
body = self.render("admin/logout.html")
status = 401
cache.clear()
status = 200
return body, status


Expand Down
12 changes: 2 additions & 10 deletions app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

import os
import os.path as op
from distutils.util import strtobool

from distutils.util import strtobool
from flask.cli import load_dotenv


Expand Down Expand Up @@ -192,15 +192,7 @@ def SQLALCHEMY_DATABASE_URI(self) -> str:

DEFAULT_WIDGET_VERSION: int = 2

MAPBOX_ACCESS_TOKEN: str = os.getenv(
"MAPBOX_ACCESS_TOKEN",
# This is a token that's floating around the web in a lot of quickstart
# examples for LeafletJS, and seems to work. ¯\_(ツ)_/¯
#
# You should not use it ideally, but as a default for very quick runs
# and demos, it should be OK.
"pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw",
) # noqa
MAPBOX_ACCESS_TOKEN: str = os.getenv("MAPBOX_ACCESS_TOKEN")

SENTRY_DSN: str | None = os.getenv("SENTRY_DSN")
SENTRY_ENVIRONMENT: str | None = os.getenv("SENTRY_ENVIRONMENT")
Expand Down
4 changes: 0 additions & 4 deletions app/data/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,6 @@ def init_db():

alembic.config.main(["upgrade", "head"])

execute_sql_from_file("define_reach.sql")
execute_sql_from_file("define_boathouse.sql")
execute_sql_from_file("define_default_options.sql")

# Now update the database
from app.data.processing.core import update_db

Expand Down
4 changes: 3 additions & 1 deletion app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ def register_jinja_env(app: Flask):
@app.template_filter("strftime")
def strftime(value: datetime.datetime, fmt: str = "%Y-%m-%d %I:%M:%S %p") -> str:
"""Render datetimes with a default format for the frontend."""
return value.strftime(fmt)
if value:
return value.strftime(fmt)

def _load_svg(file_name: str):
"""Load an svg file from `static/images/`."""
Expand Down Expand Up @@ -372,6 +373,7 @@ def pip_compile(ctx: click.Context):
)
@mail_on_fail
def email_90_day_data_command(async_: bool = False):
"""Send email containing database dump."""
from app.data.celery import send_database_exports_task

if async_:
Expand Down
7 changes: 4 additions & 3 deletions app/templates/admin/logout.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@
{% block tail %}
{{ super() }}
<script>
$(document).ready(function(){
$(document).ready(setTimeout(function(){
$.ajax({
async: false,
type: "GET",
url: "{{ url_for('admin.index') }}",
username: "logout"
username: "x-logout",
password: "x-logout"
})
.done(function(){
})
.fail(function(){
window.location.href = "{{ url_for('flagging.index') }}";
});
});
}, 500));
</script>
{% endblock %}
5 changes: 3 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ services:
web:
<<: *app-config
ports:
- 80:80
- "127.0.0.1:80:80"
- "127.0.0.1:443:443"
depends_on:
- postgres
links:
Expand Down Expand Up @@ -72,7 +73,7 @@ services:

celeryworker:
<<: *app-config
entrypoint: ["flask", "celery", "worker", "--uid", "nobody", "--gid", "nogroup"]
entrypoint: ["flask", "celery", "worker"]
ports:
- 5555:5555
depends_on:
Expand Down
Loading

0 comments on commit 17af373

Please sign in to comment.