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

Redesign the database controller to work with the HttpDb class.

parent 207fee5a
No related branches found
No related tags found
No related merge requests found
""" database.py
Controllers to insert, update and delete data in the database.
Controllers to perform operation on the database
and to retrieve information on the database model.
Author: R. Le Gac
$Id$
"""
import sqlite3
__version__ = "$Revision"
from gluon.contrib.simplejson.encoder import JSONEncoder
from gluon.contrib import simplejson as json
MSGERR = "ERROR: %s"
MSGTBL = "INVALID TABLE NAME: '%s'"
MSGTBL += "\nCHECK THE URL: /application/database/insert?table=xxx"
def index():
""" controller to handle database operations.
jsonSvc = JSONEncoder()
All information defining the operation are sent via the POST/GET methods.
It contains steering keyword defining the requested service and task.
def insert():
"""Insert a row in a table.
Return a json string with success, msg and data keywords.
Table:
The table name is encoded in the url:
/application/database/insert?table=xxx
Row data:
Row data are send by a Ext.FormPanel as a json object
Return message:
The function return a json object with 3 possible keywords:
status, errors and alert.
status:
Boolean
It is mandatory.
errors:
A dictionary.
The key is the field name.
The value is the error message.
alert:
string
When this keyword is defined an alert window popup on the
client side with the message encapsulated in the string.
"""
msg = {'success': True}
# Check that the request table exist
table = request.get_vars.table
if table not in db.tables:
msg['success'] = False
msg['alert'] = MSGTBL % table
return jsonSvc.encode(msg)
# Check validators defined in the db model
for field in db[table].fields:
if field == "id":
continue
else:
val = request.post_vars[field]
message = db[table][field].validate(val)[1]
if val and db[table][field].notnull and message:
msg['success'] = False
if 'errors' not in msg: msg['errors'] = {}
msg['errors'][field] = message
if not msg['success']: return jsonSvc.encode(msg)
# Insert the data in the table
try:
db[table].insert (**request.post_vars)
except sqlite3.Error, e:
msg['success'] = False
msg['alert'] = MSGERR % e.message
return jsonSvc.encode(msg)
# Return the status
return jsonSvc.encode(msg)
def select():
"""Select rows in a table and return them as a JSON string.
Table:
The table name is encoded in the url:
/application/database/insert?table=xxx
Order by:
This sql clause can be encoded in the url:
/application/database/insert?table=xxx&orderby=yyyy
"""
if "debug" in request.vars and request.vars.debug:
print "\nStart database.index controller"
# Check that the request table exist
table = request.get_vars.table
if table not in db.tables: return 'Invalid table: %s' % table
# Retrieve the row, handling the order by statement
if 'orderby' in request.get_vars:
col = request.get_vars.orderby
rows = db().select(db[table].ALL, orderby=col)
else:
rows = db().select(db[table].ALL)
return rows.json()
resp = dbSvc.proceed(request.vars)
if "debug" in request.vars and request.vars.debug:
print "End database.index controller\n", resp
def test():
return dict()
return json.dumps(resp)
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