Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
dj-kaos / building_blocks / querysets.py
Size: Mime:
from django.db import models
from django.db.models import Q
from django.utils.timezone import now


class ArchivableQuerySet(models.QuerySet):
    _Q_ARCHIVED = Q(_archived_at__isnull=False)

    def archived(self):
        """
        :return: queryset with db rows that are archived
        """
        return self.filter(self._Q_ARCHIVED)

    def unarchived(self):
        """
        :return: queryset with db rows that are "available" (aka not archived)
        """
        return self.filter(~self._Q_ARCHIVED)

    def archive(self):
        """
        Archive the objects in this queryset.
        :return: the return value from `.update()` i.e. the count of rows updated.
        """
        return self.update(_archived_at=now())

    def restore(self):
        """
        Restore (unarchive) the objects in this queryset.
        :return: the return value from `.update()` i.e. the count of rows updated.
        """
        return self.update(_archived_at=None)


class HasParentQuerySet(models.QuerySet):
    def roots(self):
        return self.filter(parent__isnull=True)


__all__ = (
    'ArchivableQuerySet',
    'HasParentQuerySet',
)