Skip to content

Commit

Permalink
Create a basic model for WebPush targets
Browse files Browse the repository at this point in the history
  • Loading branch information
jellybob committed Apr 1, 2024
1 parent 9104967 commit 5f2c919
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
46 changes: 46 additions & 0 deletions migrations/versions/ee286bc4206a_create_webpush_target.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"""create_webpush_target
Revision ID: ee286bc4206a
Revises: 214878041f05
Create Date: 2024-03-25 12:52:56.462206
"""

# revision identifiers, used by Alembic.
revision = "ee286bc4206a"
down_revision = "214878041f05"

from datetime import datetime

from alembic import op
import sqlalchemy as sa


def upgrade():
op.create_table(
"web_push_target",
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
sa.Column("user_id", sa.Integer(), autoincrement=False, nullable=False),
sa.Column("endpoint", sa.String(), nullable=False),
sa.Column("expires", sa.DateTime(), nullable=True),
sa.Column("subscription_info", sa.JSON(), nullable=False),
sa.Column("created", sa.DateTime(), nullable=False, default=datetime.utcnow),
sa.PrimaryKeyConstraint("id"),
sa.ForeignKeyConstraint(
["user_id"],
["user.id"],
name=op.f("fk_web_push_target_mapping_user_id_user_id"),
),
)
op.create_index(op.f("ix_web_push_target_user_id"), "web_push_target", ["user_id"])
op.create_index(
op.f("ix_web_push_target_user_id_endpoint"),
"web_push_target",
["user_id", "endpoint"],
)


def downgrade():
op.drop_index("ix_web_push_target_user_id")
op.drop_index("ix_web_push_target_user_id_endpoint")
op.drop_table("web_push_target")
8 changes: 8 additions & 0 deletions models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from . import bucketise, BaseModel
from .permission import UserPermission, Permission
from .volunteer.shift import ShiftEntry
from .web_push import WebPushTarget

CHECKIN_CODE_LEN = 16
checkin_code_re = r"[0-9a-zA-Z_-]{%s}" % CHECKIN_CODE_LEN
Expand Down Expand Up @@ -290,6 +291,13 @@ class User(BaseModel, UserMixin):
)
village = association_proxy("village_membership", "village")

web_push_targets = db.relationship(
"WebPushTarget",
cascade="all, delete-orphan",
back_populates="user",
primaryjoin="WebPushTarget.user_id == User.id",
)

def __init__(self, email: str, name: str):
self.email = email
self.name = name
Expand Down
22 changes: 22 additions & 0 deletions models/web_push.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from datetime import datetime
from main import db

from . import BaseModel


class WebPushTarget(BaseModel):
__table_name__ = "web_push_target"
id = db.Column(db.Integer, primary_key=True)
endpoint = db.Column(db.String, nullable=False)
subscription_info = db.Column(db.JSON, nullable=False)
expires = db.Column(db.DateTime, nullable=True)
user_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)
created = db.Column(db.DateTime, default=datetime.utcnow, nullable=False)

user = db.relationship("User")

def __init__(self, user, endpoint, subscription_info, expires=None):
self.user = user
self.endpoint = endpoint
self.subscription_info = subscription_info
self.expires = expires

0 comments on commit 5f2c919

Please sign in to comment.