Plain is headed towards 1.0! Subscribe for development updates →

 1from plain import signals
 2
 3from .connections import DatabaseConnection
 4from .exceptions import (
 5    ConnectionDoesNotExist,
 6    DatabaseError,
 7    DatabaseErrorWrapper,
 8    DataError,
 9    Error,
10    IntegrityError,
11    InterfaceError,
12    InternalError,
13    NotSupportedError,
14    OperationalError,
15    ProgrammingError,
16)
17
18PLAIN_VERSION_PICKLE_KEY = "_plain_version"
19
20
21db_connection = DatabaseConnection()
22
23
24# Register an event to reset saved queries when a Plain request is started.
25def reset_queries(**kwargs):
26    if db_connection.has_connection():
27        db_connection.queries_log.clear()
28
29
30signals.request_started.connect(reset_queries)
31
32
33# Register an event to reset transaction state and close connections past
34# their lifetime.
35def close_old_connections(**kwargs):
36    if db_connection.has_connection():
37        db_connection.close_if_unusable_or_obsolete()
38
39
40signals.request_started.connect(close_old_connections)
41signals.request_finished.connect(close_old_connections)
42
43
44__all__ = [
45    "db_connection",
46    "PLAIN_VERSION_PICKLE_KEY",
47    "Error",
48    "InterfaceError",
49    "DatabaseError",
50    "DataError",
51    "OperationalError",
52    "IntegrityError",
53    "InternalError",
54    "ProgrammingError",
55    "NotSupportedError",
56    "ConnectionDoesNotExist",
57    "DatabaseErrorWrapper",
58    "reset_queries",
59    "close_old_connections",
60]