Example #1
0
    def test_replacer_by_fixed_value01(self):
        civ = FakeCivility.objects.create(title='Kun')

        model_field = FakeContact._meta.get_field('civility')
        replacer1 = FixedValueReplacer(model_field=model_field, value=civ)
        self.assertEqual(model_field, replacer1.model_field)
        self.assertEqual(civ, replacer1._fixed_value)

        serialized = {
            'ctype': ContentType.objects.get_for_model(FakeContact).natural_key(),
            'field': 'civility',
            'pk':    civ.pk,
        }
        self.assertEqual(serialized, replacer1.as_dict())

        replacer2 = FixedValueReplacer.from_dict(serialized)
        self.assertIsInstance(replacer2, FixedValueReplacer)
        self.assertEqual(model_field,    replacer2.model_field)
        self.assertEqual(civ,            replacer2.get_value())

        self.assertEqual(
            _('In «{model} - {field}», replace by «{new}»').format(
                model='Test Contact',
                field=_('Civility'),
                new=civ.title,
            ),
            str(replacer1),
        )
Example #2
0
    def test_deletor_job02(self):
        "One replacement."
        user = self.create_user()

        civ = FakeCivility.objects.first()
        civ2del = FakeCivility.objects.create(title='Kun')

        contact = FakeContact.objects.create(
            user=user,
            civility=civ2del,
            last_name='Hattori',
            first_name='Genzo',
        )

        job = Job.objects.create(
            type_id=deletor_type.id,
            user=user,
        )
        DeletionCommand.objects.create(
            job=job,
            instance_to_delete=civ2del,
            replacers=[
                FixedValueReplacer(
                    model_field=FakeContact._meta.get_field('civility'),
                    value=civ,
                ),
            ],
            total_count=1,
        )
        progress = deletor_type.progress(job)
        self.assertEqual(0, progress.percentage)
        self.assertFalse(progress.label)

        deletor_type.execute(job)
        self.assertDoesNotExist(civ2del)
        self.assertEqual(civ, self.refresh(contact).civility)

        self.assertFalse(JobResult.objects.filter(job=job))

        job = self.refresh(job)
        self.assertListEqual([
            ngettext('{count} entity updated.', '{count} entities updated.',
                     1).format(count=1),
        ], deletor_type.get_stats(job))
        progress = deletor_type.progress(job)
        self.assertEqual(100, progress.percentage)
        self.assertFalse(progress.label)

        # NB: we check the description is OK _after_ the instance is deleted
        self.assertListEqual([
            _('Deleting «{object}» ({model})').format(
                object=civ2del.title,
                model='Test civility',
            ),
            _('In «{model} - {field}», replace by «{new}»').format(
                model='Test Contact',
                field=_('Civility'),
                new=civ.title,
            ),
        ], deletor_type.get_description(job))
Example #3
0
    def test_replacer_by_fixed_value04(self):
        "ManyToMany."
        cat = FakeDocumentCategory.objects.create(name='PNGs')
        m2m = FakeDocument._meta.get_field('categories')

        self.assertEqual(
            _('In «{model} - {field}», replace by «{new}»').format(
                model='Test Document',
                field=_('Categories'),
                new=cat.name,
            ), str(FixedValueReplacer(model_field=m2m, value=cat)))

        self.assertEqual(
            _('Remove from «{model} - {field}»').format(
                model='Test Document',
                field=_('Categories'),
            ), str(FixedValueReplacer(model_field=m2m)))
Example #4
0
    def test_registry01(self):
        "FixedValueReplacer."
        sector = FakeSector.objects.first()

        field1 = FakeOrganisation._meta.get_field('sector')
        field2 = FakeContact._meta.get_field('sector')
        replacer1 = FixedValueReplacer(model_field=field1, value=None)
        replacer2 = FixedValueReplacer(model_field=field2, value=sector)

        get_ct = ContentType.objects.get_for_model
        serialized = [
            [
                'fixed_value',
                {
                    'ctype': get_ct(FakeOrganisation).natural_key(),
                    'field': 'sector',
                },
            ], [
                'fixed_value',
                {
                    'ctype': get_ct(FakeContact).natural_key(),
                    'field': 'sector',
                    'pk': sector.pk,
                },
            ],
        ]
        self.assertEqual(
            serialized,
            REPLACERS_MAP.serialize([replacer1, replacer2])
        )

        replacers = REPLACERS_MAP.deserialize(serialized)
        self.assertIsList(replacers, length=2)

        d_replacer1 = replacers[0]
        self.assertIsInstance(d_replacer1, FixedValueReplacer)
        self.assertEqual(field1, d_replacer1.model_field)
        self.assertIsNone(d_replacer1.get_value())

        d_replacer2 = replacers[1]
        self.assertIsInstance(d_replacer2, FixedValueReplacer)
        self.assertEqual(field2, d_replacer2.model_field)
        self.assertEqual(sector, d_replacer2.get_value())
