def bookmark_list(): ''' Returns a list of bookmarks ''' search_form = SearchForm(request.args) # Create the base query # After this query is created, we keep iterating over it until we reach the desired level of filtering query = Bookmark.query.filter_by(user=current_user.id, deleted=False).order_by(Bookmark.added_on.desc()) # Get list of tags we'll be filtering by tag_args = request.values.getlist('tags') if len(tag_args) == 0: tag_args = None else: for tag in tag_args: # Check is any of the tags for the bookmark match up with tag query = query.filter(Bookmark.tags.any(tag)) # This means that the search form has been used if search_form.query.data is not None: # Search query type can be either basic, full text, or url # This is basic search, which searches in the bookmark titles and descriptions if search_form.parameter.data == 'basic': query = search(query, search_form.query.data, vector=Bookmark.search_vector) # XXX is this safe? user_count = Bookmark.query.filter_by(user=current_user.id, deleted=False).count() # Postgres full text search seems to fail when using non-ASCII characters # When the failure happens, all the bookmarks are returned instead # We check if this has happened, and if it has, we fall back to non-indexed search instead if query.count() == user_count: query = query.filter(or_(Bookmark.title.contains(search_form.query.data), Bookmark.description.contains(search_form.query.data))) elif search_form.parameter.data == 'ft': # We will search over the entire contents of the page here query = search(query, search_form.query.data, vector=Bookmark.fulltext_vector) user_count = Bookmark.query.filter_by(user=current_user.id, deleted=False).count() if query.count() == user_count: query = query.filter(Bookmark.full_text.contains(search_form.query.data)) # URL search lets you filter by domains or other parts of the url elif search_form.parameter.data == 'url': query = query.filter(Bookmark.main_url.contains(search_form.query.data)) else: pass # Context view takes you to the page the bookmark with a specific id is present on # Here the id is used to know which bookmark should be highlighted try: context_id = request.args['bid'] except KeyError: context_id = 0 # Pagination, with defaulting to the first page page = request.args.get('page', 1, type=int) # Users are allowed to specify how many bookmarks they want per page bookmarks_per_page = User.query.get(current_user.id).bookmarks_per_page # Paginate the results of our query pagination = query.paginate(page, per_page=bookmarks_per_page, error_out=False) delete_form = DeleteForm() return render_template("manager/bookmark_list.html", pagination=pagination, search_form=search_form, delete_form=delete_form, context_id=context_id, tag_args=tag_args)
def get(self): if "q" not in request.args: return {"error": "'q' is required"}, 400 albums = search(Album.query, request.args["q"]) artists = search(Artist.query, request.args["q"]) return { "albums": [AlbumSerializer.dump(album) for album in albums.limit(5).all()], "artists": [ArtistSerializer.dump(album) for album in artists.limit(5).all()], }
def test_supports_session_queries(self): query = self.session.query(self.Order) assert ( '("order".search_vector) @@ to_tsquery(:to_tsquery_1)' in str(search(query, 'something')) )
def build_query(self, search_query, vector=None, regconfig=None, sort=False): qs = self.union_query() return search(qs, search_query, vector, regconfig, sort)
def search(self, search_query, vector=None, catalog=None): """ Search given query with full text search. :param search_query: the search query """ return search(self, search_query, vector=vector, catalog=catalog)
async def get(self): # Prepare query query = self.application.db.query(Question) # Search search_value = self.get_argument('q', False) if search_value: query = search(query, search_value) # Sort and limit metadata = extract_metadata(query) query = order_query(self, query) query = limit_offset_query(self, query, metadata) # Get questions questions = query.all() # Prepare data to return ret = { 'data': [question.to_dict() for question in questions], 'metadata': metadata } # Returns response self.set_status(200) self.write(ret) self.finish()
def search_users(): """Search for a user and return results.""" ph = Photos.query.filter_by(id=current_user.profile_pic).first() if ph is None: cas = 'default' else: cas = ph.photoName # Returns users for current user's friend requests received_friend_requests, sent_friend_requests = get_friend_requests( session["current_user"]["id"]) # Returns query for current user's friends (not User objects) so add .all() to the end to get list of User objects friends = get_friends(session["current_user"]["id"]).all() user_input = request.args.get("q") # Search user's query in users table of db and return all search results search_results = search(db.session.query(User), user_input).all() return render_template("users/browse_friends.html", received_friend_requests=received_friend_requests, sent_friend_requests=sent_friend_requests, friends=friends, search_results=search_results, csID=str(current_user.id), csPic=str(cas))
def list(self, searchquery=None): query = super(Upcoming, self).list() if searchquery: query = search(query, searchquery) return query
def finalize_query(self, query, fltr, session, qstring=None, order_by=None): search_query = None ranked = False if qstring is not None: ft_query = and_(SearchObjectIndex.so_uuid == ObjectInfoIndex.uuid, query) q = session.query( ObjectInfoIndex, func.ts_rank_cd( SearchObjectIndex.search_vector, func.plainto_tsquery(qstring) ).label('rank'))\ .options(subqueryload(ObjectInfoIndex.search_object))\ .options(subqueryload(ObjectInfoIndex.properties)).filter(ft_query) query_result = search(q, qstring, vector=SearchObjectIndex.search_vector, sort=order_by is None, regconfig='simple') ranked = True else: query_result = session.query(ObjectInfoIndex).options(subqueryload(ObjectInfoIndex.properties)).filter(query) if order_by is not None: query_result = query_result.order_by(order_by) elif ranked is True: query_result = query_result.order_by( desc( func.ts_rank_cd( SearchObjectIndex.search_vector, func.to_tsquery(search_query) ) ) ) if 'limit' in fltr: query_result = query_result.limit(fltr['limit']) return query_result, ranked
def post(self): searching_text = request.form.get('search', '') if not searching_text: return redirect(url_for('IndexView:get_0')) query = db.session.query(Post) results = search(query, searching_text, sort=True).all() return render_template('search_results.html', query=searching_text, results=results, user=g.user)
def search_page(searchletters): """Return searched letters.""" query = session.query(Movie) query = search(query, 'Pablo') return query.first().name
def get_posts(self, keywords=None, since=None, start=0, num=30): from ..db import get_global_session, TwitterModel if keywords: keywords = keywords.strip() session = get_global_session() query = session.query(TwitterModel) if since: # Filter date assert isinstance(since, str) query = query.filter(TwitterModel.published_time >= since) if not keywords or keywords.lower() == 'fresh tweets': # Recent papers results = (query.order_by(desc(TwitterModel.published_time)) .offset(start).limit(num).all()) elif keywords.lower() == 'hot tweets': results = (query.order_by(desc(TwitterModel.popularity)) .offset(start).limit(num).all()) else: search_kw = " or ".join(keywords.split(",")) searched_query = search(query, search_kw, sort=True) results = searched_query.offset(start).limit(num).all() # Unescape HTML parser = HTMLParser() for result in results: matches = re.findall(r"(https://t\.co/[^ .]{10})", result.text) result.href_text = result.text try: for match in matches: result.href_text = result.href_text.replace(match, '<a href="{}">{}</a>'.format(match, match)) except: pass return results
def search_products(): search_input = request.form['search_input'] prod_search = search(Product.query.all(), search_input, sort='True') #Remember to fill in return render_template('index.html', **locals())
def test_search_releases(self): from sqlalchemy_searchable import search from pynab.db import Release with db_session() as db: q = db.query(Release) q = search(q, 'engaged e06') print(q.first().search_name)
def test_post_search(self): self.add_post() for word in ("test", "drinking", "drink", "please", "good"): query = search(db.session.query(Post), word) retrieved_post = query.first() self.assertIsNotNone(retrieved_post, msg="when testing for %s" % word)
def test_weighted_search_results(self): query = self.session.query(self.WeightedTextItem) first, second = search(query, 'klaatu', sort=True).all() assert first.search_vector == "'barada':2B 'klaatu':1A 'nikto':3B" assert ( second.search_vector == "'barada':3B 'gort':1A 'klaatu':2B 'nikto':4B" )
def search_results(): """Search for tasks""" task_search = request.args.get("queryterm") taskdb = db.session.query(Task) results = search(taskdb, task_search) return render_template("search_tasks.html", results=results)
def search_restaurants(): """Search for a restaurant by name or address and return results.""" user_input = request.args.get("q") # Search user's query in restaurant table of db and return all search results search_results = search(db.session.query(Restaurant), user_input).all() return render_template("restaurants_search_results.html", search_results=search_results)
def get_search(cls, request, searchterm, logged_in_alias, page=1): page_url = PageURL_WebOb(request) query = DBSession.query(Question, Qvote, Images).outerjoin( Images, Images.usralias == Question.askedbyalias).outerjoin( Qvote, and_(Question.questionasker == Qvote.questionasker, Qvote.userid == logged_in_alias)) query = search(query, searchterm) return Page(query, page, url=page_url, items_per_page=20)
def search_post(): form = SearchForm() if form.validate_on_submit(): results = search(db.session.query(models.Post), form.query.data).limit(RESULTS_PER_PAGE).all() if results: return render_template('search.html', title="Search Results - ", form=form, results=results, user=current_user) flash('No results found! Try again') return redirect('/search') return render_template('search.html', title="Search", form=form, user=current_user)
def api_search(query_string): champ_query = db.session.query(Champion) ability_query = db.session.query(ChampionAbility) summoners_query = db.session.query(Summoner) game_query = db.session.query(FeaturedGame) result = {} result['champions'] = search(champ_query, query_string).all() result['abilities'] = search(ability_query, query_string).all() result['summoners'] = search(summoners_query, query_string).all() result['featured-games'] = search(game_query, query_string).all() jsonData = {} i = 0 for model, data in result.items(): jsonData[model] = {} for result_item in data: jsonData[model][i] = result_item.serialize() i+=1 return jsonify(jsonData)
def search_model(model, search_query): # TODO: move to hogwarts.accio.py base_query = db_session.query(model) query_strings = search_query.split() search_query = ' or '.join(query_strings) search_result = search(base_query, search_query, sort=True) return search_result
def search_model(model, search_query): # TODO: move to hogwarts.accio.py base_query = db_session.query(model) query_strings = search_query.split() search_query = " or ".join(query_strings) search_result = search(base_query, search_query, sort=True) return search_result
def search_results_entries(self, search_query): now = datetime.datetime.utcnow() sql_q = query.entries(self.is_preview, archivable=False) entries = search(sql_q, search_query.lower()) # It won't need pagination for now return render_template('search-results-entries.html', search_query=search_query, entries=entries, cls=self)
def searchthread(): data_input = request.args.get("q") print(data_input) search_results = None # if current_user.hoodid and current_user.blockid: # search_results = search(db.session.query(MThread).filter( # or_(MThread.hoodid == current_user.hoodid, MThread.blockid == current_user.blockid)), data_input).all() # else: search_results = search(db.session.query(MThread), data_input).all() return render_template('search.html', search_results=search_results)
def users(): if current_user.admin is True: if request.method == 'POST': query = request.form.get('query') user_list = User.query.order_by(User.username) user_list = search(user_list, query) else: user_list = User.query.order_by(User.username).all() else: user_list = User.query.filter_by(id=current_user.id) return render_template(usersTemplate, user_list=user_list)
def search_place(): if request.method == 'POST': search_term = request.form['search_term'] distance = request.form['distance'] latitude = request.form['latitude'] longitude = request.form['longitude'] point = WKTElement('POINT({0} {1})'.format(latitude, longitude), srid=4326) results = search(db.session.query(Place).filter(func.ST_DWithin(Place.point, point, float(distance))), search_term) return render_template('search_place_results.html', results=results, search_term=search_term, distance=distance, latitude=latitude, longitude=longitude) else: return render_template('search_place.html')
def search_entries(): page_title = "Search results" if g.search_form.validate_on_submit(): search_stmt = g.search_form.search.data query = db.session.query(Entry) query = search(query, search_stmt) entries = query.order_by(Entry.create_time.desc()).all() return render_template('search_results.html', page_title = page_title, entries = entries) else: flash('No search input given!') return redirect(url_for('entries'))
def get(self): query = Artist.query if "q" in request.args: query = search(query, request.args["q"]) else: # latest additions pass return [ ArtistSerializer.dump(artist) for artist in query.limit(10).all() # TODO: paginate ]
def search_results(query, page=1): if page < 1: abort(404) posts = models.Post.query.order_by(models.Post.datetime.desc()) posts = search(posts, query).paginate(page, POSTS_PER_PAGE, False) if posts.pages < 1: return render_template('no_elements_found.html') if page > posts.pages: abort(404) return render_template('search_posts.html', query=query, posts=posts)
def search_cities(term): wanted_keys = ['page'] query_dict = build_query_dict(request.args.to_dict(), wanted_keys) matches = search(db.session.query(City), build_search_query(term), sort=True) if matches is None: return not_found() if 'page' in query_dict: return build_pages(matches, int(query_dict['page']), 'cities') else: return sql_json(City, *matches)
def search_entries(): page_title = "Search results" if g.search_form.validate_on_submit(): search_stmt = g.search_form.search.data query = db.session.query(Entry) query = search(query, search_stmt) entries = query.order_by(Entry.create_time.desc()).all() return render_template('search_results.html', page_title=page_title, entries=entries) else: flash('No search input given!') return redirect(url_for('entries'))
def get_users(query_params): """ Retrieves data for all user's who have an attribute that matches the searchText Args: query_params (dict): a dictionary containing all of the query params Returns: list: list of dicts containing users information Raises: None """ query_params = query_params if query_params else dict() logger.info( "Retrieving user information with query params of {}...".format( query_params)) limit = query_params.get('limit', 25) offset = query_params.get('offset', 0) search_text = query_params.get('searchText') search_field = query_params.get('searchField') with db_session() as session: logger.info("Constructing text search sub query...") search_sub_query = session.query(UserData.user_id) if search_text: logger.info("Searching fields for text...") search_text = unquote(search_text) search_sub_query = search(search_sub_query, search_text, sort=True) if search_field: logger.info("Filtering results by searched field...") search_field = unquote(search_field) search_sub_query = search_sub_query.filter_by( field_type=search_field) logger.info("Applying offset and limit on subquery...") search_sub_query = search_sub_query.distinct().offset(offset).limit( limit).subquery() logger.info("Retrieving users info from database...") result = session.query( UserData.field_type, UserData.field_data, UserData.user_id).filter( UserData.user_id == search_sub_query.c.user_id) logger.info("Users data retrieved") users_data = User(strict=True).dump(result, many=True).data return dict(meta=dict(limit=limit, offset=offset, count=len(users_data)), data=users_data)
def build_tweet_query(collection, query, filter_, filter_args, possibly_limit=True, story=None, cluster=None, clustered_selection_id=None, count=False): feature_filter_args = build_feature_filter(filter_args) tweets = db.session.query(model.Tweet).without_no_load_balance_comment() if filter_ == 'none': tweets = (tweets.filter(model.Tweet.collection == collection)) else: tweets = (tweets.select_from(model.filtered_tweets).join( model.Tweet).filter(model.Tweet.collection == collection).filter( model.filtered_tweets.c.collection == collection)) tweets = ( tweets.filter(*feature_filter_args) # .filter(model.Tweet.features['filter', 'is_retweet'].astext == 'false') # .filter(model.Tweet.representative == None) ) if query: tweets = search(tweets, query, sort=not count) if story is not None: ts = model.tweet_story.alias() tweets = (tweets.join(ts).order_by(None).order_by(ts.c.rank)) elif cluster is not None: assert clustered_selection_id tweets = (tweets.join(model.tweet_cluster).filter( model.tweet_cluster.c.label == cluster, model.tweet_cluster.c._clustered_selection_id == clustered_selection_id, )) if count: return db.session.scalar( tweets.selectable.with_only_columns([func.count()])) tweets = tweets.order_by(model.Tweet.created_at, model.Tweet.tweet_id) # if possibly_limit and story is None: # tweets = tweets.limit(100) return tweets
def search(self, search_query): """making query to database""" query = session.query(Product) query = search(query, search_query) results = query.limit(20).all() output = [] for item in results: out = { 'value': item.product_name, 'label': item.product_name, 'sku': item.sku } output.append(out) return output
def search_artists(term): wanted_keys = ['page'] query_dict = build_query_dict(request.args.to_dict(), wanted_keys) matches = search(db.session.query(Artist), build_search_query(term), sort=True) if matches is None: return not_found() if 'page' in query_dict: return build_pages(matches, int(query_dict['page']), 'artists') else: matches = matches.all() return sql_json(Artist, *matches)
def index(): page = request.args.get('page', 1, type=int) search_keywords = request.args.get('search', '').strip() query = Post.query.join(Post.author).order_by(Post.created_on.desc()) if search_keywords: query = search(query, search_keywords.replace(' ', ' or ')) pagination = query.paginate(page, per_page=config['POSTS_PER_PAGE'], error_out=False) posts = pagination.items return render_template('main/index.html', posts=posts, pagination=pagination, search_keywords=search_keywords)
def search(self, query: Query): params = self.params search_text = (params['search'].replace('.', ' ').strip() if 'search' in params else None) if search_text: search_by, search_query = self.get_search_query(search_text) search_expression = self.get_search_vector_for_field( query, search_by, search_query) if type(search_expression) is Query: return search_expression else: return search(query, search_query, vector=search_expression) else: return self.sort(query)
def search_results_comments(self, search_query): now = datetime.datetime.utcnow() sql_q = db.session.query(Comment) comments = search(sql_q, search_query.lower()) # The edit lag is the time the user has to edit the comment after the submission. # By deafult, it is 15 minutes, but can be configured by the Admin. edit_lag = g.blog_config.edit_lag() # get the editable comments from the DB, based on their publication date editable_cmts = editable_comments(edit_lag, session) # It won't need pagination for now return render_template('search-results-comments.html', search_query=search_query, comments=comments, editable_comments=editable_cmts, comment_anchor_id=comment_anchor_id, cls=self)
def get_city_by_id(c_id): # FULL CITY MODEL (City,Artist,Album) city_match = db.session.query(City).get(c_id) if city_match is None: return not_found() artist_match = db.session.query(City).filter(City.city_id == c_id).join( cities_artists).join(Artist).with_entities(Artist).all() #news_match = db.session.query(City).filter(City.city_id == c_id).join(cities_artists).join(Artist).join( # articles_artists).with_entities(Article).order_by(Article.date.desc()).all() news_match = search(db.session.query(Article), city_match.name, sort=True).all() json_city = sql_single_serialize(City, city_match) json_artist = sql_serialize(Artist, *artist_match) json_news = sql_serialize(Article, *news_match) final_obj = {"city": json_city, "artists": json_artist, "news": json_news} return jsonify(final_obj)
def browse(page=1): sources, categories = session.get('sources'), session.get('categories') questions = filter_questions(sources, categories).order_by(Question.id) if session.get('search') and len(session['search']) > 0: questions = sqlalchemy_searchable.search(questions, session['search'], sort=True) questions = questions.paginate(page, app.config['QUESTIONS_PER_PAGE'], False) if len(questions.items) < 1: flash('The inputted settings did not match any available questions. Please try again.') session['categories'], session['sources'] = (), () session['search'] = '' questions=filter_questions().order_by(Question.id).paginate(page, app.config['QUESTIONS_PER_PAGE'], False) sources = all_sources if current_user.is_authenticated else free_sources return render_template( 'browse.html', settings=session, sources=sources, categories=all_categories, pagination=questions, questions=[make_public(question, html=True) for question in questions.items] )
def search(self, query: Query): params = self.params search_text = ( params['search'].replace('.', ' ').strip() if 'search' in params else None ) if search_text: search_by, search_query = self.get_search_query(search_text) search_expression = self.get_search_vector_for_field( query, search_by, search_query ) if type(search_expression) is Query: return search_expression else: return search(query, search_query, vector=search_expression) else: return self.sort(query)
def search_users(): """Search for a user by email and return results.""" # Returns users for current user's friend requests received_friend_requests, sent_friend_requests = get_friend_requests(session["current_user"]["user_id"]) # Returns query for current user's friends (not User objects) so add .all() to the end to get list of User objects friends = get_friends(session["current_user"]["user_id"]).all() user_input = request.args.get("q") # Search user's query in users table of db and return all search results search_results = search(db.session.query(User), user_input).all() return render_template("friends_search_results.html", received_friend_requests=received_friend_requests, sent_friend_requests=sent_friend_requests, friends=friends, search_results=search_results)
def search(s_req, num = 1): print("Processing search request", s_req) form = SearchFormProcessor(request.forms.decode()) if request.method == 'POST' and form.validate(): req = form.request.data redirect("/search/" + req) start_time = time.time() query = session.query(Page) sssearch = ss.search(query, s_req).order_by(desc(Page.rank)) global pages_limit pages_to_display = pages_limit current_page = int(num) pages = sssearch.offset(pages_to_display*(current_page-1)).limit(pages_limit) total_pages = sssearch.count() total_time = time.time() - start_time return locals()
def search_posts_with_string(user, search_string, **kwargs): page = kwargs.pop('page', 1) limit = kwargs.pop('limit', 50) posts_query = db.session.query(Post, func.sum(Hot.number)) posts = search(posts_query, search_string)\ .outerjoin(Hot) \ .order_by(Post.date.desc())\ .group_by(Post.id) \ .limit(limit)\ .offset((page - 1) * limit)\ .all() response = [] for i, (post, hot_value) in enumerate(posts): post.hot_value = hot_value if hot_value is not None else 0 post.comments_value = len(post.comments.all()) post.hot_up = user_had_hot_up(post, user) if post.group.active is True and \ post.active is True and \ post.approved == APPROVED and \ user_can_join_group_returning_bool(user, post.group): response.append(post) return response
def test_uses_global_regconfig_as_fallback(self): query = search(self.session.query(self.TextItem.id), 'the') assert query.count() == 1
def test_supports_session_queries(self): query = self.session.query(Order) assert ( '''"order".search_vector @@ to_tsquery('english', :term)''' in str(search(query, 'something')) )
def test_search_specific_columns(self): query = search(self.session.query(self.TextItem.id), 'admin') assert query.count() == 1
def test_schoose_vector(self): query = self.TextItemQuery(self.TextMultiItem, self.session) s1 = search(query, 'ipsum', vector=self.TextMultiItem.name_vector) assert s1.first().name == 'ipsum' s2 = search(query, 'ipsum', vector=self.TextMultiItem.content_vector) assert s2.first().name == 'index'
def get_search(cls, request, searchterm, logged_in_alias, page=1): page_url = PageURL_WebOb(request) query = DBSession.query(Question, Qvote, Images).outerjoin(Images, Images.usralias == Question.askedbyalias).outerjoin(Qvote, and_(Question.questionasker==Qvote.questionasker,Qvote.userid == logged_in_alias)) query = search(query, searchterm) return Page(query, page, url=page_url, items_per_page=20)