from cms.models import Page
from cms.utils.page import _page_is_published
from haystack.forms import ModelSearchForm
from parler.utils.i18n import get_language

from project.models import Notification


def search_form(request):
    return {
        'search_form': ModelSearchForm
    }


def notification(request):
    return {'notification': Notification.objects.filter(published=True).first()}


def new_page_amount(request):
    tracked_pages = ['news', 'calendar']
    context = {}

    for tracked_page in tracked_pages:
        try:
            page = Page.objects.public().get(reverse_id=tracked_page)
            id = 'new_{}_amount'.format(tracked_page)
            amount = 0
            for page in page.get_child_pages():
                if page.is_published(get_language()) and _page_is_published(page):
                    amount = amount + 1
            context.update({
                id: amount
            })
        except Page.DoesNotExist:
            pass

    return context
