Exemplo n.º 1
0
def get_comments(id, organize_parentage = False, page_num = 1, per_page = 30, sort = 'new', target = 'story', target_id = None):
    if not organize_parentage:
        return dbsession.query(Comment).filter(Comment.submission_id == id).all()
    else:
        #@TODO: this will probably be slow in practice and would be better off as a hand-rolled SQL query
        # not implementing that at the moment because I want database agnosticism, but perhaps I will include
        # a statement for PostgreSQL soon. It could be used on Pg installs and as an example for others.
        tree = {}
        tree[id] = []
        dex = {}
        all_comments = dbsession.query(Comment).filter(Comment.submission_id == id).all()
        if target == 'story':
            roots = dbsession.query(Comment).filter(Comment.submission_id == id).filter(Comment.submission_id == Comment.parent_id)
        elif target == 'comment':
            roots = dbsession.query(Comment).filter(Comment.submission_id == id).filter(target_id == Comment.id)

        max_roots = general.count_sa_obj(roots)

        if sort == 'top':
            roots = roots.order_by(Comment.points.desc())
        else:
            # use "new" as default sort option
            roots = roots.order_by(Comment.added_on.desc())

        endpoints = get_endpoints_from_page_num(page_num, per_page)
        allowed_roots = [ ]
        [allowed_roots.append(str(root.id)) for root in roots[endpoints['start']:endpoints['end']]]

        trees = _build_comment_trees(all_comments, allowed_roots)
        tree = trees['tree']
        dex = trees['dex']
        allowed_roots = trees['allowed_roots']
        return {'tree': tree, 'dex': dex, 'comments': all_comments, 'max_comments': max_roots, 'allowed_roots': allowed_roots}
Exemplo n.º 2
0
def get_story_list(page_num = 1, per_page = 30, sort = 'new', request = None, self_only = False, section = None):
    if 'users.id' in request.session and request.session['users.id'] is not None:
        user_id = request.session['users.id']
    else:
        user_id = None

    stories = dbsession.query(Submission).options(joinedload('submitter')).filter(Submission.deleted == False).filter(Submission.render_type == 'story_md')

    if section and section.__class__ == Section:
        stories = stories.filter(Submission.section == section.id)
    elif section and section == 'all':
        pass
    else:
        # show default user sections
        if user_id is not None:
            # Get a list of sections that this user is subscribed to
            subscribed_to_list = sub_queries.get_subscribed_by_user_id(user_id)
            # Filter sections by the list we just retreived
            if len(subscribed_to_list) > 0:
                stories = stories.filter(Submission.section.in_(subscribed_to_list))

    if self_only:
        stories = stories.filter(Submission.self_post == True)

    if sort == 'top':
        stories = stories.order_by(Submission.points.desc())
    if sort == 'hot':
        if request and 'sort.hot_point_window' in request.registry.settings:
            sets = request.registry.settings
            hotness.recentize_hots(hot_point_window = general.realize_timedelta_constructor(sets['sort.hot_point_window']),
                                   hot_eligible_age = general.realize_timedelta_constructor(sets['sort.hot_eligible_age']),
                                   hot_recalc_threshold = general.realize_timedelta_constructor(sets['sort.hot_recalc_threshold']))
            stories = hotness.get_hot_stories(hot_eligible_age = general.realize_timedelta_constructor(sets['sort.hot_eligible_age'])) 
        else:
            hotness.recentize_hots()
            stories = hotness.get_hot_stories()
    if sort == 'new':
        stories = stories.order_by(Submission.added_on.desc())
    if sort == 'contro':
        hotness.recentize_contro()
        stories = hotness.get_controversial_stories()

    max_stories = general.count_sa_obj(stories)

    endpoints = get_endpoints_from_page_num(page_num, per_page)
    return {'stories': stories[endpoints['start']:endpoints['end']], 'max_stories': max_stories}