common.py 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. from mongoengine.errors import NotRegistered
  2. __all__ = ('UPDATE_OPERATORS', 'get_document', '_document_registry')
  3. UPDATE_OPERATORS = {'set', 'unset', 'inc', 'dec', 'mul',
  4. 'pop', 'push', 'push_all', 'pull',
  5. 'pull_all', 'add_to_set', 'set_on_insert',
  6. 'min', 'max', 'rename'}
  7. _document_registry = {}
  8. def get_document(name):
  9. """Get a document class by name."""
  10. doc = _document_registry.get(name, None)
  11. if not doc:
  12. # Possible old style name
  13. single_end = name.split('.')[-1]
  14. compound_end = '.%s' % single_end
  15. possible_match = [k for k in _document_registry
  16. if k.endswith(compound_end) or k == single_end]
  17. if len(possible_match) == 1:
  18. doc = _document_registry.get(possible_match.pop(), None)
  19. if not doc:
  20. raise NotRegistered("""
  21. `%s` has not been registered in the document registry.
  22. Importing the document class automatically registers it, has it
  23. been imported?
  24. """.strip() % name)
  25. return doc