# Path: boards/forms.py
# class NewTopicForm(forms.ModelForm):
#     message = forms.CharField(
#         widget=forms.Textarea(
#             attrs={'rows': 5, 'placeholder': 'What is in your mind?'}
#         ),
#         max_length=4000,
#         help_text='The max length of the text is 4000.'
#     )
# 
#     class Meta:
#         model = Topic
#         fields = ['subject', '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 new_topic(request, pk):
#     board = get_object_or_404(Board, pk=pk)
#     if request.method == 'POST':
#         form = NewTopicForm(request.POST)
#         if form.is_valid() and recaptcha_is_valid(request):
#             topic = form.save(commit=False)
#             topic.board = board
#             topic.starter = request.user
#             topic.save()
#             Post.objects.create(
#                 message=form.cleaned_data.get('message'),
#                 topic=topic,
#                 created_by=request.user
#             )
#             return redirect('topic_posts', pk=pk, topic_pk=topic.pk)
#     else:
#         form = NewTopicForm()
#     return render(request, 'new_topic.html', {'board': board, 'form': form})


from django.contrib.auth.models import User
from django.test import TestCase
from django.urls import resolve, reverse
from ..forms import NewTopicForm
from ..models import Board, Post, Topic
from ..views import new_topic
            'message': 'Lorem ipsum dolor sit amet'
        }
        self.client.post(url, data)
        self.assertTrue(Topic.objects.exists())
        self.assertTrue(Post.objects.exists())

    def test_new_topic_invalid_post_data(self):
        '''
        Invalid post data should not redirect
        The expected behavior is to show the form again with validation errors
        '''
        url = reverse('new_topic', kwargs={'pk': 1})
        response = self.client.post(url, {})
        form = response.context.get('form')
        self.assertEquals(response.status_code, 200)
        self.assertTrue(form.errors)

    def test_new_topic_invalid_post_data_empty_fields(self):
        '''
        Invalid post data should not redirect
        The expected behavior is to show the form again with validation errors
        '''
        url = reverse('new_topic', kwargs={'pk': 1})
        data = {
            'subject': '',
            'message': ''
        }
        response = self.client.post(url, data)
        self.assertEquals(response.status_code, 200)
        self.assertFalse(Topic.objects.exists())

# Path: boards/forms.py
# class NewTopicForm(forms.ModelForm):
#     message = forms.CharField(
#         widget=forms.Textarea(
#             attrs={'rows': 5, 'placeholder': 'What is in your mind?'}
#         ),
#         max_length=4000,
#         help_text='The max length of the text is 4000.'
#     )
# 
#     class Meta:
#         model = Topic
#         fields = ['subject', '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 new_topic(request, pk):
#     board = get_object_or_404(Board, pk=pk)
#     if request.method == 'POST':
#         form = NewTopicForm(request.POST)
#         if form.is_valid() and recaptcha_is_valid(request):
#             topic = form.save(commit=False)
#             topic.board = board
#             topic.starter = request.user
#             topic.save()
#             Post.objects.create(
#                 message=form.cleaned_data.get('message'),
#                 topic=topic,
#                 created_by=request.user
#             )
#             return redirect('topic_posts', pk=pk, topic_pk=topic.pk)
#     else:
#         form = NewTopicForm()
#     return render(request, 'new_topic.html', {'board': board, 'form': form})


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

            'message': 'Lorem ipsum dolor sit amet'
        }
        self.client.post(url, data)
        self.assertTrue(Topic.objects.exists())
        self.assertTrue(Post.objects.exists())

    def test_new_topic_invalid_post_data(self):
        '''
        Invalid post data should not redirect
        The expected behavior is to show the form again with validation errors
        '''
        url = reverse('new_topic', kwargs={'pk': 1})
        response = self.client.post(url, {})
        form = response.context.get('form')
        self.assertEquals(response.status_code, 200)
        self.assertTrue(form.errors)

    def test_new_topic_invalid_post_data_empty_fields(self):
        '''
        Invalid post data should not redirect
        The expected behavior is to show the form again with validation errors
        '''
        url = reverse('new_topic', kwargs={'pk': 1})
        data = {
            'subject': '',
            'message': ''
        }
        response = self.client.post(url, data)
        self.assertEquals(response.status_code, 200)
        self.assertFalse(Topic.objects.exists())

        self.assertFalse(Post.objects.exists())