def get(self): """ Get the users that the authenticated user is following. Request: `GET` `/users/following` Example response: { 'following': [ { 'document_id': 123, ... } ] } """ follower_user_id = self.request.authenticated_userid followed_user_ids = DBSession. \ query(FollowedUser.followed_user_id). \ filter(FollowedUser.follower_user_id == follower_user_id). \ all() followed_user_ids = [user_id for (user_id, ) in followed_user_ids] followed_users = get_documents_for_ids( followed_user_ids, None, user_profile_documents_config). \ get('documents') return { 'following': followed_users }
def set_linked_routes(waypoint, lang): """ Set associated routes for the given waypoint including associated routes of child and grandchild waypoints. Note that this function returns a dict and not a list! """ with_query_waypoints = _get_select_children(waypoint) route_ids = get_first_column( DBSession.query(Route.document_id) .select_from(with_query_waypoints) .join(Association, with_query_waypoints.c.document_id == Association.parent_document_id) .join(Route, Association.child_document_id == Route.document_id) .filter(Route.redirects_to.is_(None)) .order_by(with_query_waypoints.c.priority.desc(), Route.document_id.desc()) .limit(NUM_ROUTES) .all() ) total = ( DBSession.query(Route.document_id) .select_from(with_query_waypoints) .join(Association, with_query_waypoints.c.document_id == Association.parent_document_id) .join(Route, Association.child_document_id == Route.document_id) .filter(Route.redirects_to.is_(None)) .count() ) waypoint.associations["all_routes"] = get_documents_for_ids(route_ids, lang, route_documents_config, total)
def get_linked_routes(document, lang): condition_as_child = and_( Association.child_document_id == Route.document_id, Association.parent_document_id == document.document_id) condition_as_parent = and_( Association.child_document_id == document.document_id, Association.parent_document_id == Route.document_id) if document.type == WAYPOINT_TYPE: condition = condition_as_child elif document.type in [OUTING_TYPE, IMAGE_TYPE, ARTICLE_TYPE, XREPORT_TYPE]: condition = condition_as_parent else: condition = or_(condition_as_child, condition_as_parent) route_ids = get_first_column( DBSession.query(Route.document_id). filter(Route.redirects_to.is_(None)). join(Association, condition). group_by(Route.document_id). all()) return get_documents_for_ids( route_ids, lang, route_documents_config).get('documents')
def get_linked_xreports(document, lang): condition_as_child = and_( Association.child_document_id == Xreport.document_id, Association.parent_document_id == document.document_id ) condition_as_parent = and_( Association.child_document_id == document.document_id, Association.parent_document_id == Xreport.document_id ) if document.type in [WAYPOINT_TYPE, USERPROFILE_TYPE, ARTICLE_TYPE, IMAGE_TYPE]: condition = condition_as_parent elif document.type in [ROUTE_TYPE, OUTING_TYPE]: condition = condition_as_child xreport_ids = get_first_column( DBSession.query(Xreport.document_id). filter(Xreport.redirects_to.is_(None)). join( Association, condition). group_by(Xreport.document_id). all()) return get_documents_for_ids( xreport_ids, lang, xreport_documents_config).get('documents')
def get(self): """ Get the users that the authenticated user is following. Request: `GET` `/users/following` Example response: { 'following': [ { 'document_id': 123, ... } ] } """ follower_user_id = self.request.authenticated_userid followed_user_ids = DBSession. \ query(FollowedUser.followed_user_id). \ filter(FollowedUser.follower_user_id == follower_user_id). \ all() followed_user_ids = [user_id for (user_id, ) in followed_user_ids] followed_users = get_documents_for_ids( followed_user_ids, None, user_profile_documents_config). \ get('documents') return {'following': followed_users}
def get_linked_articles(document, lang): condition_as_child = and_( Association.child_document_id == Article.document_id, Association.parent_document_id == document.document_id ) condition_as_parent = and_( Association.child_document_id == document.document_id, Association.parent_document_id == Article.document_id ) if document.type == IMAGE_TYPE: condition = condition_as_parent elif document.type in [WAYPOINT_TYPE, OUTING_TYPE, ROUTE_TYPE, BOOK_TYPE, XREPORT_TYPE, USERPROFILE_TYPE]: condition = condition_as_child elif document.type == ARTICLE_TYPE: condition = or_(condition_as_child, condition_as_parent) article_ids = get_first_column( DBSession.query(Article.document_id). filter(Article.redirects_to.is_(None)). join( Association, condition). group_by(Article.document_id). all()) return get_documents_for_ids( article_ids, lang, article_documents_config).get('documents')
def get_linked_articles(document, lang): condition_as_child = and_( Association.child_document_id == Article.document_id, Association.parent_document_id == document.document_id) condition_as_parent = and_( Association.child_document_id == document.document_id, Association.parent_document_id == Article.document_id) if document.type == IMAGE_TYPE: condition = condition_as_parent elif document.type in [ WAYPOINT_TYPE, OUTING_TYPE, ROUTE_TYPE, BOOK_TYPE, XREPORT_TYPE, USERPROFILE_TYPE ]: condition = condition_as_child elif document.type == ARTICLE_TYPE: condition = or_(condition_as_child, condition_as_parent) article_ids = get_first_column( DBSession.query(Article.document_id).filter( Article.redirects_to.is_(None)).join( Association, condition).group_by(Article.document_id).all()) return get_documents_for_ids(article_ids, lang, article_documents_config).get('documents')
def set_linked_routes(waypoint, lang): """ Set associated routes for the given waypoint including associated routes of child and grandchild waypoints. Note that this function returns a dict and not a list! """ with_query_waypoints = _get_select_children(waypoint) route_ids = get_first_column( DBSession.query( Route.document_id).select_from(with_query_waypoints).join( Association, with_query_waypoints.c.document_id == Association.parent_document_id).join( Route, Association.child_document_id == Route.document_id).filter( Route.redirects_to.is_(None)).order_by( with_query_waypoints.c.priority.desc(), Route.document_id.desc()).limit(NUM_ROUTES).all()) total = DBSession.query(Route.document_id). \ select_from(with_query_waypoints). \ join( Association, with_query_waypoints.c.document_id == Association.parent_document_id). \ join( Route, Association.child_document_id == Route.document_id). \ filter(Route.redirects_to.is_(None)). \ count() waypoint.associations['all_routes'] = get_documents_for_ids( route_ids, lang, route_documents_config, total)
def get(self): """ Get the blocked users. Request: `GET` `/users/blocked` Example response: { 'blocked': [ { 'document_id': 123, ... } ] } """ blocked_user_ids = DBSession. \ query(User.id). \ filter(User.blocked). \ all() blocked_user_ids = [user_id for (user_id, ) in blocked_user_ids] blocked_users = get_documents_for_ids( blocked_user_ids, None, user_profile_documents_config). \ get('documents') return {'blocked': blocked_users}
def get_linked_users(document, lang): user_ids = get_first_column( DBSession.query(User.id).join( Association, Association.parent_document_id == User.id).filter( Association.child_document_id == document.document_id).group_by(User.id).all()) return get_documents_for_ids( user_ids, lang, user_profile_documents_config).get('documents')
def get_linked_users(document, lang): user_ids = get_first_column( DBSession.query(User.id). join(Association, Association.parent_document_id == User.id). filter(Association.child_document_id == document.document_id). group_by(User.id). all()) return get_documents_for_ids( user_ids, lang, user_profile_documents_config).get('documents')
def get_linked_waypoint_parents(document, lang): waypoint_ids = get_first_column( DBSession.query(Waypoint.document_id).filter( Waypoint.redirects_to.is_(None)).join( Association, Association.parent_document_id == Waypoint.document_id).filter( Association.child_document_id == document.document_id).group_by(Waypoint.document_id).all()) return get_documents_for_ids(waypoint_ids, lang, waypoint_documents_config).get('documents')
def get_linked_books(document, lang): book_ids = get_first_column( DBSession.query(Book.document_id).filter( Book.redirects_to.is_(None)).join( Association, and_(Association.child_document_id == document.document_id, Association.parent_document_id == Book.document_id)).group_by(Book.document_id).all()) return get_documents_for_ids(book_ids, lang, book_documents_config).get('documents')
def get_linked_outings(document, lang): outing_ids = get_first_column( DBSession.query(Outing.document_id).filter( Outing.redirects_to.is_(None)).join( Association, and_(Association.parent_document_id == Outing.document_id, Association.child_document_id == document.document_id)).group_by(Outing.document_id).all()) return get_documents_for_ids(outing_ids, lang, outing_documents_config).get('documents')
def load_documents(documents_to_load, lang): documents = {} for document_type, document_ids in documents_to_load.items(): document_config = document_configs[document_type] docs = get_documents_for_ids(document_ids, lang, document_config).get("documents") for doc in docs: documents[doc["document_id"]] = doc return documents
def get_linked_images(document, lang): image_ids = get_first_column( DBSession.query(Image.document_id).filter( Image.redirects_to.is_(None)).join( Association, and_(Association.child_document_id == Image.document_id, Association.parent_document_id == document.document_id)).order_by(asc( Image.date_time)).group_by(Image.document_id).all()) return get_documents_for_ids(image_ids, lang, image_documents_config).get('documents')
def get_linked_waypoint_parents(document, lang): waypoint_ids = get_first_column( DBSession.query(Waypoint.document_id). filter(Waypoint.redirects_to.is_(None)). join(Association, Association.parent_document_id == Waypoint.document_id). filter(Association.child_document_id == document.document_id). group_by(Waypoint.document_id). all()) return get_documents_for_ids( waypoint_ids, lang, waypoint_documents_config).get('documents')
def load_documents(documents_to_load, lang): documents = {} for document_type, document_ids in documents_to_load.items(): document_config = document_configs[document_type] docs = get_documents_for_ids(document_ids, lang, document_config).get('documents') for doc in docs: documents[doc['document_id']] = doc return documents
def get_linked_outings(document, lang): outing_ids = get_first_column( DBSession.query(Outing.document_id). filter(Outing.redirects_to.is_(None)). join( Association, and_( Association.parent_document_id == Outing.document_id, Association.child_document_id == document.document_id) ). group_by(Outing.document_id). all()) return get_documents_for_ids( outing_ids, lang, outing_documents_config).get('documents')
def get_linked_books(document, lang): book_ids = get_first_column( DBSession.query(Book.document_id). filter(Book.redirects_to.is_(None)). join( Association, and_( Association.child_document_id == document.document_id, Association.parent_document_id == Book.document_id) ). group_by(Book.document_id). all()) return get_documents_for_ids( book_ids, lang, book_documents_config).get('documents')
def get_linked_images(document, lang): image_ids = get_first_column( DBSession.query(Image.document_id). filter(Image.redirects_to.is_(None)). join( Association, and_( Association.child_document_id == Image.document_id, Association.parent_document_id == document.document_id) ). group_by(Image.document_id). all()) return get_documents_for_ids( image_ids, lang, image_documents_config).get('documents')
def set_recent_outings(waypoint, lang): """Set last 10 outings on routes associated to the given waypoint. """ t_outing_route = aliased(Association, name='a1') t_route_wp = aliased(Association, name='a2') with_query_waypoints = _get_select_children(waypoint) recent_outing_ids = get_first_column( DBSession.query(Outing.document_id). filter(Outing.redirects_to.is_(None)). join( t_outing_route, Outing.document_id == t_outing_route.child_document_id). join( t_route_wp, and_( t_route_wp.child_document_id == t_outing_route.parent_document_id, t_route_wp.child_document_type == ROUTE_TYPE, )). join( with_query_waypoints, with_query_waypoints.c.document_id == t_route_wp.parent_document_id ). order_by(Outing.date_end.desc()). limit(NUM_RECENT_OUTINGS). all()) total = DBSession.query(Outing.document_id). \ filter(Outing.redirects_to.is_(None)). \ join( t_outing_route, Outing.document_id == t_outing_route.child_document_id). \ join( t_route_wp, and_( t_route_wp.child_document_id == t_outing_route.parent_document_id, t_route_wp.child_document_type == ROUTE_TYPE, )). \ join( with_query_waypoints, with_query_waypoints.c.document_id == t_route_wp.parent_document_id ). \ count() waypoint.associations['recent_outings'] = get_documents_for_ids( recent_outing_ids, lang, outing_documents_config, total)
def set_recent_outings(waypoint, lang): """Set last 10 outings on routes associated to the given waypoint. """ t_outing_route = aliased(Association, name="a1") t_route_wp = aliased(Association, name="a2") with_query_waypoints = _get_select_children(waypoint) recent_outing_ids = get_first_column( DBSession.query(Outing.document_id) .filter(Outing.redirects_to.is_(None)) .join(t_outing_route, Outing.document_id == t_outing_route.child_document_id) .join( t_route_wp, and_( t_route_wp.child_document_id == t_outing_route.parent_document_id, t_route_wp.child_document_type == ROUTE_TYPE, ), ) .join(with_query_waypoints, with_query_waypoints.c.document_id == t_route_wp.parent_document_id) .order_by(Outing.date_end.desc()) .limit(NUM_RECENT_OUTINGS) .all() ) total = ( DBSession.query(Outing.document_id) .filter(Outing.redirects_to.is_(None)) .join(t_outing_route, Outing.document_id == t_outing_route.child_document_id) .join( t_route_wp, and_( t_route_wp.child_document_id == t_outing_route.parent_document_id, t_route_wp.child_document_type == ROUTE_TYPE, ), ) .join(with_query_waypoints, with_query_waypoints.c.document_id == t_route_wp.parent_document_id) .count() ) waypoint.associations["recent_outings"] = get_documents_for_ids( recent_outing_ids, lang, outing_documents_config, total )
def get_linked_xreports(document, lang): condition_as_child = and_( Association.child_document_id == Xreport.document_id, Association.parent_document_id == document.document_id) condition_as_parent = and_( Association.child_document_id == document.document_id, Association.parent_document_id == Xreport.document_id) if document.type in [ WAYPOINT_TYPE, USERPROFILE_TYPE, ARTICLE_TYPE, IMAGE_TYPE ]: condition = condition_as_parent elif document.type in [ROUTE_TYPE, OUTING_TYPE]: condition = condition_as_child xreport_ids = get_first_column( DBSession.query(Xreport.document_id).filter( Xreport.redirects_to.is_(None)).join( Association, condition).group_by(Xreport.document_id).all()) return get_documents_for_ids(xreport_ids, lang, xreport_documents_config).get('documents')
def set_recent_outings(route, lang): """Set last 10 outings on the given route. """ recent_outing_ids = get_first_column( DBSession.query(Outing.document_id).filter( Outing.redirects_to.is_(None)).join( Association, Outing.document_id == Association.child_document_id). filter( Association.parent_document_id == route.document_id).order_by( Outing.date_end.desc()).limit(NUM_RECENT_OUTINGS).all()) total = DBSession.query(Outing.document_id). \ filter(Outing.redirects_to.is_(None)). \ join( Association, Outing.document_id == Association.child_document_id). \ filter(Association.parent_document_id == route.document_id). \ count() route.associations['recent_outings'] = get_documents_for_ids( recent_outing_ids, lang, outing_documents_config, total)
def set_recent_outings(route, lang): """Set last 10 outings on the given route. """ recent_outing_ids = get_first_column( DBSession.query(Outing.document_id). filter(Outing.redirects_to.is_(None)). join( Association, Outing.document_id == Association.child_document_id). filter(Association.parent_document_id == route.document_id). order_by(Outing.date_end.desc()). limit(NUM_RECENT_OUTINGS). all()) total = DBSession.query(Outing.document_id). \ filter(Outing.redirects_to.is_(None)). \ join( Association, Outing.document_id == Association.child_document_id). \ filter(Association.parent_document_id == route.document_id). \ count() route.associations['recent_outings'] = get_documents_for_ids( recent_outing_ids, lang, outing_documents_config, total)
def get_linked_routes(document, lang): condition_as_child = and_( Association.child_document_id == Route.document_id, Association.parent_document_id == document.document_id) condition_as_parent = and_( Association.child_document_id == document.document_id, Association.parent_document_id == Route.document_id) if document.type == WAYPOINT_TYPE: condition = condition_as_child elif document.type in [ OUTING_TYPE, IMAGE_TYPE, ARTICLE_TYPE, XREPORT_TYPE ]: condition = condition_as_parent else: condition = or_(condition_as_child, condition_as_parent) route_ids = get_first_column( DBSession.query( Route.document_id).filter(Route.redirects_to.is_(None)).join( Association, condition).group_by(Route.document_id).all()) return get_documents_for_ids(route_ids, lang, route_documents_config).get('documents')