text: '''
  A forms system
  
  Build a form template, build a handler for its submission, receive data from end users
  '''
  
  import json
  
  from flask import Blueprint, request, abort, make_response, render_template, flash, redirect, url_for
  from flask.ext.login import current_user
  
  from portality.core import app
  
  import portality.models as models
  
  
  blueprint = Blueprint('forms', __name__)
  
  
  # a forms overview page at the top level, can list forms or say whatever needs said about forms, or catch closed forms
  @blueprint.route('/')
  def intro():
      # can add in any auth or admin redirection to closed here if necessary
      return render_template('forms/index.html')
          
  
  # a generic form closed page
  @blueprint.route('/closed')
  def closed():
      return render_template('forms/closed.html')
  
  
  # a generic form completion confirmation page
  @blueprint.route('/complete')
  def complete():
      return render_template('forms/complete.html')
  
  
  # form handling endpoint, by form name - define more for each form required
  @blueprint.route('/<ftype>', methods=['GET','POST'])
  def form(ftype='record'):
  
      # for forms requiring auth, add an auth check here
  
      klass = getattr(models, ftype[0].capitalize() + ftype[1:] )
      
      if request.method == 'GET':
          # TODO: if people are logged in it may be necessary to render a form with previously submitted data
          response = make_response(
              render_template(
                  'forms/template.html', 
                  selections={
                      "records": dropdowns(ftype)
                  },
                  data={} # if this form renders an object in the database, call it and pass it to the template here
              )
          )
          response.headers['Cache-Control'] = 'public, no-cache, no-store, max-age=0'
          response.headers['Pragma'] = 'no-cache'
          return response
  
      if request.method == 'POST':
          # call whatever sort of model this form is for
          f = klass()
          try:
              # may be useful to define a save from form method for said model
              f.save_from_form(request)
          except:
              # else default behavious is just to overwrite the record
              # you probably want at least some validation here
              for k, v in request.values.items():
                  if k not in ['submit']:
                      f.data[k] = v
              f.save()
          return redirect(url_for('.complete'))
  
  
  # get dropdown info required for the form
  def dropdowns(model,key=['name']):
      qry = {
          'query':{'match_all':{}},
          'size': 0,
          'facets':{}
      }
      if not isinstance(key,list):
          key = [key]
      for k in key:
          qry['facets'][k] = {"terms":{"field":k.replace(app.config['FACET_FIELD'],'')+app.config['FACET_FIELD'],"order":'term', "size":100000}}
      vals = []
      try:
          klass = getattr(models, model[0].capitalize() + model[1:] )
          r = klass().query(q=qry)
          for k in key:
              vals = vals + [i.get('term','') for i in r.get('facets',{}).get(k,{}).get("terms",[])]
          return vals
      except:
          return vals
  
  
  
  