def on_comment_was_posted(sender, comment, request, **kwargs): """ Post the comment if a user is authenticated or send a confirmation email. On signal django_comments.signals.comment_was_posted check if the user is authenticated or if settings.COMMENTS_XTD_CONFIRM_EMAIL is False. In both cases will post the comment. Otherwise will send a confirmation email to the person who posted the comment. """ if settings.COMMENTS_APP != "django_comments_xtd": return False if comment.user: try: user_is_authenticated = comment.user.is_authenticated() except TypeError: # Django >= 1.11 user_is_authenticated = comment.user.is_authenticated else: user_is_authenticated = False if (not settings.COMMENTS_XTD_CONFIRM_EMAIL or user_is_authenticated): if not _comment_exists(comment): new_comment = _create_comment(comment) comment.xtd_comment = new_comment signals.confirmation_received.send(sender=TmpXtdComment, comment=comment, request=request) if comment.is_public: notify_comment_followers(new_comment) else: key = signed.dumps(comment, compress=True, extra_key=settings.COMMENTS_XTD_SALT) site = get_current_site(request) send_email_confirmation_request(comment, key, site)
def on_comment_was_posted(sender, comment, request, **kwargs): """ Post the comment if a user is authenticated or send a confirmation email. On signal django.contrib.comments.signals.comment_was_posted check if the user is authenticated or if settings.COMMENTS_XTD_CONFIRM_EMAIL is False. In both cases will post the comment. Otherwise will send a confirmation email to the person who posted the comment. """ if settings.COMMENTS_APP != "django_comments_xtd": return False if (not settings.COMMENTS_XTD_CONFIRM_EMAIL or (comment.user and comment.user.is_authenticated())): if not _comment_exists(comment): new_comment = _create_comment(comment) comment.xtd_comment = new_comment notify_comment_followers(new_comment) else: ctype = request.POST["content_type"] object_pk = request.POST["object_pk"] model = models.get_model(*ctype.split(".")) target = model._default_manager.get(pk=object_pk) key = signed.dumps(comment, compress=True, extra_key=settings.COMMENTS_XTD_SALT) send_email_confirmation_request(comment, target, key)
def on_comment_was_posted(sender, comment, request, **kwargs): """ Post the comment if a user is authenticated or send a confirmation email. On signal django_comments.signals.comment_was_posted check if the user is authenticated or if settings.COMMENTS_XTD_CONFIRM_EMAIL is False. In both cases will post the comment. Otherwise will send a confirmation email to the person who posted the comment. """ if settings.COMMENTS_APP != "django_comments_xtd": return False if (not settings.COMMENTS_XTD_CONFIRM_EMAIL or (comment.user and comment.user.is_authenticated())): if not _comment_exists(comment): new_comment = _create_comment(comment) comment.xtd_comment = new_comment signals.confirmation_received.send(sender=TmpXtdComment, comment=comment, request=request) if comment.is_public: notify_comment_followers(new_comment) else: ctype = request.POST["content_type"] object_pk = request.POST["object_pk"] model = get_model(*ctype.split(".")) target = model._default_manager.get(pk=object_pk) key = signed.dumps(comment, compress=True, extra_key=settings.COMMENTS_XTD_SALT) site = get_current_site(request) send_email_confirmation_request(comment, target, key, site)
def feed_followers(gen): for instance in gen: followers[instance.user_email] = ( instance.user_name, signed.dumps(instance, compress=True, extra_key=settings.COMMENTS_XTD_SALT))
def notify_comment_followers(comment): followers = {} kwargs = { 'content_type': comment.content_type, 'object_pk': comment.object_pk, 'is_public': True, 'followup': True } previous_comments = XtdComment.objects\ .filter(**kwargs)\ .exclude(user_email=comment.user_email) def feed_followers(gen): for instance in gen: followers[instance.user_email] = ( instance.user_name, signed.dumps(instance, compress=True, extra_key=settings.COMMENTS_XTD_SALT)) try: gen = previous_comments.distinct('user_email').order_by('user_email') feed_followers(gen) except NotSupportedError: feed_followers(previous_comments) for instance in previous_comments: followers[instance.user_email] = ( instance.user_name, signed.dumps(instance, compress=True, extra_key=settings.COMMENTS_XTD_SALT)) subject = _("new comment posted") text_message_template = loader.get_template( "django_comments_xtd/email_followup_comment.txt") if settings.COMMENTS_XTD_SEND_HTML_EMAIL: html_message_template = loader.get_template( "django_comments_xtd/email_followup_comment.html") for email, (name, key) in six.iteritems(followers): mute_url = reverse('comments-xtd-mute', args=[key.decode('utf-8')]) message_context = { 'user_name': name, 'comment': comment, # 'content_object': target, 'mute_url': mute_url, 'site': comment.site } text_message = text_message_template.render(message_context) if settings.COMMENTS_XTD_SEND_HTML_EMAIL: html_message = html_message_template.render(message_context) else: html_message = None send_mail(subject, text_message, settings.COMMENTS_XTD_FROM_EMAIL, [ email, ], html=html_message)
def on_comment_was_posted(sender, comment, request, **kwargs): """ Post the comment if a user is authenticated or send a confirmation email. On signal django_comments.signals.comment_was_posted check if the user is authenticated or if settings.COMMENTS_XTD_CONFIRM_EMAIL is False. In both cases will post the comment. Otherwise will send a confirmation email to the person who posted the comment. """ if settings.COMMENTS_APP != "django_comments_xtd": return False if comment.user: user_is_authenticated = comment.user.is_authenticated else: user_is_authenticated = False if (not settings.COMMENTS_XTD_CONFIRM_EMAIL or user_is_authenticated): if not _comment_exists(comment): new_comment = _create_comment(comment) comment.xtd_comment = new_comment signals.confirmation_received.send(sender=TmpXtdComment, comment=comment, request=request) if comment.is_public: notify_comment_followers(new_comment) else: key = signed.dumps(comment, compress=True, extra_key=settings.COMMENTS_XTD_SALT) site = get_current_site(request) send_email_confirmation_request(comment, key, site)
def save(self): # resp object is a dictionary. The code key indicates the possible # four states the comment can be in: # * Comment created (http 201), # * Confirmation sent by mail (http 204), # * Comment in moderation (http 202), # * Comment rejected (http 403). site = get_current_site(self.request) resp = { 'code': -1, 'comment': self.form.get_comment_object(site_id=site.id) } resp['comment'].ip_address = self.request.META.get("REMOTE_ADDR", None) if self.request.user.is_authenticated: resp['comment'].user = self.request.user if 'comment_id' in self.data: # an edit comment operation. resp['comment'].comment_id = self.data.get('comment_id') # Signal that the comment is about to be saved responses = comment_will_be_posted.send(sender=TmpXtdComment, comment=resp['comment'], request=self.request) for (receiver, response) in responses: if response is False: resp['code'] = 403 # Rejected. return resp # Replicate logic from django_comments_xtd.views.on_comment_was_posted. if (not settings.COMMENTS_XTD_CONFIRM_EMAIL or self.request.user.is_authenticated): if not views._comment_exists(resp['comment']): new_comment = views._create_comment(resp['comment']) resp['comment'].xtd_comment = new_comment confirmation_received.send(sender=TmpXtdComment, comment=resp['comment'], request=self.request) comment_was_posted.send(sender=new_comment.__class__, comment=new_comment, request=self.request) if resp['comment'].is_public: resp['code'] = 201 views.notify_comment_followers(new_comment) else: resp['code'] = 202 else: key = signed.dumps(resp['comment'], compress=True, extra_key=settings.COMMENTS_XTD_SALT) views.send_email_confirmation_request(resp['comment'], key, site) resp['code'] = 204 # Confirmation sent by mail. return resp
def save(self): # resp object is a dictionary. The code key indicates the possible # four states the comment can be in: # * Comment created (http 201), # * Confirmation sent by mail (http 204), # * Comment in moderation (http 202), # * Comment rejected (http 403). site = get_current_site(self.request) resp = { 'code': -1, 'comment': self.form.get_comment_object(site_id=site.id) } resp['comment'].ip_address = self.request.META.get("REMOTE_ADDR", None) if self.request.user.is_authenticated: resp['comment'].user = self.request.user # Signal that the comment is about to be saved responses = comment_will_be_posted.send(sender=TmpXtdComment, comment=resp['comment'], request=self.request) for (receiver, response) in responses: if response is False: resp['code'] = 403 # Rejected. return resp # Replicate logic from django_comments_xtd.views.on_comment_was_posted. if ( not settings.COMMENTS_XTD_CONFIRM_EMAIL or self.request.user.is_authenticated ): if not views._comment_exists(resp['comment']): new_comment = views._create_comment(resp['comment']) resp['comment'].xtd_comment = new_comment confirmation_received.send(sender=TmpXtdComment, comment=resp['comment'], request=self.request) comment_was_posted.send(sender=new_comment.__class__, comment=new_comment, request=self.request) if resp['comment'].is_public: resp['code'] = 201 views.notify_comment_followers(new_comment) else: resp['code'] = 202 else: key = signed.dumps(resp['comment'], compress=True, extra_key=settings.COMMENTS_XTD_SALT) views.send_email_confirmation_request(resp['comment'], key, site) resp['code'] = 204 # Confirmation sent by mail. return resp
def notify_comment_followers(comment): followers = {} kwargs = { 'content_type': comment.content_type, 'object_pk': comment.object_pk, 'is_public': True, 'followup': True } previous_comments = XtdComment.objects\ .filter(**kwargs)\ .exclude(user_email=comment.user_email) for instance in previous_comments: followers[instance.user_email] = ( instance.user_name, signed.dumps(instance, compress=True, extra_key=settings.COMMENTS_XTD_SALT)) model = get_model(comment.content_type.app_label, comment.content_type.model) target = model._default_manager.get(pk=comment.object_pk) subject = _("new comment posted") text_message_template = loader.get_template( "django_comments_xtd/email_followup_comment.txt") if settings.COMMENTS_XTD_SEND_HTML_EMAIL: html_message_template = loader.get_template( "django_comments_xtd/email_followup_comment.html") for email, (name, key) in six.iteritems(followers): mute_url = reverse('comments-xtd-mute', args=[key]) message_context = Context({ 'user_name': name, 'comment': comment, 'content_object': target, 'mute_url': mute_url, 'site': comment.site }) text_message = text_message_template.render(message_context) if settings.COMMENTS_XTD_SEND_HTML_EMAIL: html_message = html_message_template.render(message_context) else: html_message = None send_mail(subject, text_message, settings.COMMENTS_XTD_FROM_EMAIL, [ email, ], html=html_message)
def notify_comment_followers(comment): followers = {} kwargs = {'content_type': comment.content_type, 'object_pk': comment.object_pk, 'is_public': True, 'followup': True} previous_comments = XtdComment.objects\ .filter(**kwargs)\ .exclude(user_email=comment.user_email) for instance in previous_comments: followers[instance.user_email] = ( instance.user_name, signed.dumps(instance, compress=True, extra_key=settings.COMMENTS_XTD_SALT)) model = get_model(comment.content_type.app_label, comment.content_type.model) target = model._default_manager.get(pk=comment.object_pk) subject = _("new comment posted") text_message_template = loader.get_template( "django_comments_xtd/email_followup_comment.txt") if settings.COMMENTS_XTD_SEND_HTML_EMAIL: html_message_template = loader.get_template( "django_comments_xtd/email_followup_comment.html") for email, (name, key) in six.iteritems(followers): mute_url = reverse('comments-xtd-mute', args=[key]) message_context = Context({'user_name': name, 'comment': comment, 'content_object': target, 'mute_url': mute_url, 'site': Site.objects.get_current()}) text_message = text_message_template.render(message_context) if settings.COMMENTS_XTD_SEND_HTML_EMAIL: html_message = html_message_template.render(message_context) else: html_message = None send_mail(subject, text_message, settings.COMMENTS_XTD_FROM_EMAIL, [email, ], html=html_message)
def on_comment_was_posted(sender, comment, request, **kwargs): """ Post the comment if a user is authenticated or send a confirmation email. On signal django.contrib.comments.signals.comment_was_posted check if the user is authenticated or if settings.COMMENTS_XTD_CONFIRM_EMAIL is False. In both cases will post the comment. Otherwise will send a confirmation email to the person who posted the comment. """ if ((comment.user and comment.user.is_authenticated()) or not COMMENTS_XTD_CONFIRM_EMAIL): if not _comment_exists(comment): new_comment = _create_comment(comment) comment.xtd_comment = new_comment notify_comment_followers(new_comment) else: ctype = request.POST["content_type"] object_pk = request.POST["object_pk"] model = models.get_model(*ctype.split(".")) target = model._default_manager.get(pk=object_pk) key = signed.dumps(comment, compress=True, extra_key=COMMENTS_XTD_SALT) send_email_confirmation_request(comment, target, key)