Skip to content
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

Add apitokens table to database for E-kirjasto Data API #41

Merged
merged 1 commit into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions alembic/versions/20240405_b28ac9090d40_add_apitoken_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""Add ApiToken model

Revision ID: b28ac9090d40
Revises: be8ed331efcc
Create Date: 2024-04-05 12:18:48.616932+00:00

"""

import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
revision = "b28ac9090d40"
down_revision = "be8ed331efcc"
branch_labels = None
depends_on = None


def upgrade() -> None:
op.create_table(
"apitokens",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("token", sa.String(), nullable=False),
sa.Column("label", sa.String(), nullable=False),
sa.Column("collection_id", sa.Integer(), nullable=True),
sa.Column("created", sa.DateTime(timezone=True), nullable=False),
sa.ForeignKeyConstraint(
["collection_id"], ["collections.id"], ondelete="CASCADE"
),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("label"),
sa.UniqueConstraint("token"),
)


def downgrade() -> None:
op.drop_table("apitokens")
1 change: 1 addition & 0 deletions core/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,7 @@ def _bulk_operation(self):
SAMLFederation,
)
from core.model.admin import Admin, AdminRole
from core.model.apitokens import ApiToken
from core.model.circulationevent import CirculationEvent
from core.model.classification import Classification, Genre, Subject
from core.model.collection import (
Expand Down
22 changes: 22 additions & 0 deletions core/model/apitokens.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from datetime import datetime

from sqlalchemy import Column, DateTime, ForeignKey, Integer, String
from sqlalchemy.orm import Mapped, relationship

from core.model import Base
from core.model.collection import Collection


class ApiToken(Base):
__tablename__ = "apitokens"

id = Column(Integer, primary_key=True)
token = Column(String, unique=True, nullable=False)
label = Column(String, unique=True, nullable=False)
collection_id = Column(Integer, ForeignKey("collections.id", ondelete="CASCADE"))
created = Column(DateTime(timezone=True), nullable=False, default=datetime.now())

collection: Mapped[list[Collection]] = relationship("Collection")

def __repr__(self):
return f"<ApiToken id={self.id} token={self.token} label={self.label} collection_id={self.collection_id}>"