Coverage for .tox/coverage/lib/python3.11/site-packages/wuttaweb/db/sess.py: 100%

4 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-08-27 21:18 -0500

1# -*- coding: utf-8; -*- 

2################################################################################ 

3# 

4# wuttaweb -- Web App for Wutta Framework 

5# Copyright © 2024 Lance Edgar 

6# 

7# This file is part of Wutta Framework. 

8# 

9# Wutta Framework is free software: you can redistribute it and/or modify it 

10# under the terms of the GNU General Public License as published by the Free 

11# Software Foundation, either version 3 of the License, or (at your option) any 

12# later version. 

13# 

14# Wutta Framework is distributed in the hope that it will be useful, but 

15# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 

16# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 

17# more details. 

18# 

19# You should have received a copy of the GNU General Public License along with 

20# Wutta Framework. If not, see <http://www.gnu.org/licenses/>. 

21# 

22################################################################################ 

23""" 

24Database sessions for web app 

25 

26The web app uses a different database session than other 

27(e.g. console) apps. The web session is "registered" to the HTTP 

28request/response life cycle (aka. transaction) such that the session 

29is automatically rolled back on error, and automatically committed if 

30the response is finalized without error. 

31 

32.. class:: Session 

33 

34 Primary database session class for the web app. 

35 

36 Note that you often do not need to "instantiate" this session, and 

37 can instead call methods directly on the class:: 

38 

39 from wuttaweb.db import Session 

40 

41 users = Session.query(model.User).all() 

42 

43 However in certain cases you may still want/need to instantiate it, 

44 e.g. when passing a "true/normal" session to other logic. But you 

45 can always call instance methods as well:: 

46 

47 from wuttaweb.db import Session 

48 from some_place import some_func 

49 

50 session = Session() 

51 

52 # nb. assuming func does not expect a "web" session per se, pass instance 

53 some_func(session) 

54 

55 # nb. these behave the same (instance vs. class method) 

56 users = session.query(model.User).all() 

57 users = Session.query(model.User).all() 

58""" 

59 

60from sqlalchemy import orm 

61from zope.sqlalchemy.datamanager import register 

62 

63 

64Session = orm.scoped_session(orm.sessionmaker()) 

65 

66register(Session)