Made db.create_all() conditional

This commit is contained in:
Vivek Santayana 2022-06-11 18:19:03 +01:00
parent 2c5ed21011
commit dc126459bc
2 changed files with 2 additions and 47 deletions

View File

@ -21,8 +21,8 @@ def install_scripts():
if not database_exists(Config.SQLALCHEMY_DATABASE_URI): if not database_exists(Config.SQLALCHEMY_DATABASE_URI):
create_database(Config.SQLALCHEMY_DATABASE_URI) create_database(Config.SQLALCHEMY_DATABASE_URI)
write('system.log', 'No database found. Creating a new database.') write('system.log', 'No database found. Creating a new database.')
with app.app_context(): db.create_all() with app.app_context(): db.create_all()
write('system.log', 'Creating database schema.') write('system.log', 'Creating database schema.')
if not path.isfile(f'./{data}/.encryption.key'): if not path.isfile(f'./{data}/.encryption.key'):
write('system.log', 'No encryption key found. Generating new encryption key.') write('system.log', 'No encryption key found. Generating new encryption key.')
with open(f'./{data}/.encryption.key', 'wb') as key_file: with open(f'./{data}/.encryption.key', 'wb') as key_file:

View File

@ -95,48 +95,3 @@ class User(UserMixin, db.Model):
db.session.commit() db.session.commit()
write('users.log', f'User \'{username}\' was deleted.') # TODO add current user write('users.log', f'User \'{username}\' was deleted.') # TODO add current user
class Device(db.Model):
id = db.Column(db.String(36), primary_key=True)
name = db.Column(db.String(128), nullable=False)
mac_address = db.Column(db.String(128), nullable=False)
ip_address = db.Column(db.String(128), nullable=False)
description = db.Column(db.String(250), nullable=True)
@property
def set_name(self): raise AttributeError('set_name is not a readable attribute.')
set_name.setter
def set_name(self, name:str): self.name = encrypt(name)
def get_name(self): return decrypt(self.name)
@property
def set_mac_address(self): raise AttributeError('set_mac_address is not a readable attribute.')
set_mac_address.setter
def set_mac_address(self, mac_address:str): self.mac_address = encrypt(mac_address)
def get_mac_address(self): return decrypt(self.mac_address)
@property
def set_ip_address(self): raise AttributeError('set_ip_address is not a readable attribute.')
set_ip_address.setter
def set_ip_address(self, ip_address:str): self.ip_address = encrypt(ip_address)
def get_ip_address(self): return decrypt(self.ip_address)
def add(self):
db.session.add(self)
db.session.commit()
write('commands.log', f'Device \'{self.get_name()}\' was added at the IP address \'{self.get_ip_address()}\' and the MAC address \'{self.get_mac_address()}\'.')
return True, f'Device {self.get_name()} was added.'
def delete(self):
name = self.get_name()
ip_address = self.get_ip_address()
mac_address = self.get_mac_address()
db.session.delete(self)
db.session.commit()
write('commands.log', f'Device \'{name}\' with the IP address {ip_address} and MAC address {mac_address} was deleted.')
return True, f'Device {name} was deleted.'