def form_valid(self, form): app_name = self.app_name model_name = self.model_name model_id = self.model_id model_object = get_model_obj(app_name, model_name, model_id) parent_id = self.parent_id parent_comment = Comment.objects.get_parent_comment(parent_id) user = get_user_for_request(self.request) comment_content = form.cleaned_data['content'] email = form.cleaned_data.get('email', None) or user.email time_posted = timezone.now() comment = Comment(content_object=model_object, content=comment_content, user=user, parent=parent_comment, email=email, posted=time_posted) if settings.COMMENT_ALLOW_ANONYMOUS and not user: # send response, please verify your email to post this comment. response_msg = process_anonymous_commenting(self.request, comment) messages.info(self.request, response_msg) else: comment.save() self.comment = comment return self.render_to_response(self.get_context_data())
def create(self, validated_data): request = self.context['request'] user = get_user_for_request(request) content = validated_data.get('content') email = validated_data.get('email') time_posted = timezone.now() comment = Comment(content_object=self.context['model_obj'], content=content, user=user, parent=self.context['parent_comment'], email=email, posted=time_posted) if settings.COMMENT_ALLOW_ANONYMOUS and not user: process_anonymous_commenting(request, comment, api=True) else: comment.save() return comment
def test_for_drf(self): response = process_anonymous_commenting(self.request, self.comment_obj, api=True) self.assertEqual(EmailInfo.CONFIRMATION_SENT, response)
def test_for_drf(self): response = process_anonymous_commenting(self.request, self.comment_obj, api=True) self.assertEqual(self.response_msg, response)
def test_for_django(self): response = process_anonymous_commenting(self.request, self.comment_obj) self.assertEqual(self.response_msg, response)
def form_valid(self, form): app_name = self.app_name model_name = self.model_name model_id = self.model_id model_object = get_model_obj(app_name, model_name, model_id) parent_id = self.parent_id parent_comment = Comment.objects.get_parent_comment(parent_id) user = get_user_for_request(self.request) comment_content = form.cleaned_data['content'] email = form.cleaned_data.get('email', None) or user.email time_posted = timezone.now() _comment = Comment(content_object=model_object, content=comment_content, user=user, parent=parent_comment, email=email, posted=time_posted) if settings.COMMENT_ALLOW_ANONYMOUS and not user: # send response, please verify your email to post this comment. response_msg = process_anonymous_commenting(self.request, _comment) messages.info(self.request, response_msg) else: _comment.save() self.comment = _comment # send email section current_site = get_current_site(self.request) article = self.comment.content_object author_email = article.author.email user_email = self.comment.user.email subject_email = '' messages_email = '' if author_email == user_email: author_email = False user_email = False parent_email = False if self.comment.parent: parent_email = self.comment.parent.user.email if parent_email in [author_email, user_email]: parent_email = False if author_email: subject_email = "دیدگاه جدید" messages_email = "دیدگاه جدیدی برای مقاله «{}» که شما نوینده آن هستید، ارسال شده:\n{}{}".format( article, current_site, reverse('blog:detail', kwargs={'slug': article.slug})) send_email.delay(subject_email, messages_email, author_email) if user_email: subject_email = "دیدگاه دریافت شد" messages_email = "دیدگاه شما دریافت شد و به زودی به آن پاسخ می دهیم." send_email.delay(subject_email, messages_email, user_email) if parent_email: subject_email = "پاسخ به دیدگاه شما" messages_email = "پاسخی به دیدگاه شما در مقاله «{}» ثبت شده است. برای مشاهده بر روی لینک زیر کلیک کنید:\n{}{}".format( article, current_site, reverse('blog:detail', kwargs={'slug': article.slug})) send_email.delay(subject_email, messages_email, parent_email) return self.render_to_response(self.get_context_data())