cli: initial import for import/export

This commit is contained in:
Iain Learmonth 2022-04-22 12:52:41 +01:00
parent dc59921498
commit 0c349091e7
3 changed files with 143 additions and 13 deletions

View file

@ -18,6 +18,17 @@ class AbstractConfiguration(db.Model):
self.updated = datetime.utcnow()
db.session.commit()
@classmethod
def csv_header(self):
return [
"id", "description", "added", "updated", "destroyed"
]
def csv_row(self):
return [
getattr(self, x) for x in self.csv_header()
]
class AbstractResource(db.Model):
__abstract__ = True
@ -40,30 +51,28 @@ class AbstractResource(db.Model):
self.updated = datetime.utcnow()
db.session.commit()
def csv_row(self):
return [
self[x] for x in self.csv_header()
]
def __repr__(self):
return f"<{self.__class__.__name__} #{self.id}>"
class Group(db.Model):
id = db.Column(db.Integer, primary_key=True)
class Group(AbstractConfiguration):
group_name = db.Column(db.String(80), unique=True, nullable=False)
description = db.Column(db.String(255), nullable=False)
eotk = db.Column(db.Boolean())
added = db.Column(db.DateTime(), default=datetime.utcnow, nullable=False)
updated = db.Column(db.DateTime(), default=datetime.utcnow, nullable=False)
origins = db.relationship("Origin", back_populates="group")
bridgeconfs = db.relationship("BridgeConf", back_populates="group")
alarms = db.relationship("Alarm", back_populates="group")
def as_dict(self):
return {
"id": self.id,
"name": self.group_name,
"description": self.description,
"added": self.added,
"updated": self.updated
}
@classmethod
def csv_header(self):
return super().csv_header() + [
"group_name", "eotk"
]
def __repr__(self):
return '<Group %r>' % self.group_name
@ -78,6 +87,12 @@ class Origin(AbstractConfiguration):
proxies = db.relationship("Proxy", back_populates="origin")
alarms = db.relationship("Alarm", back_populates="origin")
@classmethod
def csv_header(cls):
return [
"id", "description", "added", "updated", "destroyed", "group_id", "domain_name"
]
def as_dict(self):
return {
"id": self.id,
@ -171,6 +186,18 @@ class Alarm(db.Model):
proxy = db.relationship("Proxy", back_populates="alarms")
bridge = db.relationship("Bridge", back_populates="alarms")
@classmethod
def csv_header(cls):
return [
"id", "target", "group_id", "origin_id", "proxy_id", "bridge_id", "alarm_type",
"alarm_state", "state_changed", "last_updated", "text"
]
def csv_row(self):
return [
self[x] for x in self.csv_header()
]
def update_state(self, state: AlarmState, text: str):
if self.alarm_state != state or self.state_changed is None:
self.state_changed = datetime.utcnow()