emails: Move clear_scheduled_*emails to send_email.py.

It more properly belongs as part of our outgoing email library, not
notifications.py (which is really about email notifications).
This commit is contained in:
Tim Abbott 2019-03-15 10:48:01 -07:00
parent 89351cdd19
commit 28a4c143c8
5 changed files with 23 additions and 24 deletions

View File

@ -47,7 +47,8 @@ from zerver.lib.message import (
from zerver.lib.realm_icon import realm_icon_url
from zerver.lib.realm_logo import realm_logo_url
from zerver.lib.retention import move_messages_to_archive
from zerver.lib.send_email import send_email, FromAddress, send_email_to_admins
from zerver.lib.send_email import send_email, FromAddress, send_email_to_admins, \
clear_scheduled_emails, clear_scheduled_invitation_emails
from zerver.lib.stream_subscription import (
get_active_subscriptions_for_stream_id,
get_active_subscriptions_for_stream_ids,
@ -137,8 +138,7 @@ from zerver.lib.utils import log_statsd_event, statsd
from zerver.lib.i18n import get_language_name
from zerver.lib.alert_words import add_user_alert_words, \
remove_user_alert_words, set_user_alert_words
from zerver.lib.notifications import clear_scheduled_emails, \
clear_scheduled_invitation_emails, enqueue_welcome_emails
from zerver.lib.notifications import enqueue_welcome_emails
from zerver.lib.exceptions import JsonableError, ErrorCode, BugdownRenderingException
from zerver.lib.sessions import delete_user_sessions
from zerver.lib.upload import attachment_url_re, attachment_url_to_path_id, \

View File

@ -1,5 +1,5 @@
from typing import Any, Dict, Iterable, List, Optional, Tuple
from typing import Any, Dict, Iterable, List, Tuple
from confirmation.models import one_click_unsubscribe_link
from django.conf import settings
@ -15,7 +15,6 @@ from zerver.lib.url_encoding import personal_narrow_url, huddle_narrow_url, \
stream_narrow_url, topic_narrow_url
from zerver.models import (
Recipient,
ScheduledEmail,
UserMessage,
Stream,
get_display_recipient,
@ -457,22 +456,6 @@ def handle_missedmessage_emails(user_profile_id: int,
message_count_by_bucket[bucket_tup],
)
def clear_scheduled_invitation_emails(email: str) -> None:
"""Unlike most scheduled emails, invitation emails don't have an
existing user object to key off of, so we filter by address here."""
items = ScheduledEmail.objects.filter(address__iexact=email,
type=ScheduledEmail.INVITATION_REMINDER)
items.delete()
def clear_scheduled_emails(user_ids: List[int], email_type: Optional[int]=None) -> None:
items = ScheduledEmail.objects.filter(users__in=user_ids).distinct()
if email_type is not None:
items = items.filter(type=email_type)
for item in items:
item.users.remove(*user_ids)
if item.users.all().count() == 0:
item.delete()
def log_digest_event(msg: str) -> None:
import logging
import time

View File

@ -166,6 +166,22 @@ def send_email_to_admins(template_prefix: str, realm: Realm, from_name: Optional
send_email(template_prefix, to_user_ids=admin_user_ids, from_name=from_name,
from_address=from_address, context=context)
def clear_scheduled_invitation_emails(email: str) -> None:
"""Unlike most scheduled emails, invitation emails don't have an
existing user object to key off of, so we filter by address here."""
items = ScheduledEmail.objects.filter(address__iexact=email,
type=ScheduledEmail.INVITATION_REMINDER)
items.delete()
def clear_scheduled_emails(user_ids: List[int], email_type: Optional[int]=None) -> None:
items = ScheduledEmail.objects.filter(users__in=user_ids).distinct()
if email_type is not None:
items = items.filter(type=email_type)
for item in items:
item.users.remove(*user_ids)
if item.users.all().count() == 0:
item.delete()
def handle_send_email_format_changes(job: Dict[str, Any]) -> None:
# Reformat any jobs that used the old to_email
# and to_user_ids argument formats.

View File

@ -21,8 +21,7 @@ from zerver.models import UserProfile, Recipient, \
from zerver.lib.avatar import avatar_url
from zerver.lib.exceptions import JsonableError
from zerver.lib.send_email import send_future_email
from zerver.lib.notifications import clear_scheduled_emails
from zerver.lib.send_email import send_future_email, clear_scheduled_emails
from zerver.lib.actions import (
get_emails_from_user_ids,
get_recipient_info,

View File

@ -4,7 +4,8 @@ from typing import Callable
from confirmation.models import Confirmation, get_object_from_key, \
ConfirmationKeyException
from zerver.lib.actions import do_change_notification_settings, clear_scheduled_emails
from zerver.lib.actions import do_change_notification_settings
from zerver.lib.send_email import clear_scheduled_emails
from zerver.models import UserProfile, ScheduledEmail
from zerver.context_processors import common_context