gremmie@1: """ gremmie@1: Forms for the comments application. gremmie@1: """ gremmie@1: import datetime gremmie@1: from django import forms gremmie@1: from django.conf import settings gremmie@1: from django.contrib.contenttypes.models import ContentType gremmie@1: gremmie@1: from comments.models import Comment bgneal@963: from core.html import ImageCheckError bgneal@963: from core.html import image_check bgneal@963: from core.markup import site_markup bgneal@963: gremmie@1: gremmie@1: COMMENT_MAX_LENGTH = getattr(settings, 'COMMENT_MAX_LENGTH', 3000) gremmie@1: gremmie@1: class CommentForm(forms.Form): gremmie@1: comment = forms.CharField(label='', bgneal@312: min_length=1, gremmie@1: max_length=COMMENT_MAX_LENGTH, bgneal@134: widget=forms.Textarea(attrs={'class': 'markItUp smileyTarget'})) gremmie@1: content_type = forms.CharField(widget=forms.HiddenInput) gremmie@1: object_pk = forms.CharField(widget=forms.HiddenInput) gremmie@1: gremmie@1: def __init__(self, target_object, data=None, initial=None): gremmie@1: self.target_object = target_object gremmie@1: if initial is None: gremmie@1: initial = {} gremmie@1: initial.update({ gremmie@1: 'content_type': str(self.target_object._meta), gremmie@1: 'object_pk': str(self.target_object.pk), gremmie@1: }) gremmie@1: super(CommentForm, self).__init__(data=data, initial=initial) gremmie@1: gremmie@1: def get_comment_object(self, user, ip_address): gremmie@1: """ gremmie@1: Return a new (unsaved) comment object based on the information in this gremmie@1: form. Assumes that the form is already validated and will throw a gremmie@1: ValueError if not. gremmie@1: """ gremmie@1: if not self.is_valid(): gremmie@1: raise ValueError("get_comment_object may only be called on valid forms") gremmie@1: gremmie@1: new = Comment( gremmie@1: content_type = ContentType.objects.get_for_model(self.target_object), gremmie@1: object_id = self.target_object.pk, gremmie@1: user = user, gremmie@1: comment = self.cleaned_data["comment"], gremmie@1: ip_address = ip_address, gremmie@1: is_public = True, gremmie@1: is_removed = False, gremmie@1: ) gremmie@1: gremmie@1: # Check that this comment isn't duplicate. (Sometimes people post comments gremmie@1: # twice by mistake.) If it is, fail silently by returning the old comment. gremmie@1: today = datetime.date.today() gremmie@1: possible_duplicates = Comment.objects.filter( gremmie@1: content_type = new.content_type, gremmie@1: object_id = new.object_id, gremmie@1: user = new.user, gremmie@1: creation_date__year = today.year, gremmie@1: creation_date__month = today.month, gremmie@1: creation_date__day = today.day, gremmie@1: ) gremmie@1: for old in possible_duplicates: gremmie@1: if old.comment == new.comment: gremmie@1: return old gremmie@1: gremmie@1: return new gremmie@1: bgneal@963: def clean_comment(self): bgneal@963: comment = self.cleaned_data['comment'] bgneal@963: self.comment_html = None bgneal@963: if comment: bgneal@963: self.comment_html = site_markup(comment) bgneal@963: try: bgneal@963: image_check(self.comment_html) bgneal@963: except ImageCheckError as ex: bgneal@963: raise forms.ValidationError(str(ex)) bgneal@963: bgneal@963: return comment bgneal@963: gremmie@1: class Media: gremmie@1: css = { bgneal@127: 'all': (settings.GPP_THIRD_PARTY_CSS['markitup'] + bgneal@127: settings.GPP_THIRD_PARTY_CSS['jquery-ui']), gremmie@1: } bgneal@312: js = (settings.GPP_THIRD_PARTY_JS['markitup'] + bgneal@127: settings.GPP_THIRD_PARTY_JS['jquery-ui'] + bgneal@484: ['js/comments.js'])