# Path: aldryn_blog/models.py
# class Post(models.Model):
# 
#     title = models.CharField(_('Title'), max_length=255)
#     slug = models.SlugField(
#         verbose_name=_('Slug'),
#         max_length=255,
#         unique=True,
#         blank=True,
#         help_text=_('Used in the URL. If changed, the URL will change. Clean it to have it re-created.')
#     )
#     language = models.CharField(
#         verbose_name=_('language'),
#         max_length=5,
#         null=True,
#         blank=True,
#         choices=settings.LANGUAGES,
#         help_text=_('leave empty to display in all languages')
#     )
#     key_visual = FilerImageField(verbose_name=_('Key Visual'), blank=True, null=True)
#     lead_in = HTMLField(
#         verbose_name=_('Lead-in'),
#         help_text=_('Will be displayed in lists, and at the start of the detail page (in bold)')
#     )
#     content = PlaceholderField('aldryn_blog_post_content', related_name='aldryn_blog_posts')
#     author = models.ForeignKey(to=AUTH_USER_MODEL, verbose_name=_('Author'))
#     coauthors = models.ManyToManyField(
#         to=AUTH_USER_MODEL,
#         verbose_name=_('Co-Authors'),
#         null=True,
#         blank=True,
#         related_name='aldryn_blog_coauthors'
#     )
#     publication_start = models.DateTimeField(
#         verbose_name=_('Published Since'),
#         default=timezone.now,
#         help_text=_('Used in the URL. If changed, the URL will change.')
#     )
#     publication_end = models.DateTimeField(
#         verbose_name=_('Published Until'),
#         null=True,
#         blank=True
#     )
#     category = models.ForeignKey(
#         to=Category,
#         verbose_name=_('Category'),
#         null=True,
#         blank=True
#     )
# 
#     objects = RelatedManager()
#     published = PublishedManager()
#     tags = TaggableManager(blank=True)
#     app_data = AppDataField()
# 
#     def __unicode__(self):
#         return self.title
# 
#     def get_absolute_url(self):
#         kwargs = {
#             'year': self.publication_start.year,
#             'month': self.publication_start.month,
#             'day': self.publication_start.day,
#             'slug': self.slug
#         }
# 
#         if self.language and not getattr(settings, 'ALDRYN_BLOG_SHOW_ALL_LANGUAGES', False):
#             with override(self.language):
#                 return reverse('aldryn_blog:post-detail', kwargs=kwargs)
#         return reverse('aldryn_blog:post-detail', kwargs=kwargs)
# 
#     class Meta:
#         ordering = ['-publication_start']
# 
#     def save(self, **kwargs):
#         if not self.slug:
#             self.slug = slugify(self.title)
#         return super(Post, self).save(**kwargs)
# 
#     def get_author_slug(self):
#         # FIXME: This is a potential performance hogger
#         return get_slug_for_user(self.author)
# 
# class Category(TranslatableModel):
# 
#     translations = TranslatedFields(
#         name=models.CharField(_('Name'), max_length=255),
#         slug=models.SlugField(
#             verbose_name=_('Slug'),
#             max_length=255,
#             blank=True,
#             help_text=_('Auto-generated. Clean it to have it re-created. '
#                         'WARNING! Used in the URL. If changed, the URL will change. ')
#         ),
#         meta={'unique_together': [['slug', 'language_code']]}
#     )
# 
#     ordering = models.IntegerField(_('Ordering'), default=0)
# 
#     objects = CategoryManager()
# 
#     class Meta:
#         verbose_name = _('Category')
#         verbose_name_plural = _('Categories')
#         ordering = ['ordering']
# 
#     def __unicode__(self):
#         return self.lazy_translation_getter('name', str(self.pk))
# 
#     def get_absolute_url(self, language=None):
#         language = language or get_current_language()
#         slug = get_slug_in_language(self, language)
#         with override(language):
#             if slug:
#                 return reverse('aldryn_blog:category-posts', kwargs={'category': slug})
# 
#             # category not translated in given language
#             try:
#                 return reverse('aldryn_blog:latest-posts')
#             except (ImproperlyConfigured, NoReverseMatch):
#                 return '/%s' % language


from django.conf import settings
from django.core.urlresolvers import reverse
from django.test import TestCase
from django.contrib.auth.models import User
from django.utils import timezone
from django.utils.translation import override
from cms import api
from cms.utils import get_cms_setting
from .models import Post, Category
class PostAddTest(TestCase):
    su_username = 'user'
    su_password = 'pass'

    def setUp(self):
        self.template = get_cms_setting('TEMPLATES')[0][0]
        self.language = settings.LANGUAGES[0][0]
        self.page = api.create_page(
            'page', self.template, self.language, published=True,
            apphook='BlogApp', apphook_namespace='Blog'
        )
        api.create_title('fr', 'french page', self.page)
        self.page.publish('fr')
        self.placeholder = self.page.placeholders.all()[0]
        self.user = User.objects.create(first_name='Peter', last_name='Muster')

    def test_create_post(self):
        """
        We can create a post
        """
        title = 'First'
        post = Post.objects.create(title=title, slug='first-blog', author=self.user)
        response = self.client.get(post.get_absolute_url())
        self.assertContains(response, title)

    def test_delete_post(self):
        """
        We can delete a post
        """
        title = 'Delete Post'

# Path: aldryn_blog/models.py
# class Post(models.Model):
# 
#     title = models.CharField(_('Title'), max_length=255)
#     slug = models.SlugField(
#         verbose_name=_('Slug'),
#         max_length=255,
#         unique=True,
#         blank=True,
#         help_text=_('Used in the URL. If changed, the URL will change. Clean it to have it re-created.')
#     )
#     language = models.CharField(
#         verbose_name=_('language'),
#         max_length=5,
#         null=True,
#         blank=True,
#         choices=settings.LANGUAGES,
#         help_text=_('leave empty to display in all languages')
#     )
#     key_visual = FilerImageField(verbose_name=_('Key Visual'), blank=True, null=True)
#     lead_in = HTMLField(
#         verbose_name=_('Lead-in'),
#         help_text=_('Will be displayed in lists, and at the start of the detail page (in bold)')
#     )
#     content = PlaceholderField('aldryn_blog_post_content', related_name='aldryn_blog_posts')
#     author = models.ForeignKey(to=AUTH_USER_MODEL, verbose_name=_('Author'))
#     coauthors = models.ManyToManyField(
#         to=AUTH_USER_MODEL,
#         verbose_name=_('Co-Authors'),
#         null=True,
#         blank=True,
#         related_name='aldryn_blog_coauthors'
#     )
#     publication_start = models.DateTimeField(
#         verbose_name=_('Published Since'),
#         default=timezone.now,
#         help_text=_('Used in the URL. If changed, the URL will change.')
#     )
#     publication_end = models.DateTimeField(
#         verbose_name=_('Published Until'),
#         null=True,
#         blank=True
#     )
#     category = models.ForeignKey(
#         to=Category,
#         verbose_name=_('Category'),
#         null=True,
#         blank=True
#     )
# 
#     objects = RelatedManager()
#     published = PublishedManager()
#     tags = TaggableManager(blank=True)
#     app_data = AppDataField()
# 
#     def __unicode__(self):
#         return self.title
# 
#     def get_absolute_url(self):
#         kwargs = {
#             'year': self.publication_start.year,
#             'month': self.publication_start.month,
#             'day': self.publication_start.day,
#             'slug': self.slug
#         }
# 
#         if self.language and not getattr(settings, 'ALDRYN_BLOG_SHOW_ALL_LANGUAGES', False):
#             with override(self.language):
#                 return reverse('aldryn_blog:post-detail', kwargs=kwargs)
#         return reverse('aldryn_blog:post-detail', kwargs=kwargs)
# 
#     class Meta:
#         ordering = ['-publication_start']
# 
#     def save(self, **kwargs):
#         if not self.slug:
#             self.slug = slugify(self.title)
#         return super(Post, self).save(**kwargs)
# 
#     def get_author_slug(self):
#         # FIXME: This is a potential performance hogger
#         return get_slug_for_user(self.author)
# 
# class Category(TranslatableModel):
# 
#     translations = TranslatedFields(
#         name=models.CharField(_('Name'), max_length=255),
#         slug=models.SlugField(
#             verbose_name=_('Slug'),
#             max_length=255,
#             blank=True,
#             help_text=_('Auto-generated. Clean it to have it re-created. '
#                         'WARNING! Used in the URL. If changed, the URL will change. ')
#         ),
#         meta={'unique_together': [['slug', 'language_code']]}
#     )
# 
#     ordering = models.IntegerField(_('Ordering'), default=0)
# 
#     objects = CategoryManager()
# 
#     class Meta:
#         verbose_name = _('Category')
#         verbose_name_plural = _('Categories')
#         ordering = ['ordering']
# 
#     def __unicode__(self):
#         return self.lazy_translation_getter('name', str(self.pk))
# 
#     def get_absolute_url(self, language=None):
#         language = language or get_current_language()
#         slug = get_slug_in_language(self, language)
#         with override(language):
#             if slug:
#                 return reverse('aldryn_blog:category-posts', kwargs={'category': slug})
# 
#             # category not translated in given language
#             try:
#                 return reverse('aldryn_blog:latest-posts')
#             except (ImproperlyConfigured, NoReverseMatch):
#                 return '/%s' % language


# Path: aldryn_blog/tests.py
from django.conf import settings
from django.core.urlresolvers import reverse
from django.test import TestCase
from django.contrib.auth.models import User
from django.utils import timezone
from django.utils.translation import override
from cms import api
from cms.utils import get_cms_setting
from .models import Post, Category

class PostAddTest(TestCase):
    su_username = 'user'
    su_password = 'pass'

    def setUp(self):
        self.template = get_cms_setting('TEMPLATES')[0][0]
        self.language = settings.LANGUAGES[0][0]
        self.page = api.create_page(
            'page', self.template, self.language, published=True,
            apphook='BlogApp', apphook_namespace='Blog'
        )
        api.create_title('fr', 'french page', self.page)
        self.page.publish('fr')
        self.placeholder = self.page.placeholders.all()[0]
        self.user = User.objects.create(first_name='Peter', last_name='Muster')

    def test_create_post(self):
        """
        We can create a post
        """
        title = 'First'
        post = Post.objects.create(title=title, slug='first-blog', author=self.user)
        response = self.client.get(post.get_absolute_url())
        self.assertContains(response, title)

    def test_delete_post(self):
        """
        We can delete a post
        """
        title = 'Delete Post'

        Post.objects.create(title=title, author=self.user)