Example #5
0
    def test_deletion_command_replacers01(self):
        user = self.login()

        create_sector = FakeSector.objects.create
        sector = create_sector(title='Shinobi')
        sector2del = create_sector(title='Ninja')

        job = Job.objects.create(
            type_id=deletor_type.id,
            user=user,
        )

        field1 = FakeContact._meta.get_field('sector')
        field2 = FakeOrganisation._meta.get_field('sector')

        dcom = DeletionCommand.objects.create(
            job=job,
            instance_to_delete=sector2del,
            replacers=[
                FixedValueReplacer(model_field=field1, value=sector),
                FixedValueReplacer(model_field=field2, value=sector),
            ],
        )

        replacers = self.refresh(dcom).replacers
        self.assertIsInstance(replacers, list)
        self.assertEqual(2, len(replacers))

        replacer1 = replacers[0]
        self.assertIsInstance(replacer1, FixedValueReplacer)
        self.assertEqual(field1, replacer1.model_field)
        self.assertEqual(sector, replacer1.get_value())

        self.assertEqual(field2, replacers[1].model_field)

        with self.assertRaises(TypeError):
            DeletionCommand.objects.create(
                job=job,
                instance_to_delete=sector,
                replacers=[(1, 2)],
            )
Example #6
0
    def test_replacer_by_fixed_value02(self):
        "<None> value + other ContentType."
        model_field = FakeOrganisation._meta.get_field('sector')
        replacer1 = FixedValueReplacer(model_field=model_field, value=None)

        serialized = {
            'ctype': ContentType.objects.get_for_model(FakeOrganisation).natural_key(),
            'field': 'sector',
        }
        self.assertEqual(serialized, replacer1.as_dict())

        replacer2 = FixedValueReplacer.from_dict(serialized)
        self.assertIsInstance(replacer2, FixedValueReplacer)
        self.assertEqual(model_field, replacer2.model_field)
        self.assertIsNone(replacer2.get_value())

        self.assertEqual(
            _('Empty «{model} - {field}»').format(
                model='Test Organisation',
                field=_('Sector'),
            ),
            str(replacer1),
        )
Example #7
0
    def test_replacer_by_fixed_value03(self):
        "Explicit & implicit values."
        self.assertEqual(
            _('Empty «{model} - {field}»').format(
                model='Test Contact',
                field=_('Civility'),
            ),
            str(
                FixedValueReplacer(
                    model_field=FakeContact._meta.get_field('civility'))))

        sector = FakeSector.objects.create(title='Ninja')
        self.assertEqual(
            _('In «{model} - {field}», replace by «{new}»').format(
                model='Test Organisation',
                field=_('Sector'),
                new=sector.title,
            ),
            str(
                FixedValueReplacer(
                    model_field=FakeOrganisation._meta.get_field('sector'),
                    value=sector,
                )))
Example #8
0
    def test_deletor_job03(self):
        "Several Replacement."
        user = self.create_user()

        civ = FakeCivility.objects.first()
        civ2del = FakeCivility.objects.create(title='Kun')

        create_contact = partial(FakeContact.objects.create,
                                 user=user,
                                 civility=civ2del)
        contact1 = create_contact(last_name='Hattori', first_name='Genzo')
        contact2 = create_contact(last_name='Hattori', first_name='Hanzo')

        job = Job.objects.create(
            type_id=deletor_type.id,
            user=user,
        )
        DeletionCommand.objects.create(
            job=job,
            instance_to_delete=civ2del,
            replacers=[
                FixedValueReplacer(
                    model_field=FakeContact._meta.get_field('civility'),
                    value=civ,
                ),
            ],
        )

        deletor_type.execute(job)
        self.assertDoesNotExist(civ2del)
        self.assertEqual(civ, self.refresh(contact1).civility)
        self.assertEqual(civ, self.refresh(contact2).civility)

        self.assertFalse(JobResult.objects.filter(job=job))

        job = self.refresh(job)
        self.assertListEqual(
            [
                ngettext('{count} entity updated.',
                         '{count} entities updated.', 2).format(count=2),
            ],
            deletor_type.get_stats(job),
        )
Example #9
0
    def save(self, *args, **kwargs):
        instance = self.instance
        instance.instance_to_delete = choice_to_delete = self.choice_to_delete
        cf_value_model = choice_to_delete.custom_field.value_class

        replacement = self.cleaned_data.get('to_choice')
        if replacement:
            instance.replacers = [
                FixedValueReplacer(
                    model_field=cf_value_model._meta.get_field('value'),
                    value=replacement,
                )
            ]
        instance.total_count = cf_value_model.objects.filter(
            value=choice_to_delete,
        ).count()
        instance.job = Job.objects.create(
            type_id=deletor_type.id,
            user=self.user,
        )

        return super().save(*args, **kwargs)