from django.core.exceptions import ObjectDoesNotExist
from django.db import models
from logicaldelete import managers

try:
    from django.utils import timezone
except ImportError:
    from datetime import datetime as timezone


class Model(models.Model):
    """
    This base model provides date fields and functionality to enable logical
    delete functionality in derived models.
    """

    date_created = models.DateTimeField(default=timezone.now)
    date_modified = models.DateTimeField(default=timezone.now)
    date_removed = models.DateTimeField(null=True, blank=True)

    objects = managers.LogicalDeletedManager()

    def active(self):
        return self.date_removed is None
    active.boolean = True

    def delete(self):
        '''
        Soft delete all fk related objects that
        inherit from logicaldelete class
        '''

        # Fetch related models
        related_objs = [relation.get_accessor_name() for
                        relation in self._meta.get_all_related_objects()]

        for objs_model in related_objs:
            # Retrieve all related objects
            try:
                obj = getattr(self, objs_model)
            except ObjectDoesNotExist, AttributeError:
                # The attribute  or relation  may not
                # be instanciated.
                pass

            # Checking if inherits from logicaldelete
            if not issubclass(obj.__class__, Model):
                break
            obj.delete()

        # Soft delete the object
        self.date_removed = timezone.now()
        self.save()

    def save(self, force_insert=False, force_update=False, using=None,
             update_fields=None):
        self.date_modified = timezone.now()
        super(Model, self).save(force_insert=False, force_update=False,
                                using=None, update_fields=None)

    class Meta:
        abstract = True

@@ -1576,24 +1576,852 @@
 elf.save()%0A%0A
+    def undelete(self):%0A        '''%0A        UnSoft delete all fk related objects that%0A        inherit from logicaldelete class%0A        '''%0A%0A        # Fetch related models%0A        related_objs = %5Brelation.get_accessor_name() for%0A                        relation in self._meta.get_all_related_objects()%5D%0A%0A        for objs_model in related_objs:%0A            # Retrieve all related objects%0A            try:%0A                obj = getattr(self, objs_model)%0A            except ObjectDoesNotExist, AttributeError:%0A                # The attribute  or relation  may not%0A                # be instanciated.%0A                pass%0A%0A            # Checking if inherits from logicaldelete%0A            if not issubclass(obj.__class__, Model):%0A                break%0A            obj.undelete()%0A%0A        self.date_removed = None%0A        self.save()%0A%0A
     def save
@@ -2549,32 +2549,162 @@
  timezone.now()%0A
+%0A        if self.date_removed:%0A            # Patch for reupdating the DeletedTime%0A            self.date_removed = timezone.now()%0A%0A
         super(Mo

Fix date_removed < date_modified bug & Add undelete() method