Skip to content

Commit

Permalink
resolve #15, support foreign key on sql and django in from_query_sets
Browse files Browse the repository at this point in the history
  • Loading branch information
chfw committed Jun 2, 2016
1 parent 2b799b5 commit fc8f958
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
20 changes: 19 additions & 1 deletion pyexcel_io/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,31 @@ def from_query_sets(column_names, query_sets):
for row in query_sets:
new_array = []
for column in column_names:
value = getattr(row, column)
if '__' in column:
value = _get_complex_attribute(row, column)
else:
value = _get_simple_attribute(row, column)
if isinstance(value, (datetime.date, datetime.time)):
value = value.isoformat()
new_array.append(value)
yield new_array


def _get_complex_attribute(row, attribute):
attributes = attribute.split('__')
value = row
for attributee in attributes:
value = _get_simple_attribute(value, attributee)
return value


def _get_simple_attribute(row, attribute):
value = getattr(row, attribute)
if isinstance(value, (datetime.date, datetime.time)):
value = value.isoformat()
return value


def is_empty_array(array):
"""
Check if an array is an array of '' or not
Expand Down
12 changes: 10 additions & 2 deletions tests/test_sql_book.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column , Integer, String, Float, Date, DateTime, ForeignKey
Expand All @@ -10,7 +11,7 @@
from pyexcel_io.database.sql import SQLBookWriter, SQLTableImporter, SQLTableImportAdapter
from pyexcel_io.database.sql import SQLTableExporter, SQLTableExportAdapter, SQLBookReader
from sqlalchemy.orm import relationship, backref
from nose.tools import raises
from nose.tools import raises, eq_
import platform

engine = None
Expand All @@ -29,6 +30,7 @@ class Pyexcel(Base):
weight=Column(Float)
birth=Column(Date)


class Post(Base):
__tablename__ = 'post'
id = Column(Integer, primary_key=True)
Expand All @@ -51,6 +53,7 @@ def __init__(self, title, body, category, pub_date=None):
def __repr__(self):
return '<Post %r>' % self.title


class Category(Base):
__tablename__ = 'category'
id = Column(Integer, primary_key=True)
Expand Down Expand Up @@ -339,11 +342,16 @@ def test_read(self):
book = SQLBookReader()
book.open_content(exporter)
data = book.read_all()
import json
for key in data.keys():
data[key] = list(data[key])
assert json.dumps(data) == '{"category": [["id", "name"], [1, "News"], [2, "Sports"]], "post": [["body", "category_id", "id", "pub_date", "title"], ["formal", 1, 1, "2015-01-20T23:28:29", "Title A"], ["informal", 2, 2, "2015-01-20T23:28:30", "Title B"]]}'

def test_foreign_key(self):
all_posts = self.session.query(Post).all()
column_names = ['category__name', 'title']
data = list(from_query_sets(column_names, all_posts))
eq_(json.dumps(data), '[["category__name", "title"], ["News", "Title A"], ["Sports", "Title B"]]')

def tearDown(self):
self.session.close()

Expand Down

0 comments on commit fc8f958

Please sign in to comment.