# Path: blog/models.py
# class Post(models.Model):
#     """
#     Represents a blog post
#     """
#     title = models.CharField(verbose_name=_('Title'), max_length=200)
#     date_published = models.DateField(verbose_name=_('Date Published'), blank=True, null=True)
#     last_updated = models.DateTimeField(verbose_name=_('Last Updated'), auto_now=True)
#     # Translators: The last part of a web-page URL, usually derives from the page title, e.g. /about-us/
#     slug = models.SlugField(verbose_name=_('Slug'), max_length=200)
#     categories = models.ManyToManyField(Category, verbose_name=_('Categories'), related_name='posts', blank=True)
#     is_published = models.BooleanField(verbose_name=_('Published'), default=False)
#     is_featured = models.BooleanField(verbose_name=_('Featured'), default=False)
#     allow_comments = models.BooleanField(verbose_name=_('Allow comments'), default=True)
#     content = HTMLField(verbose_name=_('Post Content'))
#     featured_image = FileBrowseField(verbose_name=_('Featured Image'), max_length=1024,
#                                      extensions=['.jpg', '.jpeg', '.png'], blank=True)
#     meta_description = models.TextField(verbose_name=_('Description'), max_length=160, blank=True)
#     objects = PostQuerySet.as_manager()
# 
#     def __str__(self):
#         return self.title
# 
#     def save(self, *args, **kwargs):
#         # Auto-pupulate date_published for a newly published post
#         if self.is_published and not self.date_published:
#             self.date_published = timezone.now()
#         super().save(*args, **kwargs)
# 
#     def get_absolute_url(self):
#         return reverse('blog:post', kwargs={'slug': self.slug, 'pk': self.pk})
# 
#     class Meta:
#         verbose_name = _('Post')
#         # Translators: General plural without a number
#         verbose_name_plural = _('Posts')
#         ordering = ('-date_published', '-pk')
# 
# class Category(models.Model):
#     """
#     Represents a blog posts category
#     """
#     name = models.CharField(verbose_name=_('Category Name'), max_length=100, unique=True)
#     # Translators: The last part of a web-page URL, usually derives from the page title, e.g. about-us
#     slug = models.SlugField(verbose_name=_('Slug'), max_length=100, unique=True)
#     objects = CategoryQuerySet.as_manager()
# 
#     def get_published_posts_count(self):
#         return self.posts.filter(is_published=True).count()
# 
#     get_published_posts_count.short_description = _('Published Posts Count')
# 
#     def __str__(self):
#         return self.name
# 
#     def get_absolute_url(self):
#         return reverse('blog:category', kwargs={'slug': self.slug})
# 
#     class Meta:
#         verbose_name = _('Category')
#         # Translators: General plural without a number
#         verbose_name_plural = _('Categories')
#         ordering = ('name',)
# 
# Path: blog/templatetags/blog_tags.py
# @register.simple_tag
# def get_posts_digest(featured=False, posts_count=3):
#     """
#     Simple tag
# 
#     Get the lists of the latest posts (general of featured) for the blog sidebar
# 
#     :param featured: if ``True`` featured posts digest is returned
#     :param posts_count: the number of posts to include in a digest
#     :return: the digest of recent posts and "More" link
#     :rtype: :class:`SideBarObjects`
#     """
#     if featured:
#         posts = Post.objects.featured()
#         more_link = reverse('blog:featured_posts')
#     else:
#         posts = Post.objects.published()
#         more_link = reverse('blog:home')
#     more = more_link if posts.count() > posts_count else None
#     return SideBarObjects(posts[:posts_count], more)
# 
# @register.simple_tag
# def get_archive_digest(months_count=6):
#     """
#     Simple tag
# 
#     :param months_count: the number of month to include in a digest
#     :return: the list of the most recent months from the blog archive for the blog sidebar
#     :rtype: :class:`SideBarObjects`
#     """
#     months = Post.objects.published().dates(
#         'date_published', 'month', order='DESC')[:months_count + 1]
#     more = reverse('blog:archive') if len(months) > months_count else None
#     return SideBarObjects(months[:months_count], more)
# 
# @register.simple_tag
# def get_categories():
#     """
#     Simple tag
# 
#     :return: list of non-empty categories ordered by post count in desc. order
#     """
#     return Category.objects.ordered_by_post_count()
# 
# @register.simple_tag
# def get_blog_menu_links():
#     """
#     Simple tag
# 
#     :return: blog menu links for the site main menu.
#     """
#     featured = Post.objects.featured()
#     featured_link = reverse('blog:featured_posts') if featured.exists() else None
#     return (
#         MenuLink(_('Recent Posts'), reverse('blog:home')),
#         MenuLink(_('Featured Posts'), featured_link),
#         MenuLink(_('Categories'), reverse('blog:categories_list')),
#         MenuLink(_('Archive'), reverse('blog:archive'))
#     )


from datetime import date
from django.test import TestCase
from django.urls import reverse
from .models import Post, Category
from .templatetags.blog_tags import (get_posts_digest, get_archive_digest,
                                     get_categories, get_blog_menu_links)
# coding: utf-8
# Module: templatetags_tests
# Created on: 25.11.2015
# Author: Roman Miroshnychenko aka Roman V.M. (romanvm@yandex.ua)





class TemplateTagsTestCase(TestCase):
    def test_get_posts_digest(self):
        for i in range(1, 7):
            Post.objects.create(title='Post {}'.format(i),
                                date_published=date(year=2015, month=1, day=i),
                                slug='post-{}'.format(i),
                                content='<p>Lorem ipsum<p>')
        latest_posts = get_posts_digest(False, 5)
        self.assertEqual(len(latest_posts.objects), 0)

