def send_comment_notification(self): recipients = [] if self.page_object: recipients = self.page_object.comment_notification_recipients(self) subject_template = 'replies/emails/post_comment_subject.txt' body_template = 'replies/emails/post_comment.txt' context = { 'comment': self, 'domain': Site.objects.get_current().domain, } profiles = [] for profile in recipients: if self.author != profile: profiles.append(profile) reply_url = reverse('email_reply', args=[self.id]) notification_category='reply' from projects.models import Project ct = ContentType.objects.get_for_model(Project) if self.scope_object and self.scope_content_type == ct: notification_category='reply.project-{0}'.format(self.scope_object.slug) send_notifications_i18n(profiles, subject_template, body_template, context, reply_url, self.author.username, notification_category=notification_category )
def send_email_notification(instance): project = instance.project if not instance.listed: return recipients = project.participants() subject_template = 'content/emails/content_update_subject.txt' body_template = 'content/emails/content_update.txt' context = { 'instance': instance, 'project': project, 'domain': Site.objects.get_current().domain, } from_organizer = project.organizers().filter(user=instance.author).exists() profiles = [] for recipient in recipients: profile = recipient.user if instance.author != profile: profiles.append(profile) send_notifications_i18n( profiles, subject_template, body_template, context, notification_category=u'content-updated.project-{0}'.format( project.slug))
def send_wall_notification(self): if not self.project: return recipients = self.project.participants() subject_template = 'statuses/emails/wall_updated_subject.txt' body_template = 'statuses/emails/wall_updated.txt' context = { 'status': self, 'project': self.project, 'domain': Site.objects.get_current().domain, } from_organizer = self.project.organizers().filter( user=self.author).exists() profiles = [recipient.user for recipient in recipients if self.author != recipient.user] kwargs = { 'page_app_label':'activity', 'page_model': 'activity', 'page_pk' : self.activity.get().id, } if self.project: kwargs.update({ 'scope_app_label': 'projects', 'scope_model': 'project', 'scope_pk': self.project.id, }) callback_url = reverse('page_comment_callback', kwargs=kwargs) send_notifications_i18n( profiles, subject_template, body_template, context, callback_url, self.author.username, notification_category=u'course-announcement.project-{0}'.format(self.project.slug) )
def add_user_to_cohort(cohort_uri, user_uri, role, notify_organizers=False): cohort_db = _get_cohort_db(cohort_uri) username = user_uri.strip('/').split('/')[-1] if not UserProfile.objects.filter(username=username).exists(): raise ResourceNotFoundException(u'User {0} does not exist'.format(user_uri)) if db.CohortSignup.objects.filter(cohort=cohort_db, user_uri=user_uri, leave_date__isnull=True).exists(): return None signup_db = db.CohortSignup( cohort=cohort_db, user_uri=user_uri, role=role ) signup_db.save() if notify_organizers: cohort = get_cohort(cohort_uri) course = get_course(cohort['course_uri']) organizers = UserProfile.objects.filter(username__in=cohort['organizers']) context = { 'course': course, 'new_user': username } subject_template = 'courses/emails/course_join_subject.txt' body_template = 'courses/emails/course_join.txt' notification_model.send_notifications_i18n(organizers, subject_template, body_template, context) signup = { "cohort_uri": cohort_uri, "user_uri": user_uri, "role": role } return signup
def test_reply_by_email(self): subject_template = 'replies/emails/post_comment_subject.txt' body_template = 'replies/emails/post_comment.txt' context = { 'comment': self.comment, 'domain': 'example.org', } callback_url = "/{0}/comments/{1}/email_reply/".format( self.locale, self.comment.id) send_notifications_i18n([self.user], subject_template, body_template, context, callback_url, notification_category='account') self.assertEqual(ResponseToken.objects.count(), 1) token = ResponseToken.objects.all()[0] data = { u'from': [u'Testing <*****@*****.**>'], u'to': [u'reply+{0}@reply.p2pu.org'.format(token.response_token)], u'text': [u'Maybe this time\n'], } #post_notification_response(token, '*****@*****.**', 'my response') with patch('requests.post') as requests_post: response = self.client.post( '/{0}/notifications/response/'.format(self.locale), data) self.assertEqual(response.status_code, 200)
def follow_handler(sender, **kwargs): rel = kwargs.get('instance', None) created = kwargs.get('created', False) if not created or not isinstance(rel, Relationship) or rel.deleted: return activity = Activity(actor=rel.source, verb=verbs['follow'], target_object=rel) recipients = [] if rel.target_user: recipients.append(rel.target_user) else: activity.scope_object = rel.target_project for organizer in rel.target_project.organizers(): if organizer.user != rel.source: recipients.append(organizer.user) activity.save() subject_template = 'relationships/emails/new_follower_subject.txt' body_template = 'relationships/emails/new_follower.txt' context = { 'user': rel.source, 'project': rel.target_project, 'domain': Site.objects.get_current().domain } send_notifications_i18n(recipients, subject_template, body_template, context, notification_category='new-follower')
def send_comment_notification(self): recipients = [] if self.page_object: recipients = self.page_object.comment_notification_recipients(self) subject_template = 'replies/emails/post_comment_subject.txt' body_template = 'replies/emails/post_comment.txt' context = { 'comment': self, 'domain': Site.objects.get_current().domain, } profiles = [] for profile in recipients: if self.author != profile: profiles.append(profile) reply_url = reverse('email_reply', args=[self.id]) notification_category = 'reply' from projects.models import Project ct = ContentType.objects.get_for_model(Project) if self.scope_object and self.scope_content_type == ct: notification_category = 'reply.project-{0}'.format( self.scope_object.slug) send_notifications_i18n(profiles, subject_template, body_template, context, reply_url, self.author.username, notification_category=notification_category)
def follow_handler(sender, **kwargs): rel = kwargs.get('instance', None) created = kwargs.get('created', False) if not created or not isinstance(rel, Relationship) or rel.deleted: return activity = Activity(actor=rel.source, verb=verbs['follow'], target_object=rel) recipients = [] if rel.target_user: recipients.append(rel.target_user) else: activity.scope_object = rel.target_project for organizer in rel.target_project.organizers(): if organizer.user != rel.source: recipients.append(organizer.user) activity.save() subject_template = 'relationships/emails/new_follower_subject.txt' body_template = 'relationships/emails/new_follower.txt' context = { 'user': rel.source, 'project': rel.target_project, 'domain': Site.objects.get_current().domain } send_notifications_i18n(recipients, subject_template, body_template, context, notification_category='new-follower' )
def follow_handler(sender, **kwargs): rel = kwargs.get("instance", None) created = kwargs.get("created", False) if not created or not isinstance(rel, Relationship) or rel.deleted: return activity = Activity(actor=rel.source, verb=verbs["follow"], target_object=rel) recipients = [] if rel.target_user: preferences = AccountPreferences.objects.filter(user=rel.target_user, key="no_email_new_follower") for pref in preferences: if pref.value: break else: recipients.append(rel.target_user) else: activity.scope_object = rel.target_project for organizer in rel.target_project.organizers(): if organizer.user != rel.source: preferences = AccountPreferences.objects.filter( user=organizer.user, key="no_email_new_project_follower" ) for pref in preferences: if pref.value: break else: recipients.append(organizer.user) activity.save() subject_template = "relationships/emails/new_follower_subject.txt" body_template = "relationships/emails/new_follower.txt" context = {"user": rel.source, "project": rel.target_project, "domain": Site.objects.get_current().domain} send_notifications_i18n(recipients, subject_template, body_template, context)
def test_reply_by_email(self): subject_template = 'replies/emails/post_comment_subject.txt' body_template = 'replies/emails/post_comment.txt' context = { 'comment': self.comment, 'domain': 'example.org', } callback_url = "/{0}/comments/{1}/email_reply/".format(self.locale, self.comment.id) send_notifications_i18n( self.user, subject_template, body_template, context, callback_url ) self.assertEqual(ResponseToken.objects.count(), 1) token = ResponseToken.objects.all()[0] data = { u'from': [u'Testing <*****@*****.**>'], u'to': [u'reply+{0}@reply.p2pu.org'.format(token.response_token)], u'text': [u'Maybe this time\n'], } #post_notification_response(token, '*****@*****.**', 'my response') with patch('requests.post') as requests_post: response = self.client.post('/{0}/notifications/response/'.format(self.locale), data) self.assertEqual(response.status_code, 200)
def email_confirmation_code(self, url, new_user=True): """Send a confirmation email to the user after registering.""" subject_template = 'users/emails/registration_confirm_subject.txt' body_template = 'users/emails/registration_confirm.txt' context = {'confirmation_url': url, 'new_user': new_user} send_notifications_i18n([self], subject_template, body_template, context, notification_category='account' )
def send_new_signup_answer_notification(answer): recipients = answer.sign_up.project.organizers() subject_template = 'signups/emails/new_signup_answer_subject.txt' body_template = 'signups/emails/new_signup_answer.txt' context = { 'answer': answer, 'domain': Site.objects.get_current().domain, } profiles = [recipient.user for recipient in recipients] send_notifications_i18n(profiles, subject_template, body_template, context)
def email_confirmation_code(self, url, new_user=True): """Send a confirmation email to the user after registering.""" subject_template = 'users/emails/registration_confirm_subject.txt' body_template = 'users/emails/registration_confirm.txt' context = {'confirmation_url': url, 'new_user': new_user} send_notifications_i18n([self], subject_template, body_template, context, notification_category='account')
def send_course_announcement(course_uri, announcement_text): course = get_course(course_uri) cohort = get_course_cohort(course_uri) users = UserProfile.objects.filter(username__in=cohort["users"].keys()) notification_model.send_notifications_i18n( users, "courses/emails/course_announcement_subject.txt", "courses/emails/course_announcement.txt", {"course": course, "announcement_text": announcement_text}, )
def send_notification(self): """Send notification when a new submission is posted.""" subject_template = 'badges/emails/new_submission_subject.txt' body_template = 'badges/emails/new_submission.txt' context = { 'submission': self, 'domain': Site.objects.get_current().domain, } profiles = self.badge.get_adopters() send_notifications_i18n(profiles, subject_template, body_template, context)
def send_course_announcement(course_uri, announcement_text): course = get_course(course_uri) cohort = get_course_cohort(course_uri) users = UserProfile.objects.filter(username__in=cohort['users'].keys()) notification_model.send_notifications_i18n( users, 'courses/emails/course_announcement_subject.txt', 'courses/emails/course_announcement.txt', { 'course': course, 'announcement_text': announcement_text }, notification_category='course-announcement.course-{0}'.format(course['id']) )
def test_notification_with_response(self): """ Test notification with possible response """ subject_template = 'replies/emails/post_comment_subject.txt' body_template = 'replies/emails/post_comment.txt' context = { 'comment': self.comment, 'domain': 'example.org', } message_count = len(mail.outbox) send_notifications_i18n([self.user], subject_template, body_template, context, "/call/me/back") self.assertEqual(ResponseToken.objects.count(), 1) self.assertEqual(len(mail.outbox), message_count + 1)
def send_notifications_i18n(self): subject_template = 'reviews/emails/review_submitted_subject.txt' body_template = 'reviews/emails/review_submitted.txt' context = { 'course': self.project.name, 'reviewer': self.author.username, 'review_text': self.content, 'review_url': self.get_absolute_url(), 'domain': Site.objects.get_current().domain, } profiles = [recipient.user for recipient in self.project.organizers()] send_notifications_i18n(profiles, subject_template, body_template, context)
def send_abuse_report(url, reason, other, user): from users.models import UserProfile try: profile = UserProfile.objects.get(email=settings.ADMINS[0][1]) subject_template = 'abuse/emails/abuse_report_subject.txt' body_template = 'abuse/emails/abuse_report.txt' context = {'user': user, 'url': url, 'reason': reason, 'other': other} send_notifications_i18n([profile], subject_template, body_template, context, notification_category='abuse-report') except: log.debug("Error sending abuse report!")
def send_course_announcement(course_uri, announcement_text): course = get_course(course_uri) cohort = get_course_cohort(course_uri) users = UserProfile.objects.filter(username__in=cohort['users'].keys()) notification_model.send_notifications_i18n( users, 'courses/emails/course_announcement_subject.txt', 'courses/emails/course_announcement.txt', { 'course': course, 'announcement_text': announcement_text, 'domain': Site.objects.get_current().domain }, notification_category='course-announcement.course-{0}'.format( course['id']))
def test_send_notification_i18n(self): """ Test non replyable notification """ #TODO use templates and context that doesn't rely on another app! subject_template = 'replies/emails/post_comment_subject.txt' body_template = 'replies/emails/post_comment.txt' context = { 'comment': self.comment, 'domain': 'example.org', } message_count = len(mail.outbox) send_notifications_i18n([self.user], subject_template, body_template, context) self.assertEqual(ResponseToken.objects.count(), 0) self.assertEqual(len(mail.outbox), message_count + 1)
def send_abuse_report(url, reason, other, user): from users.models import UserProfile try: profile = UserProfile.objects.get(email=settings.ADMINS[0][1]) subject_template = 'drumbeat/emails/abuse_report_subject.txt' body_template = 'drumbeat/emails/abuse_report.txt' context = { 'user': user, 'url': url, 'reason': reason, 'other': other } send_notifications_i18n([profile], subject_template, body_template, context) except: log.debug("Error sending abuse report!")
def send_creation_notification(self): """Send notification when a new project is created.""" subject_template = "projects/emails/project_created_subject.txt" body_template = "projects/emails/project_created.txt" context = {"project": self, "domain": Site.objects.get_current().domain} profiles = [recipient.user for recipient in self.organizers()] send_notifications_i18n( profiles, subject_template, body_template, context, notification_category="course-created" ) if not self.test: admin_subject = render_to_string("projects/emails/admin_project_created_subject.txt", context).strip() admin_body = render_to_string("projects/emails/admin_project_created.txt", context).strip() # TODO send using notifications and get email addresses from group, not settings for admin_email in settings.ADMIN_PROJECT_CREATE_EMAIL: send_mail(admin_subject, admin_body, admin_email, [admin_email], fail_silently=True)
def send_new_signup_answer_notification(answer): recipients = answer.sign_up.project.organizers() subject_template = 'signups/emails/new_signup_answer_subject.txt' body_template = 'signups/emails/new_signup_answer.txt' context = { 'answer': answer, 'domain': Site.objects.get_current().domain, } profiles = [recipient.user for recipient in recipients] notification_category = u'course-signup.project-{0}'.format( answer.sign_up.project.slug ) send_notifications_i18n(profiles, subject_template, body_template, context, notification_category=notification_category )
def send_notification(self): """Send notification when a new submission is posted.""" subject_template = 'badges/emails/new_submission_subject.txt' body_template = 'badges/emails/new_submission.txt' context = { 'submission': self, 'domain': Site.objects.get_current().domain, } profiles = self.badge.get_adopters() send_notifications_i18n( profiles, subject_template, body_template, context, notification_category=u'badge-submission.badge-{0}'.format( self.badge.slug))
def test_notification_with_response(self): """ Test notification with possible response """ subject_template = 'replies/emails/post_comment_subject.txt' body_template = 'replies/emails/post_comment.txt' context = { 'comment': self.comment, 'domain': 'example.org', } message_count = len(mail.outbox) send_notifications_i18n([self.user], subject_template, body_template, context, "/call/me/back", notification_category='reply.course-1') self.assertEqual(ResponseToken.objects.count(), 1) self.assertEqual(len(mail.outbox), message_count + 1)
def test_send_notification_i18n(self): """ Test non replyable notification """ #TODO use templates and context that doesn't rely on another app! subject_template = 'replies/emails/post_comment_subject.txt' body_template = 'replies/emails/post_comment.txt' context = { 'comment': self.comment, 'domain': 'example.org', } message_count = len(mail.outbox) send_notifications_i18n([self.user], subject_template, body_template, context, notification_category='account') self.assertEqual(ResponseToken.objects.count(), 0) self.assertEqual(len(mail.outbox), message_count + 1)
def send_comment_notification(self): recipients = [] if self.page_object: recipients = self.page_object.comment_notification_recipients(self) subject_template = 'replies/emails/post_comment_subject.txt' body_template = 'replies/emails/post_comment.txt' context = { 'comment': self, 'domain': Site.objects.get_current().domain, } profiles = [] for profile in recipients: if self.author != profile: profiles.append(profile) reply_url = reverse('email_reply', args=[self.id]) send_notifications_i18n(profiles, subject_template, body_template, context, reply_url, self.author.username )
def send_notifications_i18n(self): subject_template = 'reviews/emails/review_submitted_subject.txt' body_template = 'reviews/emails/review_submitted.txt' context = { 'course': self.project.name, 'reviewer': self.author.username, 'review_text': self.content, 'review_url': self.get_absolute_url(), 'domain': Site.objects.get_current().domain, } profiles = [recipient.user for recipient in self.project.organizers()] send_notifications_i18n( profiles, subject_template, body_template, context, notification_category=u'course-review.project-{0}'.format( self.project.slug))
def send_creation_notification(self): """Send notification when a new project is created.""" subject_template = 'projects/emails/project_created_subject.txt' body_template = 'projects/emails/project_created.txt' context = { 'project': self, 'domain': Site.objects.get_current().domain, } profiles = [recipient.user for recipient in self.organizers()] send_notifications_i18n(profiles, subject_template, body_template, context) if not self.test: admin_subject = render_to_string( "projects/emails/admin_project_created_subject.txt", context).strip() admin_body = render_to_string( "projects/emails/admin_project_created.txt", context).strip() for admin_email in settings.ADMIN_PROJECT_CREATE_EMAIL: send_mail(admin_subject, admin_body, admin_email, [admin_email], fail_silently=True)
def message_sent_handler(sender, **kwargs): message = kwargs.get('instance', None) created = kwargs.get('created', False) if not created or not isinstance(message, Message): return recipient = message.recipient.get_profile() sender = message.sender.get_profile() reply_url = reverse('drumbeatmail_reply', kwargs={'message': message.pk}) msg_body = clean_html('rich', message.body) subject_template = 'drumbeatmail/emails/direct_message_subject.txt' body_template = 'drumbeatmail/emails/direct_message.txt' context = { 'sender': sender, 'message': msg_body, 'domain': Site.objects.get_current().domain, 'reply_url': reply_url, } send_notifications_i18n([recipient], subject_template, body_template, context, notification_category='direct-message' )
def send_email_notification(instance): project = instance.project if not instance.listed: return recipients = project.participants() subject_template = "content/emails/content_update_subject.txt" body_template = "content/emails/content_update.txt" context = {"instance": instance, "project": project, "domain": Site.objects.get_current().domain} from_organizer = project.organizers().filter(user=instance.author).exists() profiles = [] for recipient in recipients: profile = recipient.user if instance.author != profile: profiles.append(profile) send_notifications_i18n( profiles, subject_template, body_template, context, notification_category=u"content-updated.project-{0}".format(project.slug), )
def send_wall_notification(self): if not self.project: return recipients = self.project.participants() subject_template = 'statuses/emails/wall_updated_subject.txt' body_template = 'statuses/emails/wall_updated.txt' context = { 'status': self, 'project': self.project, 'domain': Site.objects.get_current().domain, } from_organizer = self.project.organizers().filter( user=self.author).exists() profiles = [] for recipient in recipients: profile = recipient.user if self.important: unsubscribed = False elif from_organizer: unsubscribed = recipient.no_organizers_wall_updates else: unsubscribed = recipient.no_participants_wall_updates if self.author != profile and not unsubscribed: profiles.append(profile) kwargs = { 'page_app_label':'activity', 'page_model': 'activity', 'page_pk' : self.activity.get().id, } if self.project: kwargs.update({ 'scope_app_label': 'projects', 'scope_model': 'project', 'scope_pk': self.project.id, }) callback_url = reverse('page_comment_callback', kwargs=kwargs) send_notifications_i18n( profiles, subject_template, body_template, context, callback_url, self.author.username )
def send_wall_notification(self): if not self.project: return recipients = self.project.participants() subject_template = 'statuses/emails/wall_updated_subject.txt' body_template = 'statuses/emails/wall_updated.txt' context = { 'status': self, 'project': self.project, 'domain': Site.objects.get_current().domain, } from_organizer = self.project.organizers().filter( user=self.author).exists() profiles = [ recipient.user for recipient in recipients if self.author != recipient.user ] kwargs = { 'page_app_label': 'activity', 'page_model': 'activity', 'page_pk': self.activity.get().id, } if self.project: kwargs.update({ 'scope_app_label': 'projects', 'scope_model': 'project', 'scope_pk': self.project.id, }) callback_url = reverse('page_comment_callback', kwargs=kwargs) send_notifications_i18n( profiles, subject_template, body_template, context, callback_url, self.author.username, notification_category=u'course-announcement.project-{0}'.format( self.project.slug))
def send_creation_notification(self): """Send notification when a new project is created.""" subject_template = 'projects/emails/project_created_subject.txt' body_template = 'projects/emails/project_created.txt' context = { 'project': self, 'domain': Site.objects.get_current().domain, } profiles = [recipient.user for recipient in self.organizers()] send_notifications_i18n(profiles, subject_template, body_template, context, notification_category='course-created' ) if not self.test: admin_subject = render_to_string( "projects/emails/admin_project_created_subject.txt", context).strip() admin_body = render_to_string( "projects/emails/admin_project_created.txt", context).strip() # TODO send using notifications and get email addresses from group, not settings for admin_email in settings.ADMIN_PROJECT_CREATE_EMAIL: send_mail(admin_subject, admin_body, admin_email, [admin_email], fail_silently=True)
def message_sent_handler(sender, **kwargs): message = kwargs.get('instance', None) created = kwargs.get('created', False) if not created or not isinstance(message, Message): return recipient = message.recipient.get_profile() sender = message.sender.get_profile() reply_url = reverse('drumbeatmail_reply', kwargs={'message': message.pk}) msg_body = clean_html('rich', message.body) subject_template = 'drumbeatmail/emails/direct_message_subject.txt' body_template = 'drumbeatmail/emails/direct_message.txt' context = { 'sender': sender, 'message': msg_body, 'domain': Site.objects.get_current().domain, 'reply_url': reply_url, } send_notifications_i18n([recipient], subject_template, body_template, context, notification_category='direct-message')
def add_user_to_cohort(cohort_uri, user_uri, role, notify_organizers=False): cohort_db = _get_cohort_db(cohort_uri) username = user_uri.strip('/').split('/')[-1] if not UserProfile.objects.filter(username=username).exists(): raise ResourceNotFoundException( u'User {0} does not exist'.format(user_uri)) if db.CohortSignup.objects.filter(cohort=cohort_db, user_uri=user_uri, leave_date__isnull=True).exists(): return None signup_db = db.CohortSignup(cohort=cohort_db, user_uri=user_uri, role=role) signup_db.save() if notify_organizers: cohort = get_cohort(cohort_uri) course = get_course(cohort['course_uri']) organizers = UserProfile.objects.filter( username__in=cohort['organizers']) context = { 'course': course, 'new_user': username, 'domain': Site.objects.get_current().domain, } subject_template = 'courses/emails/course_join_subject.txt' body_template = 'courses/emails/course_join.txt' notification_model.send_notifications_i18n( organizers, subject_template, body_template, context, notification_category='course-signup.course-{0}'.format( course['id'])) signup = {"cohort_uri": cohort_uri, "user_uri": user_uri, "role": role} return signup
def message_sent_handler(sender, **kwargs): message = kwargs.get('instance', None) created = kwargs.get('created', False) if not created or not isinstance(message, Message): return recipient = message.recipient.get_profile() preferences = AccountPreferences.objects.filter(user=recipient, key='no_email_message_received') for preference in preferences: if preference.value: return sender = message.sender.get_profile() reply_url = reverse('drumbeatmail_reply', kwargs={'message': message.pk}) msg_body = clean_html('rich', message.body) subject_template = 'drumbeatmail/emails/direct_message_subject.txt' body_template = 'drumbeatmail/emails/direct_message.txt' context = { 'sender': sender, 'message': msg_body, 'domain': Site.objects.get_current().domain, 'reply_url': reply_url, } send_notifications_i18n([recipient], subject_template, body_template, context)
def send_email_notification(instance): project = instance.project if not instance.listed: return recipients = project.participants() subject_template = 'content/emails/content_update_subject.txt' body_template = 'content/emails/content_update.txt' context = { 'instance': instance, 'project': project, 'domain': Site.objects.get_current().domain, } from_organizer = project.organizers().filter( user=instance.author).exists() profiles = [] for recipient in recipients: profile = recipient.user if from_organizer: unsubscribed = recipient.no_organizers_content_updates else: unsubscribed = recipient.no_participants_content_updates if instance.author != profile and not unsubscribed: profiles.append(profile) send_notifications_i18n(profiles, subject_template, body_template, context)