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")
def notifications_api(request): """ API for retrieving user notifications. """ if not request.user.is_authenticated(): return jsonResponse({"error": "You must be logged in to access your notifications."}) page = int(request.GET.get("page", 1)) page_size = int(request.GET.get("page_size", 10)) notifications = NotificationSet().recent_for_user(request.user.id, limit=page_size, page=page) return jsonResponse({ "html": notifications.to_HTML(), "page": page, "page_size": page_size, "count": notifications.count })