Skip to content

Instantly share code, notes, and snippets.

@zzzeek
Created May 3, 2012 16:04
Show Gist options
  • Save zzzeek/2586821 to your computer and use it in GitHub Desktop.
Save zzzeek/2586821 to your computer and use it in GitHub Desktop.
class Entry(Base):
# ...
@property
def public_children(self):
return set(c for c in self.children if c.public)
class Child(Base):
# ...
@property
def public_author(self):
if self.author.public:
return self.author
else:
return None
class Author(Base):
# ...
@property
def public_company(self):
if self.company.public:
return self.company
else:
return None
item = session.query(Entry).filter_by(public=True, id=item_id).scalar()
data = {‘id’: item.id,
‘title’: item.title,
‘date’: item.date,
‘body’: item.body}
data[‘children’] = children = []
for child in item.public_children:
child_data = {‘id’: child.id,
‘title’: child.title,
‘data’: child.data,
‘body’: child.body}
if child.public_author:
child_data[‘author’] = author = {‘id’: child.author.id,
‘name’: child.author.name}
if child.author.public_company:
author[‘company’] = {‘id’: child.author.company.id,
‘title’: child.author.company.title}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment