def query_ordered(cls): # order by most recent last_seen OR key transaction newest_date = functions.max( functions.coalesce(User.last_seen, 0), functions.coalesce(KeyTransaction.start, 0) ) query = Key.query.outerjoin(Key.holder).outerjoin(Key.current_transaction) return query.order_by(db.desc(newest_date))
def query_keyholders(cls): return User.query.filter(User.is_keyholder==True).order_by(db.desc(User.last_seen)).order_by(User.nick)