def _get_searchform_context(request, log_id): """ Returns correct context for GET. Called by results() """ log.debug('_get_searchform_context() starting') if not u'authz_info' in request.session: request.session[u'authz_info'] = {u'authorized': False} # form = SearchForm() # an unbound form form = old_forms.SearchForm() # an unbound form log.debug('form, `%s`' % repr(form)) # place_field_object = form.fields['place'] # place_field_object.choices = [(item, item) for item in sorted( common.facetResults('placeMenu').keys()) if item] # form.fields['place'] = place_field_object context = { u'form': form, u'session_authz_info': request.session[u'authz_info'], u'settings_app': settings_app, u'admin_links': common.make_admin_links( session_authz_dict=request.session[u'authz_info'], url_host=request.get_host(), log_id=log_id) } log.debug('context, ```%s```' % pprint.pformat(context)) return context
def _get_results_context(request, log_id): """ Returns correct context for POST. Called by results() """ log.debug('starting') context = {} request.encoding = u'utf-8' form = old_forms.SearchForm( request.POST) # form bound to the POST data resultsPage = 1 qstring_provided = None if request.method == u'GET': qstring_provided = request.GET.get("q", None) resultsPage = int(request.GET.get('resultsPage', resultsPage)) if form.is_valid() or qstring_provided: initial_qstring = "" if qstring_provided: initial_qstring = qstring_provided else: initial_qstring = form.generateSolrQuery() updated_qstring = common.updateQstring( initial_qstring=initial_qstring, session_authz_dict=request.session['authz_info'], log_id=common.get_log_identifier( request.session))['modified_qstring'] context = common.paginateRequest(qstring=updated_qstring, resultsPage=resultsPage, log_id=common.get_log_identifier( request.session)) log.debug('context, ```%s```' % pprint.pformat(context)) context[u'session_authz_info'] = request.session[u'authz_info'] context[u'admin_links'] = common.make_admin_links( session_authz_dict=request.session[u'authz_info'], url_host=request.get_host(), log_id=log_id) context[u'initial_qstring'] = initial_qstring log.debug('context.keys(), ```%s```' % pprint.pformat(sorted(context.keys()))) log.debug('type(context), `%s`' % type(context)) # results = context['iipResult'] # log.debug( 'type(results), `%s`' % type(results) ) # for (i, result) in enumerate(results.object_list): # log.debug( 'type(result), `%s`' % type(result) ) # log.debug( 'result, `%s`' % result ) # if i > 0: # break # 1/0 return context
def old_results(request): """ Handles /results/ GET, POST, and ajax-GET. """ def _get_results_context(request, log_id): """ Returns correct context for POST. Called by results() """ log.debug('starting') context = {} request.encoding = u'utf-8' form = old_forms.SearchForm( request.POST) # form bound to the POST data resultsPage = 1 qstring_provided = None if request.method == u'GET': qstring_provided = request.GET.get("q", None) resultsPage = int(request.GET.get('resultsPage', resultsPage)) if form.is_valid() or qstring_provided: initial_qstring = "" if qstring_provided: initial_qstring = qstring_provided else: initial_qstring = form.generateSolrQuery() updated_qstring = common.updateQstring( initial_qstring=initial_qstring, session_authz_dict=request.session['authz_info'], log_id=common.get_log_identifier( request.session))['modified_qstring'] context = common.paginateRequest(qstring=updated_qstring, resultsPage=resultsPage, log_id=common.get_log_identifier( request.session)) log.debug('context, ```%s```' % pprint.pformat(context)) context[u'session_authz_info'] = request.session[u'authz_info'] context[u'admin_links'] = common.make_admin_links( session_authz_dict=request.session[u'authz_info'], url_host=request.get_host(), log_id=log_id) context[u'initial_qstring'] = initial_qstring log.debug('context.keys(), ```%s```' % pprint.pformat(sorted(context.keys()))) log.debug('type(context), `%s`' % type(context)) # results = context['iipResult'] # log.debug( 'type(results), `%s`' % type(results) ) # for (i, result) in enumerate(results.object_list): # log.debug( 'type(result), `%s`' % type(result) ) # log.debug( 'result, `%s`' % result ) # if i > 0: # break # 1/0 return context def _get_ajax_unistring(request): """ Returns unicode string based on ajax update. Called by results() """ log_id = common.get_log_identifier(request.session) log.info('id, `%s`; starting' % log_id) initial_qstring = request.GET.get(u'qstring', u'*:*') updated_qstring = common.updateQstring(initial_qstring, request.session[u'authz_info'], log_id)[u'modified_qstring'] resultsPage = int(request.GET[u'resultsPage']) context = common.paginateRequest(qstring=updated_qstring, resultsPage=resultsPage, log_id=log_id) return_str = ajax_snippet.render_block_to_string( u'iip_search_templates/old_results.html', u'content', context) return unicode(return_str) def _get_searchform_context(request, log_id): """ Returns correct context for GET. Called by results() """ log.debug('_get_searchform_context() starting') if not u'authz_info' in request.session: request.session[u'authz_info'] = {u'authorized': False} # form = SearchForm() # an unbound form form = old_forms.SearchForm() # an unbound form log.debug('form, `%s`' % repr(form)) # place_field_object = form.fields['place'] # place_field_object.choices = [(item, item) for item in sorted( common.facetResults('placeMenu').keys()) if item] # form.fields['place'] = place_field_object context = { u'form': form, u'session_authz_info': request.session[u'authz_info'], u'settings_app': settings_app, u'admin_links': common.make_admin_links( session_authz_dict=request.session[u'authz_info'], url_host=request.get_host(), log_id=log_id) } log.debug('context, ```%s```' % pprint.pformat(context)) return context log_id = common.get_log_identifier(request.session) log.info('id, `%s`; starting' % log_id) if not u'authz_info' in request.session: request.session[u'authz_info'] = {u'authorized': False} if request.method == u'POST': # form has been submitted by user log.debug('POST, search-form was submitted by user') request.encoding = u'utf-8' form = old_forms.SearchForm(request.POST) if not form.is_valid(): log.debug('form not valid, redirecting') redirect_url = '%s://%s%s?q=*:*' % ( request.META[u'wsgi.url_scheme'], request.get_host(), reverse(u'results_url')) log.debug('redirect_url for non-valid form, ```%s```' % redirect_url) return HttpResponseRedirect(redirect_url) qstring = form.generateSolrQuery() # e.g. http://library.brown.edu/cds/projects/iip/results?q=*:* redirect_url = '%s://%s%s?q=%s' % (request.META[u'wsgi.url_scheme'], request.get_host(), reverse(u'results_url'), qstring) log.debug('redirect_url for valid form, ```%s```' % redirect_url) return HttpResponseRedirect(redirect_url) if request.method == u'GET' and request.GET.get(u'q', None) != None: log.debug('GET, with params, hit solr and show results') return render(request, u'iip_search_templates/old_results.html', _get_results_context(request, log_id)) elif request.is_ajax(): # user has requested another page, a facet, etc. log.debug('request.is_axax() is True') return HttpResponse(_get_ajax_unistring(request)) else: # regular GET, no params log.debug('GET, no params, show search form') return render(request, u'iip_search_templates/search_form.html', _get_searchform_context(request, log_id))