# Path: puput/utils.py
# def get_object_or_None(klass, *args, **kwargs):
#     """
#     Uses get() to return an object or None if the object does not exist.
#     klass may be a Model, Manager, or QuerySet object. All other passed
#     arguments and keyword arguments are used in the get() query.
#     Note: Like with get(), a MultipleObjectsReturned will be raised if more than one
#     object is found.
#     """
#     queryset = _get_queryset(klass)
#     try:
#         return queryset.get(*args, **kwargs)
#     except queryset.model.DoesNotExist:
#         return None


from datetime import date
from django.contrib.auth import get_user_model
from django.utils.dateformat import DateFormat
from django.utils.formats import date_format
from django.utils.translation import gettext_lazy as _
from django.conf import settings
from wagtail.contrib.routable_page.models import RoutablePageMixin, route
from wagtail.core.models import Page
from wagtail.search.models import Query
from .utils import get_object_or_None
        from puput.models import Tag
        from puput.models import Category

    @route(r'^(\d{4})/$')
    @route(r'^(\d{4})/(\d{2})/$')
    @route(r'^(\d{4})/(\d{2})/(\d{2})/$')
    def entries_by_date(self, request, year, month=None, day=None, *args, **kwargs):
        self.entries = self.get_entries().filter(date__year=year)
        self.search_type = _('date')
        self.search_term = year
        if month:
            self.entries = self.entries.filter(date__month=month)
            df = DateFormat(date(int(year), int(month), 1))
            self.search_term = df.format('F Y')
        if day:
            self.entries = self.entries.filter(date__day=day)
            self.search_term = date_format(date(int(year), int(month), int(day)))
        return Page.serve(self, request, *args, **kwargs)

    @route(r'^tag/(?P<tag>[-\w]+)/$')
    def entries_by_tag(self, request, tag, *args, **kwargs):

        self.search_type = _('tag')
        object_or_slug = get_object_or_None(Tag, slug=tag) or tag
        self.search_term = str(object_or_slug)
        self.entries = self.get_entries().filter(tags__slug=tag)
        return Page.serve(self, request, *args, **kwargs)

    @route(r'^category/(?P<category>[-\w]+)/$')
    def entries_by_category(self, request, category, *args, **kwargs):

        self.search_type = _('category')

# Path: puput/utils.py
# def get_object_or_None(klass, *args, **kwargs):
#     """
#     Uses get() to return an object or None if the object does not exist.
#     klass may be a Model, Manager, or QuerySet object. All other passed
#     arguments and keyword arguments are used in the get() query.
#     Note: Like with get(), a MultipleObjectsReturned will be raised if more than one
#     object is found.
#     """
#     queryset = _get_queryset(klass)
#     try:
#         return queryset.get(*args, **kwargs)
#     except queryset.model.DoesNotExist:
#         return None


# Path: puput/routes.py
from datetime import date
from django.contrib.auth import get_user_model
from django.utils.dateformat import DateFormat
from django.utils.formats import date_format
from django.utils.translation import gettext_lazy as _
from django.conf import settings
from wagtail.contrib.routable_page.models import RoutablePageMixin, route
from wagtail.core.models import Page
from wagtail.search.models import Query
from .utils import get_object_or_None
        from puput.models import Tag
        from puput.models import Category


    @route(r'^(\d{4})/$')
    @route(r'^(\d{4})/(\d{2})/$')
    @route(r'^(\d{4})/(\d{2})/(\d{2})/$')
    def entries_by_date(self, request, year, month=None, day=None, *args, **kwargs):
        self.entries = self.get_entries().filter(date__year=year)
        self.search_type = _('date')
        self.search_term = year
        if month:
            self.entries = self.entries.filter(date__month=month)
            df = DateFormat(date(int(year), int(month), 1))
            self.search_term = df.format('F Y')
        if day:
            self.entries = self.entries.filter(date__day=day)
            self.search_term = date_format(date(int(year), int(month), int(day)))
        return Page.serve(self, request, *args, **kwargs)

    @route(r'^tag/(?P<tag>[-\w]+)/$')
    def entries_by_tag(self, request, tag, *args, **kwargs):

        self.search_type = _('tag')
        object_or_slug = get_object_or_None(Tag, slug=tag) or tag
        self.search_term = str(object_or_slug)
        self.entries = self.get_entries().filter(tags__slug=tag)
        return Page.serve(self, request, *args, **kwargs)

    @route(r'^category/(?P<category>[-\w]+)/$')
    def entries_by_category(self, request, category, *args, **kwargs):

        self.search_type = _('category')

        object_or_slug = get_object_or_None(Category, slug=category) or category