Exemplo n.º 1
0
def most_relevant(request):
    session = Session()
    timer = datetime.now()
    join = sa_post.join(sa_relvote, onclause=(sa_relvote.c.post_id==sa_post.c.id))
    sel = select([sa_post.c.id, func.count(sa_relvote.c.id).label("votes")], whereclause=sa_relvote.c.date_expire > datetime.now(), 
            from_obj=[join]).group_by(sa_post.c.id).order_by("votes DESC")
    ps = session.execute(sel).fetchall()
    print ps
    delta = datetime.now() - timer
    print delta
    if not ps:
        messages.info(request, "This site has nothing relevant green vote on something")
    try:
        _id = ps[0][0]
        post = Post.objects.get(pk=_id)
    except:
        post = Post.objects.get(pk=1)
    temp_args = {'post' : post}
    children = list(Post.objects.filter(parent=post)\
            .annotate(num_votes=Count('vote'))\
            .order_by('-num_votes'))
    temp_args['p_struct'] = children
    temp_args['prof_user'] = request.user
    Session.remove()
    return render_to_response("most_relevant.html",
            temp_args,
            RequestContext(request))
Exemplo n.º 2
0
def most_relevant(request):
    session = Session()
    timer = datetime.now()
    join = sa_post.join(sa_relvote,
                        onclause=(sa_relvote.c.post_id == sa_post.c.id))
    sel = select([sa_post.c.id,
                  func.count(sa_relvote.c.id).label("votes")],
                 whereclause=sa_relvote.c.date_expire > datetime.now(),
                 from_obj=[join]).group_by(sa_post.c.id).order_by("votes DESC")
    ps = session.execute(sel).fetchall()
    print ps
    delta = datetime.now() - timer
    print delta
    if not ps:
        messages.info(
            request, "This site has nothing relevant green vote on something")
    try:
        _id = ps[0][0]
        post = Post.objects.get(pk=_id)
    except:
        post = Post.objects.get(pk=1)
    temp_args = {'post': post}
    children = list(Post.objects.filter(parent=post)\
            .annotate(num_votes=Count('vote'))\
            .order_by('-num_votes'))
    temp_args['p_struct'] = children
    temp_args['prof_user'] = request.user
    Session.remove()
    return render_to_response("most_relevant.html", temp_args,
                              RequestContext(request))
Exemplo n.º 3
0
def preload_front():
    session = Session()
    now = datetime.now()
    sel = select([
        sa_post,
        func.count(sa_relvote.c.id).label('rel_count'),
    ],
                 whereclause=and_(sa_post.c.parent_id == None, ),
                 from_obj=[
                     sa_post,
                     sa_post.outerjoin(
                         sa_relvote,
                         and_(sa_post.c.id == sa_relvote.c.post_id,
                              sa_relvote.c.date_expire > now)),
                 ]).group_by(sa_post.c.id)
    rvote_count = alias(sel)
    sel = select([
        sa_post, rvote_count.c.rel_count,
        func.count(sa_vote.c.id).label('vote_count')
    ],
                 whereclause=and_(sa_post.c.parent_id == None,
                                  sa_post.c.id == rvote_count.c.id),
                 from_obj=[
                     sa_post,
                     rvote_count,
                     sa_post.outerjoin(
                         sa_vote, and_(sa_post.c.id == sa_vote.c.post_id)),
                 ]).group_by(sa_post.c.id).order_by('-rel_count')
    posts = session.execute(sel).fetchall()
    td = datetime.now() - now
    print "time delay is " + str(td.microseconds / 1000)
    return posts
Exemplo n.º 4
0
def make_vid_sets(uid):
    session = Session()
    vsel = select([sa_vote.c.post_id], whereclause=sa_vote.c.user_id==uid,  #DJANGO USERID
                 from_obj=[sa_vote])
    vote_set = set([_id[0] for (_id) in session.execute(vsel).fetchall()])
    rvsel = select([sa_relvote.c.post_id],
            whereclause=and_(sa_relvote.c.user_id==uid, rvotes.now_clause()),
            from_obj=[sa_relvote])
    rvote_set = set([_id[0] for (_id) in session.execute(rvsel).fetchall()])
    return (vote_set, rvote_set)
Exemplo n.º 5
0
def home(request):
    #posts = models.Post.objects.filter(parent=None).all()
    posts = view_funcs.preload_front()
    temp_args = {'posts': posts}
    #MAKE VIDSET
    Session.remove()
    if request.user.is_authenticated():
        vset, rset = view_funcs.make_vid_sets(request.user.id)
        temp_args['vset'] = vset
        temp_args['rset'] = rset
    return render_to_response("home.html", temp_args, RequestContext(request))
Exemplo n.º 6
0
def home(request):
    #posts = models.Post.objects.filter(parent=None).all()
    posts = view_funcs.preload_front()
    temp_args = {'posts' : posts}
    #MAKE VIDSET
    Session.remove()
    if request.user.is_authenticated():
        vset, rset = view_funcs.make_vid_sets(request.user.id)
        temp_args['vset'] =  vset
        temp_args['rset'] =  rset
    return render_to_response("home.html",
            temp_args,
            RequestContext(request))
Exemplo n.º 7
0
def make_vid_sets(uid):
    session = Session()
    vsel = select(
        [sa_vote.c.post_id],
        whereclause=sa_vote.c.user_id == uid,  #DJANGO USERID
        from_obj=[sa_vote])
    vote_set = set([_id[0] for (_id) in session.execute(vsel).fetchall()])
    rvsel = select([sa_relvote.c.post_id],
                   whereclause=and_(sa_relvote.c.user_id == uid,
                                    rvotes.now_clause()),
                   from_obj=[sa_relvote])
    rvote_set = set([_id[0] for (_id) in session.execute(rvsel).fetchall()])
    return (vote_set, rvote_set)
Exemplo n.º 8
0
def preload_front():
    session = Session()
    now = datetime.now()
    sel = select([sa_post,
        func.count(sa_relvote.c.id).label('rel_count'),],
            whereclause=and_(sa_post.c.parent_id==None,),
            from_obj=[sa_post,
            sa_post.outerjoin(sa_relvote, and_(sa_post.c.id==sa_relvote.c.post_id,
                                               sa_relvote.c.date_expire > now)),
            ]
            ).group_by(sa_post.c.id)
    rvote_count = alias(sel)
    sel = select([sa_post, rvote_count.c.rel_count,
        func.count(sa_vote.c.id).label('vote_count')],
            whereclause=and_(sa_post.c.parent_id==None,sa_post.c.id==rvote_count.c.id),
            from_obj=[sa_post, rvote_count,
            sa_post.outerjoin(sa_vote, and_(sa_post.c.id==sa_vote.c.post_id)),
            ]
            ).group_by(sa_post.c.id).order_by('-rel_count')
    posts = session.execute(sel).fetchall()
    td =  datetime.now() - now
    print "time delay is " + str(td.microseconds/1000)
    return posts