diff --git a/modules/plugin_dbui/dbsvc.py b/modules/plugin_dbui/dbsvc.py index b01654165739ba033e148a578b03c47c59a9e750..18084be1b3b45aae3af45bfdb8a51aee4f87854a 100644 --- a/modules/plugin_dbui/dbsvc.py +++ b/modules/plugin_dbui/dbsvc.py @@ -15,6 +15,7 @@ FIELD_NOT_IN_DB = "the field '%s.%s' doesn't exist in the database." KEYWORD_MISSING = "The keyword '%s' is missing." RECORD_DELETED = "Record %s is deleted." RECORD_INSERTED = "Record is inserted with id %i." +RECORD_NOT_IN_DB = "The record '%s' doesn't exist in the table %s." RECORD_UPDATED = "Record %s is updated." TABLE_NOT_IN_DB = "The table '%s' doesn't exist in the database." @@ -163,6 +164,22 @@ class DbSvc(BaseSvc): return fields + def _get_record(self, table, id): + """Helper function to get the record id in a form which can be decoded + by the JsonReader. Foreign key are resolved. + Return a dictionary where key are the field encoded TableField. + + """ + db = self.environment['db'] + record = {} + + for field in db[table].fields: + key = encode_field(table, field) + record[key] = db[table][id][field] + + return record + + def _is_field_in_table(self, table, field): """Helper method to check that the field belongs to the table. Return a boolean. @@ -230,13 +247,9 @@ class DbSvc(BaseSvc): # return the complete record # mandatory, since this is the one which is display in the grid. - record = {} - for field in db[table].fields: - key = encode_field(table, field) - record[key] = db[table][id][field] + record = self._get_record(table, id) self.dbg("End DbSvc.create.", RECORD_INSERTED % id) - return {SUCCESS: True, 'msg': RECORD_INSERTED % id, ROOT: record} @@ -263,13 +276,19 @@ class DbSvc(BaseSvc): self._check_request(arg) + db = self.environment['db'] table = arg['tableName'] + table_id = encode_field(table, 'id') + for id in arg[ROOT]: - del self.environment['db'][table][id] + if not db[table][id]: + return {"success": False, + "msg": RECORD_NOT_IN_DB % (id, table), + ROOT: {table_id: id}} + + del db[table][id] self.dbg("End DbSvc.destroy") - - table_id = encode_field(table, 'id') return {"success": True, "msg": RECORD_DELETED % id, ROOT: {table_id: id}} @@ -369,17 +388,13 @@ class DbSvc(BaseSvc): # the client send modified fields only # update the database accordingly - db = self.environment['db'] id = fields['id'] table = arg['tableName'] - db[table][id] = fields + self.environment['db'][table][id] = fields # return the complete record # mandatory, since this is the one which is display in the grid. - record = {} - for field in db[table].fields: - key = encode_field(table, field) - record[key] = db[table][id][field] + record = self._get_record(table, id) self.dbg("End DbSvc.update.") return {SUCCESS: True, "msg": RECORD_UPDATED % id, ROOT: record}