def send_notification_mail(self, post_id): post = ExperimentPost.objects.get(id=post_id) experiment = post.experiment user_ids = list(post.comments.all().values_list('created_by__id', flat=True)) if post.created_by: user_ids.append(post.created_by.id) if experiment.created_by: user_ids.append(experiment.created_by.id) users = User.objects.filter(id__in=user_ids).distinct() for user in users: if user.id != self.request.user.id: mail_sent = send_template_mail( recipient=user.email, subject=_('New comment'), template='comment_notification', variables={ "user": f'{self.request.user.first_name} {self.request.user.last_name}', "experiment_url": f'{os.environ.get("BASE_FRONTEND_URL")}/fi/kokeilu/' + post.experiment.slug } ) if not mail_sent: logger.error( 'Could not send comment notification email for user ' 'id %s.', user.id )
def send_publish_mail(self): obj = self.get_object() users = obj.responsible_users.all() for user in users: mail_sent = send_template_mail( recipient=user.email, subject=_('Your experiment has been published'), template='publish_notification', variables={} ) if not mail_sent: logger.error( 'Could not send experiment publish notification email for user ' 'id %s.', user.id )
def send_registration_notification(self): try: mail_sent = send_template_mail( recipient=self.email, subject=_('Thank you for your registration'), template='registration_notification', variables={ 'user': self, }) except Exception as e: logger.exception(e) return if not mail_sent: logger.error( 'Could not send registration notification email for user ' 'id %s.', self.id)
def send_notification_mail(self, experiment_slug): experiment = Experiment.objects.get(slug=experiment_slug) if experiment.created_by and experiment.created_by.id != self.request.user.id: mail_sent = send_template_mail( recipient=experiment.created_by.email, subject=_('New comment'), template='comment_notification', variables={ "user": f'{self.request.user.first_name} {self.request.user.last_name}', "experiment_url": f'{os.environ.get("BASE_FRONTEND_URL")}/fi/kokeilu/' + experiment.slug } ) if not mail_sent: logger.error( 'Could not send comment notification email for user ' 'id %s.', experiment.created_by.id )
def run(self): for r in self.recipients: mail_sent = send_template_mail( recipient=r, subject=_('New experiment that you might be interested in'), template='new_experiment_notification', variables={ "user": self.user_name, "profile_url": f'{os.environ.get("BASE_FRONTEND_URL")}?login-to-profile=true', "experiment_url": f'{os.environ.get("BASE_FRONTEND_URL")}/kokeilu/' + self.experiment_slug }) sleep(0.2) if not mail_sent: logger.error( 'Could not send responsible user add notification email for user ' 'email %s.', r)
def update(self, instance, validated_data): """Send notification to the newly added responsible user, in case the responsible_users field was changed. """ if 'responsible_users' in validated_data: resp_users = instance.responsible_users.all() for user in validated_data['responsible_users']: if user not in resp_users: mail_sent = send_template_mail( recipient=user.email, subject=_('You have been added to an experiment'), template='experiment_user_add_notification', variables={ "user": self.context['request'].user.get_full_name(), "experiment_name": instance.name, "profile_url": f'{os.environ.get("BASE_FRONTEND_URL")}?login-to-profile=true' }) if not mail_sent: logger.error( 'Could not send responsible user add notification email for user ' 'id %s.', user.id) if not instance.is_published and validated_data.get( 'is_published') is True: send_to = [] themes = instance.themes.all() for user in UserProfile.objects.filter( send_experiment_notification=True).exclude( user__in=instance.responsible_users.all()): user_themes = user.interested_in_themes.all() for theme in themes: if theme in user_themes: send_to.append(user.user.email) send_to = list(set(send_to)) ExperimentEmailThread( send_to, instance.slug, self.context['request'].user.get_full_name()).start() return super().update(instance, validated_data)