-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jackson Chadfield
committed
Jul 31, 2019
1 parent
82b29a9
commit d93d021
Showing
10 changed files
with
177 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
# -*- coding: utf-8 -*- | ||
import datetime | ||
import enum | ||
import random | ||
import string | ||
from decimal import Decimal | ||
from typing import Union, List | ||
|
||
from sqlalchemy import func | ||
|
||
from .extensions import db | ||
|
||
# Alias common SQLAlchemy names | ||
Column = db.Column | ||
relationship = db.relationship | ||
|
||
# Type Declarations | ||
MString = Union[str, db.Column] | ||
MInteger = Union[int, db.Column] | ||
MDecimal = Union[Decimal, db.Column] | ||
MBoolean = Union[bool, db.Column] | ||
MDateTime = Union[datetime.datetime, db.Column] | ||
MDate = Union[datetime.date, db.Column] | ||
|
||
|
||
class QuestionType(enum.Enum): | ||
Numeric = 0 | ||
Text = 1 | ||
|
||
@property | ||
def type(self): | ||
return { | ||
QuestionType.Numeric: NumericQuestion, | ||
QuestionType.Text: TextQuestion | ||
}[self] | ||
|
||
|
||
class User(db.Model): | ||
id: MInteger = db.Column(db.Integer, primary_key=True) | ||
username: MString = db.Column(db.String(25), unique=True, nullable=False) | ||
password = db.Column(db.Binary(60), nullable=False) | ||
|
||
quizzes: List["Quiz"] = relationship("Quiz", back_populates="owner") | ||
|
||
created_on: MDateTime = db.Column(db.DateTime(timezone=True), nullable=False, server_default=func.now()) | ||
|
||
|
||
class Quiz(db.Model): | ||
id: MInteger = db.Column(db.Integer, primary_key=True) | ||
name: MString = db.Column(db.String(30), unique=True, nullable=False) | ||
|
||
questions: List[Union["Question", "QuestionABC"]] = db.relationship('Question', back_populates="quiz", | ||
order_by="Question.sort_index") | ||
|
||
owner_id = db.Column(db.Integer, db.ForeignKey('user.id')) | ||
owner = db.relationship("User", back_populates="quizzes") | ||
|
||
created_on: MDateTime = db.Column(db.DateTime(timezone=True), nullable=False, server_default=func.now()) | ||
|
||
|
||
class Question(db.Model): | ||
__mapper_args__ = {'polymorphic_on': "type"} | ||
|
||
id: MInteger = db.Column(db.Integer, primary_key=True) | ||
text: MString = db.Column(db.Text, nullable=False) | ||
sort_index = db.Column(db.Integer, nullable=False) | ||
|
||
type: QuestionType = db.Column(db.Enum(QuestionType), nullable=False) | ||
|
||
quiz_id = db.Column(db.Integer, db.ForeignKey('quiz.id'), nullable=False) | ||
quiz: Quiz = db.relationship('Quiz', back_populates="questions") | ||
|
||
def __init__(self, *args, **kwargs): | ||
raise NotImplementedError | ||
|
||
def __eq__(self, other): | ||
raise NotImplementedError | ||
|
||
|
||
class NumericQuestion(Question): | ||
__tablename__ = "numericQuestion" | ||
__mapper_args__ = {'polymorphic_identity': QuestionType.Numeric} | ||
|
||
id = Column(db.Integer, db.ForeignKey('question.id'), primary_key=True) | ||
answer = db.Column(db.Numeric(precision=12, scale=5)) # 0000000.00000 | ||
|
||
def __init__(self, *args, **kwargs): | ||
super(Question, self).__init__(*args, **kwargs) | ||
|
||
def __eq__(self, other): | ||
return other == self.answer | ||
|
||
|
||
class TextQuestion(Question): | ||
__tablename__ = "textQuestion" | ||
__mapper_args__ = {'polymorphic_identity': QuestionType.Text} | ||
|
||
id = Column(db.Integer, db.ForeignKey('question.id'), primary_key=True) | ||
answer = db.Column(db.Text) | ||
|
||
def __init__(self, *args, **kwargs): | ||
super(Question, self).__init__(*args, **kwargs) | ||
|
||
def __eq__(self, other): | ||
return other == self.answer | ||
|
||
|
||
class Room(db.Model): | ||
id: MInteger = db.Column(db.Integer, primary_key=True) | ||
active: MBoolean = db.Column(db.Boolean, default=False) | ||
key: MString = db.Column(db.String(8), default=lambda: Room.generate_key()) | ||
|
||
controller_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) | ||
controller = db.relationship("User") | ||
|
||
quiz_id = db.Column(db.Integer, db.ForeignKey('quiz.id'), nullable=False) | ||
quiz: Quiz = db.relationship('Quiz') | ||
|
||
created_on: MDateTime = db.Column(db.DateTime(timezone=True), nullable=False, server_default=func.now()) | ||
|
||
@staticmethod | ||
def generate_key(): | ||
while True: | ||
temp_key = ''.join(random.choices(string.ascii_uppercase + string.digits, k=8)) | ||
if not db.session.query(db.exists().where(Room.active, Room.key == temp_key)).scalar(): | ||
return temp_key |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from flask_sqlalchemy import SQLAlchemy | ||
|
||
db = SQLAlchemy() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters