# Path: boards/forms.py
# class PostForm(forms.ModelForm):
#     class Meta:
#         model = Post
#         fields = ['message', ]
# 
# Path: boards/models.py
# class Board(models.Model):
#     name = models.CharField(max_length=30, unique=True)
#     description = models.CharField(max_length=100)
# 
#     def __str__(self):
#         return self.name
# 
#     def get_posts_count(self):
#         return Post.objects.filter(topic__board=self).count()
# 
#     def get_last_post(self):
#         return Post.objects.filter(topic__board=self).order_by('-created_at').first()
# 
# class Post(models.Model):
#     message = models.TextField(max_length=4000)
#     topic = models.ForeignKey(Topic, related_name='posts')
#     created_at = models.DateTimeField(auto_now_add=True)
#     updated_at = models.DateTimeField(null=True)
#     created_by = models.ForeignKey(User, related_name='posts')
#     updated_by = models.ForeignKey(User, null=True, related_name='+')
# 
#     def __str__(self):
#         truncated_message = Truncator(self.message)
#         return truncated_message.chars(30)
# 
#     def get_message_as_markdown(self):
#         return mark_safe(markdown(self.message, safe_mode='escape'))
# 
# class Topic(models.Model):
#     subject = models.CharField(max_length=255)
#     last_updated = models.DateTimeField(auto_now_add=True)
#     board = models.ForeignKey(Board, related_name='topics')
#     starter = models.ForeignKey(User, related_name='topics')
#     views = models.PositiveIntegerField(default=0)
# 
#     def __str__(self):
#         return self.subject
# 
#     def get_page_count(self):
#         count = self.posts.count()
#         pages = count / 20
#         return math.ceil(pages)
# 
#     def has_many_pages(self, count=None):
#         if count is None:
#             count = self.get_page_count()
#         return count > 6
# 
#     def get_page_range(self):
#         count = self.get_page_count()
#         if self.has_many_pages(count):
#             return range(1, 5)
#         return range(1, count + 1)
# 
#     def get_last_ten_posts(self):
#         return self.posts.order_by('-created_at')[:10]
# 
# Path: boards/views.py
# @login_required
# def reply_topic(request, pk, topic_pk):
#     topic = get_object_or_404(Topic, board__pk=pk, pk=topic_pk)
#     if request.method == 'POST':
#         form = PostForm(request.POST)
#         if form.is_valid() and recaptcha_is_valid(request):
#             post = form.save(commit=False)
#             post.topic = topic
#             post.created_by = request.user
#             post.save()
# 
#             topic.last_updated = timezone.now()
#             topic.save()
# 
#             topic_url = reverse('topic_posts', kwargs={'pk': pk, 'topic_pk': topic_pk})
#             topic_post_url = '{url}?page={page}#{id}'.format(
#                 url=topic_url,
#                 id=post.pk,
#                 page=topic.get_page_count()
#             )
# 
#             return redirect(topic_post_url)
#     else:
#         form = PostForm()
#     return render(request, 'reply_topic.html', {'topic': topic, 'form': form})


from django.contrib.auth.models import User
from django.test import TestCase
from django.urls import resolve, reverse
from ..forms import PostForm
from ..models import Board, Post, Topic
from ..views import reply_topic
        self.assertIsInstance(form, PostForm)

    def test_form_inputs(self):
        '''
        The view must contain two inputs: csrf, message textarea
        '''
        self.assertContains(self.response, '<input', 1)
        self.assertContains(self.response, '<textarea', 1)


class SuccessfulReplyTopicTests(ReplyTopicTestCase):
    def setUp(self):
        super().setUp()
        self.client.login(username=self.username, password=self.password)
        self.response = self.client.post(self.url, {'message': 'hello, world!'})

    def test_redirection(self):
        '''
        A valid form submission should redirect the user
        '''
        url = reverse('topic_posts', kwargs={'pk': self.board.pk, 'topic_pk': self.topic.pk})
        topic_posts_url = '{url}?page=1#2'.format(url=url)
        self.assertRedirects(self.response, topic_posts_url)

    def test_reply_created(self):
        '''
        The total post count should be 2
        The one created in the `ReplyTopicTestCase` setUp
        and another created by the post data in this class
        '''

