diff --git a/app/models/category.py b/app/models/category.py new file mode 100644 index 0000000000000000000000000000000000000000..593c31b4829610c0d2dea157649dcdcb23e3d573 --- /dev/null +++ b/app/models/category.py @@ -0,0 +1,10 @@ +from ..database import db + + +class Category(db.Model): + code = db.Column(db.String, primary_key=True) + title = db.Column(db.String) + + @property + def name(self) -> str: + return self.title if self.title else self.code diff --git a/app/models/person.py b/app/models/person.py index 49cc97b195776748e47469132d5b00b82d8bf783..2e4f3670d026e84d1fa845572fc59a84d7fca352 100644 --- a/app/models/person.py +++ b/app/models/person.py @@ -5,6 +5,7 @@ class Person(db.Model): username = db.Column(db.String, primary_key=True) firstname = db.Column(db.String, nullable=False) lastname = db.Column(db.String, nullable=False) + categories = db.Column(db.ARRAY(db.String), nullable=False) @property def full_name(self) -> str: diff --git a/app/models/project.py b/app/models/project.py index d6792a0ce831d21724624a36132f160108de523b..e511eefc394ad26894be65bca7c97e838a9cb0f0 100644 --- a/app/models/project.py +++ b/app/models/project.py @@ -1,4 +1,5 @@ from ..database import db +from .category import Category from .dialect_compatibility import utcnow @@ -8,6 +9,7 @@ class Project(db.Model): on_behalf = db.Column(db.String) title = db.Column(db.String) description = db.Column(db.String) + category = db.Column(db.String, db.ForeignKey(Category.code), nullable=False) timestamp = db.Column(db.DateTime, nullable=False, server_default=utcnow()) deleted = db.Column(db.DateTime) diff --git a/migrations/versions/c5b560bb3d58_add_category_information.py b/migrations/versions/c5b560bb3d58_add_category_information.py new file mode 100644 index 0000000000000000000000000000000000000000..a2a31d536525a0ff9d2f386f5748ebafa5e1862d --- /dev/null +++ b/migrations/versions/c5b560bb3d58_add_category_information.py @@ -0,0 +1,31 @@ +"""Add category information. + +Revision ID: c5b560bb3d58 +Revises: c7458a7dd4d8 +Create Date: 2023-02-03 17:59:59.982256 + +""" +import sqlalchemy as sa +from alembic import op + +# revision identifiers, used by Alembic. +revision = "c5b560bb3d58" +down_revision = "c7458a7dd4d8" +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column( + "person", sa.Column("categories", sa.ARRAY(sa.String()), nullable=False) + ) + op.add_column("project", sa.Column("category", sa.String(), nullable=False)) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_column("project", "category") + op.drop_column("person", "categories") + # ### end Alembic commands ### diff --git a/migrations/versions/e794d182f9d3_add_foreign_key_for_category_in_project_.py b/migrations/versions/e794d182f9d3_add_foreign_key_for_category_in_project_.py new file mode 100644 index 0000000000000000000000000000000000000000..48d22015013db6b724c480d0189a92a387f4292e --- /dev/null +++ b/migrations/versions/e794d182f9d3_add_foreign_key_for_category_in_project_.py @@ -0,0 +1,34 @@ +"""Add foreign key for category in project table. + +Revision ID: e794d182f9d3 +Revises: c5b560bb3d58 +Create Date: 2023-02-03 18:02:46.930212 + +""" +import sqlalchemy as sa +from alembic import op + +# revision identifiers, used by Alembic. +revision = "e794d182f9d3" +down_revision = "c5b560bb3d58" +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table( + "category", + sa.Column("code", sa.String(), nullable=False), + sa.Column("title", sa.String(), nullable=True), + sa.PrimaryKeyConstraint("code"), + ) + op.create_foreign_key(None, "project", "category", ["category"], ["code"]) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_constraint(None, "project", type_="foreignkey") + op.drop_table("category") + # ### end Alembic commands ###