def update(self, id_, data): """Update a record identified by `id_` with `data`.""" # OERPLib credentials oerplib.tools.session.save(id_, data['oerplib'], rc_file=db.OERPLIB_FILE) del data['oerplib'] # Application specific data new_gid = data.pop('group_id') db_data = db.read() for gid, gdata in db_data.iteritems(): if id_ in gdata['servers']: # If the server group has not changed, update it in place if gid == new_gid: gdata['servers'][id_].update(data) # Otherwise, remove the server from its old group, and add it # to the new one else: gdata['servers'].pop(id_) db_data[new_gid]['servers'][id_] = data break # Update the data db.write(db_data) self.updated.emit('server', id_) # Signal if the group has changed if gid != new_gid: self.group_changed.emit('server', id_, gid, new_gid)
def read_all(self): """Return all records data.""" db_data = db.read() data = {} for gdata in db_data.itervalues(): for sid, sdata in gdata['servers'].iteritems(): for rid, rdata in sdata.get('dependencies', {}).iteritems(): data[rid] = rdata data[rid]['server_id'] = sid return data
def read(self, id_): """Return data related to the record identified by `id_`.""" db_data = db.read() for gdata in db_data.itervalues(): for sid, sdata in gdata['servers'].iteritems(): if id_ in sdata.get('dependencies', {}): rdata = sdata['dependencies'][id_] rdata['server_id'] = sid return rdata return None
def delete(self, id_): """Delete a record identified by `id_`.""" db_data = db.read() for gdata in db_data.itervalues(): for sdata in gdata['servers'].itervalues(): if id_ in sdata.get('dependencies', {}): del sdata['dependencies'][id_] db.write(db_data) self.deleted.emit('dependency', id_) return
def read(self, id_): """Return data related to the record identified by `id_`.""" db_data = db.read() oe_data = oerplib.tools.session.get(id_, rc_file=db.OERPLIB_FILE) for gid, gdata in db_data.iteritems(): if id_ in gdata['servers']: sdata = gdata['servers'][id_] sdata['oerplib'] = oe_data sdata['group_id'] = gid return sdata return None
def read_all(self): """Return all records data.""" db_data = db.read() data = {} for gid, gdata in db_data.iteritems(): for sid, sdata in gdata['servers'].iteritems(): oe_data = oerplib.tools.session.get(sid, rc_file=db.OERPLIB_FILE) data[sid] = sdata data[sid]['oerplib'] = oe_data data[sid]['group_id'] = gid return data
def delete(self, id_): """Delete a record identified by `id_`.""" # OERPLib credentials oerplib.tools.session.remove(id_, rc_file=db.OERPLIB_FILE) # Application specific data db_data = db.read() for gdata in db_data.itervalues(): if id_ in gdata['servers']: del gdata['servers'][id_] db.write(db_data) self.deleted.emit('server', id_) break
def update(self, id_, data): """Update a record identified by `id_` with `data`.""" data_copy = data.copy() data_copy.pop('server_id') db_data = db.read() for gdata in db_data.itervalues(): for sdata in gdata['servers'].itervalues(): if id_ in sdata.get('dependencies', {}): sdata['dependencies'][id_].update(data_copy) db.write(db_data) self.updated.emit('dependency', id_, data) return
def create(self, data): """Create a new record from `data` and return its ID.""" id_ = uuid.uuid4().hex db_data = db.read() data_copy = data.copy() sid = data_copy.pop('server_id') gid = Controller['server'].read(sid)['group_id'] if 'dependencies' not in db_data[gid]['servers'][sid]: db_data[gid]['servers'][sid]['dependencies'] = {} db_data[gid]['servers'][sid]['dependencies'][id_] = data_copy db.write(db_data) self.created.emit('dependency', id_, data) return id_
def create(self, data): """Create a new record from `data` and return its ID.""" id_ = uuid.uuid4().hex # OERPLib credentials oerplib.tools.session.save(id_, data['oerplib'], rc_file=db.OERPLIB_FILE) del data['oerplib'] # Application specific data db_data = db.read() gid = data['group_id'] db_data[gid]['servers'][id_] = data db.write(db_data) self.created.emit('server', id_) return id_
def init(app): """Insert default data if the database is empty (first launch).""" if not db.read(): # Default server group Controller['group'].create({'name': _(u"Servers")})