annotate comments/forms.py @ 1087:7fb9bc5a1232

Add progress bar to donations page.
author Brian Neal <bgneal@gmail.com>
date Tue, 03 May 2016 21:17:28 -0500
parents 21c592cac71c
children
rev   line source
gremmie@1 1 """
gremmie@1 2 Forms for the comments application.
gremmie@1 3 """
gremmie@1 4 import datetime
gremmie@1 5 from django import forms
gremmie@1 6 from django.conf import settings
gremmie@1 7 from django.contrib.contenttypes.models import ContentType
gremmie@1 8
gremmie@1 9 from comments.models import Comment
bgneal@963 10 from core.html import ImageCheckError
bgneal@963 11 from core.html import image_check
bgneal@963 12 from core.markup import site_markup
bgneal@963 13
gremmie@1 14
gremmie@1 15 COMMENT_MAX_LENGTH = getattr(settings, 'COMMENT_MAX_LENGTH', 3000)
gremmie@1 16
gremmie@1 17 class CommentForm(forms.Form):
gremmie@1 18 comment = forms.CharField(label='',
bgneal@312 19 min_length=1,
gremmie@1 20 max_length=COMMENT_MAX_LENGTH,
bgneal@134 21 widget=forms.Textarea(attrs={'class': 'markItUp smileyTarget'}))
gremmie@1 22 content_type = forms.CharField(widget=forms.HiddenInput)
gremmie@1 23 object_pk = forms.CharField(widget=forms.HiddenInput)
gremmie@1 24
gremmie@1 25 def __init__(self, target_object, data=None, initial=None):
gremmie@1 26 self.target_object = target_object
gremmie@1 27 if initial is None:
gremmie@1 28 initial = {}
gremmie@1 29 initial.update({
gremmie@1 30 'content_type': str(self.target_object._meta),
gremmie@1 31 'object_pk': str(self.target_object.pk),
gremmie@1 32 })
gremmie@1 33 super(CommentForm, self).__init__(data=data, initial=initial)
gremmie@1 34
gremmie@1 35 def get_comment_object(self, user, ip_address):
gremmie@1 36 """
gremmie@1 37 Return a new (unsaved) comment object based on the information in this
gremmie@1 38 form. Assumes that the form is already validated and will throw a
gremmie@1 39 ValueError if not.
gremmie@1 40 """
gremmie@1 41 if not self.is_valid():
gremmie@1 42 raise ValueError("get_comment_object may only be called on valid forms")
gremmie@1 43
gremmie@1 44 new = Comment(
gremmie@1 45 content_type = ContentType.objects.get_for_model(self.target_object),
gremmie@1 46 object_id = self.target_object.pk,
gremmie@1 47 user = user,
gremmie@1 48 comment = self.cleaned_data["comment"],
gremmie@1 49 ip_address = ip_address,
gremmie@1 50 is_public = True,
gremmie@1 51 is_removed = False,
gremmie@1 52 )
gremmie@1 53
gremmie@1 54 # Check that this comment isn't duplicate. (Sometimes people post comments
gremmie@1 55 # twice by mistake.) If it is, fail silently by returning the old comment.
gremmie@1 56 today = datetime.date.today()
gremmie@1 57 possible_duplicates = Comment.objects.filter(
gremmie@1 58 content_type = new.content_type,
gremmie@1 59 object_id = new.object_id,
gremmie@1 60 user = new.user,
gremmie@1 61 creation_date__year = today.year,
gremmie@1 62 creation_date__month = today.month,
gremmie@1 63 creation_date__day = today.day,
gremmie@1 64 )
gremmie@1 65 for old in possible_duplicates:
gremmie@1 66 if old.comment == new.comment:
gremmie@1 67 return old
gremmie@1 68
gremmie@1 69 return new
gremmie@1 70
bgneal@963 71 def clean_comment(self):
bgneal@963 72 comment = self.cleaned_data['comment']
bgneal@963 73 self.comment_html = None
bgneal@963 74 if comment:
bgneal@963 75 self.comment_html = site_markup(comment)
bgneal@963 76 try:
bgneal@963 77 image_check(self.comment_html)
bgneal@963 78 except ImageCheckError as ex:
bgneal@963 79 raise forms.ValidationError(str(ex))
bgneal@963 80
bgneal@963 81 return comment