def index(): """ URLs: - /search Function: - Display the search box - Display search results """ # Create the search form to put at the top of the page. form = search_form(submit_button='Search') # Set the title of the page. response.title = 'Search' # Process the form, keeping the text in the search box to remind the # user what they searched for. if form.process(keepvalues=True).accepted: # If a user is logged in, show results from their own collection, # including private comics, at the top of the page. Also, exclude # that user's results from the second section, which searches all # other users' comics. if auth.user_id is not None: my_results = db_access.search(form.vars.search_term, only_user=auth.user_id, include_private=True) other_results = db_access.search(form.vars.search_term, except_user=auth.user_id, include_private=False) # If no user is logged in, search across all users' public comics. else: my_results = None other_results = db_access.search(form.vars.search_term, include_private=False) else: my_results = None other_results = None # Add owner information to each comic. if my_results is not None: my_results = [(comic, db_access.get_user(comic.owner_id)) for comic in my_results] if other_results is not None: other_results = [(comic, db_access.get_user(comic.owner_id)) for comic in other_results] return dict(form=form, my_results=my_results, other_results=other_results)
def results(): """ Display results from the user's search. """ # Set the title of the page. response.title = 'Search Results' # Get the user input. user_input = { 'search_term': request.vars.item_name, 'category': request.vars.category, 'owner_name': request.vars.owner_name, 'value_from': request.vars.value_min, 'value_to': request.vars.value_max, 'include_owned': (request.vars.list_type == 'owned' or request.vars.list_type == 'both'), 'include_wishlist': (request.vars.list_type == 'wishlist' or request.vars.list_type == 'both') } # Fancy manipulation so only values that are set are actually passed in. # Basically, look at user_input, and create a new dictionary called filters # where each item in user_input is only copied across if it's not None and # it's not an empty string. filters = {key: user_input[key] for key in user_input if (user_input[key] is not None and user_input[key] != '')} # Convert value_from and value_to into numbers. If they typed something that's # not a number, remove the value from filters. if 'value_from' in filters: try: filters['value_from'] = float(filters['value_from']) except: del filters['value_from'] if 'value_to' in filters: try: filters['value_to'] = float(filters['value_to']) except: del filters['value_to'] # Now splat filters into db_access.search(). This takes each item in filters # and turns it into an argument to search(). eg if filters contains # {search_term: 'hi'} # then this will call # db_access.search(search_term='hi') # Look up "python kwargs" on Google if you still don't get it. results = db_access.search(db, auth.user_id, **filters) return dict(results=results, user_input=user_input, filters=filters)