Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""Create the moderation_status column."""

import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql

revision = "bd0cc0e6ed54"
down_revision = "1d26b96db4af"


def upgrade() -> None:
moderation_status_type = postgresql.ENUM(
"APPROVED",
"DENIED",
"SPAM",
"PENDING",
name="moderationstatus",
)
moderation_status_type.create(op.get_bind(), checkfirst=True)

op.add_column(
"annotation",
sa.Column("moderation_status", moderation_status_type, nullable=True),
)


def downgrade() -> None:
op.drop_column("annotation", "moderation_status")
26 changes: 21 additions & 5 deletions h/models/annotation.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
import datetime
from enum import Enum
from uuid import UUID

import sqlalchemy as sa
from sqlalchemy.dialects import postgresql as pg
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.ext.mutable import MutableDict, MutableList
from sqlalchemy.orm import Mapped, relationship

from h.db import Base, types
from h.models.group import Group
from h.util import markdown_render, uri
from h.util.user import split_user


class ModerationStatus(Enum):
APPROVED = "APPROVED"
PENDING = "PENDING"
DENIED = "DENIED"
SPAM = "SPAM"
Comment on lines +17 to +21
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I posted a comment on the POC about whether we want to use strings in the DB here or just ints?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This type of enum is stored in the DB as an integer. I reckon this is the right way way to do "translate strings to integers" when you do not care about the actual integer value.

If we wanted to just rename a value we might not bother on the DB or we could alter the type:

ALTER TYPE name RENAME value deniedtorejected;

that would just change the type and no rows would need to be updated.



class Annotation(Base):
"""Model class representing a single annotation."""

# Expose the ModerationStatus directly here
ModerationStatus = ModerationStatus

__tablename__ = "annotation"
__table_args__ = (
# Tags are stored in an array-type column, and indexed using a
Expand Down Expand Up @@ -68,7 +80,7 @@ class Annotation(Base):
index=True,
)

group = sa.orm.relationship(
group = relationship(
Group,
primaryjoin=(Group.pubid == groupid),
foreign_keys=[groupid],
Expand Down Expand Up @@ -138,11 +150,15 @@ class Annotation(Base):
uselist=True,
)

mentions = sa.orm.relationship("Mention", back_populates="annotation")
mentions = relationship("Mention", back_populates="annotation")

notifications = sa.orm.relationship(
"Notification", back_populates="source_annotation"
)
notifications = relationship("Notification", back_populates="source_annotation")

moderation_status: Mapped[ModerationStatus | None]
"""Current moderation status of the annotation.

None means the annotation is either "approved" before this column was added or it's a private annotation.
"""

@property
def uuid(self):
Expand Down