Skip to content

Commit

Permalink
Merge pull request #2 from Pegase745/1.0.0-wip
Browse files Browse the repository at this point in the history
Correction for showing columns results of relations in the table
  • Loading branch information
Pegase745 committed Aug 12, 2013
2 parents c9a88ed + 037e387 commit 5ab96d8
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 14 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,19 @@ class User(Base):
id = Column(Integer, primary_key=True)
name = Column(Text, unique=True)
created_at = Column(DateTime, default=datetime.datetime.utcnow)
address = relationship("Address", uselist=False, backref="user")

def __init__(self, name):
self.name = name

class Address(Base):
__tablename__ = 'addresses'
id = Column(Integer, primary_key=True)
description = Column(Text, unique=True)
user_id = Column(Integer, ForeignKey('users.id'))

def __init__(self, description):
self.description = description
```

**views.py**
Expand All @@ -42,10 +52,11 @@ def simple_example(request):
columns = []
columns.append(ColumnDT('id'))
columns.append(ColumnDT('name', None, _upper))
columns.append(ColumnDT('address.description'))
columns.append(ColumnDT('created_at', None , str))

# defining the initial query depending on your purpose
query = DBSession.query(User)
query = DBSession.query(User).join(Address).filter(Address.id > 14)

# instantiating a DataTable for the query and table needed
rowTable = DataTables(request, User, query, columns)
Expand All @@ -62,6 +73,7 @@ def simple_example(request):
<tr>
<th>Id</th>
<th>User name</th>
<th>Address description</th>
<th>Created at</th>
</tr>
</thead>
Expand Down
2 changes: 1 addition & 1 deletion datatables/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- coding: utf-8 -*-
__VERSION__ = '0.1.0'
__VERSION__ = '0.1.1'

from datatables import *
29 changes: 18 additions & 11 deletions datatables/datatables.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals

from sqlalchemy.sql.expression import asc, desc
from sqlalchemy.sql import or_

from collections import namedtuple

ColumnTuple = namedtuple('ColumnDT', ['column_name', 'mData', 'filter'])


def get_attr(sqla_object, attribute):
"""Returns the value of an attribute of an SQLAlchemy entity
"""
output = sqla_object
for x in attribute.split('.'):
output = getattr(output, x)
return output


class ColumnDT(ColumnTuple):
"""Class defining a DataTables Column with a ColumnTuple:
Expand Down Expand Up @@ -72,7 +80,6 @@ def output_result(self):

return output


def run(self):
"""Launch filtering, sorting and paging processes to output results
"""
Expand All @@ -97,10 +104,10 @@ def run(self):
row = dict()
for j in range(len(self.columns)):
col = self.columns[j]
tmp_row = get_attr(self.results[i], col.column_name)
if col.filter:
row[col.mData if col.mData else str(j)] = col.filter(getattr(self.results[i], col.column_name))
else:
row[col.mData if col.mData else str(j)] = getattr(self.results[i], col.column_name)
tmp_row = col.filter(tmp_row)
row[col.mData if col.mData else str(j)] = tmp_row
formatted_results.append(row)

self.results = formatted_results
Expand All @@ -112,12 +119,11 @@ def filtering(self):
search_value = self.request_values.get('sSearch')
conditions = []

if(search_value) and (search_value != ""):
if search_value:
for col in self.columns:
conditions.append(getattr(self.sqla_object, col.column_name).like("%" + search_value + "%"))

condition = or_(*conditions)
print condition
self.query = self.query.filter(condition)

# count after filtering
Expand All @@ -133,16 +139,17 @@ def sorting(self):

Order = namedtuple('order', ['name', 'dir'])

if ( self.request_values.get('iSortCol_0') != "" ) \
and ( self.request_values.get('iSortingCols') > 0 ):
if self.request_values.get('iSortCol_0') \
and self.request_values.get('iSortingCols') > 0:

for i in range(int(self.request_values['iSortingCols'])):
sorting.append(Order( self.columns[int(self.request_values['iSortCol_'+str(i)])].column_name,
self.request_values['sSortDir_'+str(i)]))

for sort in sorting:
sort_name = self.sqla_object.__tablename__ + '.' + sort.name
self.query = self.query.order_by(
asc(sort.name) if sort.dir == 'asc' else desc(sort.name))
asc(sort_name) if sort.dir == 'asc' else desc(sort_name))


def paging(self):
Expand Down
Empty file added test-project/CHANGES.txt
Empty file.
Empty file added test-project/README.txt
Empty file.
1 change: 1 addition & 0 deletions test-project/testproject/templates/home.pt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<tr>
<th>Id</th>
<th>User name</th>
<th>Address description</th>
<th>Created at</th>
</tr>
</thead>
Expand Down
3 changes: 2 additions & 1 deletion test-project/testproject/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ def simple_example(request):
columns = []
columns.append(ColumnDT('id'))
columns.append(ColumnDT('name', None, _upper))
columns.append(ColumnDT('address.description'))
columns.append(ColumnDT('created_at', None , str))

# defining the initial query depending on your purpose
query = DBSession.query(User)
query = DBSession.query(User).join(Address).filter(Address.id > 14)

# instantiating a DataTable for the query and table needed
rowTable = DataTables(request, User, query, columns)
Expand Down

0 comments on commit 5ab96d8

Please sign in to comment.