# Path: Blog/comments/models.py
# class Comment(models.Model):
#     user        = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)
#     content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
#     object_id = models.PositiveIntegerField()
#     content_object = GenericForeignKey('content_type', 'object_id')
#     parent = models.ForeignKey("self", blank=True, null=True)
# 
#     content     = models.TextField()
#     timestamp   = models.DateTimeField(auto_now_add=True)
# 
#     objects = CommentManager()
# 
#     class Meta:
#         ordering = ['-timestamp']
# 
#     def __unicode__(self):
#         return str(self.user.username)
# 
#     def __str__(self):
#         return str(self.user.username)
# 
#     def children(self):
#         return Comment.objects.filter(parent=self)
# 
#     def get_absolute_url(self):
#         return reverse("comments:thread", kwargs={"id":self.id})
# 
#     @property
#     def is_parent(self):
#         if self.parent is not None:
#             return False
#         else:
#             return True
# 
# Path: Blog/comments/forms.py
# class CommentForm(forms.Form):
#     content_type = forms.CharField(widget=forms.HiddenInput)
#     object_id = forms.IntegerField(widget=forms.HiddenInput)
#     # parent_id = forms.IntegerField(widget=forms.HiddenInput)
#     content = forms.CharField(widget=forms.Textarea)


from django.shortcuts import render, get_object_or_404
from django.contrib.contenttypes.models import ContentType
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect, HttpResponseForbidden
from .models import Comment
from .forms import CommentForm
        obj.delete()
        messages.success(request, "Comment deleted!")
        return HttpResponseRedirect(post_url)

    context = {
        "object":obj
    }
    return render(request, "comments/delete.html",context)

def comment_thread(request, id):
    comment = get_object_or_404(Comment, id=id)
    if not comment.is_parent:
        comment = comment.parent
    initial_data = {
        'content_type':comment.content_type,
        'object_id':comment.object_id
    }
    form = CommentForm(request.POST or None, initial=initial_data)
    if form.is_valid() and request.user.is_authenticated():
        ctype = form.cleaned_data.get("content_type")
        content_type = ContentType.objects.get(model=ctype)
        obj_id = form.cleaned_data.get("object_id")
        content = form.cleaned_data.get("content")
        parent_obj = None
        try:
            parent_id = int(request.POST.get("parent_id"))
        except:
            parent_id = None

        if parent_id:

# Path: Blog/comments/models.py
# class Comment(models.Model):
#     user        = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)
#     content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
#     object_id = models.PositiveIntegerField()
#     content_object = GenericForeignKey('content_type', 'object_id')
#     parent = models.ForeignKey("self", blank=True, null=True)
# 
#     content     = models.TextField()
#     timestamp   = models.DateTimeField(auto_now_add=True)
# 
#     objects = CommentManager()
# 
#     class Meta:
#         ordering = ['-timestamp']
# 
#     def __unicode__(self):
#         return str(self.user.username)
# 
#     def __str__(self):
#         return str(self.user.username)
# 
#     def children(self):
#         return Comment.objects.filter(parent=self)
# 
#     def get_absolute_url(self):
#         return reverse("comments:thread", kwargs={"id":self.id})
# 
#     @property
#     def is_parent(self):
#         if self.parent is not None:
#             return False
#         else:
#             return True
# 
# Path: Blog/comments/forms.py
# class CommentForm(forms.Form):
#     content_type = forms.CharField(widget=forms.HiddenInput)
#     object_id = forms.IntegerField(widget=forms.HiddenInput)
#     # parent_id = forms.IntegerField(widget=forms.HiddenInput)
#     content = forms.CharField(widget=forms.Textarea)


# Path: Blog/comments/views.py
from django.shortcuts import render, get_object_or_404
from django.contrib.contenttypes.models import ContentType
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect, HttpResponseForbidden
from .models import Comment
from .forms import CommentForm

        obj.delete()
        messages.success(request, "Comment deleted!")
        return HttpResponseRedirect(post_url)

    context = {
        "object":obj
    }
    return render(request, "comments/delete.html",context)

def comment_thread(request, id):
    comment = get_object_or_404(Comment, id=id)
    if not comment.is_parent:
        comment = comment.parent
    initial_data = {
        'content_type':comment.content_type,
        'object_id':comment.object_id
    }
    form = CommentForm(request.POST or None, initial=initial_data)
    if form.is_valid() and request.user.is_authenticated():
        ctype = form.cleaned_data.get("content_type")
        content_type = ContentType.objects.get(model=ctype)
        obj_id = form.cleaned_data.get("object_id")
        content = form.cleaned_data.get("content")
        parent_obj = None
        try:
            parent_id = int(request.POST.get("parent_id"))
        except:
            parent_id = None

        if parent_id:

            parent_qs = Comment.objects.filter(id=parent_id)