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.

connections.py 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. from threading import local
  2. from django.core.mail import get_connection
  3. from .settings import get_backend
  4. # Copied from Django 1.8's django.core.cache.CacheHandler
  5. class ConnectionHandler(object):
  6. """
  7. A Cache Handler to manage access to Cache instances.
  8. Ensures only one instance of each alias exists per thread.
  9. """
  10. def __init__(self):
  11. self._connections = local()
  12. def __getitem__(self, alias):
  13. try:
  14. return self._connections.connections[alias]
  15. except AttributeError:
  16. self._connections.connections = {}
  17. except KeyError:
  18. pass
  19. try:
  20. backend = get_backend(alias)
  21. except KeyError:
  22. raise KeyError('%s is not a valid backend alias' % alias)
  23. connection = get_connection(backend)
  24. connection.open()
  25. self._connections.connections[alias] = connection
  26. return connection
  27. def all(self):
  28. return getattr(self._connections, 'connections', {}).values()
  29. def close(self):
  30. for connection in self.all():
  31. connection.close()
  32. connections = ConnectionHandler()