From 17be6e916531bf937b372ab047f9f89132f2ada3 Mon Sep 17 00:00:00 2001 From: Tim Abbott Date: Thu, 8 Nov 2018 13:40:27 -0800 Subject: [PATCH] confirmation: Move one_click_unsubscribe_link out of notifications.py. This helps prevent a problematic import sequence when we start using it from signals.py. --- confirmation/models.py | 9 +++++++++ zerver/lib/digest.py | 3 ++- zerver/lib/notifications.py | 11 +---------- zerver/tests/test_signup.py | 5 +++-- 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/confirmation/models.py b/confirmation/models.py index 154de6dacf..7b99e4a5c3 100644 --- a/confirmation/models.py +++ b/confirmation/models.py @@ -123,6 +123,15 @@ _properties = { Confirmation.REALM_CREATION: ConfirmationType('check_prereg_key_and_redirect'), } +def one_click_unsubscribe_link(user_profile: UserProfile, email_type: str) -> str: + """ + Generate a unique link that a logged-out user can visit to unsubscribe from + Zulip e-mails without having to first log in. + """ + return create_confirmation_link(user_profile, user_profile.realm.host, + Confirmation.UNSUBSCRIBE, + url_args = {'email_type': email_type}) + # Functions related to links generated by the generate_realm_creation_link.py # management command. # Note that being validated here will just allow the user to access the create_realm diff --git a/zerver/lib/digest.py b/zerver/lib/digest.py index 12a64c9c84..40ff81706b 100644 --- a/zerver/lib/digest.py +++ b/zerver/lib/digest.py @@ -10,7 +10,8 @@ from django.template import loader from django.conf import settings from django.utils.timezone import now as timezone_now -from zerver.lib.notifications import build_message_list, one_click_unsubscribe_link +from confirmation.models import one_click_unsubscribe_link +from zerver.lib.notifications import build_message_list from zerver.lib.send_email import send_future_email, FromAddress from zerver.lib.url_encoding import encode_stream from zerver.models import UserProfile, UserMessage, Recipient, Stream, \ diff --git a/zerver/lib/notifications.py b/zerver/lib/notifications.py index f55bb42d25..fde99872cf 100644 --- a/zerver/lib/notifications.py +++ b/zerver/lib/notifications.py @@ -1,7 +1,7 @@ from typing import cast, Any, Dict, Iterable, List, Mapping, Optional, Sequence, Tuple -from confirmation.models import Confirmation, create_confirmation_link +from confirmation.models import Confirmation, one_click_unsubscribe_link from django.conf import settings from django.template import loader from django.utils.timezone import now as timezone_now @@ -37,15 +37,6 @@ import ujson from collections import defaultdict import pytz -def one_click_unsubscribe_link(user_profile: UserProfile, email_type: str) -> str: - """ - Generate a unique link that a logged-out user can visit to unsubscribe from - Zulip e-mails without having to first log in. - """ - return create_confirmation_link(user_profile, user_profile.realm.host, - Confirmation.UNSUBSCRIBE, - url_args = {'email_type': email_type}) - def relative_to_full_url(base_url: str, content: str) -> str: # Convert relative URLs to absolute URLs. fragment = lxml.html.fromstring(content) diff --git a/zerver/tests/test_signup.py b/zerver/tests/test_signup.py index b9e03f7b70..3790e18797 100644 --- a/zerver/tests/test_signup.py +++ b/zerver/tests/test_signup.py @@ -17,7 +17,8 @@ from mock import patch, MagicMock from zerver.lib.test_helpers import MockLDAP, get_test_image_file, avatar_disk_path from confirmation.models import Confirmation, create_confirmation_link, MultiuseInvite, \ - generate_key, confirmation_url, get_object_from_key, ConfirmationKeyException + generate_key, confirmation_url, get_object_from_key, ConfirmationKeyException, \ + one_click_unsubscribe_link from confirmation import settings as confirmation_settings from zerver.forms import HomepageForm, WRONG_SUBDOMAIN_ERROR, check_subdomain_available @@ -55,7 +56,7 @@ from zerver.lib.avatar import avatar_url from zerver.lib.mobile_auth_otp import xor_hex_strings, ascii_to_hex, \ otp_encrypt_api_key, is_valid_otp, hex_to_ascii, otp_decrypt_api_key from zerver.lib.notifications import enqueue_welcome_emails, \ - one_click_unsubscribe_link, followup_day2_email_delay + followup_day2_email_delay from zerver.lib.subdomains import is_root_domain_available from zerver.lib.test_helpers import find_key_by_email, queries_captured, \ HostRequestMock, load_subdomain_token