class GroupModel(SQL.Model): ''' Representing one group containing iCPEs and Users ''' __tablename__ = 'group' id = SQL.Column(SQL.Integer, primary_key=True) name = SQL.Column(SQL.String(50)) email = SQL.Column(SQL.String(120)) description = SQL.Column(SQL.String(250)) date_created = SQL.Column(SQL.DateTime) users = SQL.relationship('UserModel', secondary=user_list, backref='groups') mqtts = SQL.relationship('MQTTModel', secondary=mqtt_list, backref='groups') nodes = SQL.relationship('NodeModel', secondary=node_list, backref='groups') location = SQL.relationship('LocationModel', uselist=False, backref='group') messages = SQL.relationship('MessageModel', backref='group', cascade='save-update, merge, delete') def __init__(self, name, email=None, description=None): self.name = name self.email = email self.description = str(description) self.date_created = datetime.now() def columns(self): return ['name', 'email', 'description'] def to_json(self): if self.location: location = self.location.to_json() else: location = None return { 'name': self.name, 'email': self.email, 'created': str(self.date_created), 'description': self.description, 'users': [user.email for user in self.users], 'nodes': [node.name for node in self.nodes], 'location': location, 'url': url_for('admin_view.admin_group', name=NodeDefender.serializer.dumps(self.name)) }
class UserModel(SQL.Model): ''' Table of Users Users is a part of a Group Password is encrypted ''' __tablename__ = 'user' id = SQL.Column(SQL.Integer, primary_key=True) firstname = SQL.Column(SQL.String(30)) lastname = SQL.Column(SQL.String(40)) email = SQL.Column(SQL.String(191), unique=True) password = SQL.Column(SQL.String(191)) enabled = SQL.Column(SQL.Boolean()) date_confirmed = SQL.Column(SQL.DateTime) date_created = SQL.Column(SQL.DateTime) date_last_login = SQL.Column(SQL.DateTime) date_current_login = SQL.Column(SQL.DateTime) last_login_ip = SQL.Column(SQL.String(100)) current_login_ip = SQL.Column(SQL.String(100)) login_count = SQL.Column(SQL.Integer) technician = SQL.Column(SQL.Boolean) administrator = SQL.Column(SQL.Boolean) superuser = SQL.Column(SQL.Boolean) messages = SQL.relationship('MessageModel', backref='user', cascade='save-update, merge, delete') def __init__(self, email): self.email = email self.firstname = None self.lastname = None self.password = None self.active = False self.date_confirmed = None self.date_created = datetime.now() self.technician = False self.administrator = False self.superuser = False def columns(self): return ['firstname', 'lastname'] def to_json(self): return { 'firstName': self.firstname, 'lastName': self.lastname, 'email': self.email, 'role': self.get_role(), 'enabled': self.enabled, 'dateCreated': str(self.date_created), 'dateConfirmed': str(self.date_confirmed) } def is_active(self): return True def get_id(self): return str(self.id) def is_authenticated(self): return self.authenticated def is_anonymous(self): return False def verify_password(self, password): if NodeDefender.bcrypt.check_password_hash(self.password, password): return True else: return False def get_role(self): if self.superuser: return 'superuser' elif self.administrator: return 'administrator' elif self.technician: return 'technician' else: return 'observer' def set_role(self, role): if role.lower() == 'observer': self.technician = False self.administrator = False self.superuser = False elif role.lower() == 'technician': self.technician = True self.administrator = False self.superuser = False elif role.lower() == 'administrator': self.technician = True self.administrator = True self.observer = False elif role.lower() == 'superuser': self.technician = True self.administrator = True self.superuser = True else: raise AttributeError('Wrong kind of role') def has_role(self, role): try: return getattr(self, role.lower()) except AttributeError: return None
from NodeDefender.db.sql import SQL from datetime import datetime from NodeDefender.db.sql.node import LocationModel import NodeDefender from flask import url_for user_list = SQL.Table( 'user_list', SQL.Column('group_id', SQL.Integer, SQL.ForeignKey('group.id')), SQL.Column('user_id', SQL.Integer, SQL.ForeignKey('user.id'))) node_list = SQL.Table( 'node_list', SQL.Column('group_id', SQL.Integer, SQL.ForeignKey('group.id')), SQL.Column('node_id', SQL.Integer, SQL.ForeignKey('node.id'))) mqtt_list = SQL.Table( 'mqtt_list', SQL.Column('group_id', SQL.Integer, SQL.ForeignKey('group.id')), SQL.Column('mqtt_id', SQL.Integer, SQL.ForeignKey('mqtt.id'))) class GroupModel(SQL.Model): ''' Representing one group containing iCPEs and Users ''' __tablename__ = 'group' id = SQL.Column(SQL.Integer, primary_key=True) name = SQL.Column(SQL.String(50)) email = SQL.Column(SQL.String(120)) description = SQL.Column(SQL.String(250)) date_created = SQL.Column(SQL.DateTime)
class LocationModel(SQL.Model): ''' One-to-one Table representing Location for iCPE ''' __tablename__ = 'location' id = SQL.Column(SQL.Integer, primary_key=True) node_id = SQL.Column(SQL.Integer, SQL.ForeignKey('node.id')) group_id = SQL.Column(SQL.Integer, SQL.ForeignKey('group.id')) street = SQL.Column(SQL.String(30)) city = SQL.Column(SQL.String(30)) latitude = SQL.Column(SQL.String(10)) longitude = SQL.Column(SQL.String(10)) def __init__(self, street, city, latitude, longitude): self.street = street self.city = city self.latitude = latitude self.longitude = longitude def to_json(self): return { 'street': self.street, 'city': self.city, 'latitude': self.latitude, 'longitude': self.longitude } def __repr__(self): return '<%r, %r>' % (self.street, self.city)
class NodeModel(SQL.Model): ''' Nodes represent a place that can contain one or more iCPEs Parent is the GroupModel that owns the Node Alias is the Name of the Node Location is stored in one-to-one Relation(LocationModel) Notes is to enter notebook for the Node NoteSticky is a sticky note for that node iCPES is relationship to List of iCPEs in that Node ''' __tablename__ = 'node' id = SQL.Column(SQL.Integer, primary_key=True) name = SQL.Column(SQL.String(40), unique=True) description = SQL.Column(SQL.String(128)) location = SQL.relationship('LocationModel', uselist=False, backref='node') date_created = SQL.Column(SQL.DateTime) notes = SQL.relationship('NodeNotesModel', backref='node') notesticky = SQL.Column(SQL.String(150)) icpe = SQL.relationship('iCPEModel', backref='node', uselist=False) heat = SQL.relationship('HeatModel', backref="node", cascade="save-update, merge, delete") power = SQL.relationship('PowerModel', backref="node", cascade="save-update, merge, delete") events = SQL.relationship('EventModel', backref="node", cascade="save-update, merge, delete") messages = SQL.relationship('MessageModel', backref='node', cascade='save-update, merge, delete') def __init__(self, name): self.name = name self.date_created = datetime.now() def columns(self): return ['name', 'description', 'notesticky'] def to_json(self): if self.location: location = self.location.to_json() else: location = None if self.icpe: icpe = NodeDefender.db.icpe.get(self.icpe.mac_address) else: icpe = False return { 'name': self.name, 'description': self.description, 'location': location, 'url': url_for('node_view.nodes_node', name=NodeDefender.serializer.dumps(self.name)), 'icpe': icpe }
class MQTTModel(SQL.Model): __tablename__ = 'mqtt' id = SQL.Column(SQL.Integer, primary_key=True) host = SQL.Column(SQL.String(128)) port = SQL.Column(SQL.Integer) username = SQL.Column(SQL.String(64)) password = SQL.Column(SQL.String(64)) icpes = SQL.relationship('iCPEModel', secondary=mqtt_icpe, backref=SQL.backref('mqtt', lazy='dynamic')) date_created = SQL.Column(SQL.DateTime) def __init__(self, host, port, username=None, password=None): self.host = host self.port = int(port) self.username = username self.password = password self.date_created = datetime.now() def online(self): return NodeDefender.db.mqtt.online(self.host, self.port) def to_json(self): return { 'id': str(self.id), 'host': self.host, 'port': self.port, 'date_created': str(self.date_created), 'online': True, 'username ': None, 'password': None, 'groups': [group.name for group in self.groups], 'icpes': [icpe.mac_address for icpe in self.icpes] }
from NodeDefender.db.sql import SQL from NodeDefender.db.sql.icpe import iCPEModel from datetime import datetime import NodeDefender mqtt_icpe = SQL.Table( 'mqtt_icpe', SQL.Column('mqtt_id', SQL.Integer, SQL.ForeignKey('mqtt.id')), SQL.Column('icpe_id', SQL.Integer, SQL.ForeignKey('icpe.id'))) class MQTTModel(SQL.Model): __tablename__ = 'mqtt' id = SQL.Column(SQL.Integer, primary_key=True) host = SQL.Column(SQL.String(128)) port = SQL.Column(SQL.Integer) username = SQL.Column(SQL.String(64)) password = SQL.Column(SQL.String(64)) icpes = SQL.relationship('iCPEModel', secondary=mqtt_icpe, backref=SQL.backref('mqtt', lazy='dynamic')) date_created = SQL.Column(SQL.DateTime) def __init__(self, host, port, username=None, password=None): self.host = host self.port = int(port) self.username = username self.password = password self.date_created = datetime.now() def online(self):