def handle(self, *args, **options): self.trampoline_config = get_trampoline_config() try: self.parse_options(**options) self.run(*args, **options) except Exception as exc: if options.get('traceback', False): traceback.print_exc() self.print_error(repr(exc)) sys.exit(1)
def handle(self, *args, **options): self.trampoline_config = get_trampoline_config() try: self.parse_options(**options) self.run(*args, **options) except Exception as exc: if options.get('traceback', False): traceback.print_exc() self.print_error(repr(exc)) sys.exit(1)
def test_es_delete(self): # Asynchronous call. token = Token.objects.create(name='token') self.assertDocExists(token) token.es_delete() self.assertDocDoesntExist(Token, token.pk) # Synchronous call. token = Token.objects.create(name='token') self.assertDocExists(token) token.es_delete(async=False) self.assertDocDoesntExist(Token, token.pk) # Fail silently if document doesn't exist. token.es_delete() from trampoline import get_trampoline_config trampoline_config = get_trampoline_config() # Fake delete to raise exception. backup_delete = trampoline_config.connection.delete def delete_raise_exception(*args, **kwargs): raise RuntimeError trampoline_config.connection.delete = delete_raise_exception # Fail silently token.es_delete() # Hard fail. settings.TRAMPOLINE['OPTIONS']['fail_silently'] = False with self.assertRaises(RuntimeError): token.es_delete() settings.TRAMPOLINE['OPTIONS']['fail_silently'] = True trampoline_config.connection.delete = backup_delete
""" Mixins for trampoline. """ from django.contrib.contenttypes.models import ContentType from trampoline import get_trampoline_config from trampoline.tasks import es_delete_doc from trampoline.tasks import es_index_object trampoline_config = get_trampoline_config() class ESIndexableMixin(object): """ Provide the required methods and attributes to index django models. """ es_doc_type = None es_auto_doc_type_mapping = False @classmethod def get_indexable_queryset(cls): # pragma: no cover return cls._default_manager.all() @classmethod def get_es_doc_type(cls): # pragma: no cover return cls.es_doc_type def is_indexable(self): return True def is_index_update_needed(self):
""" Test management commands for trampoline. """ from django.core.management import call_command from elasticsearch_dsl import Index from tests.base import BaseTestCase from tests.models import Token from trampoline import get_trampoline_config trampoline_config = get_trampoline_config() class TestCommands(BaseTestCase): def tearDown(self): super(TestCommands, self).tearDown() # Delete remnants of previous tests. Index('foobar').delete(ignore=404) Index('foobar_target').delete(ignore=404) def test_es_create_index(self): # Index name required. with self.assertRaises(SystemExit): call_command('es_create_index') # Index name isn't defined. with self.assertRaises(SystemExit): call_command('es_create_index', index_name='doesntexist') # Dry run.
def begin(self): self.trampoline_config = get_trampoline_config() if not self.trampoline_config.is_disabled: for index_name in self.trampoline_config.indices: self.patch_doc_types(index_name)