forked from muneeb-ali/apache-flask
-
Notifications
You must be signed in to change notification settings - Fork 3
/
db.py
67 lines (52 loc) · 1.94 KB
/
db.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
"""
:author: Miten Chauhan
This file holds the database model. Each class represents an SQL table which
is automatically converted into SQL by the SQLAlchemy database package.
"""
import os
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
basedir = os.path.abspath(os.path.dirname(__file__))
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'app.db')
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = SQLALCHEMY_DATABASE_URI
db = SQLAlchemy(app)
class Device(db.Model):
"""Table model for all Devices.
This class represents the table model for all the devices.
Attributes:
id: an automatically generated id for a device.
label: the name of the device.
d_type: the type of the device (e.g. node or sensor)
"""
id = db.Column(db.Integer, primary_key=True)
label = db.Column(db.String(80), unique=True)
d_type = db.Column(db.String(80))
def __init__(self, label, d_type):
""" Inits Device with relevant user info.
"""
self.label = label
self.d_type = d_type
def __repr__(self):
"""A string representation of an item stored in the table.
"""
return '<Device %r>' % self.label
# Edge Table
class Edge(db.Model):
"""Table model for an Edge.
Attributes:
id: an automaticallt generated id for an edge.
Device_id: the id of the device the edge has a relationship with.
device: the relationship between an Edge and a Device.
"""
id = db.Column(db.Integer, primary_key=True)
Device_id = db.Column(db.Integer, db.ForeignKey('device.id'))
device = db.relationship('Device', backref=db.backref('posts', lazy='dynamic'))
def __init__(self, device):
"""Inits Edge with relevant user info.
"""
self.device = device
def __repr__(self):
"""A String representation of an edge.
"""
return '<Device %r>' % self.device