# Path: blog/models.py
# class Post(models.Model):
#     """
#     Represents a blog post
#     """
#     title = models.CharField(verbose_name=_('Title'), max_length=200)
#     date_published = models.DateField(verbose_name=_('Date Published'), blank=True, null=True)
#     last_updated = models.DateTimeField(verbose_name=_('Last Updated'), auto_now=True)
#     # Translators: The last part of a web-page URL, usually derives from the page title, e.g. /about-us/
#     slug = models.SlugField(verbose_name=_('Slug'), max_length=200)
#     categories = models.ManyToManyField(Category, verbose_name=_('Categories'), related_name='posts', blank=True)
#     is_published = models.BooleanField(verbose_name=_('Published'), default=False)
#     is_featured = models.BooleanField(verbose_name=_('Featured'), default=False)
#     allow_comments = models.BooleanField(verbose_name=_('Allow comments'), default=True)
#     content = HTMLField(verbose_name=_('Post Content'))
#     featured_image = FileBrowseField(verbose_name=_('Featured Image'), max_length=1024,
#                                      extensions=['.jpg', '.jpeg', '.png'], blank=True)
#     meta_description = models.TextField(verbose_name=_('Description'), max_length=160, blank=True)
#     objects = PostQuerySet.as_manager()
# 
#     def __str__(self):
#         return self.title
# 
#     def save(self, *args, **kwargs):
#         # Auto-pupulate date_published for a newly published post
#         if self.is_published and not self.date_published:
#             self.date_published = timezone.now()
#         super().save(*args, **kwargs)
# 
#     def get_absolute_url(self):
#         return reverse('blog:post', kwargs={'slug': self.slug, 'pk': self.pk})
# 
#     class Meta:
#         verbose_name = _('Post')
#         # Translators: General plural without a number
#         verbose_name_plural = _('Posts')
#         ordering = ('-date_published', '-pk')
# 
# class Category(models.Model):
#     """
#     Represents a blog posts category
#     """
#     name = models.CharField(verbose_name=_('Category Name'), max_length=100, unique=True)
#     # Translators: The last part of a web-page URL, usually derives from the page title, e.g. about-us
#     slug = models.SlugField(verbose_name=_('Slug'), max_length=100, unique=True)
#     objects = CategoryQuerySet.as_manager()
# 
#     def get_published_posts_count(self):
#         return self.posts.filter(is_published=True).count()
# 
#     get_published_posts_count.short_description = _('Published Posts Count')
# 
#     def __str__(self):
#         return self.name
# 
#     def get_absolute_url(self):
#         return reverse('blog:category', kwargs={'slug': self.slug})
# 
#     class Meta:
#         verbose_name = _('Category')
#         # Translators: General plural without a number
#         verbose_name_plural = _('Categories')
#         ordering = ('name',)
# 
# Path: blog/templatetags/blog_tags.py
# @register.simple_tag
# def get_posts_digest(featured=False, posts_count=3):
#     """
#     Simple tag
# 
#     Get the lists of the latest posts (general of featured) for the blog sidebar
# 
#     :param featured: if ``True`` featured posts digest is returned
#     :param posts_count: the number of posts to include in a digest
#     :return: the digest of recent posts and "More" link
#     :rtype: :class:`SideBarObjects`
#     """
#     if featured:
#         posts = Post.objects.featured()
#         more_link = reverse('blog:featured_posts')
#     else:
#         posts = Post.objects.published()
#         more_link = reverse('blog:home')
#     more = more_link if posts.count() > posts_count else None
#     return SideBarObjects(posts[:posts_count], more)
# 
# @register.simple_tag
# def get_archive_digest(months_count=6):
#     """
#     Simple tag
# 
#     :param months_count: the number of month to include in a digest
#     :return: the list of the most recent months from the blog archive for the blog sidebar
#     :rtype: :class:`SideBarObjects`
#     """
#     months = Post.objects.published().dates(
#         'date_published', 'month', order='DESC')[:months_count + 1]
#     more = reverse('blog:archive') if len(months) > months_count else None
#     return SideBarObjects(months[:months_count], more)
# 
# @register.simple_tag
# def get_categories():
#     """
#     Simple tag
# 
#     :return: list of non-empty categories ordered by post count in desc. order
#     """
#     return Category.objects.ordered_by_post_count()
# 
# @register.simple_tag
# def get_blog_menu_links():
#     """
#     Simple tag
# 
#     :return: blog menu links for the site main menu.
#     """
#     featured = Post.objects.featured()
#     featured_link = reverse('blog:featured_posts') if featured.exists() else None
#     return (
#         MenuLink(_('Recent Posts'), reverse('blog:home')),
#         MenuLink(_('Featured Posts'), featured_link),
#         MenuLink(_('Categories'), reverse('blog:categories_list')),
#         MenuLink(_('Archive'), reverse('blog:archive'))
#     )


# Path: blog/tests_templatetags.py
from datetime import date
from django.test import TestCase
from django.urls import reverse
from .models import Post, Category
from .templatetags.blog_tags import (get_posts_digest, get_archive_digest,
                                     get_categories, get_blog_menu_links)

# coding: utf-8
# Module: templatetags_tests
# Created on: 25.11.2015
# Author: Roman Miroshnychenko aka Roman V.M. (romanvm@yandex.ua)





class TemplateTagsTestCase(TestCase):
    def test_get_posts_digest(self):
        for i in range(1, 7):
            Post.objects.create(title='Post {}'.format(i),
                                date_published=date(year=2015, month=1, day=i),
                                slug='post-{}'.format(i),
                                content='<p>Lorem ipsum<p>')
        latest_posts = get_posts_digest(False, 5)
        self.assertEqual(len(latest_posts.objects), 0)

        posts = Post.objects.all()