annotate gpp/comments/forms.py @ 133:c515b7401078

Use the new common way to apply markItUp to textareas and to get the smiley and markdown help dialogs for all the remaining apps except for forums and comments.
author Brian Neal <bgneal@gmail.com>
date Fri, 27 Nov 2009 00:21:47 +0000
parents 2d299909e074
children b8474ffe76d9
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
gremmie@1 10
gremmie@1 11 COMMENT_MAX_LENGTH = getattr(settings, 'COMMENT_MAX_LENGTH', 3000)
gremmie@1 12
gremmie@1 13 class CommentForm(forms.Form):
gremmie@1 14 comment = forms.CharField(label='',
gremmie@1 15 min_length=1,
gremmie@1 16 max_length=COMMENT_MAX_LENGTH,
gremmie@1 17 widget=forms.Textarea)
gremmie@1 18 content_type = forms.CharField(widget=forms.HiddenInput)
gremmie@1 19 object_pk = forms.CharField(widget=forms.HiddenInput)
gremmie@1 20
gremmie@1 21 def __init__(self, target_object, data=None, initial=None):
gremmie@1 22 self.target_object = target_object
gremmie@1 23 if initial is None:
gremmie@1 24 initial = {}
gremmie@1 25 initial.update({
gremmie@1 26 'content_type': str(self.target_object._meta),
gremmie@1 27 'object_pk': str(self.target_object.pk),
gremmie@1 28 })
gremmie@1 29 super(CommentForm, self).__init__(data=data, initial=initial)
gremmie@1 30
gremmie@1 31 def get_comment_object(self, user, ip_address):
gremmie@1 32 """
gremmie@1 33 Return a new (unsaved) comment object based on the information in this
gremmie@1 34 form. Assumes that the form is already validated and will throw a
gremmie@1 35 ValueError if not.
gremmie@1 36 """
gremmie@1 37 if not self.is_valid():
gremmie@1 38 raise ValueError("get_comment_object may only be called on valid forms")
gremmie@1 39
gremmie@1 40 new = Comment(
gremmie@1 41 content_type = ContentType.objects.get_for_model(self.target_object),
gremmie@1 42 object_id = self.target_object.pk,
gremmie@1 43 user = user,
gremmie@1 44 comment = self.cleaned_data["comment"],
gremmie@1 45 ip_address = ip_address,
gremmie@1 46 is_public = True,
gremmie@1 47 is_removed = False,
gremmie@1 48 )
gremmie@1 49
gremmie@1 50 # Check that this comment isn't duplicate. (Sometimes people post comments
gremmie@1 51 # twice by mistake.) If it is, fail silently by returning the old comment.
gremmie@1 52 today = datetime.date.today()
gremmie@1 53 possible_duplicates = Comment.objects.filter(
gremmie@1 54 content_type = new.content_type,
gremmie@1 55 object_id = new.object_id,
gremmie@1 56 user = new.user,
gremmie@1 57 creation_date__year = today.year,
gremmie@1 58 creation_date__month = today.month,
gremmie@1 59 creation_date__day = today.day,
gremmie@1 60 )
gremmie@1 61 for old in possible_duplicates:
gremmie@1 62 if old.comment == new.comment:
gremmie@1 63 return old
gremmie@1 64
gremmie@1 65 return new
gremmie@1 66
gremmie@1 67 class Media:
gremmie@1 68 css = {
bgneal@127 69 'all': (settings.GPP_THIRD_PARTY_CSS['markitup'] +
bgneal@127 70 settings.GPP_THIRD_PARTY_CSS['jquery-ui']),
gremmie@1 71 }
bgneal@127 72 js = (settings.GPP_THIRD_PARTY_JS['markitup'] +
bgneal@127 73 settings.GPP_THIRD_PARTY_JS['jquery-ui'] +
bgneal@127 74 ('js/comments.js', ))