# Path: puput/utils.py
# def strip_prefix_and_ending_slash(path):
#     """
#     If puput and wagtail are registered with a prefix, it needs to be removed
#     so the 'entry_page_serve_slug' or 'blog_page_feed_slug' resolutions can work.
#     Ex, here with a dynamic (i18n_patterns()) + a static prefix :
#     urlpatterns += i18n_patterns(
#         url(r'^blah/', include('puput.urls')),
#         url(r'^blah/', include(wagtail_urls)),
#     )
#     The prefix is simply the root where Wagtail page are served.
#     https://github.com/torchbox/wagtail/blob/stable/1.8.x/wagtail/wagtailcore/urls.py#L36
#     """
#     return path.replace(reverse('wagtail_serve', args=[""]), '', 1).rstrip("/")


from django.db import models
from django.db.models import Count
from wagtail.core.models import PageManager
from .utils import strip_prefix_and_ending_slash
        from .models import BlogPage



class TagManager(models.Manager):

    def most_common(self, blog_page):
        entries = blog_page.get_entries()
        return self.filter(entrypage__in=entries).annotate(num_times=Count('entrypage')).order_by('-num_times')


class CategoryManager(models.Manager):

    def with_uses(self, blog_page):
        entries = blog_page.get_entries()
        return self.filter(entrypage__in=entries).distinct()


class BlogManager(PageManager):

    def get_by_path(self, blog_path):
        # Look for the blog checking all the path
        blogs = BlogPage.objects.filter(slug=blog_path.split("/")[-1])

# Path: puput/utils.py
# def strip_prefix_and_ending_slash(path):
#     """
#     If puput and wagtail are registered with a prefix, it needs to be removed
#     so the 'entry_page_serve_slug' or 'blog_page_feed_slug' resolutions can work.
#     Ex, here with a dynamic (i18n_patterns()) + a static prefix :
#     urlpatterns += i18n_patterns(
#         url(r'^blah/', include('puput.urls')),
#         url(r'^blah/', include(wagtail_urls)),
#     )
#     The prefix is simply the root where Wagtail page are served.
#     https://github.com/torchbox/wagtail/blob/stable/1.8.x/wagtail/wagtailcore/urls.py#L36
#     """
#     return path.replace(reverse('wagtail_serve', args=[""]), '', 1).rstrip("/")


# Path: puput/managers.py
from django.db import models
from django.db.models import Count
from wagtail.core.models import PageManager
from .utils import strip_prefix_and_ending_slash
        from .models import BlogPage




class TagManager(models.Manager):

    def most_common(self, blog_page):
        entries = blog_page.get_entries()
        return self.filter(entrypage__in=entries).annotate(num_times=Count('entrypage')).order_by('-num_times')


class CategoryManager(models.Manager):

    def with_uses(self, blog_page):
        entries = blog_page.get_entries()
        return self.filter(entrypage__in=entries).distinct()


class BlogManager(PageManager):

    def get_by_path(self, blog_path):
        # Look for the blog checking all the path
        blogs = BlogPage.objects.filter(slug=blog_path.split("/")[-1])

        for blog in blogs: