Git Product home page Git Product logo

sqlalchemy-datatables's Introduction

sqlalchemy-datatables Build Status PyPi Version Gittip

Usage

The package is available on PyPI

pip install sqlalchemy-datatables

A simple example

models.py

class User(Base):
    __tablename__ = 'users'

    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

from .models import DBSession, User
from datatables import ColumnDT, DataTables

def _upper(chain):
    ret = chain.upper()
    if ret:
        return ret
    else:
        return chain

@view_config(route_name='home', renderer='templates/home.pt')
def home(request):
    return {'project': 'test-project'}

@view_config(route_name='simple_example', request_method='GET', renderer='json')
def simple_example(request):
    # defining columns
    columns = []
    columns.append(ColumnDT('id'))
    columns.append(ColumnDT('name', filter=_upper))
    columns.append(ColumnDT('address.description')) # where address is an SQLAlchemy Relation
    columns.append(ColumnDT('created_at', filter=str))

    # defining the initial query depending on your purpose
    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)

    # returns what is needed by DataTable
    return rowTable.output_result()

templates/home.pt

<!--do your stuff-->
<table class="table" id="simple-example">
    <thead>
        <tr>
            <th>Id</th>
            <th>User name</th>
            <th>Address description</th>
            <th>Created at</th>
        </tr>
    </thead>
    <tbody></tbody>
</table>
<!--do your stuff-->
<script type="text/javascript" charset="utf-8">
    $(document).ready(function(){
        $('#simple-example').dataTable({
            "bProcessing": true,
            "bServerSide": true,
            "sAjaxSource": "${request.route_path('simple_example')}"
        });
    });
</script>

Testing the Pyramid based ''test-project''

$ git clone
$ virtualenv --no-site-packages sqlalchemy-datatables
$ cd sqlalchemy-datatables/
Once only:
$ bin/python setup.py develop

$ cd test-project/
$ $venv/bin/python setup.py develop
$ $venv/bin/initialize_test-project_db development.ini
$ $venv/bin/pserve development.ini

Open a web browser and got to the url localhost:6543

Changelog

v0.1.6 (16/12/2013)

  • Allow package to run with python 3.*
  • README modifications

v0.1.5 (18/10/2013)

  • Become aware of bSearchable_* properties, while doing a global search
  • Fixed bug throwed while searching Id columns
  • Fixing bug throwed while showing in ColumnDT, an SQLAlchemy's @hybrid_property or a Python @property
  • README modifications

v0.1.4 (17/09/2013)

  • Searching individual columns, with special like possibilities

v0.1.3 (16/09/2013)

  • Fixing setup.py's README error on develop

v0.1.2 (13/08/2013)

  • Fixing errors due to relationships (filtering, sorting)
  • Setting filter default value to str, avoiding JSON serializable type errors

v0.1.1 (12/08/2013)

  • Fixing viewing column relationships

v0.1.0 (12/08/2013)

  • First version

License

Copyright (c) 2013 Michel Nemnom

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

sqlalchemy-datatables's People

Contributors

bitdeli-chef avatar ddimmich avatar orf avatar pegase745 avatar rodnsi avatar rosickey avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.