Merge pull request #476 from leonelcamara/master
Small Refactoring of BaseAdapter.delete
This commit is contained in:
+18
-15
@@ -1638,24 +1638,11 @@ class BaseAdapter(ConnectionPool):
|
|||||||
|
|
||||||
def delete(self, tablename, query):
|
def delete(self, tablename, query):
|
||||||
sql = self._delete(tablename, query)
|
sql = self._delete(tablename, query)
|
||||||
### special code to handle CASCADE in SQLite & SpatiaLite
|
|
||||||
db = self.db
|
|
||||||
table = db[tablename]
|
|
||||||
if self.dbengine in ('sqlite', 'spatialite') and table._referenced_by:
|
|
||||||
deleted = [x[table._id.name] for x in db(query).select(table._id)]
|
|
||||||
### end special code to handle CASCADE in SQLite & SpatiaLite
|
|
||||||
self.execute(sql)
|
self.execute(sql)
|
||||||
try:
|
try:
|
||||||
counter = self.cursor.rowcount
|
counter = self.cursor.rowcount
|
||||||
except:
|
except:
|
||||||
counter = None
|
counter = None
|
||||||
### special code to handle CASCADE in SQLite & SpatiaLite
|
|
||||||
if self.dbengine in ('sqlite', 'spatialite') and counter:
|
|
||||||
for field in table._referenced_by:
|
|
||||||
if field.type == 'reference '+table._tablename \
|
|
||||||
and field.ondelete == 'CASCADE':
|
|
||||||
db(field.belongs(deleted)).delete()
|
|
||||||
### end special code to handle CASCADE in SQLite & SpatiaLite
|
|
||||||
return counter
|
return counter
|
||||||
|
|
||||||
def get_table(self, query):
|
def get_table(self, query):
|
||||||
@@ -2451,6 +2438,22 @@ class SQLiteAdapter(BaseAdapter):
|
|||||||
return '(%s REGEXP %s)' % (self.expand(first),
|
return '(%s REGEXP %s)' % (self.expand(first),
|
||||||
self.expand(second, 'string'))
|
self.expand(second, 'string'))
|
||||||
|
|
||||||
|
def delete(self, tablename, query):
|
||||||
|
# SQLite requires its own delete to handle CASCADE
|
||||||
|
db = self.db
|
||||||
|
table = db[tablename]
|
||||||
|
deleted = [x[table._id.name] for x in db(query).select(table._id)]
|
||||||
|
|
||||||
|
counter = super(SQLiteAdapter, self).delete(tablename, query)
|
||||||
|
|
||||||
|
if counter:
|
||||||
|
for field in table._referenced_by:
|
||||||
|
if field.type == 'reference '+ tablename \
|
||||||
|
and field.ondelete == 'CASCADE':
|
||||||
|
db(field.belongs(deleted)).delete()
|
||||||
|
|
||||||
|
return counter
|
||||||
|
|
||||||
def select(self, query, fields, attributes):
|
def select(self, query, fields, attributes):
|
||||||
"""
|
"""
|
||||||
Simulate `SELECT ... FOR UPDATE` with `BEGIN IMMEDIATE TRANSACTION`.
|
Simulate `SELECT ... FOR UPDATE` with `BEGIN IMMEDIATE TRANSACTION`.
|
||||||
@@ -11258,8 +11261,8 @@ class Rows(object):
|
|||||||
"""
|
"""
|
||||||
returns the data as list of trees.
|
returns the data as list of trees.
|
||||||
|
|
||||||
:param parent_name: the name of the field to holding the reference to
|
:param parent_name: the name of the field holding the reference to the
|
||||||
the parent (default parent_id).
|
parent (default parent_id).
|
||||||
:param children_name: the name where the children of each row will be
|
:param children_name: the name where the children of each row will be
|
||||||
stored as a list (default children).
|
stored as a list (default children).
|
||||||
:param render: whether we will render the fields using their represent
|
:param render: whether we will render the fields using their represent
|
||||||
|
|||||||
+3
-4
@@ -1382,8 +1382,8 @@ class Scheduler(MetaScheduler):
|
|||||||
def get_workers(self, only_ticker=False):
|
def get_workers(self, only_ticker=False):
|
||||||
""" Returns a dict holding worker_name : {**columns}
|
""" Returns a dict holding worker_name : {**columns}
|
||||||
representing all "registered" workers
|
representing all "registered" workers
|
||||||
only_ticker returns only the worker running as a TICKER,
|
only_ticker returns only the workers running as a TICKER,
|
||||||
if there is any
|
if there are any
|
||||||
"""
|
"""
|
||||||
db = self.db
|
db = self.db
|
||||||
if only_ticker:
|
if only_ticker:
|
||||||
@@ -1392,14 +1392,13 @@ class Scheduler(MetaScheduler):
|
|||||||
workers = db(db.scheduler_worker.id > 0).select()
|
workers = db(db.scheduler_worker.id > 0).select()
|
||||||
all_workers = {}
|
all_workers = {}
|
||||||
for row in workers:
|
for row in workers:
|
||||||
all_workers[row.worker_name] = Storage(dict(
|
all_workers[row.worker_name] = Storage(
|
||||||
status=row.status,
|
status=row.status,
|
||||||
first_heartbeat=row.first_heartbeat,
|
first_heartbeat=row.first_heartbeat,
|
||||||
last_heartbeat=row.last_heartbeat,
|
last_heartbeat=row.last_heartbeat,
|
||||||
group_names=row.group_names,
|
group_names=row.group_names,
|
||||||
is_ticker=row.is_ticker,
|
is_ticker=row.is_ticker,
|
||||||
worker_stats=row.worker_stats
|
worker_stats=row.worker_stats
|
||||||
)
|
|
||||||
)
|
)
|
||||||
return all_workers
|
return all_workers
|
||||||
|
|
||||||
|
|||||||
+11
-13
@@ -35,7 +35,6 @@ import re
|
|||||||
import cStringIO
|
import cStringIO
|
||||||
from gluon.globals import current
|
from gluon.globals import current
|
||||||
from gluon.http import redirect
|
from gluon.http import redirect
|
||||||
from gluon.utils import get_callable_argspec
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import gluon.settings as settings
|
import gluon.settings as settings
|
||||||
@@ -940,7 +939,7 @@ class SQLFORM(FORM):
|
|||||||
# - add translatable label for record ID
|
# - add translatable label for record ID
|
||||||
# - add third column to right of fields, populated from the col3 dict
|
# - add third column to right of fields, populated from the col3 dict
|
||||||
|
|
||||||
widgets = Storage(dict(
|
widgets = Storage(
|
||||||
string=StringWidget,
|
string=StringWidget,
|
||||||
text=TextWidget,
|
text=TextWidget,
|
||||||
json=JSONWidget,
|
json=JSONWidget,
|
||||||
@@ -960,9 +959,9 @@ class SQLFORM(FORM):
|
|||||||
checkboxes=CheckboxesWidget,
|
checkboxes=CheckboxesWidget,
|
||||||
autocomplete=AutocompleteWidget,
|
autocomplete=AutocompleteWidget,
|
||||||
list=ListWidget,
|
list=ListWidget,
|
||||||
))
|
)
|
||||||
|
|
||||||
formstyles = Storage(dict(
|
formstyles = Storage(
|
||||||
table3cols=formstyle_table3cols,
|
table3cols=formstyle_table3cols,
|
||||||
table2cols=formstyle_table2cols,
|
table2cols=formstyle_table2cols,
|
||||||
divs=formstyle_divs,
|
divs=formstyle_divs,
|
||||||
@@ -970,7 +969,7 @@ class SQLFORM(FORM):
|
|||||||
bootstrap=formstyle_bootstrap,
|
bootstrap=formstyle_bootstrap,
|
||||||
bootstrap3=formstyle_bootstrap3,
|
bootstrap3=formstyle_bootstrap3,
|
||||||
inline=formstyle_inline,
|
inline=formstyle_inline,
|
||||||
))
|
)
|
||||||
|
|
||||||
FIELDNAME_REQUEST_DELETE = 'delete_this_record'
|
FIELDNAME_REQUEST_DELETE = 'delete_this_record'
|
||||||
FIELDKEY_DELETE_RECORD = 'delete_record'
|
FIELDKEY_DELETE_RECORD = 'delete_record'
|
||||||
@@ -1297,9 +1296,13 @@ class SQLFORM(FORM):
|
|||||||
raise RuntimeError('formstyle not found')
|
raise RuntimeError('formstyle not found')
|
||||||
|
|
||||||
if callable(formstyle):
|
if callable(formstyle):
|
||||||
# backward compatibility, 4 argument function is the old style
|
try:
|
||||||
args, varargs, keywords, defaults = get_callable_argspec(formstyle)
|
table = formstyle(self, xfields)
|
||||||
if defaults and len(args) - len(defaults) == 4 or len(args) == 4:
|
for id, a, b, c in xfields:
|
||||||
|
self.field_parent[id] = getattr(b, 'parent', None) \
|
||||||
|
if isinstance(b, XmlComponent) else None
|
||||||
|
except TypeError:
|
||||||
|
# backward compatibility, 4 argument function is the old style
|
||||||
table = TABLE()
|
table = TABLE()
|
||||||
for id, a, b, c in xfields:
|
for id, a, b, c in xfields:
|
||||||
newrows = formstyle(id, a, b, c)
|
newrows = formstyle(id, a, b, c)
|
||||||
@@ -1309,11 +1312,6 @@ class SQLFORM(FORM):
|
|||||||
newrows = [newrows]
|
newrows = [newrows]
|
||||||
for newrow in newrows:
|
for newrow in newrows:
|
||||||
table.append(newrow)
|
table.append(newrow)
|
||||||
else:
|
|
||||||
table = formstyle(self, xfields)
|
|
||||||
for id, a, b, c in xfields:
|
|
||||||
self.field_parent[id] = getattr(b, 'parent', None) \
|
|
||||||
if isinstance(b, XmlComponent) else None
|
|
||||||
else:
|
else:
|
||||||
raise RuntimeError('formstyle not supported')
|
raise RuntimeError('formstyle not supported')
|
||||||
return table
|
return table
|
||||||
|
|||||||
Reference in New Issue
Block a user