Files
COMP-2707-final-project/backend/app/models.py

147 lines
4.7 KiB
Python

from app import db
import sqlalchemy as sa
from flask_login import UserMixin
from datetime import datetime
from werkzeug.security import generate_password_hash, check_password_hash
enrollment = db.Table(
"enrollment",
sa.Column("user_id", sa.ForeignKey("user.id"), primary_key=True),
sa.Column("course_id", sa.ForeignKey("course.id"), primary_key=True),
)
class User(UserMixin, db.Model):
id = sa.Column(sa.Integer, primary_key=True)
username = sa.Column(sa.String(64), index=True, unique=True)
role = sa.Column(sa.String(32), index=True)
email = sa.Column(sa.String(120), index=True, unique=True)
password_hash = sa.Column(sa.String(128))
last_seen = sa.Column(sa.DateTime, default=datetime.utcnow)
token = sa.Column(sa.String(32), index=True, unique=True)
enrolled_courses = db.relationship(
"Course",
secondary=enrollment,
backref=db.backref("students", lazy="dynamic"),
lazy="dynamic",
)
def __repr__(self) -> str:
return f"<User {self.username}>"
def set_password(self, password) -> None:
self.password_hash = generate_password_hash(password)
def check_password(self, password) -> bool:
return check_password_hash(self.password_hash, password)
def is_enrolled(self, c) -> bool:
return self.enrolled_courses.filter(enrollment.c.course_id == c.id).count() > 0
def enroll(self, c) -> bool:
if not self.is_enrolled(c):
self.enrolled_courses.append(c)
db.session.commit()
return True
return False
def unenroll(self, c) -> bool:
if self.is_enrolled(c):
self.enrolled_courses.remove(c)
db.session.commit()
return True
return False
def to_dict(self) -> dict:
return {
"id": self.id,
"username": self.username,
"email": self.email,
"role": self.role,
}
def from_dict(self, data, new_user=False) -> None:
for field in ["role", "username", "email"]:
if field in data:
setattr(self, field, data[field])
if new_user and "password" in data:
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)
course_code = sa.Column(sa.String(32), index=True)
description = sa.Column(sa.Text, index=True)
instructor = sa.Column(sa.ForeignKey(User.id), index=True)
created_at = sa.Column(sa.DateTime)
assignments = db.relationship("Assignment", backref="course", lazy="dynamic")
content = db.relationship("Content", backref="course", lazy="dynamic")
def __repr__(self) -> str:
return f"<Course {self.course_code}>"
def from_dict(self, data) -> None:
for field in ["name", "course_code", "description", "instructor"]:
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", "name", "course_code", "description", "created_at"]:
d[f] = getattr(self, f)
d["instructor"] = User.query.get(self.instructor).username
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)
due_date = sa.Column(sa.DateTime)
created_at = sa.Column(sa.DateTime)
def from_dict(self, data) -> None:
for field in ["name", "course_id", "description", "due_date"]:
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", "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", "course_id"]:
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