Beispiel #1
0
    def save_formset_deletion_allowed_if_only(
        self, request, form, formset, change, classes
    ):
        """
        Helper which has is most useful when used inside ``save_formsets``

        To use this method properly, your model has to inherit from
        ``towel.deletion.Model``. This is NOT enforced. If you ignore this, the
        instances will be deleted by ``formset.save()`` as is the default with
        Django.

        Example::

            def save_formsets(self, requset, form, formsets, change):
                # Allow deleting states when the cascade includes
                # ``SomeModel`` instances only.
                self.save_formset_deletion_allowed_if_only(
                    request, form, formsets['states'], change, [SomeModel])

                # No special handling for other formsets
                self.save_formset(request, form, formsets['otherformset'],
                    change=change)

        """
        with deletion.protect():
            self.save_formset(request, form, formset, change)

        for instance in formset.deleted_objects:
            related = set(related_classes(instance))

            related.discard(instance.__class__)
            for class_ in classes:
                related.discard(class_)

            if len(related):
                pretty_classes = [
                    force_text(class_._meta.verbose_name_plural) for class_ in related
                ]

                if len(pretty_classes) > 1:
                    pretty_classes = "".join(
                        (
                            ", ".join(pretty_classes[:-1]),
                            ugettext(" and "),
                            pretty_classes[-1],
                        )
                    )
                else:
                    pretty_classes = pretty_classes[-1]

                self.add_message(
                    request,
                    "deletion_denied_related",
                    {"pretty_classes": pretty_classes},
                )
            else:
                instance.delete()
Beispiel #2
0
    def save_formset_deletion_allowed_if_only(self, request, form, formset,
                                              change, classes):
        """
        Helper which has is most useful when used inside ``save_formsets``

        To use this method properly, your model has to inherit from
        ``towel.deletion.Model``. This is NOT enforced. If you ignore this, the
        instances will be deleted by ``formset.save()`` as is the default with
        Django.

        Example::

            def save_formsets(self, requset, form, formsets, change):
                # Allow deleting states when the cascade includes
                # ``SomeModel`` instances only.
                self.save_formset_deletion_allowed_if_only(
                    request, form, formsets['states'], change, [SomeModel])

                # No special handling for other formsets
                self.save_formset(request, form, formsets['otherformset'], change)

        """
        with deletion.protect():
            self.save_formset(request, form, formset, change)

        for instance in formset.deleted_objects:
            related = set(related_classes(instance))

            related.discard(instance.__class__)
            for class_ in classes:
                related.discard(class_)

            if len(related):
                pretty_classes = [
                    unicode(class_._meta.verbose_name_plural)
                    for class_ in related
                ]
                if len(pretty_classes) > 1:
                    pretty_classes = u''.join((u', '.join(pretty_classes[:-1]),
                                               _(' and '), pretty_classes[-1]))
                else:
                    pretty_classes = pretty_classes[-1]

                messages.error(
                    request,
                    _('Deletion of %(instance)s not allowed: <small>There are %(classes)s related to this object.</small>'
                      ) % {
                          'instance': instance,
                          'classes': pretty_classes,
                      })
            else:
                instance.delete()
Beispiel #3
0
    def save_formset_deletion_allowed_if_only(self, request, form, formset,
                                              change, classes):
        """
        Helper which has is most useful when used inside ``save_formsets``

        To use this method properly, your model has to inherit from
        ``towel.deletion.Model``. This is NOT enforced. If you ignore this, the
        instances will be deleted by ``formset.save()`` as is the default with
        Django.

        Example::

            def save_formsets(self, requset, form, formsets, change):
                # Allow deleting states when the cascade includes
                # ``SomeModel`` instances only.
                self.save_formset_deletion_allowed_if_only(
                    request, form, formsets['states'], change, [SomeModel])

                # No special handling for other formsets
                self.save_formset(request, form, formsets['otherformset'],
                    change=change)

        """
        with deletion.protect():
            self.save_formset(request, form, formset, change)

        for instance in formset.deleted_objects:
            related = set(related_classes(instance))

            related.discard(instance.__class__)
            for class_ in classes:
                related.discard(class_)

            if len(related):
                pretty_classes = [
                    force_text(class_._meta.verbose_name_plural)
                    for class_ in related]

                if len(pretty_classes) > 1:
                    pretty_classes = ''.join((
                        ', '.join(pretty_classes[:-1]),
                        ugettext(' and '),
                        pretty_classes[-1],
                    ))
                else:
                    pretty_classes = pretty_classes[-1]

                self.add_message(request, 'deletion_denied_related', {
                    'pretty_classes': pretty_classes})
            else:
                instance.delete()
Beispiel #4
0
    def test_deletion(self):
        person = Person.objects.create()

        email = person.emailaddress_set.create()
        self.assertEqual(EmailAddress.objects.count(), 1)
        email.delete()
        self.assertEqual(EmailAddress.objects.count(), 0)
        email = person.emailaddress_set.create()
        self.assertEqual(EmailAddress.objects.count(), 1)
        with deletion.protect():
            email.delete()
        self.assertEqual(EmailAddress.objects.count(), 1)
        email.delete()
        self.assertEqual(EmailAddress.objects.count(), 0)
Beispiel #5
0
    def save_formset_deletion_allowed_if_only(self, request, form, formset, change, classes):
        """
        Helper which has is most useful when used inside ``save_formsets``

        To use this method properly, your model has to inherit from
        ``towel.deletion.Model``. This is NOT enforced. If you ignore this, the
        instances will be deleted by ``formset.save()`` as is the default with
        Django.

        Example::

            def save_formsets(self, requset, form, formsets, change):
                # Allow deleting states when the cascade includes
                # ``SomeModel`` instances only.
                self.save_formset_deletion_allowed_if_only(
                    request, form, formsets['states'], change, [SomeModel])

                # No special handling for other formsets
                self.save_formset(request, form, formsets['otherformset'], change)

        """
        with deletion.protect():
            self.save_formset(request, form, formset, change)

        for instance in formset.deleted_objects:
            related = set(related_classes(instance))

            related.discard(instance.__class__)
            for class_ in classes:
                related.discard(class_)

            if len(related):
                pretty_classes = [unicode(class_._meta.verbose_name_plural) for class_ in related]
                if len(pretty_classes) > 1:
                    pretty_classes = u''.join((
                        u', '.join(pretty_classes[:-1]),
                        _(' and '),
                        pretty_classes[-1]))
                else:
                    pretty_classes = pretty_classes[-1]

                messages.error(request, _('Deletion of %(instance)s not allowed: <small>There are %(classes)s related to this object.</small>') % {
                    'instance': instance,
                    'classes': pretty_classes,
                    })
            else:
                instance.delete()