Skip to content

Commit

Permalink
Add cfp_tag model
Browse files Browse the repository at this point in the history
  • Loading branch information
SamLR committed Dec 22, 2023
1 parent 4d956e1 commit 4df79d8
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
63 changes: 63 additions & 0 deletions migrations/versions/0042c470500c_add_cfp_tags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""add cfp tags
Revision ID: 0042c470500c
Revises: 279d06c3289e
Create Date: 2023-12-22 10:08:06.559664
"""

# revision identifiers, used by Alembic.
revision = '0042c470500c'
down_revision = '279d06c3289e'

from alembic import op
import sqlalchemy as sa


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('proposal_tag_version',
sa.Column('proposal_id', sa.Integer(), autoincrement=False, nullable=False),
sa.Column('tag_id', sa.Integer(), autoincrement=False, nullable=False),
sa.Column('transaction_id', sa.BigInteger(), autoincrement=False, nullable=False),
sa.Column('operation_type', sa.SmallInteger(), nullable=False),
sa.PrimaryKeyConstraint('proposal_id', 'tag_id', 'transaction_id', name=op.f('pk_proposal_tag_version'))
)
op.create_index(op.f('ix_proposal_tag_version_operation_type'), 'proposal_tag_version', ['operation_type'], unique=False)
op.create_index(op.f('ix_proposal_tag_version_transaction_id'), 'proposal_tag_version', ['transaction_id'], unique=False)
op.create_table('tag',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('tag', sa.String(), nullable=False),
sa.PrimaryKeyConstraint('id', name=op.f('pk_tag')),
sa.UniqueConstraint('tag', name=op.f('uq_tag_tag'))
)
op.create_table('tag_version',
sa.Column('id', sa.Integer(), autoincrement=False, nullable=False),
sa.Column('tag', sa.String(), autoincrement=False, nullable=False),
sa.Column('transaction_id', sa.BigInteger(), autoincrement=False, nullable=False),
sa.Column('operation_type', sa.SmallInteger(), nullable=False),
sa.PrimaryKeyConstraint('id', 'transaction_id', name=op.f('pk_tag_version'))
)
op.create_index(op.f('ix_tag_version_operation_type'), 'tag_version', ['operation_type'], unique=False)
op.create_index(op.f('ix_tag_version_transaction_id'), 'tag_version', ['transaction_id'], unique=False)
op.create_table('proposal_tag',
sa.Column('proposal_id', sa.Integer(), nullable=False),
sa.Column('tag_id', sa.Integer(), nullable=False),
sa.ForeignKeyConstraint(['proposal_id'], ['proposal.id'], name=op.f('fk_proposal_tag_proposal_id_proposal')),
sa.ForeignKeyConstraint(['tag_id'], ['tag.id'], name=op.f('fk_proposal_tag_tag_id_tag')),
sa.PrimaryKeyConstraint('proposal_id', 'tag_id', name=op.f('pk_proposal_tag'))
)
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('proposal_tag')
op.drop_index(op.f('ix_tag_version_transaction_id'), table_name='tag_version')
op.drop_index(op.f('ix_tag_version_operation_type'), table_name='tag_version')
op.drop_table('tag_version')
op.drop_table('tag')
op.drop_index(op.f('ix_proposal_tag_version_transaction_id'), table_name='proposal_tag_version')
op.drop_index(op.f('ix_proposal_tag_version_operation_type'), table_name='proposal_tag_version')
op.drop_table('proposal_tag_version')
# ### end Alembic commands ###
8 changes: 8 additions & 0 deletions models/cfp.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

from main import db
from .user import User
from .cfp_tag import ProposalTag
from . import BaseModel


Expand Down Expand Up @@ -370,6 +371,13 @@ class Proposal(BaseModel):
notice_required = db.Column(db.String)
private_notes = db.Column(db.String)

tags = db.relationship(
"Tag",
backref="proposals",
cascade="all",
secondary=ProposalTag,
)

# Flags
needs_help = db.Column(db.Boolean, nullable=False, default=False)
needs_money = db.Column(db.Boolean, nullable=False, default=False)
Expand Down
48 changes: 48 additions & 0 deletions models/cfp_tag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from main import db
import sqlalchemy
from . import BaseModel


class Tag(BaseModel):
__versioned__: dict = {}
__tablename__ = "tag"

id = db.Column(db.Integer, primary_key=True)
tag = db.Column(db.String, nullable=False, unique=True)

def __init__(self, tag: str):
self.tag = tag.strip().lower()

def __str__(self):
return self.tag

def __repr__(self):
return f"<Tag {self.id} '{self.tag}'>"

@classmethod
def serialise_tags(self, tag_list: list["Tag"]) -> str:
return ",".join([str(t) for t in tag_list])

@classmethod
def parse_serialised_tags(cls, tag_str: str) -> list["Tag"]:
res = []
tag_list = [t.strip().lower() for t in tag_str.split(",")]
for tag_value in tag_list:
if len(tag_value) == 0:
continue
tag = cls.query.filter_by(tag=tag_value).one_or_none()
if tag:
res.append(tag)
else:
res.append(Tag(tag_value))
return res


ProposalTag: sqlalchemy.Table = db.Table(
"proposal_tag",
BaseModel.metadata,
db.Column(
"proposal_id", db.Integer, db.ForeignKey("proposal.id"), primary_key=True
),
db.Column("tag_id", db.Integer, db.ForeignKey("tag.id"), primary_key=True),
)

0 comments on commit 4df79d8

Please sign in to comment.