digest_email: Add endpoint for rendering digest to the web.

Adds "/digest/" endpoint for rendering content of digest email
to the web.

Fixes #9974
This commit is contained in:
Roman Godov 2018-08-12 23:09:34 +03:00 committed by Tim Abbott
parent a0b0ccf972
commit 9c8eeaed85
4 changed files with 31 additions and 2 deletions

View File

@ -1,4 +1,4 @@
from typing import Any, Callable, Dict, Iterable, List, Set, Tuple from typing import Any, Callable, Dict, Iterable, List, Set, Tuple, Union
from collections import defaultdict from collections import defaultdict
import datetime import datetime
@ -193,7 +193,8 @@ def enough_traffic(unread_pms: str, hot_conversations: str, new_streams: int, ne
return True return True
return False return False
def handle_digest_email(user_profile_id: int, cutoff: float) -> None: def handle_digest_email(user_profile_id: int, cutoff: float,
render_to_web: bool = False) -> Union[None, Dict[str, Any]]:
user_profile = get_user_profile_by_id(user_profile_id) user_profile = get_user_profile_by_id(user_profile_id)
# We are disabling digest emails for soft deactivated users for the time. # We are disabling digest emails for soft deactivated users for the time.
@ -257,6 +258,9 @@ def handle_digest_email(user_profile_id: int, cutoff: float) -> None:
user_profile, cutoff_date) user_profile, cutoff_date)
context["new_users"] = new_users context["new_users"] = new_users
if render_to_web:
return context
# We don't want to send emails containing almost no information. # We don't want to send emails containing almost no information.
if enough_traffic(context["unread_pms"], context["hot_conversations"], if enough_traffic(context["unread_pms"], context["hot_conversations"],
new_streams_count, new_users_count): new_streams_count, new_users_count):
@ -265,3 +269,4 @@ def handle_digest_email(user_profile_id: int, cutoff: float) -> None:
send_future_email('zerver/emails/digest', user_profile.realm, to_user_ids=[user_profile.id], send_future_email('zerver/emails/digest', user_profile.realm, to_user_ids=[user_profile.id],
from_name="Zulip Digest", from_address=FromAddress.NOREPLY, from_name="Zulip Digest", from_address=FromAddress.NOREPLY,
context=context) context=context)
return None

View File

@ -270,3 +270,9 @@ class TestDigestEmailMessages(ZulipTestCase):
do_create_user('abc@mit.edu', password='abc', realm=user.realm, full_name='abc', short_name='abc') do_create_user('abc@mit.edu', password='abc', realm=user.realm, full_name='abc', short_name='abc')
gathered_no_of_user, _ = gather_new_users(user, cutoff) gathered_no_of_user, _ = gather_new_users(user, cutoff)
self.assertEqual(gathered_no_of_user, 0) self.assertEqual(gathered_no_of_user, 0)
class TestDigestContentInBrowser(ZulipTestCase):
def test_get_digest_content_in_browser(self) -> None:
self.login(self.example_email('hamlet'))
result = self.client_get("/digest/")
self.assert_in_success_response(["A lot has happened on Zulip while you've been away!"], result)

14
zerver/views/digest.py Normal file
View File

@ -0,0 +1,14 @@
import time
from django.http import HttpRequest, HttpResponse
from django.shortcuts import render
from django.utils.timezone import now as timezone_now
from zerver.lib.digest import handle_digest_email, DIGEST_CUTOFF
from zerver.decorator import zulip_login_required
from datetime import timedelta
@zulip_login_required
def digest_page(request: HttpRequest) -> HttpResponse:
user_profile_id = request.user.id
cutoff = time.mktime((timezone_now() - timedelta(days=DIGEST_CUTOFF)).timetuple())
context = handle_digest_email(user_profile_id, cutoff, render_to_web=True)
return render(request, 'zerver/emails/compiled/digest.html', context=context)

View File

@ -33,6 +33,7 @@ import zerver.views.user_settings
import zerver.views.muting import zerver.views.muting
import zerver.views.streams import zerver.views.streams
import zerver.views.realm import zerver.views.realm
import zerver.views.digest
from zerver.lib.rest import rest_dispatch from zerver.lib.rest import rest_dispatch
@ -416,6 +417,9 @@ i18n_urls = [
zerver.views.auth.show_deactivation_notice, zerver.views.auth.show_deactivation_notice,
name='zerver.views.auth.show_deactivation_notice'), name='zerver.views.auth.show_deactivation_notice'),
# Displays digest email content in browser.
url(r'^digest/$', zerver.views.digest.digest_page),
# Registration views, require a confirmation ID. # Registration views, require a confirmation ID.
url(r'^accounts/register/social/(\w+)$', url(r'^accounts/register/social/(\w+)$',
zerver.views.auth.start_social_signup, zerver.views.auth.start_social_signup,