This repository has been archived by the owner on Apr 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
85 lines (67 loc) · 1.74 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import os
from sqlalchemy import Column, String, Integer, create_engine
from flask_sqlalchemy import SQLAlchemy
import json
from settings import DB_NAME, DB_HOST, DB_USER, DB_PASSWORD, DATABASE_URL
if not DATABASE_URL:
database_path = 'postgresql://{}:{}@{}/{}'.format(DB_USER, DB_PASSWORD, DB_HOST, DB_NAME)
else:
database_path = DATABASE_URL
db = SQLAlchemy()
'''
setup_db(app)
binds a flask application and a SQLAlchemy service
'''
def setup_db(app, database_path=database_path):
app.config["SQLALCHEMY_DATABASE_URI"] = database_path
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db.app = app
db.init_app(app)
db.create_all()
'''
Question
'''
class Question(db.Model):
__tablename__ = 'questions'
id = Column(Integer, primary_key=True)
question = Column(String)
answer = Column(String)
category = Column(String)
difficulty = Column(Integer)
def __init__(self, question, answer, category, difficulty, id=None):
if id:
self.id = id
self.question = question
self.answer = answer
self.category = category
self.difficulty = difficulty
def insert(self):
db.session.add(self)
db.session.commit()
def update(self):
db.session.commit()
def delete(self):
db.session.delete(self)
db.session.commit()
def format(self):
return {
'id': self.id,
'question': self.question,
'answer': self.answer,
'category': self.category,
'difficulty': self.difficulty
}
'''
Category
'''
class Category(db.Model):
__tablename__ = 'categories'
id = Column(Integer, primary_key=True)
type = Column(String)
def __init__(self, type):
self.type = type
def format(self):
return {
'id': self.id,
'type': self.type
}