<s> from pyramid . session import SignedCookieSessionFactory <EOL> from h . security import derive_key <EOL> def model ( request ) : <EOL> session = { } <EOL> session [ '<STR_LIT>' ] = request . session . get_csrf_token ( ) <EOL> session [ '<STR_LIT>' ] = request . authenticated_userid <EOL> session [ '<STR_LIT>' ] = _current_groups ( request ) <EOL> session [ '<STR_LIT>' ] = request . feature . all ( ) <EOL> session [ '<STR_LIT>' ] = { } <EOL> user = request . authenticated_user <EOL> if user and not user . sidebar_tutorial_dismissed : <EOL> session [ '<STR_LIT>' ] [ '<STR_LIT>' ] = True <EOL> return session <EOL> def pop_flash ( request ) : <EOL> return { k : request . session . pop_flash ( k ) <EOL> for k in [ '<STR_LIT:error>' , '<STR_LIT:info>' , '<STR_LIT>' , '<STR_LIT:success>' ] } <EOL> def _group_sort_key ( group ) : <EOL> """<STR_LIT>""" <EOL> return ( group . name . lower ( ) , group . pubid ) <EOL> def _current_groups ( request ) : <EOL> """<STR_LIT>""" <EOL> groups = [ <EOL> { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:id>' : '<STR_LIT>' , '<STR_LIT>' : True } , <EOL> ] <EOL> userid = request . authenticated_userid <EOL> if userid is None : <EOL> return groups <EOL> user = request . authenticated_user <EOL> if user is None : <EOL> return groups <EOL> for group in sorted ( user . groups , key = _group_sort_key ) : <EOL> groups . append ( { <EOL> '<STR_LIT:name>' : group . name , <EOL> '<STR_LIT:id>' : group . pubid , <EOL> '<STR_LIT:url>' : request . route_url ( '<STR_LIT>' , <EOL> pubid = group . pubid , <EOL> slug = group . slug ) , <EOL> } ) <EOL> return groups <EOL> def includeme ( config ) : <EOL> registry = config . registry <EOL> settings = registry . settings <EOL> session_secret = derive_key ( settings [ '<STR_LIT>' ] , b'<STR_LIT>' ) <EOL> session_factory = SignedCookieSessionFactory ( session_secret , httponly = True ) <EOL> config . set_session_factory ( session_factory ) </s>
