Coverage for /home/runner/work/viur-core/viur-core/viur/src/viur/core/render/html/env/session.py: 0%

17 statements  

« prev     ^ index     » next       coverage.py v7.6.3, created at 2024-10-16 22:16 +0000

1import typing as t 

2 

3from viur.core.render.html.utils import jinjaGlobalFunction 

4from viur.core import current 

5from ..default import Render 

6 

7 

8@jinjaGlobalFunction 

9def getSession(render: Render) -> dict: 

10 """ 

11 Jinja2 global: Allows templates to store variables server-side inside the session. 

12 

13 Note: This is done in a separated part of the session for security reasons. 

14 

15 :returns: A dictionary of session variables. 

16 """ 

17 currSess = current.session.get() 

18 if not currSess.get("JinjaSpace"): 

19 currSess["JinjaSpace"] = {} 

20 return currSess.get("JinjaSpace") 

21 

22 

23@jinjaGlobalFunction 

24def setSession(render: Render, name: str, value: str) -> None: 

25 """ 

26 Jinja2 global: Allows templates to store variables on server-side inside the session. 

27 

28 Note: This is done in a separated part of the session for security reasons. 

29 

30 :param render: The html-renderer instance. 

31 :param name: Name of the key 

32 :param value: Value to store with name. 

33 """ 

34 sessionData = getSession(render) 

35 sessionData[name] = value 

36 currSess = current.session.get() 

37 currSess["JinjaSpace"] = sessionData 

38 currSess.markChanged()