class User(db.Model): """ User Model for storing user related details """ __tablename__ = "user" id = db.Column(db.Integer, primary_key=True, autoincrement=True) email = db.Column(db.String(255), unique=True, nullable=False) registered_on = db.Column(db.DateTime, nullable=False) admin = db.Column(db.Boolean, nullable=False, default=False) public_id = db.Column(db.String(100), unique=True) username = db.Column(db.String(50), unique=True) password_hash = db.Column(db.String(100)) @property def password(self): raise AttributeError('password: write-only field') @password.setter def password(self, password): self.password_hash = flask_bcrypt.generate_password_hash(password).decode('utf-8') def check_password(self, password): return flask_bcrypt.check_password_hash(self.password_hash, password) @staticmethod def encode_auth_token(user_id): """ Generates the Auth Token :return: string """ try: payload = { 'exp': datetime.datetime.utcnow() + datetime.timedelta(days=1, seconds=5), 'iat': datetime.datetime.utcnow(), 'sub': user_id } return jwt.encode( payload, key, algorithm='HS256' ) except Exception as e: return e @staticmethod def decode_auth_token(auth_token): """ Decodes the auth token :param auth_token: :return: integer|string """ try: payload = jwt.decode(auth_token, key) return payload['sub'] except jwt.ExpiredSignatureError: return 'Signature expired. Please log in again.' except jwt.InvalidTokenError: return 'Invalid token. Please log in again.' def __repr__(self): return "<User '{}'>".format(self.username)
class Host(db.Model): __tablename__ = 'hosts' uuid = db.Column(UUIDType(binary=False), primary_key=True, default=uuid.uuid4()) code = db.Column(db.String(64), unique=True, nullable=False) ip_address = db.Column(IPAddressType, unique=True, nullable=False) conn_user = db.Column(db.String(64), nullable=False) lab_uuid = db.Column(UUIDType(binary=False), db.ForeignKey('labs.uuid')) def __init__(self, data): self.uuid = uuid.uuid4() self.code = data['code'].upper() self.ip_address = data['ip_address'] self.conn_user = data['conn_user'] def save(self): try: db.session.add(self) db.session.commit() except KeyError or IntegrityError as e: db.session.rollback() raise e @staticmethod def get(host_uuid=None): return Host.query.get(host_uuid) if host_uuid else Host.query.all() def update(self, data): if 'code' in data and data['code'] != "": self.code = data['code'] if 'ip_address' in data and data['ip_address'] != "": self.ip_address = data['ip_address'] if 'conn_user' in data and data['conn_user'] != "": self.conn_user = data['conn_user'] if 'lab_uuid' in data and data['lab_uuid'] != "": self.lab_uuid = uuid.UUID(data['lab_uuid']) try: db.session.commit() except Exception as e: db.session.rollback() raise e @staticmethod def delete(host): try: db.session.delete(host) db.session.commit() except UnmappedInstanceError as e: db.session.rollback() raise e def to_dict(self): return dict(uuid=str(self.uuid), code=self.code, ip_address=self.ip_address.compressed, conn_user=self.conn_user, lab_id=self.lab.id)
class Role(RoleMixin, db_sec.Model): __tablename__ = 'role' id = db_sec.Column(db_sec.Integer(), primary_key=True) name = db_sec.Column(db_sec.String(80), unique=True) description = db_sec.Column(db_sec.String(255)) def __eq__(self, other): return (self.name == other or self.name == getattr(other, 'name', None)) def __ne__(self, other): return (self.name != other and self.name != getattr(other, 'name', None))
class Comment(db.Model): """ A Comment about a blog Post """ __tablename__ = 'comments' id = db.Column(db.Integer, primary_key=True) author = db.Column(db.String(100), nullable=False) body = db.Column(db.Text, nullable=False) created = db.Column(db.DateTime, default=datetime.utcnow()) post_id = db.Column(db.Integer, db.ForeignKey('posts.id'))
class User(UserMixin, db_sec.Model): __tablename__ = 'user' id = db_sec.Column(db_sec.Integer, primary_key=True) email = db_sec.Column(db_sec.String(255), unique=True) password = db_sec.Column(db_sec.String(120)) active = db_sec.Column(db_sec.Boolean()) confirmed_at = db_sec.Column(db_sec.DateTime()) last_login_at = db_sec.Column(db_sec.DateTime()) current_login_at = db_sec.Column(db_sec.DateTime()) last_login_ip = db_sec.Column(db_sec.String(100)) current_login_ip = db_sec.Column(db_sec.String(100)) login_count = db_sec.Column(db_sec.Integer) registered_at = db_sec.Column(db_sec.DateTime()) roles = db_sec.relationship('Role', secondary=roles_users, backref=db_sec.backref('user', lazy='dynamic')) def has_role(self, role): return role in self.roles
class User(db.Model, UserMixin): """ Site-wide User model :param int id: unique user id :param str username: The name of the User :param str password: The User's password :param bool active: Is this user's account enabled? :param str email: The User's email """ __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(64), nullable=False, unique=True) password = db.Column(db.String(255), nullable=False, default='') active = db.Column(db.Boolean(), nullable=False, default=True) email = db.Column(db.String) def __init__(self, username, password, email='', active=True): self.username = username self.password = password self.email = email self.active = active
class TransactionCategory(db.Model): __tablename__ = 'transaction_category' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(50)) def __init__(self, name): self.name = name @property def serialize(self): return {'id': self.id, 'name': self.name}
class Post(db.Model): """ A Post defining a blog article :param int id: Unique id for this post :param str title: Unique blog post title :param str body: The complete text of the post, written in Markdown :param datetime created: When the post was created :param int author_id: the id of the author who wrote this article (possibly worthless) """ __tablename__ = 'posts' id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100), unique=True) body = db.Column(db.Text) created = db.Column(db.DateTime, default=datetime.utcnow()) author_id = db.Column(db.Integer, db.ForeignKey(User.id)) comments = relationship("Comment", backref='posts') # TODO: tags for each post? @classmethod def query_all(cls): """ Return all stored, ordered in reverse chronological (newest to oldest) :return Query: """ return Post.query.order_by(desc(Post.created)) @classmethod def group_by_year_month(cls): """ Returns all posts grouped by year and month :return dict: { (year, month): [Posts] } """ order_group = collections.OrderedDict() posts = Post.query.with_entities(Post.id, Post.title, Post.created).order_by( desc(Post.created)) for ((year, month), grouped_posts) in groupby( posts, lambda x: (x.created.year, x.created.month)): grouped = [] for post in grouped_posts: slugged_post = post._asdict() slugged_post['slug'] = slugify(post.title, '-') grouped.append(slugged_post) order_group[(year, calendar.month_name[month])] = grouped return order_group @property def slug(self): """ Returns 'sluggified' version of the title :return str: """ return slugify(self.title, '-') def __repr__(self): return '<Post %s>' % self.title
class Transaction(db.Model): __tablename__ = 'transaction' user_id = db.Column(db.Integer, db.ForeignKey(User.id)) user = db.relationship('User') id = db.Column(db.Integer, primary_key=True) description = db.Column(db.String(200)) occurrence_date = db.Column(db.DateTime()) booking_date = db.Column(db.DateTime()) amount = db.Column(db.Numeric(precision=10, scale=4)) is_credit = db.Column(db.Boolean()) category_id = db.Column(db.Integer, db.ForeignKey(TransactionCategory.id)) category = db.relationship('TransactionCategory') account_id = db.Column(db.Integer, db.ForeignKey(Account.id)) account = db.relationship('Account')
class Config(db.Model): __tablename__ = 'config' id = db.Column(db.Integer, primary_key=True) local_qemu_uri = db.Column(db.String(255), nullable=False, default='qemu:///system') domain_images_dir = db.Column(db.String(255), nullable=False, default='/var/lib/libvirt/images/') template_images_dir = db.Column(db.String(255), nullable=False, default='/var/lib/dvls/images/') domain_definitions_dir = db.Column(db.String(255), nullable=False, default='/etc/libvirt/qemu/') template_definitions_dir = db.Column(db.String(255), nullable=False, default='/var/lib/dvls/qemu/') conn_user = db.Column(db.String(255), nullable=False, default='dvls') @staticmethod def init(): try: config = Config() db.session.add(config) db.session.commit() app.config.update(config.to_dict()) except Exception as e: db.session.rollback() raise e @staticmethod def is_initialized(): return True if Config.query.all().__len__() != 0 else False @staticmethod def get(): return Config.query.all()[0].to_dict() def update(self, config): if 'LOCAL_QEMU_URI' in config and config['LOCAL_QEMU_URI'] != "": self.local_qemu_uri = config['LOCAL_QEMU_URI'] if 'DOMAIN_IMAGES_DIR' in config and config['DOMAIN_IMAGES_DIR'] != "": self.domain_images_dir = config['DOMAIN_IMAGES_DIR'] if 'TEMPLATE_IMAGES_DIR' in config and config['TEMPLATE_IMAGES_DIR'] != "": self.template_images_dir = config['TEMPLATE_IMAGES_DIR'] if 'DOMAIN_DEFINITIONS_DIR' in config and config['DOMAIN_DEFINITIONS_DIR'] != "": self.domain_definitions_dir = config['DOMAIN_DEFINITIONS_DIR'] if 'TEMPLATE_DEFINITIONS_DIR' in config and config['TEMPLATE_DEFINITIONS_DIR'] != "": self.template_definitions_dir = config['TEMPLATE_DEFINITIONS_DIR'] if 'CONN_USER' in config and config['CONN_USER'] != "": self.conn_user = config['CONN_USER'] try: db.session.commit() app.config.update(self.to_dict()) print(self.to_dict()) except Exception as e: db.session.rollback() raise e def to_dict(self): return dict(LOCAL_QEMU_URI=self.local_qemu_uri, DOMAIN_IMAGES_DIR=self.domain_images_dir, DOMAIN_DEFINITIONS_DIR=self.domain_definitions_dir, TEMPLATE_IMAGES_DIR=self.template_images_dir, TEMPLATE_DEFINITIONS_DIR=self.template_definitions_dir, CONN_USER=self.conn_user)
class User(db.Model, UserMixin): __tablename__ = 'user' id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(50), unique=True, nullable=False) password = db.Column(db.String(120), nullable=False) name = db.Column(db.String(50), nullable=False) def __init__(self, json): self.email = json['email'] self.password = generate_password_hash(json['password']) self.name = json['name'] # Flask-Login methods def get_auth_token(self): """Returns the user's authentication token.""" data = [unicode(self.id), self.password] return login_serializer.dumps(data) def is_authenticated(self): return True @property def serialize(self): return {'id': self.id, 'email': self.email, 'password': self.password}
class Institution(db.Model): __tablename__ = 'institution' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(50), nullable=False) create_date = db.Column(db.DateTime(), nullable=False) def __init__(self, json): self.name = json['name'] self.create_date = datetime.datetime.now() @property def serialize(self): return { 'id': self.id, 'name': self.name, 'create_date': unix_time(self.create_date) }
class Lab(db.Model): __tablename__ = 'labs' uuid = db.Column(UUIDType(binary=False), primary_key=True, default=uuid.uuid4()) code = db.Column(db.String(64), unique=True, nullable=False) description = db.Column(db.Text, nullable=False) start_ip_range = db.Column(IPAddressType, unique=True, nullable=False) end_ip_range = db.Column(IPAddressType, unique=True, nullable=False) hosts_vcpus = db.Column(db.Integer, nullable=False) hosts_memory = db.Column(db.Integer, nullable=False) hosts_disk = db.Column(db.Integer, nullable=False) hosts = db.relationship('Host', backref='lab', lazy=True, cascade="all, delete-orphan") def __init__(self, data): self.uuid = uuid.uuid4() self.code = data['code'].upper() self.description = data['description'] self.start_ip_range = ipaddress.ip_address(data['start_ip_range']) self.end_ip_range = ipaddress.ip_address(data['end_ip_range']) self.hosts_vcpus = data['hosts']['vcpus'] self.hosts_memory = data['hosts']['memory'] self.hosts_disk = data['hosts']['disk'] def save(self): """Adds into database an existent laboratory :return: None """ try: db.session.add(self) db.session.commit() except KeyError or IntegrityError as e: db.session.rollback() raise e def remove(self): """Deletes from database an existent laboratory :return: None """ try: db.session.delete(self) db.session.commit() except UnmappedInstanceError as e: db.session.rollback() raise e def add_host(self, host): """Appends into the laboratory a new host :param host: :return: None """ if not self.has_host(host): self.hosts.append(host) def remove_host(self, host): """Removes from database an existent host :param host: :return: None """ if self.has_host(host): self.hosts.remove(host) def has_host(self, host): """Checks if the host is appended in the laboratory :param host: :return: True or False """ return True if host in self.hosts else False @staticmethod def get(lab_uuid=None): """Gets a list with all laboratories if None lab_uuid is passed. If lab_uuid is present, it returns the laboratory defined by lab_uuid :param lab_uuid: UUID of the laboratory :return: """ try: return Lab.query.get(lab_uuid) if lab_uuid else Lab.query.all() except Exception as e: raise e def update(self, data): if 'code' in data and data['code'] != "": self.code = data['code'].upper() if 'description' in data and data['description'] != "": self.description = data['description'] if 'start_ip_range' in data and data['start_ip_range'] != "": self.start_ip_range = data['start_ip_range'] if 'end_ip_range' in data and data['end_ip_range'] != "": self.end_ip_range = data['end_ip_range'] if 'hosts' in data: if 'vcpus' in data['hosts'] and data['hosts']['vcpus'] != "": self.hosts_vcpus = data['hosts']['vcpus'] if 'memory' in data['hosts'] and data['hosts']['memory'] != "": self.hosts_memory = data['hosts']['memory'] if 'disk' in data['hosts'] and data['hosts']['disk'] != "": self.hosts_disk = data['hosts']['disk'] try: db.session.commit() except Exception as e: db.session.rollback() raise e @staticmethod def delete(lab): try: db.session.delete(lab) db.session.commit() except UnmappedInstanceError as e: db.session.rollback() raise e def to_dict(self): return dict(uuid=str(self.uuid), code=self.code, description=self.description, start_ip_range=self.start_ip_range.compressed, end_ip_range=self.end_ip_range.compressed, hosts=dict(total=self.hosts.__len__(), vcpus=self.hosts_vcpus, memory=self.hosts_memory, disk=self.hosts_disk))
class Template(db.Model): __tablename__ = 'templates' uuid = db.Column(UUIDType(binary=False), primary_key=True, default=uuid.uuid4()) timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow()) name = db.Column(db.String(64), unique=True, nullable=False) description = db.Column(db.Text, nullable=True) vcpus = db.Column(db.Integer, nullable=False) memory = db.Column(db.Integer, nullable=False) xml_path = db.Column(db.String(255), nullable=False) images_path = db.Column(db.PickleType, nullable=False) def __init__(self, data): self.uuid = uuid.uuid4() self.name = data['name'] self.description = data['description'] self.vcpus = data['vcpus'] self.memory = round(data['memory'] * 0.000976562) # KiB to MiB self.xml_path = data['xml_path'] self.images_path = pickle.dumps(data['images_path']) def save(self): try: db.session.add(self) db.session.commit() except KeyError or IntegrityError as e: db.session.rollback() raise e @staticmethod def get(template_uuid=None): return Template.query.get( template_uuid) if template_uuid else Template.query.all() def update(self, data): if 'name' in data and data['name'] != "": self.name = data['name'] if 'description' in data and data['description'] != "": self.description = data['description'] try: db.session.commit() except Exception as e: db.session.rollback() raise e @staticmethod def delete(template): try: db.session.delete(template) db.session.commit() except UnmappedInstanceError as e: db.session.rollback() raise e def to_dict(self): return dict(uuid=str(self.uuid), timestamp=self.timestamp, name=self.name, description=self.description, vcpus=self.vcpus, memory=self.memory, xml_path=self.xml_path, images_path=pickle.loads(self.images_path))