-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
66 lines (51 loc) · 2.67 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Create SQLAlchemy models from the Base class
from sqlalchemy import MetaData, CheckConstraint, Column, ForeignKey, func, Integer, String, DateTime
from sqlalchemy.dialects.mysql import BINARY
from sqlalchemy.orm import relationship
import uuid
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class User(Base):
__tablename__ = "users_data"
__table_args__ = {'schema': 'webappdb'}
user_id = Column(String(36), primary_key=True, index=True,
default=lambda: str(uuid.uuid4()))
first_name = Column(String(45))
last_name = Column(String(45))
email = Column(String(50), unique=True, index=True)
password = Column(String(70))
account_created = Column(DateTime, default=func.now())
account_updated = Column(DateTime, default=func.now(), onupdate=func.now())
assignments = relationship("Assignment", back_populates="users")
class Assignment(Base):
__tablename__ = "assignment_data"
__table_args__ = {'schema': 'webappdb'}
assignment_id = Column(String(36), primary_key=True,
index=True, default=str(uuid.uuid4()), unique=True)
name = Column(String(45), nullable=False)
points = Column(Integer, CheckConstraint(
'points>=1 AND points <= 10'), nullable=False)
num_of_attempts = Column(Integer, CheckConstraint(
'num_of_attempts>=1 and num_of_attempts<=3'), nullable=False)
deadline = Column(DateTime, nullable=False)
assignment_created = Column(DateTime, default=func.now())
assignment_updated = Column(
DateTime, default=func.now(), onupdate=func.now())
u_id = Column(String(36), ForeignKey("webappdb.users_data.user_id"))
users = relationship("User", back_populates="assignments")
submissions = relationship(
"UserAssignmentSubmission", back_populates="assignment", cascade="all, delete-orphan")
class UserAssignmentSubmission(Base):
__tablename__ = "submission_data"
__table_args__ = {'schema': 'webappdb'}
id = Column(String(36), primary_key=True, index=True,
default=lambda: str(uuid.uuid4()), unique=True)
assignment_id = Column(String(36), ForeignKey(
"webappdb.assignment_data.assignment_id",ondelete='CASCADE'), nullable=False)
student_id = Column(String(36), ForeignKey(
"webappdb.users_data.user_id"), nullable=False)
submission_url = Column(String(), nullable=False)
submission_date = Column(DateTime, nullable=False, default=func.now())
submission_updated = Column(DateTime, onupdate=func.now(), nullable=False)
attempts = Column(Integer, nullable=False, default=1)
assignment = relationship("Assignment", back_populates="submissions")