comparison gpp/comments/forms.py @ 1:dbd703f7d63a

Initial import of sg101 stuff from private repository.
author gremmie
date Mon, 06 Apr 2009 02:43:12 +0000
parents
children b6263ac72052
comparison
equal deleted inserted replaced
0:900ba3c7b765 1:dbd703f7d63a
1 """
2 Forms for the comments application.
3 """
4 import datetime
5 from django import forms
6 from django.conf import settings
7 from django.contrib.contenttypes.models import ContentType
8
9 from comments.models import Comment
10
11 COMMENT_MAX_LENGTH = getattr(settings, 'COMMENT_MAX_LENGTH', 3000)
12
13 class CommentForm(forms.Form):
14 comment = forms.CharField(label='',
15 min_length=1,
16 max_length=COMMENT_MAX_LENGTH,
17 widget=forms.Textarea)
18 content_type = forms.CharField(widget=forms.HiddenInput)
19 object_pk = forms.CharField(widget=forms.HiddenInput)
20
21 def __init__(self, target_object, data=None, initial=None):
22 self.target_object = target_object
23 if initial is None:
24 initial = {}
25 initial.update({
26 'content_type': str(self.target_object._meta),
27 'object_pk': str(self.target_object.pk),
28 })
29 super(CommentForm, self).__init__(data=data, initial=initial)
30
31 def get_comment_object(self, user, ip_address):
32 """
33 Return a new (unsaved) comment object based on the information in this
34 form. Assumes that the form is already validated and will throw a
35 ValueError if not.
36 """
37 if not self.is_valid():
38 raise ValueError("get_comment_object may only be called on valid forms")
39
40 new = Comment(
41 content_type = ContentType.objects.get_for_model(self.target_object),
42 object_id = self.target_object.pk,
43 user = user,
44 comment = self.cleaned_data["comment"],
45 ip_address = ip_address,
46 is_public = True,
47 is_removed = False,
48 )
49
50 # Check that this comment isn't duplicate. (Sometimes people post comments
51 # twice by mistake.) If it is, fail silently by returning the old comment.
52 today = datetime.date.today()
53 possible_duplicates = Comment.objects.filter(
54 content_type = new.content_type,
55 object_id = new.object_id,
56 user = new.user,
57 creation_date__year = today.year,
58 creation_date__month = today.month,
59 creation_date__day = today.day,
60 )
61 for old in possible_duplicates:
62 if old.comment == new.comment:
63 return old
64
65 return new
66
67 class Media:
68 css = {
69 'all': ('js/markitup/skins/markitup/style.css',
70 'js/markitup/sets/markdown/style.css')
71 }
72 js = (
73 'js/jquery-1.2.6.min.js',
74 'js/comments.js',
75 'js/markitup/jquery.markitup.pack.js',
76 'js/markitup/sets/markdown/set.js',
77 )