def email_unread_notifications(timeframe): """ Looks for all unread notifcations and sends each user one email with a summary. Marks any sent notifications as "read". timeframe may be: * 'daily' - only send to users who have the daily email setting * 'weekly' - only send to users who have the weekly email setting * 'all' - send all notifications """ users = db.notifications.find({"read": False}).distinct("uid") for uid in users: profile = UserProfile(id=uid) if profile.settings["email_notifications"] != timeframe and timeframe != 'all': continue notifications = NotificationSet().unread_for_user(uid) try: user = User.objects.get(id=uid) except User.DoesNotExist: continue message_html = render_to_string("email/notifications_email.html", { "notifications": notifications, "recipient": user.first_name }) #message_text = util.strip_tags(message_html) subject = "New Activity on Sefaria from %s" % notifications.actors_string() from_email = "Sefaria <*****@*****.**>" to = user.email msg = EmailMultiAlternatives(subject, message_html, from_email, [to]) msg.content_subtype = "html" # Main content is now text/html #msg.attach_alternative(message_text, "text/plain") msg.send() notifications.mark_read(via="email")
def email_unread_notifications(timeframe): """ Looks for all unread notifications and sends each user one email with a summary. Marks any sent notifications as "read". timeframe may be: * 'daily' - only send to users who have the daily email setting * 'weekly' - only send to users who have the weekly email setting * 'all' - send all notifications """ from sefaria.model.notification import NotificationSet users = db.notifications.find({ "read": False, "is_global": False }).distinct("uid") for uid in users: profile = UserProfile(id=uid) if profile.settings[ "email_notifications"] != timeframe and timeframe != 'all': continue notifications = NotificationSet().unread_personal_for_user(uid) if len(notifications) == 0: continue try: user = User.objects.get(id=uid) except User.DoesNotExist: continue if "interface_language" in profile.settings: translation.activate(profile.settings["interface_language"][0:2]) message_html = render_to_string("email/notifications_email.html", { "notifications": notifications, "recipient": user.first_name }) actors_string = notifications.actors_string() # TODO Hebrew subjects if actors_string: verb = "have" if " and " in actors_string else "has" subject = "%s %s new activity on Sefaria" % (actors_string, verb) elif notifications.like_count() > 0: noun = "likes" if notifications.like_count() > 1 else "like" subject = "%d new %s on your Source Sheet" % ( notifications.like_count(), noun) from_email = "Sefaria <*****@*****.**>" to = user.email msg = EmailMultiAlternatives(subject, message_html, from_email, [to]) msg.content_subtype = "html" try: msg.send() notifications.mark_read(via="email") except AnymailRecipientsRefused: print('bad email address: {}'.format(to)) if "interface_language" in profile.settings: translation.deactivate()
def email_unread_notifications(timeframe): """ Looks for all unread notifications and sends each user one email with a summary. Marks any sent notifications as "read". timeframe may be: * 'daily' - only send to users who have the daily email setting * 'weekly' - only send to users who have the weekly email setting * 'all' - send all notifications """ from sefaria.model.notification import NotificationSet users = db.notifications.find({"read": False, "is_global": False}).distinct("uid") for uid in users: profile = UserProfile(id=uid) if profile.settings["email_notifications"] != timeframe and timeframe != 'all': continue notifications = NotificationSet().unread_personal_for_user(uid) if notifications.count() == 0: continue try: user = User.objects.get(id=uid) except User.DoesNotExist: continue if "interface_language" in profile.settings: translation.activate(profile.settings["interface_language"][0:2]) message_html = render_to_string("email/notifications_email.html", {"notifications": notifications, "recipient": user.first_name}) #message_text = util.strip_tags(message_html) actors_string = notifications.actors_string() # TODO Hebrew subjects if actors_string: verb = "have" if " and " in actors_string else "has" subject = "%s %s new activity on Sefaria" % (actors_string, verb) elif notifications.like_count() > 0: noun = "likes" if notifications.like_count() > 1 else "like" subject = "%d new %s on your Source Sheet" % (notifications.like_count(), noun) from_email = "Sefaria <*****@*****.**>" to = user.email msg = EmailMultiAlternatives(subject, message_html, from_email, [to]) msg.content_subtype = "html" # Main content is now text/html #msg.attach_alternative(message_text, "text/plain") try: msg.send() notifications.mark_read(via="email") except AnymailRecipientsRefused: print u'bad email address: {}'.format(to) if "interface_language" in profile.settings: translation.deactivate()
def email_unread_notifications(timeframe): """ Looks for all unread notifications and sends each user one email with a summary. Marks any sent notifications as "read". timeframe may be: * 'daily' - only send to users who have the daily email setting * 'weekly' - only send to users who have the weekly email setting * 'all' - send all notifications """ from sefaria.model.notification import NotificationSet users = db.notifications.find({ "read": False, "is_global": False }).distinct("uid") for uid in users: profile = UserProfile(id=uid) if profile.settings[ "email_notifications"] != timeframe and timeframe != 'all': continue notifications = NotificationSet().unread_personal_for_user(uid) if notifications.count() == 0: continue try: user = User.objects.get(id=uid) except User.DoesNotExist: continue if "interface_language" in profile.settings: translation.activate(profile.settings["interface_language"][0:2]) message_html = render_to_string("email/notifications_email.html", { "notifications": notifications, "recipient": user.first_name }) #message_text = util.strip_tags(message_html) actors_string = notifications.actors_string() verb = "have" if " and " in actors_string else "has" subject = "%s %s new activity on Sefaria" % (actors_string, verb) from_email = "Sefaria <*****@*****.**>" to = user.email msg = EmailMultiAlternatives(subject, message_html, from_email, [to]) msg.content_subtype = "html" # Main content is now text/html #msg.attach_alternative(message_text, "text/plain") msg.send() notifications.mark_read(via="email") if "interface_language" in profile.settings: translation.deactivate()
def email_unread_notifications(timeframe): """ Looks for all unread notifcations and sends each user one email with a summary. Marks any sent notifications as "read". timeframe may be: * 'daily' - only send to users who have the daily email setting * 'weekly' - only send to users who have the weekly email setting * 'all' - send all notifications """ users = db.notifications.find({"read": False}).distinct("uid") for uid in users: profile = UserProfile(id=uid) if profile.settings[ "email_notifications"] != timeframe and timeframe != 'all': continue notifications = NotificationSet().unread_for_user(uid) try: user = User.objects.get(id=uid) except User.DoesNotExist: continue message_html = render_to_string("email/notifications_email.html", { "notifications": notifications, "recipient": user.first_name }) #message_text = util.strip_tags(message_html) subject = "New Activity on Sefaria from %s" % notifications.actors_string( ) from_email = "The Sefaria Project <*****@*****.**>" to = user.email msg = EmailMultiAlternatives(subject, message_html, from_email, [to]) msg.content_subtype = "html" # Main content is now text/html #msg.attach_alternative(message_text, "text/plain") msg.send() notifications.mark_read(via="email")