-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
52 lines (40 loc) · 1.98 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
# encoding: utf-8
from datetime import datetime
from werkzeug.security import generate_password_hash, check_password_hash
from exts import db
class User(db.Model):
__tablename__ = "user"
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
telephone = db.Column(db.String(11), nullable=False)
username = db.Column(db.String(50), nullable=False)
password = db.Column(db.String(100), nullable=False)
def __init__(self, *args, **kwargs):
self.telephone = kwargs.get("telephone")
self.username = kwargs.get("username")
password = kwargs.get("password")
self.password = generate_password_hash(password)
def check_password(self, raw_password):
result = check_password_hash(self.password, raw_password)
return result
class Question(db.Model):
__tablename__ = "question"
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
title = db.Column(db.String(100), nullable=False)
content = db.Column(db.Text(), nullable=False)
create_time = db.Column(db.DateTime, default=datetime.now)
author_id = db.Column(db.Integer, db.ForeignKey("user.id"))
author = db.relationship("User", backref=db.backref("questions"))
class Answer(db.Model):
__tablename__ = "answer"
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
content = db.Column(db.Text, nullable=False)
create_time = db.Column(db.DateTime, default=datetime.now)
question_id = db.Column(db.Integer, db.ForeignKey("question.id"))
author_id = db.Column(db.Integer, db.ForeignKey("user.id"))
question = db.relationship("Question", backref=db.backref("answers"), order_by=id.desc())
author = db.relationship("User", backref=db.backref("answers"), order_by=id.desc())
class Sensor(db.Model):
__tablename__ = "sensor"
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
temperature = db.Column(db.Float, nullable=False)
datetime = db.Column(db.DateTime, default=datetime.now)