#70 Add content model

This commit is contained in:
2023-04-14 16:47:27 -04:00
parent b03c50445f
commit 80ccc17eef
2 changed files with 71 additions and 2 deletions

View File

@@ -107,7 +107,7 @@ class Assignment(db.Model):
created_at = sa.Column(sa.DateTime)
def from_dict(self, data) -> None:
for field in ["name", "course_id", "description", "due_date"]:
for field in ["name", "course_id", "description", "due_date"]:
if field in data:
setattr(self, field, data[field])
@@ -116,7 +116,30 @@ class Assignment(db.Model):
def to_dict(self) -> dict:
d = {}
for f in ["id", "name", "course_id", "description", "due_date", "created_at"]:
for f in ["id", "name", "course_id", "description", "due_date", "created_at"]:
d[f] = getattr(self, f)
return d
class Content(db.Model):
id = sa.Column(sa.Integer, primary_key=True)
name = sa.Column(sa.String(128), index=True)
body = sa.Column(sa.Text, index=True)
course_id = sa.Column(sa.ForeignKey(Course.id), index=True)
created_at = sa.Column(sa.DateTime)
def from_dict(self, data) -> None:
for field in ["name", "body"]:
if field in data:
setattr(self, field, data[field])
if not self.created_at:
self.created_at = datetime.now()
def to_dict(self) -> dict:
d = {}
for f in ["id", "course_id", "name", "body", "created_at"]:
d[f] = getattr(self, f)
return d

View File

@@ -0,0 +1,46 @@
"""Create content model
Revision ID: c62aec1c6b91
Revises: 1e88e783d238
Create Date: 2023-04-14 16:46:43.513842
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = 'c62aec1c6b91'
down_revision = '1e88e783d238'
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('content',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=128), nullable=True),
sa.Column('body', sa.Text(), nullable=True),
sa.Column('course_id', sa.Integer(), nullable=True),
sa.Column('created_at', sa.DateTime(), nullable=True),
sa.ForeignKeyConstraint(['course_id'], ['course.id'], ),
sa.PrimaryKeyConstraint('id')
)
with op.batch_alter_table('content', schema=None) as batch_op:
batch_op.create_index(batch_op.f('ix_content_body'), ['body'], unique=False)
batch_op.create_index(batch_op.f('ix_content_course_id'), ['course_id'], unique=False)
batch_op.create_index(batch_op.f('ix_content_name'), ['name'], unique=False)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('content', schema=None) as batch_op:
batch_op.drop_index(batch_op.f('ix_content_name'))
batch_op.drop_index(batch_op.f('ix_content_course_id'))
batch_op.drop_index(batch_op.f('ix_content_body'))
op.drop_table('content')
# ### end Alembic commands ###