Skip to content
Snippets Groups Projects
Commit 38aca9dd authored by Renaud Le Gac's avatar Renaud Le Gac
Browse files

Add the method _get_record and protection in the destroy method.

parent e145e37a
No related branches found
No related tags found
No related merge requests found
...@@ -15,6 +15,7 @@ FIELD_NOT_IN_DB = "the field '%s.%s' doesn't exist in the database." ...@@ -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." KEYWORD_MISSING = "The keyword '%s' is missing."
RECORD_DELETED = "Record %s is deleted." RECORD_DELETED = "Record %s is deleted."
RECORD_INSERTED = "Record is inserted with id %i." 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." RECORD_UPDATED = "Record %s is updated."
TABLE_NOT_IN_DB = "The table '%s' doesn't exist in the database." TABLE_NOT_IN_DB = "The table '%s' doesn't exist in the database."
...@@ -163,6 +164,22 @@ class DbSvc(BaseSvc): ...@@ -163,6 +164,22 @@ class DbSvc(BaseSvc):
return fields 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): def _is_field_in_table(self, table, field):
"""Helper method to check that the field belongs to the table. """Helper method to check that the field belongs to the table.
Return a boolean. Return a boolean.
...@@ -230,13 +247,9 @@ class DbSvc(BaseSvc): ...@@ -230,13 +247,9 @@ class DbSvc(BaseSvc):
# return the complete record # return the complete record
# mandatory, since this is the one which is display in the grid. # mandatory, since this is the one which is display in the grid.
record = {} record = self._get_record(table, id)
for field in db[table].fields:
key = encode_field(table, field)
record[key] = db[table][id][field]
self.dbg("End DbSvc.create.", RECORD_INSERTED % id) self.dbg("End DbSvc.create.", RECORD_INSERTED % id)
return {SUCCESS: True, 'msg': RECORD_INSERTED % id, ROOT: record} return {SUCCESS: True, 'msg': RECORD_INSERTED % id, ROOT: record}
...@@ -263,13 +276,19 @@ class DbSvc(BaseSvc): ...@@ -263,13 +276,19 @@ class DbSvc(BaseSvc):
self._check_request(arg) self._check_request(arg)
db = self.environment['db']
table = arg['tableName'] table = arg['tableName']
table_id = encode_field(table, 'id')
for id in arg[ROOT]: 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") self.dbg("End DbSvc.destroy")
table_id = encode_field(table, 'id')
return {"success": True, "msg": RECORD_DELETED % id, ROOT: {table_id: id}} return {"success": True, "msg": RECORD_DELETED % id, ROOT: {table_id: id}}
...@@ -369,17 +388,13 @@ class DbSvc(BaseSvc): ...@@ -369,17 +388,13 @@ class DbSvc(BaseSvc):
# the client send modified fields only # the client send modified fields only
# update the database accordingly # update the database accordingly
db = self.environment['db']
id = fields['id'] id = fields['id']
table = arg['tableName'] table = arg['tableName']
db[table][id] = fields self.environment['db'][table][id] = fields
# return the complete record # return the complete record
# mandatory, since this is the one which is display in the grid. # mandatory, since this is the one which is display in the grid.
record = {} record = self._get_record(table, id)
for field in db[table].fields:
key = encode_field(table, field)
record[key] = db[table][id][field]
self.dbg("End DbSvc.update.") self.dbg("End DbSvc.update.")
return {SUCCESS: True, "msg": RECORD_UPDATED % id, ROOT: record} return {SUCCESS: True, "msg": RECORD_UPDATED % id, ROOT: record}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment