-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
78 lines (56 loc) · 1.82 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
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
import os
# initialize
app = Flask(__name__)
basedir = os.path.abspath(os.path.dirname(__file__))
# Database
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///'+os.path.join(basedir,'db.sqlite')
app.config['SQLALCHAMEY_TRACK_MODIFICATIONS'] =False
# init DB
db = SQLAlchemy(app)
#init marshmallow
ma = Marshmallow(app)
class Topics(db.Model):
id = db.Column(db.Integer,primary_key=True,autoincrement=True)
topic = db.Column(db.String(100),unique=True)
@property
def total_questions(self):
print('self id = ',self.id)
return Question.query.filter(Question.topic==self.id).count()
@property
def to_json(self):
return {'id': self.id,'topic_name': self.topic,'total_questions':self.total_questions}
#def __str__(self,):
# return f'id : {self.id},topic : {self.topic}'
def __init__(self,topic):
self.topic =topic
# MCQ Schema
class TopicsSchema(ma.Schema):
class Meta:
fields = ['id','topic' ]
model =Topics
# MCQ class
class Question(db.Model):
id = db.Column(db.Integer,primary_key=True,autoincrement=True)
question = db.Column(db.String(255),unique=True)
answer = db.Column(db.String(255))
topic = db.Column(db.Integer,db.ForeignKey('topics.id'))
def __init__(self,question,answer,topic):
self.question=question
self.answer=answer
self.topic=topic
@property
def to_json(self):
dic={}
dic['id']=self.id
dic['question']=self.question
dic['answer'] = self.answer
dic['topic'] = self.topic
return dic
# MCQ Schema
class QuestionSchema(ma.Schema):
class Meta:
#fields = ['id','question','answer','topic' ]
model =Question