moved pluralize logic into sqlhtml for speed reasons, thanks Paolo P.
This commit is contained in:
+1
-1
Submodule gluon/packages/dal updated: c0c8d29ba3...49ddb26bf0
+51
-17
@@ -47,9 +47,8 @@ try:
|
|||||||
except ImportError:
|
except ImportError:
|
||||||
settings = {}
|
settings = {}
|
||||||
|
|
||||||
widget_class = re.compile('^\w*')
|
|
||||||
|
|
||||||
REGEX_ALIAS_MATCH = re.compile('^(.*) AS (.*)$')
|
REGEX_WIDGET_CLASS = re.compile(r'^\w*')
|
||||||
|
|
||||||
|
|
||||||
def add_class(a, b):
|
def add_class(a, b):
|
||||||
@@ -143,6 +142,34 @@ def show_if(cond):
|
|||||||
raise RuntimeError("Not Implemented Error")
|
raise RuntimeError("Not Implemented Error")
|
||||||
|
|
||||||
|
|
||||||
|
PLURALIZE_RULES = None
|
||||||
|
|
||||||
|
def pluralize(singular, rules=None):
|
||||||
|
if rules is None:
|
||||||
|
global PLURALIZE_RULES
|
||||||
|
if PLURALIZE_RULES is None:
|
||||||
|
PLURALIZE_RULES = [
|
||||||
|
(re.compile('child$'), re.compile('child$'), 'children'),
|
||||||
|
(re.compile('oot$'), re.compile('oot$'), 'eet'),
|
||||||
|
(re.compile('ooth$'), re.compile('ooth$'), 'eeth'),
|
||||||
|
(re.compile('l[eo]af$'), re.compile('l([eo])af$'), 'l\\1aves'),
|
||||||
|
(re.compile('sis$'), re.compile('sis$'), 'ses'),
|
||||||
|
(re.compile('man$'), re.compile('man$'), 'men'),
|
||||||
|
(re.compile('ife$'), re.compile('ife$'), 'ives'),
|
||||||
|
(re.compile('eau$'), re.compile('eau$'), 'eaux'),
|
||||||
|
(re.compile('lf$'), re.compile('lf$'), 'lves'),
|
||||||
|
(re.compile('[sxz]$'), re.compile('$'), 'es'),
|
||||||
|
(re.compile('[^aeioudgkprt]h$'), re.compile('$'), 'es'),
|
||||||
|
(re.compile('(qu|[^aeiou])y$'), re.compile('y$'), 'ies'),
|
||||||
|
(re.compile('$'), re.compile('$'), 's'),
|
||||||
|
]
|
||||||
|
rules = PLURALIZE_RULES
|
||||||
|
for line in rules:
|
||||||
|
re_search, re_sub, replace = line
|
||||||
|
plural = re_search.search(singular) and re_sub.sub(replace, singular)
|
||||||
|
if plural: return plural
|
||||||
|
|
||||||
|
|
||||||
class FormWidget(object):
|
class FormWidget(object):
|
||||||
"""
|
"""
|
||||||
Helper for SQLFORM to generate form input fields (widget), related to the
|
Helper for SQLFORM to generate form input fields (widget), related to the
|
||||||
@@ -165,7 +192,7 @@ class FormWidget(object):
|
|||||||
attr = dict(
|
attr = dict(
|
||||||
_id='%s_%s' % (field.tablename, field.name),
|
_id='%s_%s' % (field.tablename, field.name),
|
||||||
_class=cls._class or
|
_class=cls._class or
|
||||||
widget_class.match(str(field.type)).group(),
|
REGEX_WIDGET_CLASS.match(str(field.type)).group(),
|
||||||
_name=field.name,
|
_name=field.name,
|
||||||
requires=field.requires,
|
requires=field.requires,
|
||||||
)
|
)
|
||||||
@@ -1541,7 +1568,7 @@ class SQLFORM(FORM):
|
|||||||
# SQLCustomType has a widget, use it
|
# SQLCustomType has a widget, use it
|
||||||
inp = field.type.widget(field, default)
|
inp = field.type.widget(field, default)
|
||||||
else:
|
else:
|
||||||
field_type = widget_class.match(str(field.type)).group()
|
field_type = REGEX_WIDGET_CLASS.match(str(field.type)).group()
|
||||||
field_type = field_type in self.widgets and field_type or 'string'
|
field_type = field_type in self.widgets and field_type or 'string'
|
||||||
inp = self.widgets[field_type].widget(field, default)
|
inp = self.widgets[field_type].widget(field, default)
|
||||||
|
|
||||||
@@ -3180,7 +3207,8 @@ class SQLFORM(FORM):
|
|||||||
return dict(form=form)
|
return dict(form=form)
|
||||||
|
|
||||||
"""
|
"""
|
||||||
request, T = current.request, current.T
|
request = current.request
|
||||||
|
T = current.T
|
||||||
if args is None:
|
if args is None:
|
||||||
args = []
|
args = []
|
||||||
|
|
||||||
@@ -3198,8 +3226,7 @@ class SQLFORM(FORM):
|
|||||||
links = {}
|
links = {}
|
||||||
if constraints is None:
|
if constraints is None:
|
||||||
constraints = {}
|
constraints = {}
|
||||||
field = None
|
field = name = None
|
||||||
name = None
|
|
||||||
|
|
||||||
def format(table, row):
|
def format(table, row):
|
||||||
if not row:
|
if not row:
|
||||||
@@ -3210,10 +3237,14 @@ class SQLFORM(FORM):
|
|||||||
return table._format(row)
|
return table._format(row)
|
||||||
else:
|
else:
|
||||||
return '#' + str(row.id)
|
return '#' + str(row.id)
|
||||||
|
|
||||||
|
def plural(table):
|
||||||
|
return table._plural or pluralize(table._singular.lower()).capitalize()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
nargs = len(args) + 1
|
nargs = len(args) + 1
|
||||||
previous_tablename, previous_fieldname, previous_id = \
|
previous_tablename = table._tablename
|
||||||
table._tablename, None, None
|
previous_fieldname = previous_id = None
|
||||||
while len(request.args) > nargs:
|
while len(request.args) > nargs:
|
||||||
key = request.args(nargs)
|
key = request.args(nargs)
|
||||||
if '.' in key:
|
if '.' in key:
|
||||||
@@ -3234,11 +3265,12 @@ class SQLFORM(FORM):
|
|||||||
if previous_id:
|
if previous_id:
|
||||||
if record[previous_fieldname] != int(previous_id):
|
if record[previous_fieldname] != int(previous_id):
|
||||||
raise HTTP(400)
|
raise HTTP(400)
|
||||||
previous_tablename, previous_fieldname, previous_id = \
|
previous_tablename = tablename
|
||||||
tablename, fieldname, id
|
previous_fieldname = fieldname
|
||||||
|
previous_id = id
|
||||||
name = format(db[referee], record)
|
name = format(db[referee], record)
|
||||||
breadcrumbs.append(
|
breadcrumbs.append(
|
||||||
LI(A(T(db[referee]._plural),
|
LI(A(T(plural(db[referee])),
|
||||||
cid=request.cid,
|
cid=request.cid,
|
||||||
_href=url()),
|
_href=url()),
|
||||||
SPAN(divider, _class='divider'),
|
SPAN(divider, _class='divider'),
|
||||||
@@ -3319,8 +3351,8 @@ class SQLFORM(FORM):
|
|||||||
if tb:
|
if tb:
|
||||||
multiple_links = len(linked_fieldnames) > 1
|
multiple_links = len(linked_fieldnames) > 1
|
||||||
for fieldname in linked_fieldnames:
|
for fieldname in linked_fieldnames:
|
||||||
t = T(tb._plural) if not multiple_links else \
|
t = T(plural(tb)) if not multiple_links else \
|
||||||
T(tb._plural + '(' + fieldname + ')')
|
T("%s(%s)" % (plural(tb), fieldname))
|
||||||
args0 = tablename + '.' + fieldname
|
args0 = tablename + '.' + fieldname
|
||||||
linked.append(
|
linked.append(
|
||||||
lambda row, t=t, nargs=nargs, args0=args0:
|
lambda row, t=t, nargs=nargs, args0=args0:
|
||||||
@@ -3332,7 +3364,7 @@ class SQLFORM(FORM):
|
|||||||
user_signature=user_signature, **kwargs)
|
user_signature=user_signature, **kwargs)
|
||||||
|
|
||||||
if isinstance(grid, DIV):
|
if isinstance(grid, DIV):
|
||||||
header = table._plural
|
header = plural(table)
|
||||||
next = grid.create_form or grid.update_form or grid.view_form
|
next = grid.create_form or grid.update_form or grid.view_form
|
||||||
breadcrumbs.append(LI(
|
breadcrumbs.append(LI(
|
||||||
A(T(header), cid=request.cid, _href=url()),
|
A(T(header), cid=request.cid, _href=url()),
|
||||||
@@ -3397,6 +3429,8 @@ class SQLTABLE(TABLE):
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
REGEX_ALIAS_MATCH = '^(.*) AS (.*)$'
|
||||||
|
|
||||||
def __init__(self,
|
def __init__(self,
|
||||||
sqlrows,
|
sqlrows,
|
||||||
linkto=None,
|
linkto=None,
|
||||||
@@ -3450,7 +3484,7 @@ class SQLTABLE(TABLE):
|
|||||||
if isinstance(f, field_types):
|
if isinstance(f, field_types):
|
||||||
headers[c] = make_name(f)
|
headers[c] = make_name(f)
|
||||||
else:
|
else:
|
||||||
headers[c] = REGEX_ALIAS_MATCH.sub(r'\2', c)
|
headers[c] = re.sub(self.REGEX_ALIAS_MATCH, r'\2', c)
|
||||||
if colgroup:
|
if colgroup:
|
||||||
cols = [COL(_id=c.replace('.', '-'), data={'column': i + 1})
|
cols = [COL(_id=c.replace('.', '-'), data={'column': i + 1})
|
||||||
for i, c in enumerate(columns)]
|
for i, c in enumerate(columns)]
|
||||||
@@ -3476,7 +3510,7 @@ class SQLTABLE(TABLE):
|
|||||||
row.append(TH(A(headers.get(c, c),
|
row.append(TH(A(headers.get(c, c),
|
||||||
_href=th_link + '?orderby=' + c, cid=cid)))
|
_href=th_link + '?orderby=' + c, cid=cid)))
|
||||||
else:
|
else:
|
||||||
row.append(TH(headers.get(c, REGEX_ALIAS_MATCH.sub(r'\2', c))))
|
row.append(TH(headers.get(c, re.sub(self.REGEX_ALIAS_MATCH, r'\2', c))))
|
||||||
|
|
||||||
if extracolumns: # new implement dict
|
if extracolumns: # new implement dict
|
||||||
for c in extracolumns:
|
for c in extracolumns:
|
||||||
|
|||||||
Reference in New Issue
Block a user