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

Generate project abbreviation code for project name #1401

Merged
merged 11 commits into from
Dec 6, 2023
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""Add unique constraint to projects abbreviation

Revision ID: 88b0bbb53b72
Revises: 03791c319e2b
Create Date: 2023-12-05 11:34:09.723702

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '88b0bbb53b72'
down_revision = '03791c319e2b'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('projects', schema=None) as batch_op:
batch_op.create_unique_constraint('uq_projects_abbreviation', ['abbreviation'])
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('projects', schema=None) as batch_op:
batch_op.drop_constraint('uq_projects_abbreviation', type_='unique')
# ### end Alembic commands ###
7 changes: 6 additions & 1 deletion epictrack-api/src/api/models/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class Project(BaseModelVersioned):
proponent_id = Column(ForeignKey("proponents.id"), nullable=False)
region_id_env = Column(ForeignKey("regions.id"), nullable=True)
region_id_flnro = Column(ForeignKey("regions.id"), nullable=True)
abbreviation = Column(String(10), nullable=True)
abbreviation = Column(String(10), nullable=True, unique=True)
sub_type = relationship("SubType", foreign_keys=[sub_type_id], lazy="select")
type = relationship("Type", foreign_keys=[type_id], lazy="select")
proponent = relationship("Proponent", foreign_keys=[proponent_id], lazy="select")
Expand All @@ -82,6 +82,11 @@ def check_existence(cls, name, project_id=None):
return True
return False

@classmethod
def get_by_abbreviation(cls, abbreviation: str):
"""Get project by abbreviation."""
return Project.query.filter_by(abbreviation=abbreviation).first()

def as_dict(self, recursive=True):
"""Return JSON Representation."""
data = super().as_dict(recursive)
Expand Down
Loading
Loading