# Path: boards/forms.py
# class PostForm(forms.ModelForm):
#     class Meta:
#         model = Post
#         fields = ['message', ]
# 
# Path: boards/models.py
# class Board(models.Model):
#     name = models.CharField(max_length=30, unique=True)
#     description = models.CharField(max_length=100)
# 
#     def __str__(self):
#         return self.name
# 
#     def get_posts_count(self):
#         return Post.objects.filter(topic__board=self).count()
# 
#     def get_last_post(self):
#         return Post.objects.filter(topic__board=self).order_by('-created_at').first()
# 
# class Post(models.Model):
#     message = models.TextField(max_length=4000)
#     topic = models.ForeignKey(Topic, related_name='posts')
#     created_at = models.DateTimeField(auto_now_add=True)
#     updated_at = models.DateTimeField(null=True)
#     created_by = models.ForeignKey(User, related_name='posts')
#     updated_by = models.ForeignKey(User, null=True, related_name='+')
# 
#     def __str__(self):
#         truncated_message = Truncator(self.message)
#         return truncated_message.chars(30)
# 
#     def get_message_as_markdown(self):
#         return mark_safe(markdown(self.message, safe_mode='escape'))
# 
# class Topic(models.Model):
#     subject = models.CharField(max_length=255)
#     last_updated = models.DateTimeField(auto_now_add=True)
#     board = models.ForeignKey(Board, related_name='topics')
#     starter = models.ForeignKey(User, related_name='topics')
#     views = models.PositiveIntegerField(default=0)
# 
#     def __str__(self):
#         return self.subject
# 
#     def get_page_count(self):
#         count = self.posts.count()
#         pages = count / 20
#         return math.ceil(pages)
# 
#     def has_many_pages(self, count=None):
#         if count is None:
#             count = self.get_page_count()
#         return count > 6
# 
#     def get_page_range(self):
#         count = self.get_page_count()
#         if self.has_many_pages(count):
#             return range(1, 5)
#         return range(1, count + 1)
# 
#     def get_last_ten_posts(self):
#         return self.posts.order_by('-created_at')[:10]
# 
# Path: boards/views.py
# @login_required
# def reply_topic(request, pk, topic_pk):
#     topic = get_object_or_404(Topic, board__pk=pk, pk=topic_pk)
#     if request.method == 'POST':
#         form = PostForm(request.POST)
#         if form.is_valid() and recaptcha_is_valid(request):
#             post = form.save(commit=False)
#             post.topic = topic
#             post.created_by = request.user
#             post.save()
# 
#             topic.last_updated = timezone.now()
#             topic.save()
# 
#             topic_url = reverse('topic_posts', kwargs={'pk': pk, 'topic_pk': topic_pk})
#             topic_post_url = '{url}?page={page}#{id}'.format(
#                 url=topic_url,
#                 id=post.pk,
#                 page=topic.get_page_count()
#             )
# 
#             return redirect(topic_post_url)
#     else:
#         form = PostForm()
#     return render(request, 'reply_topic.html', {'topic': topic, 'form': form})


# Path: boards/tests/test_view_reply_topic.py
from django.contrib.auth.models import User
from django.test import TestCase
from django.urls import resolve, reverse
from ..forms import PostForm
from ..models import Board, Post, Topic
from ..views import reply_topic

        self.assertIsInstance(form, PostForm)

    def test_form_inputs(self):
        '''
        The view must contain two inputs: csrf, message textarea
        '''
        self.assertContains(self.response, '<input', 1)
        self.assertContains(self.response, '<textarea', 1)


class SuccessfulReplyTopicTests(ReplyTopicTestCase):
    def setUp(self):
        super().setUp()
        self.client.login(username=self.username, password=self.password)
        self.response = self.client.post(self.url, {'message': 'hello, world!'})

    def test_redirection(self):
        '''
        A valid form submission should redirect the user
        '''
        url = reverse('topic_posts', kwargs={'pk': self.board.pk, 'topic_pk': self.topic.pk})
        topic_posts_url = '{url}?page=1#2'.format(url=url)
        self.assertRedirects(self.response, topic_posts_url)

    def test_reply_created(self):
        '''
        The total post count should be 2
        The one created in the `ReplyTopicTestCase` setUp
        and another created by the post data in this class
        '''

        self.assertEquals(Post.objects.count(), 2)