# 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
    if not request.user == comment.user:
        messages.add_message(request,
                             messages.ERROR,
                             "You can only edit your own comments.")
        return HttpResponseRedirect(reverse('proposal',
                                            args=[
                                                str(comment.proposal.id),
                                                comment.proposal.slug
                                            ]))
    else:
        if request.method == "POST":
            if comment.is_new():
                form = CommentForm(request.POST, instance=comment)
                if form.is_valid():
                    form.save()
                    messages.add_message(request,
                                         messages.SUCCESS,
                                         "Comment edited.")
                    return HttpResponseRedirect(reverse("proposal", args=[
                        str(comment.proposal.id), comment.proposal.slug
                    ]) + "#comment_" + str(comment.id))
                else:
                    messages.add_message(request,
                                         messages.ERROR,
                                         "Invalid comment")
            else:
                messages.add_message(request,
                                     messages.ERROR,
                                     "You can only edit a comment in the "
                                     "first 5 minutes.")

# 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

    if not request.user == comment.user:
        messages.add_message(request,
                             messages.ERROR,
                             "You can only edit your own comments.")
        return HttpResponseRedirect(reverse('proposal',
                                            args=[
                                                str(comment.proposal.id),
                                                comment.proposal.slug
                                            ]))
    else:
        if request.method == "POST":
            if comment.is_new():
                form = CommentForm(request.POST, instance=comment)
                if form.is_valid():
                    form.save()
                    messages.add_message(request,
                                         messages.SUCCESS,
                                         "Comment edited.")
                    return HttpResponseRedirect(reverse("proposal", args=[
                        str(comment.proposal.id), comment.proposal.slug
                    ]) + "#comment_" + str(comment.id))
                else:
                    messages.add_message(request,
                                         messages.ERROR,
                                         "Invalid comment")
            else:
                messages.add_message(request,
                                     messages.ERROR,
                                     "You can only edit a comment in the "
                                     "first 5 minutes.")

        form = CommentForm(instance=comment)