diff --git a/husky_directory/blueprints/search.py b/husky_directory/blueprints/search.py index b9b2c99..6e9537d 100644 --- a/husky_directory/blueprints/search.py +++ b/husky_directory/blueprints/search.py @@ -139,6 +139,38 @@ def handle_search_exception(e: Exception, context: RenderingContext): "email help@uw.edu describing your problem." ) + @staticmethod + def set_preferences_for_cookie(context: RenderingContext): + """ + When the search query is too short, the request_input data is dropped, but we still need a value for + 'result_detail' + """ + result_detail = "summary" + if context.request_input and context.request_input.length: + result_detail = context.request_input.length + + preferences = PreferencesCookie(result_detail=result_detail).json( + exclude_unset=True, exclude_none=True + ) + return preferences + + def check_context(self, context: RenderingContext, request: Request): + if context.request_input: + return context + else: + context.request_input = SearchDirectoryFormInput( + method=request.form["method"], + population=request.form["population"], + length=request.form["length"], + person_href=None, + render_method=request.form["method"], + render_query="", + render_population=request.form["population"], + render_length=request.form["length"], + include_test_identities=False, + ) + return context + def search_listing( self, request: Request, @@ -160,15 +192,15 @@ def search_listing( self.logger.exception(str(e)) SearchBlueprint.handle_search_exception(e, context) finally: + context = self.check_context(context, request) response: Response = make_response( render_template( "views/search_results.html", **context.dict(exclude_none=True) ), context.status_code, ) - preferences = PreferencesCookie( - result_detail=context.request_input.length - ).json(exclude_unset=True, exclude_none=True) + + preferences = self.set_preferences_for_cookie(context) response.set_cookie( settings.session_settings.preferences_cookie_name, value=preferences ) diff --git a/husky_directory/templates/search.html b/husky_directory/templates/search.html index 81de3d8..fb2dadc 100644 --- a/husky_directory/templates/search.html +++ b/husky_directory/templates/search.html @@ -12,7 +12,8 @@

Search by

value="{{ search_value }}" style="color:black; height:43px; z-index:0;" id="query" {{ 'autofocus' if search_value is blank }} - name="query"> + name="query" + >