#5-create-course #30
@@ -1,17 +1,18 @@
|
|||||||
from app import db
|
from app import db
|
||||||
|
import sqlalchemy as sa
|
||||||
from flask_login import UserMixin
|
from flask_login import UserMixin
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from werkzeug.security import generate_password_hash, check_password_hash
|
from werkzeug.security import generate_password_hash, check_password_hash
|
||||||
|
|
||||||
|
|
||||||
class User(UserMixin, db.Model):
|
class User(UserMixin, db.Model):
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
id = sa.Column(sa.Integer, primary_key=True)
|
||||||
username = db.Column(db.String(64), index=True, unique=True)
|
username = sa.Column(sa.String(64), index=True, unique=True)
|
||||||
role = db.Column(db.String(32), index=True)
|
role = sa.Column(sa.String(32), index=True)
|
||||||
email = db.Column(db.String(120), index=True, unique=True)
|
email = sa.Column(sa.String(120), index=True, unique=True)
|
||||||
password_hash = db.Column(db.String(128))
|
password_hash = sa.Column(sa.String(128))
|
||||||
last_seen = db.Column(db.DateTime, default=datetime.utcnow)
|
last_seen = sa.Column(sa.DateTime, default=datetime.utcnow)
|
||||||
token = db.Column(db.String(32), index=True, unique=True)
|
token = sa.Column(sa.String(32), index=True, unique=True)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return f"<User {self.username}>"
|
return f"<User {self.username}>"
|
||||||
@@ -35,3 +36,12 @@ class User(UserMixin, db.Model):
|
|||||||
setattr(self, field, data[field])
|
setattr(self, field, data[field])
|
||||||
if new_user and "password" in data:
|
if new_user and "password" in data:
|
||||||
self.set_password(data["password"])
|
self.set_password(data["password"])
|
||||||
|
|
||||||
|
class Course(db.Model):
|
||||||
|
id = sa.Column(sa.Integer, primary_key=True)
|
||||||
|
name = sa.Column(sa.String(128), index=True)
|
||||||
|
description = sa.Column(sa.Text, index=True)
|
||||||
|
instructor = sa.Column(sa.ForeignKey(User.id), index=True)
|
||||||
|
created_at = sa.Column(sa.DateTime, default=datetime.now())
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
import dotenv
|
import dotenv
|
||||||
|
|
||||||
dotenv.load_dotenv()
|
dotenv.load_dotenv()
|
||||||
|
|
||||||
from app import create_app, db
|
from app import create_app, db
|
||||||
from app.models import User
|
from app.models import Course, User
|
||||||
from flask_cors import CORS
|
from flask_cors import CORS
|
||||||
|
|
||||||
app = create_app()
|
app = create_app()
|
||||||
@@ -11,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}
|
return {"db": db, "User": User, "Course": Course}
|
||||||
|
|||||||
46
backend/migrations/versions/471b4225837e_.py
Normal file
46
backend/migrations/versions/471b4225837e_.py
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
"""empty message
|
||||||
|
|
||||||
|
Revision ID: 471b4225837e
|
||||||
|
Revises: 8e48199f1417
|
||||||
|
Create Date: 2023-04-06 14:53:43.061616
|
||||||
|
|
||||||
|
"""
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = '471b4225837e'
|
||||||
|
down_revision = '8e48199f1417'
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
op.create_table('course',
|
||||||
|
sa.Column('id', sa.Integer(), nullable=False),
|
||||||
|
sa.Column('name', sa.String(length=128), nullable=True),
|
||||||
|
sa.Column('description', sa.Text(), nullable=True),
|
||||||
|
sa.Column('instructor', sa.Integer(), nullable=True),
|
||||||
|
sa.Column('created_at', sa.DateTime(), nullable=True),
|
||||||
|
sa.ForeignKeyConstraint(['instructor'], ['user.id'], ),
|
||||||
|
sa.PrimaryKeyConstraint('id')
|
||||||
|
)
|
||||||
|
with op.batch_alter_table('course', schema=None) as batch_op:
|
||||||
|
batch_op.create_index(batch_op.f('ix_course_description'), ['description'], unique=False)
|
||||||
|
batch_op.create_index(batch_op.f('ix_course_instructor'), ['instructor'], unique=False)
|
||||||
|
batch_op.create_index(batch_op.f('ix_course_name'), ['name'], unique=False)
|
||||||
|
|
||||||
|
# ### end Alembic commands ###
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
# ### commands auto generated by Alembic - please adjust! ###
|
||||||
|
with op.batch_alter_table('course', schema=None) as batch_op:
|
||||||
|
batch_op.drop_index(batch_op.f('ix_course_name'))
|
||||||
|
batch_op.drop_index(batch_op.f('ix_course_instructor'))
|
||||||
|
batch_op.drop_index(batch_op.f('ix_course_description'))
|
||||||
|
|
||||||
|
op.drop_table('course')
|
||||||
|
# ### end Alembic commands ###
|
||||||
Reference in New Issue
Block a user