diff --git a/files/routes/search.py b/files/routes/search.py index a7bc7cd65..a6301b264 100644 --- a/files/routes/search.py +++ b/files/routes/search.py @@ -196,88 +196,86 @@ def searchposts(v): domain_obj=domain_obj ) -# @app.get("/search/comments") -# @auth_desired -# def searchcomments(v): +@app.get("/search/comments") +@auth_desired +def searchcomments(v): -# query = request.values.get("q", '').strip() + query = request.values.get("q", '').strip() -# try: page = max(1, int(request.values.get("page", 1))) -# except: page = 1 + try: page = max(1, int(request.values.get("page", 1))) + except: page = 1 -# sort = request.values.get("sort", "top").lower() -# t = request.values.get('t', 'all').lower() + sort = request.values.get("sort", "top").lower() + t = request.values.get('t', 'all').lower() -# criteria=searchparse(query) + criteria=searchparse(query) -# comments = g.db.query(Comment).filter(Comment.parent_submission != None).join(Comment.comment_aux) + comments = g.db.query(Comment).options(lazyload('*')).filter(Comment.parent_submission != None).join(Comment.comment_aux) -# if 'q' in criteria: -# words=criteria['q'].split() -# words=[CommentAux.body.ilike('%'+x+'%') for x in words] -# words=tuple(words) -# comments=comments.filter(*words) + if 'q' in criteria: + words=criteria['q'].split() + words=[CommentAux.body.ilike('%'+x+'%') for x in words] + words=tuple(words) + comments=comments.filter(*words) -# if not(v and v.admin_level >= 3): -# comments = comments.filter( -# Comment.deleted_utc == 0, -# Comment.is_banned == False) + if not(v and v.admin_level >= 3): + comments = comments.filter( + Comment.deleted_utc == 0, + Comment.is_banned == False) -# if t: -# now = int(time.time()) -# if t == 'hour': -# cutoff = now - 3600 -# elif t == 'day': -# cutoff = now - 86400 -# elif t == 'week': -# cutoff = now - 604800 -# elif t == 'month': -# cutoff = now - 2592000 -# elif t == 'year': -# cutoff = now - 31536000 -# else: -# cutoff = 0 -# comments = comments.filter(Comment.created_utc >= cutoff) + if t: + now = int(time.time()) + if t == 'hour': + cutoff = now - 3600 + elif t == 'day': + cutoff = now - 86400 + elif t == 'week': + cutoff = now - 604800 + elif t == 'month': + cutoff = now - 2592000 + elif t == 'year': + cutoff = now - 31536000 + else: + cutoff = 0 + comments = comments.filter(Comment.created_utc >= cutoff) -# comments=comments.options(contains_eager(Comment.comment_aux)) - -# if sort == "new": -# comments = comments.order_by(Comment.created_utc.desc()).all() -# elif sort == "old": -# comments = comments.order_by(Comment.created_utc.asc()).all() -# elif sort == "controversial": -# comments = sorted(comments.all(), key=lambda x: x.score_disputed, reverse=True) -# elif sort == "top": -# comments = sorted(comments.all(), key=lambda x: x.score, reverse=True) -# elif sort == "bottom": -# comments = sorted(comments.all(), key=lambda x: x.score) - -# total = len(list(comments)) -# firstrange = 25 * (page - 1) -# secondrange = firstrange+26 -# comments = comments[firstrange:secondrange] -# ids = [x.id for x in comments] + comments=comments.options(contains_eager(Comment.comment_aux)) + if sort == "new": + comments = comments.order_by(Comment.created_utc.desc()) + elif sort == "old": + comments = comments.order_by(Comment.created_utc.asc()) + elif sort == "controversial": + comments = comments.order_by(-1 * Comment.upvotes * (Comment.downvotes+1)) + elif sort == "top": + comments = comments.order_by(Comment.downvotes - Comment.upvotes) + elif sort == "bottom": + comments = comments.order_by(Comment.upvotes - Comment.downvotes) + + total = comments.count() + + comments = comments.offset(25 * (page - 1)).limit(26).all() + ids = [x[0] for x in comments] -# next_exists = (len(ids) > 25) -# ids = ids[:25] + next_exists = (len(ids) > 25) + ids = ids[:25] -# comments = get_comments(ids, v=v) + comments = get_comments(ids, v=v) -# if request.headers.get("Authorization"): return [x.json for x in comments] -# else: return render_template("search_comments.html", v=v, query=query, total=total, page=page, comments=comments, sort=sort, t=t, next_exists=next_exists) + if request.headers.get("Authorization"): return [x.json for x in comments] + else: return render_template("search_comments.html", v=v, query=query, total=total, page=page, comments=comments, sort=sort, t=t, next_exists=next_exists) @app.get("/search/users")