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

complete version debug with simple table.

parent 4f5eacf8
No related branches found
No related tags found
No related merge requests found
......@@ -8,7 +8,7 @@ __version__ = "$Revision$"
from basesvc import BaseSvc
from cfgsvc2 import ROOT, SUCCESS, TOTAL
from gluon.contrib import simplejson as json
from helper import decode_field, rows_serializer
from helper import encode_field, decode_field, rows_serializer
FIELD_NOT_IN_DB = "the field '%s.%s' doesn't exist in the database."
......@@ -57,7 +57,7 @@ class DbSvc2(BaseSvc):
"""
def _check_request(self, arg):
def _check_request(self, arg, keywords=('tableName', 'records')):
"""Helper function to check that the request arg is well formed.
The dictionary arg contains the following keys:
......@@ -67,7 +67,7 @@ class DbSvc2(BaseSvc):
dbFields
The list of table fields.
records (ROOT)
records (optional)
A dictionary containing the new / update values for field
as well as the identifier of the record to be create, updated
or delete.
......@@ -77,9 +77,9 @@ class DbSvc2(BaseSvc):
"""
# check that the request is well formed
for kword in ('tableName', 'dbFields', ROOT):
for kword in keywords:
if kword not in arg:
raise DbSvcException(KEYWORD_MISSING % kw)
raise DbSvcException(KEYWORD_MISSING % kword)
table = arg['tableName']
self._is_table_in_db(table)
......@@ -142,6 +142,7 @@ class DbSvc2(BaseSvc):
"""
fields = {}
table = arg['tableName']
# Remove fields in the record which do not belong to the table.
# The JsonStore send the foreign key and the the pointing field
......@@ -211,7 +212,7 @@ class DbSvc2(BaseSvc):
as well as the identifier of the record to be updated (id)
Return a dictionary with status, message and id of the update row:
{success: True, msg: 'blalbla', records:{id:xx}}
{success: True, msg: 'blalbla', data:{TableId:xx}}
"""
self.dbg("Start DbSvc.create")
......@@ -221,11 +222,15 @@ class DbSvc2(BaseSvc):
if SUCCESS in fields and fields[SUCCESS] == False:
return fields
table = arg['tableName']
id = self.environment['db'][table].insert (**fields)
table_id = encode_field(table, 'id')
self.dbg("End DbSvc.create.", RECORD_INSERTED % id)
return {SUCCESS: True, "msg": RECORD_INSERTED % id, ROOT: {"id": id}}
# NOTE Ext JS 3.3.2 request explicitly the word 'data'
# if you use records (ROOT) javascript raise an exception !!!
return {SUCCESS: True, 'msg': RECORD_INSERTED % id, 'data': {table_id: id}}
def destroy(self, arg):
......@@ -251,6 +256,7 @@ class DbSvc2(BaseSvc):
self._check_request(arg)
table = arg['tableName']
for id in arg[ROOT]:
del self.environment['db'][table][id]
......@@ -290,7 +296,7 @@ class DbSvc2(BaseSvc):
"""
self.dbg("Start DbSvc.read")
self._check_request(arg)
self._check_request(arg, ('tableName', 'dbFields'))
db = self.environment['db']
# build the list of SQLField object
......@@ -315,6 +321,7 @@ class DbSvc2(BaseSvc):
# interrogate the database and serialize the records as a list:
# [{TableField: value, }, {...}, ...]
table = arg['tableName']
rows = db(query).select(*fields, **kwargs)
li = rows_serializer(rows)
results = db(db[table].id).count()
......@@ -340,7 +347,7 @@ class DbSvc2(BaseSvc):
as well as the identifier of the record to be updated (id)
Return a dictionary with status, message and id of the update row:
{success: True, msg: 'blalbla', records:{id:xx}}
{success: True, msg: 'blalbla', records:{TableId:xx}}
"""
self.dbg("Start DbSvc.update.")
......@@ -352,7 +359,9 @@ class DbSvc2(BaseSvc):
return fields
id = fields['id']
table = arg['tableName']
self.environment['db'][table][id] = fields
table_id = encode_field(table, 'id')
self.dbg("End DbSvc.update.")
return {SUCCESS: True, "msg": RECORD_UPDATED % id, ROOT: {"id": id}}
return {SUCCESS: True, "msg": RECORD_UPDATED % id, ROOT: {table_id: id}}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment