Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

__init__.py 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. from django.core import signals
  2. from django.db.utils import (
  3. DEFAULT_DB_ALIAS, DJANGO_VERSION_PICKLE_KEY, ConnectionHandler,
  4. ConnectionRouter, DatabaseError, DataError, Error, IntegrityError,
  5. InterfaceError, InternalError, NotSupportedError, OperationalError,
  6. ProgrammingError,
  7. )
  8. __all__ = [
  9. 'connection', 'connections', 'router', 'DatabaseError', 'IntegrityError',
  10. 'InternalError', 'ProgrammingError', 'DataError', 'NotSupportedError',
  11. 'Error', 'InterfaceError', 'OperationalError', 'DEFAULT_DB_ALIAS',
  12. 'DJANGO_VERSION_PICKLE_KEY',
  13. ]
  14. connections = ConnectionHandler()
  15. router = ConnectionRouter()
  16. # DatabaseWrapper.__init__() takes a dictionary, not a settings module, so we
  17. # manually create the dictionary from the settings, passing only the settings
  18. # that the database backends care about.
  19. # We load all these up for backwards compatibility, you should use
  20. # connections['default'] instead.
  21. class DefaultConnectionProxy:
  22. """
  23. Proxy for accessing the default DatabaseWrapper object's attributes. If you
  24. need to access the DatabaseWrapper object itself, use
  25. connections[DEFAULT_DB_ALIAS] instead.
  26. """
  27. def __getattr__(self, item):
  28. return getattr(connections[DEFAULT_DB_ALIAS], item)
  29. def __setattr__(self, name, value):
  30. return setattr(connections[DEFAULT_DB_ALIAS], name, value)
  31. def __delattr__(self, name):
  32. return delattr(connections[DEFAULT_DB_ALIAS], name)
  33. def __eq__(self, other):
  34. return connections[DEFAULT_DB_ALIAS] == other
  35. connection = DefaultConnectionProxy()
  36. # Register an event to reset saved queries when a Django request is started.
  37. def reset_queries(**kwargs):
  38. for conn in connections.all():
  39. conn.queries_log.clear()
  40. signals.request_started.connect(reset_queries)
  41. # Register an event to reset transaction state and close connections past
  42. # their lifetime.
  43. def close_old_connections(**kwargs):
  44. for conn in connections.all():
  45. conn.close_if_unusable_or_obsolete()
  46. signals.request_started.connect(close_old_connections)
  47. signals.request_finished.connect(close_old_connections)