Mercurial > public > sg101
comparison comments/forms.py @ 581:ee87ea74d46b
For Django 1.4, rearranged project structure for new manage.py.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 05 May 2012 17:10:48 -0500 |
parents | gpp/comments/forms.py@bbbc357ac5f3 |
children | 4619290d171d |
comparison
equal
deleted
inserted
replaced
580:c525f3e0b5d0 | 581:ee87ea74d46b |
---|---|
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(attrs={'class': 'markItUp smileyTarget'})) | |
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': (settings.GPP_THIRD_PARTY_CSS['markitup'] + | |
70 settings.GPP_THIRD_PARTY_CSS['jquery-ui']), | |
71 } | |
72 js = (settings.GPP_THIRD_PARTY_JS['markitup'] + | |
73 settings.GPP_THIRD_PARTY_JS['jquery-ui'] + | |
74 ['js/comments.js']) |