diff --git a/husky_directory/app_config.py b/husky_directory/app_config.py index 2b66c8a8..e76f7e43 100644 --- a/husky_directory/app_config.py +++ b/husky_directory/app_config.py @@ -119,6 +119,7 @@ class SessionSettings(FlaskConfigurationSettings): cookie_name: str = Field( "edu.uw.directory.session", env="SESSION_COOKIE_NAME", flask_config_key="_env" ) + preferences_cookie_name: str = Field("edu.uw.directory.preferences") secret_key: SecretStr = Field(None, env="SECRET_KEY", flask_config_key="_env") lifetime_seconds: int = Field( 600, env="PERMANENT_SESSION_LIFETIME", flask_config_key="_env" diff --git a/husky_directory/blueprints/search.py b/husky_directory/blueprints/search.py index b63c9d19..0017e755 100644 --- a/husky_directory/blueprints/search.py +++ b/husky_directory/blueprints/search.py @@ -2,7 +2,14 @@ from logging import Logger from typing import Optional, Union -from flask import Blueprint, Request, render_template, send_file +from flask import ( + Blueprint, + Request, + Response, + make_response, + render_template, + send_file, +) from inflection import humanize, underscore from injector import Injector, inject, singleton from pydantic import ValidationError @@ -10,6 +17,7 @@ from werkzeug.local import LocalProxy from husky_directory.app_config import ApplicationConfig +from husky_directory.models.common import PreferencesCookie from husky_directory.models.search import ( DirectoryBaseModel, Person, @@ -56,9 +64,20 @@ def __init__(self, logger: Logger, injector: Injector): ) @staticmethod - def index(session: LocalProxy, settings: ApplicationConfig): - context = RenderingContext( - uwnetid=session.get("uwnetid"), show_experimental=settings.show_experimental + def index(request: Request, session: LocalProxy, settings: ApplicationConfig): + preferences_cookie = request.cookies.get( + settings.session_settings.preferences_cookie_name + ) + if preferences_cookie: + preferences = PreferencesCookie.parse_raw(preferences_cookie) + else: + preferences = PreferencesCookie() + context = RenderingContext.construct( + uwnetid=session.get("uwnetid"), + show_experimental=settings.show_experimental, + request_input=SearchDirectoryFormInput.construct( + render_length=preferences.result_detail + ), ) return ( render_template("views/index.html", **context.dict(exclude_none=True)), @@ -144,9 +163,16 @@ def search_listing( logger.exception(str(e)) SearchBlueprint.handle_search_exception(e, context) finally: - return ( + 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) + response.set_cookie( + settings.session_settings.preferences_cookie_name, value=preferences + ) + return response diff --git a/husky_directory/models/common.py b/husky_directory/models/common.py index 38f82552..80758514 100644 --- a/husky_directory/models/common.py +++ b/husky_directory/models/common.py @@ -12,6 +12,8 @@ from pydantic import BaseModel, validator +from husky_directory.models.enum import ResultDetail + class UWDepartmentRole(BaseModel): """Denotes that an identity has some role within the UW (e.g., a job title, or class level).""" @@ -129,3 +131,14 @@ def validate_namespace(cls, v: Union[str, RecordNamespace]) -> RecordNamespace: if not isinstance(v, RecordNamespace): return RecordNamespace.validate(v) return v + + +class PreferencesCookie(BaseModel): + class Config: + use_enum_values = True + + result_detail: ResultDetail = ResultDetail.summary + + @property + def result_detail_enum(self) -> ResultDetail: + return ResultDetail(self.result_detail) diff --git a/husky_directory/templates/search.html b/husky_directory/templates/search.html index 54867ac9..81de3d8d 100644 --- a/husky_directory/templates/search.html +++ b/husky_directory/templates/search.html @@ -11,7 +11,7 @@

Search by