-
Notifications
You must be signed in to change notification settings - Fork 0
SQ-821: Announcements to users via CLI and webpage #65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 10 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
95e70ee
api: annoucements db model, schema and crud ops
RMCrean 92f5d1f
api/v1/core endpoints for health + annoucements
RMCrean dcb7890
api: finalise annoucement db format + migration
RMCrean a41d2ea
api: add annoucement db to admin panel
RMCrean b56c1b8
frontend: annoucements banner(s) on home page
RMCrean 0087790
cli: display annoucements from divbase server on user login
RMCrean 68e7bd7
cli+api: clean up announcemnts logic + docs
RMCrean ad774db
admin panel: display enums as enums not strings
RMCrean 628753c
api+cli:spell announcements correctly in module names...
RMCrean 800f616
frontend: style announcements
RMCrean fb2c55b
drop unused logging statement
RMCrean e740c9a
fix typos of word announcement
RMCrean 44c69d9
cli+frontend: format annoucements better
RMCrean 113b7be
cli: unit tests for displaying announcements
RMCrean File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
31 changes: 31 additions & 0 deletions
31
packages/divbase-api/src/divbase_api/crud/announcements.py
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| """ | ||
| Crud operations on the announcements table, | ||
| which stores announcements that can be displayed to users on the frontend and the cli. | ||
|
|
||
| Starlette admin will manage the creation/editing/deletion of announcements, | ||
| so this module only covers retrieving active announcements to be displayed on frontend or by CLI. | ||
| """ | ||
|
|
||
| from datetime import datetime, timezone | ||
|
|
||
| from sqlalchemy import select | ||
| from sqlalchemy.ext.asyncio import AsyncSession | ||
|
|
||
| from divbase_api.models.announcements import AnnouncementDB, AnnouncementTarget | ||
| from divbase_lib.api_schemas.announcements import AnnouncementResponse | ||
|
|
||
|
|
||
| async def get_active_announcements(db: AsyncSession, target: AnnouncementTarget) -> list[AnnouncementResponse]: | ||
| """Get active announcements for a given target (cli, web or both).""" | ||
|
|
||
| if target == AnnouncementTarget.BOTH: | ||
| raise ValueError("Target cannot be both when retrieving announcements. Please specify either cli or web.") | ||
|
|
||
| stmt = ( | ||
| select(AnnouncementDB) | ||
| .where((AnnouncementDB.target == target) | (AnnouncementDB.target == AnnouncementTarget.BOTH)) | ||
| .where((AnnouncementDB.auto_expire_at.is_(None)) | (AnnouncementDB.auto_expire_at > datetime.now(timezone.utc))) | ||
| ) | ||
| result = await db.execute(stmt) | ||
| announcements = result.scalars().all() | ||
| return [AnnouncementResponse.model_validate(announcement) for announcement in announcements] | ||
This file contains hidden or 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 hidden or 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
56 changes: 56 additions & 0 deletions
56
packages/divbase-api/src/divbase_api/migrations/versions/2026-02-09_add_annoucements_db.py
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| """add annoucements db | ||
|
RMCrean marked this conversation as resolved.
Outdated
|
||
|
|
||
| Revision ID: 2ec979fc9dbb | ||
| Revises: 3e168ddc857e | ||
| Create Date: 2026-02-09 09:43:52.822149 | ||
|
|
||
| """ | ||
|
|
||
| from typing import Sequence, Union | ||
|
|
||
| import sqlalchemy as sa | ||
| from alembic import op | ||
|
|
||
| # revision identifiers, used by Alembic. | ||
| revision: str = "2ec979fc9dbb" | ||
| down_revision: Union[str, Sequence[str], None] = "3e168ddc857e" | ||
| branch_labels: Union[str, Sequence[str], None] = None | ||
| depends_on: Union[str, Sequence[str], None] = None | ||
|
|
||
|
|
||
| def upgrade() -> None: | ||
| """Upgrade schema.""" | ||
| # ### commands auto generated by Alembic - please adjust! ### | ||
| op.create_table( | ||
| "announcement", | ||
| sa.Column("heading", sa.String(length=200), nullable=False), | ||
| sa.Column("message", sa.String(length=1000), nullable=True), | ||
| sa.Column("target", sa.Enum("BOTH", "CLI", "WEB", name="announcementtarget"), nullable=False), | ||
| sa.Column("level", sa.Enum("INFO", "SUCCESS", "WARNING", "DANGER", name="announcementlevel"), nullable=False), | ||
| sa.Column("auto_expire_at", sa.DateTime(timezone=True), nullable=True), | ||
| sa.Column("id", sa.Integer(), autoincrement=True, nullable=False), | ||
| sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False), | ||
| sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False), | ||
| sa.PrimaryKeyConstraint("id"), | ||
| ) | ||
| op.create_index(op.f("ix_announcement_heading"), "announcement", ["heading"], unique=False) | ||
| op.create_index(op.f("ix_announcement_id"), "announcement", ["id"], unique=False) | ||
| op.create_index(op.f("ix_announcement_level"), "announcement", ["level"], unique=False) | ||
| op.create_index(op.f("ix_announcement_target"), "announcement", ["target"], unique=False) | ||
| # ### end Alembic commands ### | ||
|
|
||
|
|
||
| def downgrade() -> None: | ||
| """Downgrade schema.""" | ||
| # ### commands auto generated by Alembic - please adjust! ### | ||
| op.drop_index(op.f("ix_announcement_target"), table_name="announcement") | ||
| op.drop_index(op.f("ix_announcement_level"), table_name="announcement") | ||
| op.drop_index(op.f("ix_announcement_id"), table_name="announcement") | ||
| op.drop_index(op.f("ix_announcement_heading"), table_name="announcement") | ||
| op.drop_table("announcement") | ||
| # ### end Alembic commands ### | ||
|
|
||
| # Human added, explicitly drop enums | ||
| # see https://github.com/sqlalchemy/alembic/issues/886 | ||
| op.execute("DROP TYPE IF EXISTS announcementtarget") | ||
| op.execute("DROP TYPE IF EXISTS announcementlevel") | ||
This file contains hidden or 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
52 changes: 52 additions & 0 deletions
52
packages/divbase-api/src/divbase_api/models/announcements.py
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| """ | ||
| Announcements DB Model. | ||
|
|
||
| Stores announcements that can be displayed to users on the frontend (banner) or via the CLI. | ||
| """ | ||
|
|
||
| from datetime import datetime | ||
| from enum import StrEnum | ||
|
|
||
| from sqlalchemy import DateTime, Enum, String | ||
| from sqlalchemy.orm import Mapped, mapped_column | ||
|
|
||
| from divbase_api.models.base import BaseDBModel | ||
|
|
||
|
|
||
| class AnnouncementTarget(StrEnum): | ||
| """Possible targets for announcements.""" | ||
|
|
||
| BOTH = "both" | ||
| CLI = "cli" | ||
| WEB = "web" | ||
|
|
||
|
|
||
| class AnnouncementLevel(StrEnum): | ||
| """ | ||
| Possible levels for announcements. | ||
| These match bootstrap alert levels, so this will control the announcement styling on the frontend. | ||
|
RMCrean marked this conversation as resolved.
|
||
| """ | ||
|
|
||
| INFO = "info" | ||
| SUCCESS = "success" | ||
| WARNING = "warning" | ||
| DANGER = "danger" | ||
|
|
||
|
|
||
| class AnnouncementDB(BaseDBModel): | ||
| """ | ||
| DB Model for an announcement. | ||
|
|
||
| id, created_at and updated_at are inherited from BaseDBModel. | ||
| """ | ||
|
|
||
| __tablename__ = "announcement" | ||
|
|
||
| heading: Mapped[str] = mapped_column(String(200), index=True) | ||
| message: Mapped[str | None] = mapped_column(String(1000), nullable=True) | ||
| target: Mapped[AnnouncementTarget] = mapped_column(Enum(AnnouncementTarget), index=True) | ||
| level: Mapped[AnnouncementLevel] = mapped_column(Enum(AnnouncementLevel), index=True) | ||
| auto_expire_at: Mapped[datetime | None] = mapped_column(DateTime(timezone=True), nullable=True) | ||
|
|
||
| def __repr__(self) -> str: | ||
| return f"<AnnouncementDB id={self.id}, heading={self.heading}, target={self.target}, level={self.level}>" | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| """ | ||
| Core API routes for divbase, including health checks and announcements. | ||
|
|
||
| Note that unlike every other API route these routes are not behind authentication... | ||
|
brinkdp marked this conversation as resolved.
|
||
| """ | ||
|
|
||
| import logging | ||
|
|
||
| from fastapi import APIRouter, Depends, status | ||
| from sqlalchemy.ext.asyncio import AsyncSession | ||
|
|
||
| from divbase_api.crud.announcements import get_active_announcements | ||
| from divbase_api.db import get_db | ||
| from divbase_api.models.announcements import AnnouncementTarget | ||
| from divbase_lib.api_schemas.announcements import AnnouncementResponse | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
RMCrean marked this conversation as resolved.
Outdated
|
||
| core_router = APIRouter() | ||
|
|
||
|
|
||
| @core_router.get("/health", status_code=status.HTTP_200_OK, response_model=dict[str, str]) | ||
| def health(): | ||
| """Basic health check endpoint for the server.""" | ||
| return {"status": "ok"} | ||
|
|
||
|
|
||
| @core_router.get("/announcements", status_code=status.HTTP_200_OK, response_model=list[AnnouncementResponse]) | ||
| async def announcements(db: AsyncSession = Depends(get_db)): | ||
| """Returns active announcements for CLI users from the server.""" | ||
| return await get_active_announcements(db=db, target=AnnouncementTarget.CLI) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.