django-comments

what is key difference between posting a new comment and replying to existing comments?


I want to find out the key differences when assembling comment form between posting a new comment and replying to existing comments and how are they implemented in source code of django_comments_xtd?

I reviewed the source code for a quite a long time and did not find out the answer.


Solution

  • In django_comments_xtd , the system distinguishs whether a comment is a reply to a previous post / or a new , independent comment is by referring to the reply_to data field.

    For a new comment, the reply_to field is having a default value of "0", but for reply to a previous commment (say ID=14), then the reply_to data field will be having a value of "14".

    For the source code, One of the places you may refer to is the python file forms.py (an extract is attached below). For sure there are other places using this reply_to data field for rendering the site so you may search for the reply_to string to discover if you have further interests in this aspect.

    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
                followup_suffix = ('_%d' % comment.pk)
            else:
                followup_suffix = ''
    
            super(CommentForm, self).__init__(*args, **kwargs)
    
            self.fields['name'].label = _("Name")
            self.fields['name'].widget = forms.TextInput(
                attrs={'placeholder': _('name'), 'class': 'form-control'})
    
            self.fields['email'].label = _("Mail")
            self.fields['email'].help_text = _("Required for comment verification")
            self.fields['email'].widget = forms.TextInput(
                attrs={'placeholder': _('mail address'), 'class': 'form-control'})
    
            self.fields['url'].label = _("Link")
            self.fields['url'].required = False
            self.fields['url'].widget = forms.TextInput(attrs={
                    'placeholder': _('url your name links to (optional)'),
                    'class': 'form-control'})
    
            self.fields['comment'].widget = forms.Textarea(
                attrs={'placeholder': _('Your comment'), 'class': 'form-control'})
            self.fields['comment'].max_length = settings.COMMENT_MAX_LENGTH
            self.fields['comment'].widget.attrs.pop('cols')
            self.fields['comment'].widget.attrs.pop('rows')
    
            self.fields['followup'].widget.attrs['id'] = (
                'id_followup%s' % followup_suffix)
            self.fields['followup'].widget.attrs['class'] = "custom-control-input"
            self.fields['followup'].initial = settings.COMMENTS_XTD_DEFAULT_FOLLOWUP
    
        def get_comment_model(self):
            return TmpXtdComment
    
        def get_comment_create_data(self, site_id=None):
            data = super(CommentForm, self).get_comment_create_data(site_id=site_id)
            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