Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #131: A mapper function to map the frontend columns data with backend. A new yadcf_data params. Some small improvements. #132

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions datatables/clean_regex.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,29 @@ def clean_regex(regex):

# these characters are escaped (all except alternation | and escape \)
# see http://www.regular-expressions.info/refquick.html
escape_chars = '[^$.?*+(){}'
escape_chars = "[^$.?*+(){}"

# remove any escape chars
ret_regex = ret_regex.replace('\\', '')
ret_regex = ret_regex.replace("\\", "")

# escape any characters which are used by regex
# could probably concoct something incomprehensible using re.sub() but
# prefer to write clear code with this loop
# note expectation that no characters have already been escaped
for c in escape_chars:
ret_regex = ret_regex.replace(c, '\\' + c)
ret_regex = ret_regex.replace(c, "\\" + c)

# remove any double alternations until these don't exist any more
while True:
old_regex = ret_regex
ret_regex = ret_regex.replace('||', '|')
ret_regex = ret_regex.replace("||", "|")
if old_regex == ret_regex:
break

# if last char is alternation | remove it because this
# will cause operational error
# this can happen as user is typing in global search box
while len(ret_regex) >= 1 and ret_regex[-1] == '|':
while len(ret_regex) >= 1 and ret_regex[-1] == "|":
ret_regex = ret_regex[:-1]

# and back to the caller
Expand Down
51 changes: 30 additions & 21 deletions datatables/column_dt.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@

from datatables.search_methods import SEARCH_METHODS

NULLS_ORDER = ['nullsfirst', 'nullslast']
NULLS_ORDER = ["nullsfirst", "nullslast"]

ColumnTuple = namedtuple('ColumnDT', [
'sqla_expr',
'column_name',
'mData',
'search_method',
'nulls_order',
'global_search',
])
ColumnTuple = namedtuple(
"ColumnDT",
[
"sqla_expr",
"column_name",
"mData",
"search_method",
"nulls_order",
"global_search",
"yadcf_data",
],
)


class ColumnDT(ColumnTuple):
Expand Down Expand Up @@ -45,7 +49,10 @@ class ColumnDT(ColumnTuple):
- 'nullsfirst'
- 'nullslast'.
:param global_search: search this column for the global search box

:param yadcf_data : define if the data needs to be returned for yadcf plugin.
Possible values:
- False
- True (default)
:type sqla_expr: SQLAlchemy query expression
:type mData: str
:type search_method: str
Expand All @@ -57,24 +64,25 @@ class ColumnDT(ColumnTuple):
"""

def __new__(
cls,
sqla_expr,
column_name=None,
mData=None,
search_method='string_contains',
nulls_order=None,
global_search=True,
cls,
sqla_expr,
column_name=None,
mData=None,
search_method="string_contains",
nulls_order=None,
global_search=True,
yadcf_data=True,
):
"""Set default values due to namedtuple immutability."""
if nulls_order and nulls_order not in NULLS_ORDER:
raise ValueError(
'{} is not an allowed value for nulls_order.'.format(
nulls_order))
"{} is not an allowed value for nulls_order.".format(nulls_order)
)

if search_method not in SEARCH_METHODS:
raise ValueError(
'{} is not an allowed value for search_method.'.format(
search_method))
"{} is not an allowed value for search_method.".format(search_method)
)

return super(ColumnDT, cls).__new__(
cls,
Expand All @@ -84,4 +92,5 @@ def __new__(
search_method,
nulls_order,
global_search,
yadcf_data,
)
Loading