# Path: comments/models.py
# class Comment(Content):
#     # The maximum length of 'text' is 6100 because a proposal can have 6000
#     # characters in its body and 100 in its title. Because amendments are
#     # stored as Comments, they must be at least as long as Proposals.
#     text = models.CharField(max_length=6100)
#     proposal = models.ForeignKey('proposals.Proposal', null=False,
#                                  related_name="comments")
#     replyTo = models.ForeignKey("self", null=True)
#     isAmendment = models.BooleanField(default=False)
#     votes = GenericRelation(Vote)
# 
#     objects = CommentManager()
# 
#     def get_content_type():
#         if not hasattr(Comment, '_content_type'):
#             Comment._content_type = ContentType.objects.get(
#                 app_label="comments", model="comment")
#         return Comment._content_type
# 
#     def save(self, *args, **kwargs):
#         is_initial = not self.pk
#         super(Comment, self).save(*args, **kwargs)
#         # when the comment is first created, add a vote by the commenter
#         if is_initial:
#             Vote.objects.create(user=self.user, content=self, isVoteUp=True)
# 
#     def is_new(self):
#         """
#         :return: True if the comment is less than 5 minutes old
#         """
#         timesince = datetime.datetime.now() - self.createdAt
#         return timesince < datetime.timedelta(minutes=5)
# 
#     def get_replies(self, sort="popularity"):
#         if sort == "popularity":
#             return Comment.objects.filter(replyTo=self).filter(isHidden=False)\
#                 .select_related('user').order_by("rank")
#         elif sort == "chronological":
#             return Comment.objects.filter(replyTo=self).filter(isHidden=False)\
#                 .select_related('user').order_by("createdAt")
# 
#     def get_score(self):
#         return self.upVotes - self.downVotes
# 
#     def __unicode__(self):
#         return "%s" % self.text
# 
# Path: comments/forms.py
# class CommentForm(SwearFilteredModelForm):
#     text = forms.CharField(widget=forms.Textarea(
#         attrs={"class": "form-control",
#                "rows": "3",
#                "maxlength": "6000",
#                "onkeyup": "countChars(this, 6000)"}))
# 
#     class Meta:
#         model = Comment
#         fields = ['text']
# 
# Path: users/views.py
# def request_login(request):
#     messages.add_message(request, messages.INFO,
#                          "You must be logged in to do this")
#     return HttpResponseRedirect(reverse('frontpage'))


from django.shortcuts import render
from django.contrib import messages
from django.http import HttpResponseRedirect, Http404
from django.core.urlresolvers import reverse
from django.contrib.auth import get_user_model
from comments.models import Comment
from comments.forms import CommentForm
from users.views import request_login
                             messages.ERROR,
                             "You may only delete your own comments. "
                             "Please submit a report to request another "
                             "user's comment be hidden.")
        return HttpResponseRedirect(reverse('proposal',
                                            args=[
                                                str(comment.proposal.id),
                                                comment.proposal.slug
                                            ]))
    if request.method == 'POST':
        if request.POST['action'] == 'delete':
            if comment.get_replies().count() > 0:
                comment.text = "This comment has been deleted by its creator."
                comment.user = get_user_model().objects.get_deleted_content_user()
                comment.save()
                messages.add_message(request, messages.SUCCESS, "Comment deleted")
                return HttpResponseRedirect(reverse("proposal",
                                                    kwargs={"proposal_id":
                                                            comment.proposal.id,
                                                            "slug":
                                                            comment.proposal.slug})
                                            + "#comment_" + str(comment.id))
            else:
                comment.delete()
                messages.add_message(request, messages.SUCCESS, "Comment deleted")
                return HttpResponseRedirect(reverse("frontpage"))
    else:
        if request.is_ajax():
            extend_template = "ajax_base.html"
        else:

# Path: comments/models.py
# class Comment(Content):
#     # The maximum length of 'text' is 6100 because a proposal can have 6000
#     # characters in its body and 100 in its title. Because amendments are
#     # stored as Comments, they must be at least as long as Proposals.
#     text = models.CharField(max_length=6100)
#     proposal = models.ForeignKey('proposals.Proposal', null=False,
#                                  related_name="comments")
#     replyTo = models.ForeignKey("self", null=True)
#     isAmendment = models.BooleanField(default=False)
#     votes = GenericRelation(Vote)
# 
#     objects = CommentManager()
# 
#     def get_content_type():
#         if not hasattr(Comment, '_content_type'):
#             Comment._content_type = ContentType.objects.get(
#                 app_label="comments", model="comment")
#         return Comment._content_type
# 
#     def save(self, *args, **kwargs):
#         is_initial = not self.pk
#         super(Comment, self).save(*args, **kwargs)
#         # when the comment is first created, add a vote by the commenter
#         if is_initial:
#             Vote.objects.create(user=self.user, content=self, isVoteUp=True)
# 
#     def is_new(self):
#         """
#         :return: True if the comment is less than 5 minutes old
#         """
#         timesince = datetime.datetime.now() - self.createdAt
#         return timesince < datetime.timedelta(minutes=5)
# 
#     def get_replies(self, sort="popularity"):
#         if sort == "popularity":
#             return Comment.objects.filter(replyTo=self).filter(isHidden=False)\
#                 .select_related('user').order_by("rank")
#         elif sort == "chronological":
#             return Comment.objects.filter(replyTo=self).filter(isHidden=False)\
#                 .select_related('user').order_by("createdAt")
# 
#     def get_score(self):
#         return self.upVotes - self.downVotes
# 
#     def __unicode__(self):
#         return "%s" % self.text
# 
# Path: comments/forms.py
# class CommentForm(SwearFilteredModelForm):
#     text = forms.CharField(widget=forms.Textarea(
#         attrs={"class": "form-control",
#                "rows": "3",
#                "maxlength": "6000",
#                "onkeyup": "countChars(this, 6000)"}))
# 
#     class Meta:
#         model = Comment
#         fields = ['text']
# 
# Path: users/views.py
# def request_login(request):
#     messages.add_message(request, messages.INFO,
#                          "You must be logged in to do this")
#     return HttpResponseRedirect(reverse('frontpage'))


# Path: comments/views.py
from django.shortcuts import render
from django.contrib import messages
from django.http import HttpResponseRedirect, Http404
from django.core.urlresolvers import reverse
from django.contrib.auth import get_user_model
from comments.models import Comment
from comments.forms import CommentForm
from users.views import request_login

                             messages.ERROR,
                             "You may only delete your own comments. "
                             "Please submit a report to request another "
                             "user's comment be hidden.")
        return HttpResponseRedirect(reverse('proposal',
                                            args=[
                                                str(comment.proposal.id),
                                                comment.proposal.slug
                                            ]))
    if request.method == 'POST':
        if request.POST['action'] == 'delete':
            if comment.get_replies().count() > 0:
                comment.text = "This comment has been deleted by its creator."
                comment.user = get_user_model().objects.get_deleted_content_user()
                comment.save()
                messages.add_message(request, messages.SUCCESS, "Comment deleted")
                return HttpResponseRedirect(reverse("proposal",
                                                    kwargs={"proposal_id":
                                                            comment.proposal.id,
                                                            "slug":
                                                            comment.proposal.slug})
                                            + "#comment_" + str(comment.id))
            else:
                comment.delete()
                messages.add_message(request, messages.SUCCESS, "Comment deleted")
                return HttpResponseRedirect(reverse("frontpage"))
    else:
        if request.is_ajax():
            extend_template = "ajax_base.html"
        else:

            extend_template = "base.html"