def ingrd(): """ Search an Ingredient and show results""" form = SearchForm(request.form) results = None if request.method == "POST": if form.validate_on_submit(): ingredient = form.name.data results = like_ingredient_name(ingredient) return render_template('search/results.html', var={ 'form': form, 'results': results })
def search(request): form = SearchForm(request.GET, request.FILES) # Simple validation if form.is_valid(): # Break down query terms qterms = query_terms(form.cleaned_data["q"]) cq = None iq = None # Builds a complex database query based on search terms for term in qterms: # Customer fullname LIKE [any qterm] cq_inc = ( Q(last_name__icontains=term) | Q(first_name__icontains=term) | Q(email__icontains=term) | Q(student_id__icontains=term) ) # Item {name, owner} LIKE [any qterm] OR label = [any qterm] iq_inc = ( Q(name__icontains=term) | Q(label_id__icontains=term) | Q(owner__first_name__icontains=term) | Q(owner__last_name__icontains=term) ) if cq is None: # implies iq = None cq = cq_inc iq = iq_inc else: cq = cq | cq_inc iq = iq | iq_inc # Fetches data from database for response customers = [ {"key": customer.pk, "id": customer.student_id, "name": customer.full_name, "extra": customer.email} for customer in Customer.objects.filter(cq) ] items = [ {"key": item.pk, "id": item.label_id, "name": item.name, "extra": item.get_type_display()} for item in Item.objects.filter(iq) ] context = {"items": items, "customers": customers} if "t" in request.GET and request.GET["t"] == "json": return context else: return render_to_response("search/results.html", context, context_instance=RequestContext(request)) else: # Returns the form; will not be used aside from testing return render_to_response("search/search.html", {"form": form}, context_instance=RequestContext(request))
def search(): form = SearchForm() if form.validate_on_submit(): name = sp.search_artist(form.artist_name.data)['name'] genres = sp.search_artist(form.artist_name.data)['genres'] popularity = sp.search_artist(form.artist_name.data)['popularity'] followers = sp.search_artist(form.artist_name.data)['followers'] fav_art = FavArtist(name=name, genres=str(genres), followers=followers, popularity=popularity, artist=current_user) db.session.add(fav_art) db.session.commit() #db.drop_all() flash('added artist info', 'success') return redirect( url_for('search.searchresult', name=name, genres=genres, popularity=popularity, followers=followers)) return render_template('search.html', form=form)
def inject_search_form(): # inject search form to all templates return dict(search_form=SearchForm())
def before_request(): if current_user.is_authenticated: current_user.last_seen = datetime.utcnow() db.session.commit() g.search_form = SearchForm()