

def get_epistle_roots(id=None, target='recipient', include_read=False):
    if (not id):
        return 'Sorry, you have to provide a valid ID.'
    q = dbsession.query(Epistle).filter(sqlalchemy.or_(sqlalchemy.and_((Epistle.parent_type == 'epistle'), (Epistle.parent == None)), (Epistle.parent_type != 'epistle')))
    if ((target == 'sender') or (target == 'out')):
        q = q.filter((Epistle.sender == id)).filter((Epistle.parent_type == 'epistle'))
        include_read = True
    else:
        q = q.filter((Epistle.recipient == id))
    if (target == 'read'):
        include_read = True
    if (not include_read):
        q = q.filter((Epistle.unread == True))
    return q.order_by(Epistle.added_on.desc()).all()

dataset/ETHPy150Open sjuxax/raggregate/raggregate/queries/epistle.py get_epistle_roots/original