Skip to content

Instantly share code, notes, and snippets.

@rainiera
Created May 3, 2017 22:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rainiera/dca8c0e8ab21b7c274092b5a3eb43572 to your computer and use it in GitHub Desktop.
Save rainiera/dca8c0e8ab21b7c274092b5a3eb43572 to your computer and use it in GitHub Desktop.
enable user/proposal search v1

Since typeahead.js is easiest to use when the list of strings to match with has been pre-populated, and we're not going to have 1000 proposals or users at any one University's instance of Enable, we are going to pre-populate the list for typeahead.js by making a DB request upon every render_template. Once you finish this, I can implement a global in-memory cache of the Enable users and proposals list, but with little time to build and smaller traffic, this iteration of the search bar can require a DB request per call for now.

Make a function that returns a list of both user.username along with their user.profile.name AND proposal.title, for every user in User and for every proposal in Proposal. You'd have to put from app.proposal.models import Proposal and from app.profile.models import User, and make a DB call. In fact, just read enable/app/proposal/views:12 to see the function I'm talking about.

  • Follow this StackOverflow answer to be able to call this function in the base template emable/app/templates/base.html and

  • populate a JavaScript array for typeahead.js to use (trace the code happening in enable/app/templates/proposal/create.html:46,37,15 to understand what I'm talking about).

  • Also figure out a way to put an <a href for each proposal and user in the dropdown that's produced by typeahead.

  • Put the search box with the typeahead class somewhere in the nav element of base.html

  • Have a check in the base template for the user permissions (such that anyone logged in can perform the search) AND also that the create/view proposal templates aren't the ones being rendered at this endpoint, because I'm not sure what would happen with multiple typeahead instances in the same template.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment