-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathforms.py
267 lines (192 loc) · 9.39 KB
/
forms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import datetime
from django.contrib.comments.forms import CommentForm as DjangoCommentForm
from django.forms.util import ErrorDict
from django import forms
from django.contrib.comments.signals import comment_will_be_posted
from django.contrib.contenttypes.models import ContentType
from django.utils.encoding import force_unicode
from django.conf import settings
from django.db import models
from django.template.loader import render_to_string
from django.contrib.auth.models import User
from model_comments.models import Comment
from twigcorp.utils import Url
class CommentForm(DjangoCommentForm):
honeypot = forms.CharField(required=False, label='')
from_url = forms.CharField(widget=forms.HiddenInput())
# Functions which you can safely override.
def clean_model_comment(self, request, cleaned_data):
"""
Twig: Do not override self.clean().
Overriding this will allow you to do custom validation on your forms.
You must returned cleaned_data.
"""
#raise NotImplementedError("CommentForm.clean_model_comment(): You need to override this method and return cleaned_data")
return cleaned_data
def get_target_model(self):
"""
Return the model type you wish to comment on.
ie. Shirt, Store, BlogPost, etc
Raise NotImplementedError if your form is a base form that should not be used.
"""
return models.Model
def get_comment_model(self):
"""
Twig: Overriding this so we know to replace the Model.
Get the comment model to create with this form. Subclasses in custom
comment apps should override this, get_comment_create_data, and perhaps
check_for_duplicate_comment to provide custom comment models.
"""
# raise NotImplementedError("CommentForm.get_comment_model(): You must override this.")
return Comment
def pre_save(self, request, comment):
"""
Override this if you want to know when a comment is just about to be saved.
"""
pass
def post_save(self, request, comment):
"""
Override this if you want to know when a comment was saved.
"""
pass
def get_comment_create_data(self):
"""
Twig: Overrides the original one because we CAN set IP and user information into this.
"""
return {
'content_type': ContentType.objects.get_for_model(self.target_object),
'object_pk': force_unicode(self.target_object._get_pk_val()),
'user_name': self.cleaned_data["name"],
'user_email': self.cleaned_data["email"],
'user_url': self.cleaned_data["url"],
'comment': self.cleaned_data["comment"],
'submit_date': datetime.datetime.now(),
'site_id': settings.SITE_ID,
'is_public': True,
'is_removed': False,
# Twig: TODO: replace this with the X-REFERER address or something for transparent proxies
'ip_address': self.request.META.get("REMOTE_ADDR", None),
'user': self.request.user if self.request.user.is_authenticated() else None,
}
def is_form_for_object(self, obj):
"""
Returns True if this form is appropriatefor the given object.
"""
return isinstance(obj, self.get_target_model())
# End of functions you can safely override
def set_request(self, request):
self.request = request
if not self.fields['from_url'].initial:
self.fields['from_url'].initial = unicode(Url(request))
if request.user.is_authenticated():
if not self.fields['name'].initial:
self.fields['name'].initial = request.user.username
if not self.fields['email'].initial:
self.fields['email'].initial = request.user.email
def is_preview(self):
return self.is_valid() and self.preview if hasattr(self, 'preview') else False
def validate_data(self, request):
self.request = request
return self.is_valid()
def clean_from_url(self):
from_url = self.cleaned_data['from_url']
if not from_url:
raise forms.ValidationError('CommentForm.clean(): from_url not set')
return from_url
def clean_comment(self):
comment = self.cleaned_data['comment']
if not comment:
raise forms.ValidationError('Please enter a comment.')
return comment
def clean_email(self):
email = self.cleaned_data.get('email', None)
if self.request.user.is_anonymous() and not email:
raise forms.ValidationError("Email is required for unregistered users.")
# Impose the limits that the framework forgot
if len(email) > 75:
raise forms.ValidationError("Ensure this value has at most 75 characters (it has %s)." % len(email))
return email
# Impose the limits that the framework forgot
def clean_url(self):
url = self.cleaned_data['url']
if len(url) > 200:
raise forms.ValidationError("Ensure this value has at most 200 characters (it has %s)." % len(url))
return url
def clean(self):
"""
Implements a hefty clean() function, but this is mostly to maintain compatibility with the
standard comments module.
You should not override this. Please use the function clean_model_comment() instead.
"""
if not hasattr(self, 'request'):
raise forms.ValidationError("CommentForm.clean(): You must call validate_data(request)")
# If there are basic errors, display them before getting to the more complicated ones
if self.errors:
return self.cleaned_data
# Do custom validation first
cleaned_data = super(CommentForm, self).clean()
# Strip whitespace off fields
for fieldname in [ 'comment', 'email', 'name', 'url' ]:
cleaned_data[fieldname] = cleaned_data[fieldname].strip()
cleaned_data = self.clean_model_comment(self.request, cleaned_data)
# Prevent anonymous users masquerading as registered users
if self.request.user.is_anonymous():
email = cleaned_data.get('email', None)
if email is None:
raise forms.ValidationError("Unregistered users must fill in the email field.")
if getattr(settings, 'COMMENTS_CHECK_REGISTERED_USER_EMAIL', False):
try:
user = User.objects.get(email__iexact=email)
raise forms.ValidationError("This email beings to a registered user. If this is yours, please log in.")
except User.DoesNotExist:
pass
# Begin sending out "pre-save" messages
# This requires the form to be valid!
if not self.errors:
comment = self.get_comment_object()
# Send out the original signal to maintain compatibility
responses = comment_will_be_posted.send(
sender=comment.__class__,
comment=comment,
request=self.request,
)
for (receiver, response) in responses:
if response == False:
raise forms.ValidationError("comment_will_be_posted receiver %r killed the comment" % receiver.__name__)
self.comment = comment
return cleaned_data
def get_comment_object(self):
"""
Overrides this behaviour so we can call it without the form being valid.
Reason being we can do validation checks using the original "comment_will_be_posted" within the clean method.
This allows us to maintain compatibility with extensions such as spam filters, etc.
"""
# if not self.is_valid():
# raise ValueError("get_comment_object may only be called on valid forms")
# Twig: The rest is just original code
CommentModel = self.get_comment_model()
new = CommentModel(**self.get_comment_create_data())
new = self.check_for_duplicate_comment(new)
return new
def __unicode__(self):
ctype = ContentType.objects.get_for_model(self.target_object)
model = ctype.model_class()
template_list = [
# The template filename formats are identical to the way Django comment previews are.
# Django v1.0 and v1.1 allowed the underscore format.
# Twig: Rather than calling them previews, we just called them "form.html"
"comments/%s_%s_model_comment_form.html" % (model._meta.app_label, model._meta.module_name),
"comments/%s_model_comment_form.html" % model._meta.app_label,
# Now the usual directory based template heirarchy.
"comments/%s/%s/model_comment_form.html" % (model._meta.app_label, model._meta.module_name),
"comments/%s/model_comment_form.html" % model._meta.app_label,
"comments/model_comment_form.html",
]
# try:
return render_to_string(template_list, { 'form': self, })
# except Exception, e:
# print e
# Default handler
return super(CommentForm, self).__unicode__()
def get_model_name(self):
return self.target_object.__class__.__name__