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

Avoid duplicates in select menus (with yadcf) #102

Open
wants to merge 3 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
39 changes: 39 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,45 @@ It is framework agnostic, tested with `Pyramid <http://www.pylonsproject.org/>`_

It only depends on SQLAlchemy, and is compatible with version **1.10.x** of DataTables.

**Small contribution:**
In this fork has been added a natural sort method for PostgreSQL models. It needs a custom function to be created in the db:

.. code-block:: sql

--
-- This file replaces a previous one which is now available at
-- /junk/naturalsort-hack.sql which had certain drawbacks and
-- was far more complex. This approach is simpler, though it
-- has the drawback of not dealing with locales (all comparisons
-- end up made as if in C locale).
--
-- To use:
--
-- SELECT ... ORDER BY naturalsort(column);
--
-- +optionally,
--
-- CREATE INDEX ON yourtable (naturalsort(column));
--
-- (The basic method is to prefix each numeric substring with its
-- length, then sort as a bytea to get C locale and \x00 delimiters
-- between fragments)
--

create or replace function naturalsort(text)
returns bytea
language sql
immutable strict
as $f$
select string_agg(convert_to(coalesce(r[2],
length(length(r[1])::text) || length(r[1])::text || r[1]),
'SQL_ASCII'),'\x00')
from regexp_matches($1, '0*([0-9]+)|([^0-9]+)', 'g') r;
$f$;

-- end


|Build Status| |PyPi Version| |Coverage|

.. |Build Status| image:: https://travis-ci.org/Pegase745/sqlalchemy-datatables.svg?branch=master
Expand Down
10 changes: 6 additions & 4 deletions datatables/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def yadcf_multi_select(expr, value):
ColumnTuple = namedtuple(
'ColumnDT',
['sqla_expr', 'column_name', 'mData', 'search_method',
'nulls_order', 'global_search'])
'nulls_order', 'global_search', 'sort_method'])


class InvalidParameter(Exception):
Expand Down Expand Up @@ -178,7 +178,7 @@ class ColumnDT(ColumnTuple):

def __new__(cls, sqla_expr, column_name=None, mData=None,
search_method='string_contains', nulls_order=None,
global_search=True):
global_search=True, sort_method=None):
"""Set default values for mData and filter.

On creation, sets default None values for mData and string value for
Expand All @@ -194,7 +194,7 @@ def __new__(cls, sqla_expr, column_name=None, mData=None,

return super(ColumnDT, cls).__new__(
cls, sqla_expr, column_name, mData, search_method,
nulls_order, global_search)
nulls_order, global_search, sort_method)


class DataTables:
Expand Down Expand Up @@ -277,7 +277,7 @@ def _set_yadcf_data(self, query):
query=query, exclude=i)
v = filtered.add_columns(col.sqla_expr).distinct().all()
self.yadcf_params.append(
('yadcf_data_{:d}'.format(i), [r[0] for r in v]))
('yadcf_data_{:d}'.format(i), [r[0] for r in sorted(set(v))]))

def run(self):
"""Launch filtering, sorting and paging to output results."""
Expand Down Expand Up @@ -374,6 +374,8 @@ def _set_sort_expressions(self):
column = self.columns[column_nr]
direction = self.params.get('order[{:d}][dir]'.format(i))
sort_expr = column.sqla_expr
if column.sort_method is not None:
sort_expr = column.sort_method
if direction == 'asc':
sort_expr = sort_expr.asc()
elif direction == 'desc':
Expand Down
3 changes: 2 additions & 1 deletion examples/flask_tut/flask_tut/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Flask tutorial views."""
from flask import render_template, request, jsonify
from flask import Flask
from sqlalchemy import func

from datatables import ColumnDT, DataTables

Expand Down Expand Up @@ -33,7 +34,7 @@ def data():
# defining columns
columns = [
ColumnDT(User.id),
ColumnDT(User.name),
ColumnDT(User.name, sort_method=func.public.naturalsort(User.name)),
ColumnDT(Address.description),
ColumnDT(User.created_at)
]
Expand Down
2 changes: 1 addition & 1 deletion examples/pyramid_tut/pyramid_tut/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def data(request):
# in the table
columns = [
ColumnDT(User.id),
ColumnDT(User.name),
ColumnDT(User.name, sort_method=func.public.naturalsort(User.name)),
ColumnDT(Address.description),
ColumnDT(func.strftime('%d-%m-%Y', User.birthday)),
ColumnDT(User.age)
Expand Down