Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
plugin_dbui
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Admin message
Gitlab has been updated. More info
here
.
Show more breadcrumbs
w2pext
plugin_dbui
Commits
54649a7a
Commit
54649a7a
authored
16 years ago
by
Renaud Le Gac
Browse files
Options
Downloads
Patches
Plain Diff
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
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
controllers/database.py
+16
-94
16 additions, 94 deletions
controllers/database.py
with
16 additions
and
94 deletions
controllers/database.py
+
16
−
94
View file @
54649a7a
"""
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
+=
"
\n
CHECK 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
"
\n
Start 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
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment