diff --git a/apis/paios/openapi.yaml b/apis/paios/openapi.yaml index 68a4dd9e..d1b34dbb 100644 --- a/apis/paios/openapi.yaml +++ b/apis/paios/openapi.yaml @@ -1363,6 +1363,7 @@ components: $ref: '#/components/schemas/uuid4' user_id: type: string + format: uuid nullable: true expiration_dt: type: string diff --git a/backend/models.py b/backend/models.py index 251cbe4f..c7df9030 100644 --- a/backend/models.py +++ b/backend/models.py @@ -61,7 +61,7 @@ class Persona(SQLModelBase, table=True): class Share(SQLModelBase, table=True): id: str = Field(primary_key=True) # the short URL tag, eg abcd-efgh-ijkl resource_id: str = Field(foreign_key="resource.id") # the bot ID - user_id: str | None = Field(default=None) # the user granted access (optional) + user_id: str | None = Field(foreign_key="user.id") # the user granted access (optional) expiration_dt: datetime | None = Field(default=None) # the link expiration date/time (optional) is_revoked: bool = Field() diff --git a/backend/schemas.py b/backend/schemas.py index fce66742..65217001 100644 --- a/backend/schemas.py +++ b/backend/schemas.py @@ -70,12 +70,10 @@ class ShareBaseSchema(BaseModel): expiration_dt: Optional[datetime] = None is_revoked: Optional[bool] = False - @field_serializer('user_id') + @field_serializer('user_id', when_used='unless-none') def serialize_user_id(self, user_id: str, _info): if user_id: return user_id - else: - return "" @field_serializer('expiration_dt', when_used='unless-none') def serialize_expiration_dt(self, dt: datetime, _info): diff --git a/frontend/src/shares.tsx b/frontend/src/shares.tsx index 9a656437..7bf560c2 100644 --- a/frontend/src/shares.tsx +++ b/frontend/src/shares.tsx @@ -11,6 +11,9 @@ const shareFilters = [ , + + + , ]; export const ShareList = () => ( @@ -20,6 +23,9 @@ export const ShareList = () => ( + + + @@ -32,6 +38,9 @@ export const ShareShow = () => ( + + + @@ -44,6 +53,9 @@ export const ShareEdit = () => ( + + + @@ -56,6 +68,9 @@ export const ShareCreate = () => ( + + + diff --git a/migrations/env.py b/migrations/env.py index 499904f3..4c0a717a 100644 --- a/migrations/env.py +++ b/migrations/env.py @@ -57,6 +57,7 @@ def run_migrations_offline() -> None: literal_binds=True, dialect_opts={"paramstyle": "named"}, include_schemas=True, + render_as_batch=True, ) with context.begin_transaction(): @@ -81,6 +82,7 @@ def run_migrations_online() -> None: connection=connection, target_metadata=target_metadata, include_schemas=True, + render_as_batch=True, ) with context.begin_transaction(): diff --git a/migrations/versions/4716615fdc5d_share_add_userid_foreignkey_constraint.py b/migrations/versions/4716615fdc5d_share_add_userid_foreignkey_constraint.py new file mode 100644 index 00000000..a2596d17 --- /dev/null +++ b/migrations/versions/4716615fdc5d_share_add_userid_foreignkey_constraint.py @@ -0,0 +1,29 @@ +"""share convert user to FK + +Revision ID: 4716615fdc5d +Revises: 187855982332 +Create Date: 2024-10-03 12:24:37.618156 + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa +import sqlmodel + + +# revision identifiers, used by Alembic. +revision: str = '4716615fdc5d' +down_revision: Union[str, None] = '187855982332' +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + with op.batch_alter_table('share', schema=None) as batch_op: + batch_op.create_foreign_key('user_id', 'user', ['user_id'], ['id']) + + +def downgrade() -> None: + with op.batch_alter_table('share', schema=None) as batch_op: + batch_op.drop_constraint(None, type_='foreignkey')