def test_lazy_model_operation(self, apps): """ Tests apps.lazy_model_operation(). """ model_classes = [] initial_pending = set(apps._pending_operations) def test_func(*models): model_classes[:] = models class LazyA(models.Model): pass # Test models appearing twice, and models appearing consecutively model_keys = [ ('apps', model_name) for model_name in ['lazya', 'lazyb', 'lazyb', 'lazyc', 'lazya'] ] apps.lazy_model_operation(test_func, *model_keys) # LazyModelA shouldn't be waited on since it's already registered, # and LazyModelC shouldn't be waited on until LazyModelB exists. self.assertSetEqual( set(apps._pending_operations) - initial_pending, {('apps', 'lazyb')}) # Test that multiple operations can wait on the same model apps.lazy_model_operation(test_func, ('apps', 'lazyb')) class LazyB(models.Model): pass self.assertListEqual(model_classes, [LazyB]) # Now we are just waiting on LazyModelC. self.assertSetEqual( set(apps._pending_operations) - initial_pending, {('apps', 'lazyc')}) class LazyC(models.Model): pass # Everything should be loaded - make sure the callback was executed properly. self.assertListEqual(model_classes, [LazyA, LazyB, LazyB, LazyC, LazyA])
def __new__(mcs, name, bases, attrs): from gdpr.loading import purpose_register new_class = super().__new__(mcs, name, bases, attrs) if hasattr(new_class, 'slug') and new_class.slug: if new_class.slug in purpose_register: raise ImproperlyConfigured( 'More anonymization purposes with slug {}'.format( new_class.slug)) purpose_register.register(new_class.slug, new_class) def set_source_model_class(model): new_class.source_model_class = model if isinstance(new_class.source_model_class, str): apps.lazy_model_operation( set_source_model_class, make_model_tuple(new_class.source_model_class)) return new_class
def test_lazy_model_operation(self): """ Tests apps.lazy_model_operation(). """ model_classes = [] initial_pending = set(apps._pending_operations) def test_func(*models): model_classes[:] = models class LazyA(models.Model): pass # Test models appearing twice, and models appearing consecutively model_keys = [('apps', model_name) for model_name in ['lazya', 'lazyb', 'lazyb', 'lazyc', 'lazya']] apps.lazy_model_operation(test_func, *model_keys) # LazyModelA shouldn't be waited on since it's already registered, # and LazyModelC shouldn't be waited on until LazyModelB exists. self.assertSetEqual(set(apps._pending_operations) - initial_pending, {('apps', 'lazyb')}) # Test that multiple operations can wait on the same model apps.lazy_model_operation(test_func, ('apps', 'lazyb')) class LazyB(models.Model): pass self.assertListEqual(model_classes, [LazyB]) # Now we are just waiting on LazyModelC. self.assertSetEqual(set(apps._pending_operations) - initial_pending, {('apps', 'lazyc')}) class LazyC(models.Model): pass # Everything should be loaded - make sure the callback was executed properly. self.assertListEqual(model_classes, [LazyA, LazyB, LazyB, LazyC, LazyA])
def ready(self): if "makemigrations" in argv and not GENERATE_MIGRATIONS: return fields_for_fieldbustier.update( parse_fields_for_fieldbustier( getattr(settings, "ADD_FIELD_DJANGO_FIELDBUSTIER", []))) replace_fields_for_fieldbustier.update( parse_fields_for_fieldbustier( getattr(settings, "REPLACE_FIELD_DJANGO_FIELDBUSTIER", []))) delete_fields_for_fieldbustier.extend( getattr(settings, "DELETE_FIELD_DJANGO_FIELDBUSTIER", [])) for model_key in fields_for_fieldbustier: apps.lazy_model_operation(add_fields_to_model, model_key) for model_key in replace_fields_for_fieldbustier: apps.lazy_model_operation(replace_fields_of_model, model_key) for field_config in delete_fields_for_fieldbustier: model_key = field_config.app_name, field_config.model_klass.lower() apps.lazy_model_operation(delete_fields_of_model, model_key)
Injects custom fields onto the given sender model as defined by the ``EXTRA_MODEL_FIELDS`` setting. This is a closure over the "fields" variable. """ model_key = sender._meta.app_label, sender._meta.model_name for field_name, field in fields.get(model_key, {}): field.contribute_to_class(sender, field_name) if DJANGO_VERSION < (1, 9): if fields: class_prepared.connect(add_extra_model_fields, dispatch_uid="FQFEQ#rfq3r") else: for model_key in fields: apps.lazy_model_operation(add_extra_model_fields, model_key) # Override django.contrib.admin.site with LazyAdminSite. It must # be bound to a separate name (admin_site) for access in autodiscover # below. admin_site = LazyAdminSite() admin.site = admin_site django_autodiscover = admin.autodiscover def autodiscover(*args, **kwargs): """ Replaces django's original autodiscover to add a call to LazyAdminSite's lazy_registration. """
Injects custom fields onto the given sender model as defined by the ``EXTRA_MODEL_FIELDS`` setting. This is a closure over the "fields" variable. """ model_key = sender._meta.app_label, sender._meta.model_name for field_name, field in fields.get(model_key, {}): field.contribute_to_class(sender, field_name) if DJANGO_VERSION < (1, 9): if fields: class_prepared.connect(add_extra_model_fields, dispatch_uid="FQFEQ#rfq3r") else: for model_key in fields: apps.lazy_model_operation(add_extra_model_fields, model_key) # Override django.contrib.admin.site with LazyAdminSite. It must # be bound to a separate name (admin_site) for access in autodiscover # below. admin_site = LazyAdminSite() admin.site = admin_site django_autodiscover = admin.autodiscover def autodiscover(*args, **kwargs): """ Replaces django's original autodiscover to add a call to LazyAdminSite's lazy_registration.