"""Managers of Zinnia"""
from datetime import datetime

from django.db import models
from django.contrib.sites.models import Site

DRAFT = 0
HIDDEN = 1
PUBLISHED = 2


def tags_published():
    """Return the published tags"""
    from tagging.models import Tag
    from zinnia.models import Entry
    tags_entry_published = Tag.objects.usage_for_queryset(
        Entry.published.all())
    # Need to do that until the issue #44 of django-tagging is fixed
    return Tag.objects.filter(name__in=[t.name for t in tags_entry_published])


class AuthorPublishedManager(models.Manager):
    """Manager to retrieve published authors"""

    def get_query_set(self):
        """Return published authors"""
        now = datetime.now()
        return super(AuthorPublishedManager, self).get_query_set().filter(
            entry__status=PUBLISHED,
            entry__start_publication__lte=now,
            entry__end_publication__gt=now,
            entry__sites=Site.objects.get_current()
            ).distinct()


def entries_published(queryset):
    """Return only the entries published"""
    now = datetime.now()
    return queryset.filter(status=PUBLISHED,
                           start_publication__lte=now,
                           end_publication__gt=now,
                           sites=Site.objects.get_current())


class EntryPublishedManager(models.Manager):
    """Manager to retrieve published entries"""

    def get_query_set(self):
        """Return published entries"""
        return entries_published(
            super(EntryPublishedManager, self).get_query_set())

    def on_site(self):
        """Return entries published on current site"""
        return super(EntryPublishedManager, self).get_query_set(
            ).filter(sites=Site.objects.get_current())

    def search(self, pattern):
        """Top level search method on entries"""
        try:
            return self.advanced_search(pattern)
        except ImportError:
            return self.basic_search(pattern)

    def advanced_search(self, pattern):
        """Advanced search on entries"""
        from pyparsing import ParseException
        from zinnia.search import advanced_search
        try:
            return advanced_search(pattern)
        except ParseException:
            # Raise an ImportError to fallback in basic_search
            raise ImportError

    def basic_search(self, pattern):
        """Basic search on entries"""
        lookup = None
        for pattern in pattern.split():
            query_part = models.Q(content__icontains=pattern) | \
                         models.Q(excerpt__icontains=pattern) | \
                         models.Q(title__icontains=pattern)
            if lookup is None:
                lookup = query_part
            else:
                lookup |= query_part

        return self.get_query_set().filter(lookup)

@@ -1941,22 +1941,10 @@
 cept
- ImportError
 :%0A
+
     
@@ -2071,53 +2071,8 @@
 %22%22%22%0A
-        from pyparsing import ParseException%0A
     
@@ -2117,33 +2117,16 @@
 _search%0A
-        try:%0A    
         
@@ -2160,132 +2160,8 @@
 ern)
-%0A        except ParseException:%0A            # Raise an ImportError to fallback in basic_search%0A            raise ImportError
 %0A%0A  

simplify the search