#10 Create assignment model
This commit is contained in:
@@ -58,7 +58,7 @@ class User(UserMixin, db.Model):
|
|||||||
"id": self.id,
|
"id": self.id,
|
||||||
"username": self.username,
|
"username": self.username,
|
||||||
"email": self.email,
|
"email": self.email,
|
||||||
"role": self.role
|
"role": self.role,
|
||||||
}
|
}
|
||||||
|
|
||||||
def from_dict(self, data, new_user=False) -> None:
|
def from_dict(self, data, new_user=False) -> None:
|
||||||
@@ -76,6 +76,7 @@ class Course(db.Model):
|
|||||||
description = sa.Column(sa.Text, index=True)
|
description = sa.Column(sa.Text, index=True)
|
||||||
instructor = sa.Column(sa.ForeignKey(User.id), index=True)
|
instructor = sa.Column(sa.ForeignKey(User.id), index=True)
|
||||||
created_at = sa.Column(sa.DateTime)
|
created_at = sa.Column(sa.DateTime)
|
||||||
|
assignments = db.relationship("Assignment", backref="course", lazy="dynamic")
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return f"<Course {self.course_code}>"
|
return f"<Course {self.course_code}>"
|
||||||
@@ -95,3 +96,11 @@ class Course(db.Model):
|
|||||||
|
|
||||||
d["instructor"] = User.query.get(self.instructor).username
|
d["instructor"] = User.query.get(self.instructor).username
|
||||||
return d
|
return d
|
||||||
|
|
||||||
|
|
||||||
|
class Assignment(db.Model):
|
||||||
|
id = sa.Column(sa.Integer, primary_key=True)
|
||||||
|
name = sa.Column(sa.String(128), index=True)
|
||||||
|
course_id = sa.Column(sa.ForeignKey(Course.id), index=True)
|
||||||
|
description = sa.Column(sa.Text, index=True)
|
||||||
|
created_at = sa.Column(sa.DateTime)
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import dotenv
|
|||||||
dotenv.load_dotenv()
|
dotenv.load_dotenv()
|
||||||
|
|
||||||
from app import create_app, db
|
from app import create_app, db
|
||||||
from app.models import Course, User
|
from app.models import Assignment, Course, User
|
||||||
from flask_cors import CORS
|
from flask_cors import CORS
|
||||||
|
|
||||||
app = create_app()
|
app = create_app()
|
||||||
@@ -12,4 +12,4 @@ CORS(app, supports_credentials=True, resources={r"/.*": {"origins": r".*localhos
|
|||||||
|
|
||||||
@app.shell_context_processor
|
@app.shell_context_processor
|
||||||
def make_shell_context():
|
def make_shell_context():
|
||||||
return {"db": db, "User": User, "Course": Course}
|
return {"db": db, "User": User, "Course": Course, "Assignment": Assignment}
|
||||||
|
|||||||
46
backend/migrations/versions/cab0d39ef662_.py
Normal file
46
backend/migrations/versions/cab0d39ef662_.py
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
"""Create assignment model
|
||||||
|
|
||||||
|
Revision ID: cab0d39ef662
|
||||||
|
Revises: 862905f5e34a
|
||||||
|
Create Date: 2023-04-13 18:27:15.748107
|
||||||
|
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = 'cab0d39ef662'
|
||||||
|
down_revision = '862905f5e34a'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.create_table('assignment',
|
||||||
|
sa.Column('id', sa.Integer(), nullable=False),
|
||||||
|
sa.Column('name', sa.String(length=128), nullable=True),
|
||||||
|
sa.Column('course_id', sa.Integer(), nullable=True),
|
||||||
|
sa.Column('description', sa.Text(), nullable=True),
|
||||||
|
sa.Column('created_at', sa.DateTime(), nullable=True),
|
||||||
|
sa.ForeignKeyConstraint(['course_id'], ['course.id'], ),
|
||||||
|
sa.PrimaryKeyConstraint('id')
|
||||||
|
)
|
||||||
|
with op.batch_alter_table('assignment', schema=None) as batch_op:
|
||||||
|
batch_op.create_index(batch_op.f('ix_assignment_course_id'), ['course_id'], unique=False)
|
||||||
|
batch_op.create_index(batch_op.f('ix_assignment_description'), ['description'], unique=False)
|
||||||
|
batch_op.create_index(batch_op.f('ix_assignment_name'), ['name'], unique=False)
|
||||||
|
|
||||||
|
# ### end Alembic commands ###
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
with op.batch_alter_table('assignment', schema=None) as batch_op:
|
||||||
|
batch_op.drop_index(batch_op.f('ix_assignment_name'))
|
||||||
|
batch_op.drop_index(batch_op.f('ix_assignment_description'))
|
||||||
|
batch_op.drop_index(batch_op.f('ix_assignment_course_id'))
|
||||||
|
|
||||||
|
op.drop_table('assignment')
|
||||||
|
# ### end Alembic commands ###
|
||||||
Reference in New Issue
Block a user