-
Notifications
You must be signed in to change notification settings - Fork 157
/
forms.py
56 lines (49 loc) · 2.26 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
from django import forms
try:
from django.apps import apps
except ImportError:
from django.db.models.loading import cache as apps
from django.utils.translation import ugettext_lazy as _
try:
from django_comments.forms import CommentForm
except ImportError:
from django.contrib.comments.forms import CommentForm
from django_comments_xtd.conf import settings
from django_comments_xtd.models import TmpXtdComment
class XtdCommentForm(CommentForm):
followup = forms.BooleanField(required=False,
label=_("Notify me about follow-up comments"))
reply_to = forms.IntegerField(required=True, initial=0,
widget=forms.HiddenInput())
def __init__(self, *args, **kwargs):
comment = kwargs.pop("comment", None)
if comment:
initial = kwargs.pop("initial", {})
initial.update({"reply_to": comment.pk})
kwargs["initial"] = initial
super(CommentForm, self).__init__(*args, **kwargs)
self.fields['name'] = forms.CharField(
widget=forms.TextInput(attrs={'placeholder': _('name')}))
self.fields['email'] = forms.EmailField(
label=_("Email"), help_text=_("Required for comment verification"),
widget=forms.TextInput(attrs={'placeholder': _('email')})
)
self.fields['url'] = forms.URLField(
required=False,
widget=forms.TextInput(attrs={'placeholder': _('website')}))
self.fields['comment'] = forms.CharField(
widget=forms.Textarea(attrs={'placeholder': _('comment')}),
max_length=settings.COMMENT_MAX_LENGTH)
def get_comment_model(self):
return TmpXtdComment
def get_comment_create_data(self, site_id=None):
data = super(CommentForm, self).get_comment_create_data()
ctype = data.get('content_type')
object_pk = data.get('object_pk')
model = apps.get_model(ctype.app_label, ctype.model)
target = model._default_manager.get(pk=object_pk)
data.update({'thread_id': 0, 'level': 0, 'order': 1,
'parent_id': self.cleaned_data['reply_to'],
'followup': self.cleaned_data['followup'],
'content_object': target})
return data