# Path: app_author/views.py
# def author_single_view(request, slug):
#     """
#     Render Single User
#     :param request:
#     :param slug:
#     :return:
#     """
#     author = get_object_or_404(Profile, slug=slug)
#     author_forum_list = Forum.objects.filter(forum_author=author.id).order_by("-is_created")[:10]
#     author_comments = Comment.objects.filter(comment_author=author.id).order_by("-is_created")[:10]
#     total_forums = Forum.objects.filter(forum_author=author.id).annotate(num_comments=Count('forum_author'))
#     total_comments = Comment.objects.filter(comment_author=author.id).annotate(num_comments=Count('comment_author'))
#     template = 'app_author/author_single.html'
#     context = {
#         'author': author,
#         'author_forum_list': author_forum_list,
#         'author_comments': author_comments,
#         'total_forums': total_forums,
#         'total_comments': total_comments
#     }
#     return render(request, template, context)
# 
# @login_required
# def author_edit_view(request, slug):
#     """
#     Render User Edit Form
#     :param request:
#     :param slug:
#     :return:
#     """
#     author = get_object_or_404(Profile, slug=slug)
#     if request.method == "POST":
#         form = ProfileForm(request.POST, request.FILES, instance=author)
#         if form.is_valid():
#             author = form.save(commit=False)
#             author.save()
#             return redirect('author:author_single', slug=slug)
#     elif request.user != author.user:
#         return redirect('forum:forum_list')
#     else:
#         form = ProfileForm(instance=author)
#     return render(request, 'app_author/author_edit.html', {'author': author, 'form': form})


from django.contrib.auth.models import User
from django.test import TestCase, RequestFactory, Client
from django.urls import reverse
from app_author.views import author_single_view, author_edit_view



class AuthorViewTestCase(TestCase):

    def setUp(self):
        self.factory = RequestFactory()

    def test_retrieve_author_single_view(self):
        user = User.objects.create_user(
            username='john',
            email='john.doe@gmail.com',
            password='testing123',
        )

        url = reverse('author:author_single', args=[user.profile.slug])
        request = self.factory.get(path=url)

# Path: app_author/views.py
# def author_single_view(request, slug):
#     """
#     Render Single User
#     :param request:
#     :param slug:
#     :return:
#     """
#     author = get_object_or_404(Profile, slug=slug)
#     author_forum_list = Forum.objects.filter(forum_author=author.id).order_by("-is_created")[:10]
#     author_comments = Comment.objects.filter(comment_author=author.id).order_by("-is_created")[:10]
#     total_forums = Forum.objects.filter(forum_author=author.id).annotate(num_comments=Count('forum_author'))
#     total_comments = Comment.objects.filter(comment_author=author.id).annotate(num_comments=Count('comment_author'))
#     template = 'app_author/author_single.html'
#     context = {
#         'author': author,
#         'author_forum_list': author_forum_list,
#         'author_comments': author_comments,
#         'total_forums': total_forums,
#         'total_comments': total_comments
#     }
#     return render(request, template, context)
# 
# @login_required
# def author_edit_view(request, slug):
#     """
#     Render User Edit Form
#     :param request:
#     :param slug:
#     :return:
#     """
#     author = get_object_or_404(Profile, slug=slug)
#     if request.method == "POST":
#         form = ProfileForm(request.POST, request.FILES, instance=author)
#         if form.is_valid():
#             author = form.save(commit=False)
#             author.save()
#             return redirect('author:author_single', slug=slug)
#     elif request.user != author.user:
#         return redirect('forum:forum_list')
#     else:
#         form = ProfileForm(instance=author)
#     return render(request, 'app_author/author_edit.html', {'author': author, 'form': form})


# Path: app_author/tests/test_views.py
from django.contrib.auth.models import User
from django.test import TestCase, RequestFactory, Client
from django.urls import reverse
from app_author.views import author_single_view, author_edit_view




class AuthorViewTestCase(TestCase):

    def setUp(self):
        self.factory = RequestFactory()

    def test_retrieve_author_single_view(self):
        user = User.objects.create_user(
            username='john',
            email='john.doe@gmail.com',
            password='testing123',
        )

        url = reverse('author:author_single', args=[user.profile.slug])
        request = self.factory.get(path=url)

        response = author_single_view(request, user.profile.slug)