def tearDown(self): """Tears down the tests.""" # Unregister the model. reversion.unregister(self.model) # Clear the database. Revision.objects.all().delete() self.model.objects.all().delete()
def tearDown(self): """Tears down the tests.""" # Unregister the model. reversion.unregister(TestModel) # Clear the database. Version.objects.all().delete() TestModel.objects.all().delete()
def check_deregistration(self, test_model): # Unregister the model and text. reversion.unregister(test_model) self.assertFalse(reversion.is_registered(test_model)) self.assertRaises(RegistrationError, lambda: reversion.unregister(test_model)) self.assertTrue(test_model not in reversion.get_registered_models()) self.assertRaises(RegistrationError, lambda: isinstance(reversion.get_adapter(test_model)))
def setUp(self): # Unregister all registered models. self.initial_registered_models = [] for registered_model in reversion.get_registered_models(): self.initial_registered_models.append((registered_model, reversion.get_adapter(registered_model).__class__)) reversion.unregister(registered_model) # Register the test models. reversion.register(ReversionTestModel1) reversion.register(ReversionTestModel2) # Create some test data. self.test11 = ReversionTestModel1.objects.create( name = "model1 instance1 version1", ) self.test12 = ReversionTestModel1.objects.create( name = "model1 instance2 version1", ) self.test21 = ReversionTestModel2.objects.create( name = "model2 instance1 version1", ) self.test22 = ReversionTestModel2.objects.create( name = "model2 instance2 version1", ) self.user = User() if CUSTOM_USER_MODEL: setattr(self.user, User.USERNAME_FIELD, '1') else: self.user.username = "******"
def tearDown(self): super(AdminRegisterInlineTest, self).tearDown() if reversion.is_registered(TestModelInline): reversion.unregister(TestModelInline) if reversion.is_registered(TestModelGenericInline): reversion.unregister(TestModelGenericInline) admin.site.unregister(TestModelParent)
def tearDown(self): """Deletes the versioned site model.""" reversion.unregister(Site) self.site.delete() Version.objects.all().delete()
def setUp(self): # Unregister all registered models. self.initial_registered_models = [] for registered_model in reversion.get_registered_models(): self.initial_registered_models.append( (registered_model, reversion.get_adapter(registered_model).__class__)) reversion.unregister(registered_model) # Register the test models. reversion.register(ReversionTestModel1) reversion.register(ReversionTestModel2) reversion.register(ReversionTestModel3, eager_signals=[pre_delete]) # Create some test data. self.test11 = ReversionTestModel1.objects.create( name="model1 instance1 version1", ) self.test12 = ReversionTestModel1.objects.create( name="model1 instance2 version1", ) self.test21 = ReversionTestModel2.objects.create( name="model2 instance1 version1", ) self.test22 = ReversionTestModel2.objects.create( name="model2 instance2 version1", ) self.test31 = ReversionTestModel3.objects.create( name="model3 instance1 version1", ) self.test32 = ReversionTestModel3.objects.create( name="model3 instance2 version1", ) self.user = User.objects.create(username="******", )
def testEagerRegistration(self): # Register the model and test. reversion.register(ReversionTestModel3, eager_signals=[pre_delete]) self.assertTrue(reversion.is_registered(ReversionTestModel3)) self.assertRaises( RegistrationError, lambda: reversion.register(ReversionTestModel3, eager_signals=[pre_delete])) self.assertTrue( ReversionTestModel3 in reversion.get_registered_models()) self.assertTrue( isinstance(reversion.get_adapter(ReversionTestModel3), reversion.VersionAdapter)) self.assertEquals( [], reversion.default_revision_manager._signals[ReversionTestModel3]) self.assertEquals([pre_delete], reversion.default_revision_manager. _eager_signals[ReversionTestModel3]) # Unregister the model and text. reversion.unregister(ReversionTestModel3) self.assertFalse(reversion.is_registered(ReversionTestModel3)) self.assertRaises(RegistrationError, lambda: reversion.unregister(ReversionTestModel3)) self.assertTrue( ReversionTestModel3 not in reversion.get_registered_models()) self.assertRaises( RegistrationError, lambda: isinstance(reversion.get_adapter(ReversionTestModel3))) self.assertFalse( ReversionTestModel3 in reversion.default_revision_manager._signals) self.assertFalse(ReversionTestModel3 in reversion.default_revision_manager._eager_signals)
def handle(self, *args, **options): # pylint: disable=broad-except start = options['start'] end = options['end'] to_do_amount = options['next'] # We turn off reversion of ActivitySubmissions as we don't want to bloat the DB reversion.unregister(ActivitySubmission) reversion.unregister(Well) num_wells = 0 if to_do_amount: wells = self.find_next_n_wells_without_legacy_records( start, to_do_amount) else: wells = self.find_wells_without_legacy_records(start, end) num_wells = len(wells) if num_wells == 0: self.stdout.write( self.style.ERROR( f'No records found between well tag number {start} and {end}' )) return print( f'Creating {num_wells} legacy records from well_tag_number {wells[0].well_tag_number} to {wells[len(wells) - 1].well_tag_number}' ) failures = [] start = timer() for well in wells: try: self.create_legacy_record(well) except Exception as err: failures.append(well.well_tag_number) print( f'Error creating legacy record for well_tag_number {well.well_tag_number}' ) # logger.exception(err) print(traceback.format_exc(limit=8)) end = timer() num_fails = len(failures) num_created = num_wells - num_fails if num_created > 0: success_msg = 'Created {} legacy reports in {:.2f}s'.format( num_created, end - start) self.stdout.write(self.style.SUCCESS(success_msg)) if num_fails > 0: failed_wells = ', '.join(map(str, failures)) error_msg = 'Failed to create {} legacy reports for wells: {}' \ .format(num_fails, failed_wells) clues_msg = 'See above stack traces for clues to why these failed' self.stdout.write(self.style.ERROR(error_msg)) self.stdout.write(self.style.ERROR(clues_msg))
def setUp(self): super(FollowModelsTest, self).setUp() reversion.unregister(ReversionTestModel1) reversion.register(ReversionTestModel1, follow=("testfollowmodel_set",)) reversion.register(TestFollowModel, follow=("test_model_1", "test_model_2s")) self.follow1 = TestFollowModel.objects.create(name="related instance1 version 1", test_model_1=self.test11) self.follow1.test_model_2s.add(self.test21, self.test22)
def setUp(self): # Unregister all registered models. self.initial_registered_models = [] for registered_model in reversion.get_registered_models(): self.initial_registered_models.append((registered_model, reversion.get_adapter(registered_model).__class__)) reversion.unregister(registered_model) # Register the test models. reversion.register(ReversionTestModel1) reversion.register(ReversionTestModel2) reversion.register(ReversionTestModel3, eager_signals=[pre_delete]) # Create some test data. self.test11 = ReversionTestModel1.objects.create( name = "model1 instance1 version1", ) self.test12 = ReversionTestModel1.objects.create( name = "model1 instance2 version1", ) self.test21 = ReversionTestModel2.objects.create( name = "model2 instance1 version1", ) self.test22 = ReversionTestModel2.objects.create( name = "model2 instance2 version1", ) self.test31 = ReversionTestModel3.objects.create( name = "model3 instance1 version1", ) self.test32 = ReversionTestModel3.objects.create( name = "model3 instance2 version1", ) self.user = User.objects.create( username = "******", )
def testCreateRevisionFollow(self): reversion.unregister(TestModel) reversion.register(TestModel, follow=("related_instances",)) obj_2 = TestModel.objects.create() with reversion.create_revision(): obj_1 = TestModel.objects.create() obj_1.related_instances.add(obj_2) self.assertSingleRevision((obj_1, obj_2))
def testCreateRevisionFollow(self): reversion.unregister(TestModel) reversion.register(TestModel, follow=("related_instances", )) obj_2 = TestModel.objects.create() with reversion.create_revision(): obj_1 = TestModel.objects.create() obj_1.related_instances.add(obj_2) self.assertSingleRevision((obj_1, obj_2))
def testCreateRevisionIgnoreDuplicates(self): reversion.unregister(TestModel) reversion.register(TestModel, ignore_duplicates=True) with reversion.create_revision(): obj = TestModel.objects.create() with reversion.create_revision(): obj.save() self.assertSingleRevision((obj, ))
def testCreateRevisionIgnoreDuplicates(self): reversion.unregister(TestModel) reversion.register(TestModel, ignore_duplicates=True) with reversion.create_revision(): obj = TestModel.objects.create() with reversion.create_revision(): obj.save() self.assertSingleRevision((obj,))
def testCanUnregisterModel(self): """Tests that a model can be unregistered.""" reversion.unregister(TestModel) self.assertFalse(reversion.is_registered(TestModel)) # Check that duplicate unregistration is disallowed. self.assertRaises(RegistrationError, lambda: reversion.unregister(TestModel)) # Re-register the model. reversion.register(TestModel)
def testFieldDictFieldFields(self): reversion.unregister(TestModel) reversion.register(TestModel, fields=("name",)) with reversion.create_revision(): obj = TestModel.objects.create() self.assertEqual(Version.objects.get_for_object(obj).get().field_dict, { "name": "v1", })
def tearDown(self): """Tears down the tests.""" # Unregister the model. reversion.unregister(self.model) # Clear the database. Revision.objects.all().delete() self.model.objects.all().delete() # Clear references. del self.test
def testFieldDictFieldExclude(self): reversion.unregister(TestModel) reversion.register(TestModel, exclude=("name",)) with reversion.create_revision(): obj = TestModel.objects.create() self.assertEqual(Version.objects.get_for_object(obj).get().field_dict, { "id": obj.pk, "related_instances": [], })
def tearDown(self): """Tears down the tests.""" # Unregister the models. reversion.unregister(ReversionTestModel) reversion.unregister(TestManyToManyModel) # Clear the database. Revision.objects.all().delete() ReversionTestModel.objects.all().delete() TestManyToManyModel.objects.all().delete()
def tearDown(self): """Tears down the tests.""" # Unregister the model. reversion.unregister(ReversionTestModel) # Clear the database. Revision.objects.all().delete() ReversionTestModel.objects.all().delete() # Clear references. del self.test
def tearDown(self): """Tears down the tests.""" # Unregister the models. reversion.unregister(TestModel) reversion.unregister(TestRelatedModel) # Clear the database. Version.objects.all().delete() TestModel.objects.all().delete() TestRelatedModel.objects.all().delete()
def tearDown(self): """Deletes the versioned site model.""" # Unregister the model. reversion.unregister(TestModel) # Clear the database. Version.objects.all().delete() TestModel.objects.all().delete() # Clear references. del self.test_0 del self.test_1
def check_deregistration(self, test_model): # Unregister the model and text. reversion.unregister(test_model) self.assertFalse(reversion.is_registered(test_model)) self.assertRaises(RegistrationError, lambda: reversion.unregister(test_model)) self.assertTrue(test_model not in reversion.get_registered_models()) self.assertRaises( RegistrationError, lambda: isinstance(reversion.get_adapter(test_model)))
def setUp(self): super(FollowModelsTest, self).setUp() reversion.unregister(ReversionTestModel1) reversion.register(ReversionTestModel1, follow=("testfollowmodel_set",)) reversion.register(TestFollowModel, follow=("test_model_1", "test_model_2s",)) self.follow1 = TestFollowModel.objects.create( name = "related instance1 version 1", test_model_1 = self.test11, ) self.follow1.test_model_2s.add(self.test21, self.test22)
def setUp(self): reversion.unregister(Car) reversion.unregister(Factory) reversion.register(Factory, follow=["cars"]) reversion.register(Car) super(FactoryCarReverseRelationModelTest, self).setUp() test_data = TestData(verbose=False) self.factory = test_data.create_Factory_reverse_relation_data() queryset = get_for_object(self.factory) self.version_ids = queryset.values_list("pk", flat=True)
def testRegistration(self): # Register the model and test. reversion.register(ReversionTestModel1) self.assertTrue(reversion.is_registered(ReversionTestModel1)) self.assertRaises(RegistrationError, lambda: reversion.register(ReversionTestModel1)) self.assertTrue(ReversionTestModel1 in reversion.get_registered_models()) self.assertTrue(isinstance(reversion.get_adapter(ReversionTestModel1), reversion.VersionAdapter)) # Unregister the model and text. reversion.unregister(ReversionTestModel1) self.assertFalse(reversion.is_registered(ReversionTestModel1)) self.assertRaises(RegistrationError, lambda: reversion.unregister(ReversionTestModel1)) self.assertTrue(ReversionTestModel1 not in reversion.get_registered_models()) self.assertRaises(RegistrationError, lambda: isinstance(reversion.get_adapter(ReversionTestModel1)))
def tearDown(self): # Unregister the test models. reversion.unregister(ReversionTestModel1) reversion.unregister(ReversionTestModel2) reversion.unregister(ReversionTestModel3) # Delete the test models. ReversionTestModel1.objects.all().delete() ReversionTestModel2.objects.all().delete() ReversionTestModel3.objects.all().delete() User.objects.all().delete() del self.test11 del self.test12 del self.test21 del self.test22 del self.test31 del self.test32 del self.user # Delete the revisions index. Revision.objects.all().delete() # Unregister all remaining models. for registered_model in reversion.get_registered_models(): reversion.unregister(registered_model) # Re-register initial registered models. for initial_model, adapter in self.initial_registered_models: reversion.register(initial_model, adapter_cls=adapter) del self.initial_registered_models
def testInlineAdmin(self): self.assertTrue(reversion.is_registered(InlineTestParentModel)) # make sure model is following the child FK self.assertTrue('children' in reversion.get_adapter(InlineTestParentModel).follow) self.createInlineObjects() # unregister model reversion.unregister(InlineTestParentModel) self.assertFalse(reversion.is_registered(InlineTestParentModel)) # re-register without following reversion.register(InlineTestParentModel) self.assertTrue(reversion.is_registered(InlineTestParentModel)) # make sure model is NOT following the child FK self.assertFalse('children' in reversion.get_adapter(InlineTestParentModel).follow) self.createInlineObjects()
def testRevertDelete(self): reversion.unregister(TestModel) reversion.register(TestModel, follow=("related_instances",)) with reversion.create_revision(): obj_1 = TestModel.objects.create() obj_2 = TestModel.objects.create() with reversion.create_revision(): obj_1.related_instances.add(obj_2) obj_1.name = "v2" obj_1.save() Version.objects.get_for_object(obj_1)[1].revision.revert(delete=True) obj_1.refresh_from_db() self.assertEqual(obj_1.name, "v1") self.assertFalse(TestModel.objects.filter(pk=obj_2.pk).exists())
def tearDown(self): # Unregister the test models. reversion.unregister(ReversionTestModel1) reversion.unregister(ReversionTestModel2) # Delete the test models. ReversionTestModel1.objects.all().delete() ReversionTestModel2.objects.all().delete() User.objects.all().delete() del self.test11 del self.test12 del self.test21 del self.test22 del self.user # Delete the revisions index. Revision.objects.all().delete()
def testInlineAdmin(self): self.assertTrue(reversion.is_registered(InlineTestParentModel)) # make sure model is following the child FK self.assertTrue( 'children' in reversion.get_adapter(InlineTestParentModel).follow) self.createInlineObjects() # unregister model reversion.unregister(InlineTestParentModel) self.assertFalse(reversion.is_registered(InlineTestParentModel)) # re-register without following reversion.register(InlineTestParentModel) self.assertTrue(reversion.is_registered(InlineTestParentModel)) # make sure model is NOT following the child FK self.assertFalse( 'children' in reversion.get_adapter(InlineTestParentModel).follow) self.createInlineObjects()
def testEagerRegistration(self): # Register the model and test. reversion.register(ReversionTestModel3, eager_signals=[pre_delete]) self.assertTrue(reversion.is_registered(ReversionTestModel3)) self.assertRaises(RegistrationError, lambda: reversion.register(ReversionTestModel3, eager_signals=[pre_delete])) self.assertTrue(ReversionTestModel3 in reversion.get_registered_models()) self.assertTrue(isinstance(reversion.get_adapter(ReversionTestModel3), reversion.VersionAdapter)) self.assertEquals([], reversion.default_revision_manager._signals[ReversionTestModel3]) self.assertEquals([pre_delete], reversion.default_revision_manager._eager_signals[ReversionTestModel3]) # Unregister the model and text. reversion.unregister(ReversionTestModel3) self.assertFalse(reversion.is_registered(ReversionTestModel3)) self.assertRaises(RegistrationError, lambda: reversion.unregister(ReversionTestModel3)) self.assertTrue(ReversionTestModel3 not in reversion.get_registered_models()) self.assertRaises(RegistrationError, lambda: isinstance(reversion.get_adapter(ReversionTestModel3))) self.assertFalse(ReversionTestModel3 in reversion.default_revision_manager._signals) self.assertFalse(ReversionTestModel3 in reversion.default_revision_manager._eager_signals)
def tearDown(self): # Unregister the test models. reversion.unregister(ReversionTestModel1) reversion.unregister(ReversionTestModel2) reversion.unregister(ReversionTestModel3) # Unregister all remaining models. for registered_model in reversion.get_registered_models(): reversion.unregister(registered_model) # Re-register initial registered models. for initial_model, adapter in self.initial_registered_models: reversion.register(initial_model, adapter_cls=adapter) del self.initial_registered_models
def register_type(type_name, model_class): from cms.utils.helpers import reversion_register from .models import TextNG, TextNGVariableBase try: from reversion import unregister except ImportError: from reversion.revisions import unregister if type_name in _registry: if _registry[type_name] == model_class: # already registered return else: raise exceptions.VariableTypeAlreadyRegistered( 'The type "%s" is already registered by %s' % (type_name, _registry[type_name].__name__)) if not issubclass(model_class, TextNGVariableBase): raise exceptions.InvalidType( '%s is not a subclass of TextNGVariableBase' % model_class.__name__) try: field = model_class._meta.get_field_by_name('value')[0] except FieldDoesNotExist: raise exceptions.InvalidType('%s does not define a "value" field' % model_class.__name__) if not field.null: raise exceptions.InvalidType('"value" field of %s is not nullable' % model_class.__name__) _registry[type_name] = model_class fields = [ rel.get_accessor_name() for rel in TextNG._meta.get_all_related_objects() if issubclass(rel.related_model, TextNGVariableBase) ] try: #TextNG registration need to be upgraded unregister(TextNG) except: pass reversion_register(TextNG, follow=fields) reversion_register(model_class)
def register_type(type_name, model_class): from cms.utils.helpers import reversion_register from .models import TextNG, TextNGVariableBase try: from reversion import unregister except ImportError: from reversion.revisions import unregister if type_name in _registry: if _registry[type_name] == model_class: # already registered return else: raise exceptions.VariableTypeAlreadyRegistered( 'The type "%s" is already registered by %s' % (type_name, _registry[type_name].__name__) ) if not issubclass(model_class, TextNGVariableBase): raise exceptions.InvalidType('%s is not a subclass of TextNGVariableBase' % model_class.__name__) try: field = model_class._meta.get_field_by_name('value')[0] except FieldDoesNotExist: raise exceptions.InvalidType('%s does not define a "value" field' % model_class.__name__) if not field.null: raise exceptions.InvalidType('"value" field of %s is not nullable' % model_class.__name__) _registry[type_name] = model_class fields = [ rel.get_accessor_name() for rel in TextNG._meta.get_all_related_objects() if issubclass(rel.related_model, TextNGVariableBase)] try: #TextNG registration need to be upgraded unregister(TextNG) except: pass reversion_register(TextNG, follow=fields) reversion_register(model_class)
def add_reversion_following(cls, follow): """ DeviceAdmin is used by other modules that register InlineModelAdmin using monkey patching. The default implementation of reversion.register ignores such inlines and does not update the "follow" field accordingly. This method updates the "follow" fields of the Device model by unregistering the Device model from reversion and re-registering it. Only the" "follow" option is updated. """ device_reversion_options = reversion.revisions._registered_models[ reversion.revisions._get_registration_key(Device)] following = set(device_reversion_options.follow).union(set(follow)) reversion.unregister(Device) reversion.register( model=Device, fields=device_reversion_options.fields, follow=following, format=device_reversion_options.format, for_concrete_model=device_reversion_options.for_concrete_model, ignore_duplicates=device_reversion_options.ignore_duplicates, use_natural_foreign_keys=device_reversion_options. use_natural_foreign_keys, )
def setUp(self): unregister(Person) unregister(Car) unregister(Factory) revisions.register(Factory, follow=["building_ptr", "cars", "workers"]) revisions.register(Car) revisions.register(Person, follow=["pets"]) super(FactoryCarReverseRelationModelTest, self).setUp() test_data = TestData(verbose=False) self.factory = test_data.create_Factory_reverse_relation_data() queryset = Version.objects.get_for_object(self.factory) self.version_ids = queryset.values_list("pk", flat=True)
def tearDown(self): reversion.unregister(TestFollowModel) TestFollowModel.objects.all().delete() del self.follow1 super(FollowModelsTest, self).tearDown()
def tearDown(self): """Tears down the tests.""" reversion.unregister(TestModel)
def tearDown(self): super(MultiTableInheritanceApiTest, self).tearDown() reversion.unregister(ReversionTestModel1Child) ReversionTestModel1Child.objects.all().delete() del self.testchild1
def testUnregisterNotRegistered(self): with self.assertRaises(reversion.RegistrationError): reversion.unregister(User)
def testUnregister(self): reversion.unregister(TestModel) self.assertFalse(reversion.is_registered(TestModel))