mirror of
https://github.com/zulip/zulip.git
synced 2025-11-10 08:56:10 +00:00
notifications: Use create_confirmation_link for unsubscription.
This commit is contained in:
@@ -39,19 +39,20 @@ def get_object_from_key(confirmation_key):
|
|||||||
obj.save(update_fields=['status'])
|
obj.save(update_fields=['status'])
|
||||||
return obj
|
return obj
|
||||||
|
|
||||||
def create_confirmation_link(obj, host, confirmation_type):
|
def create_confirmation_link(obj, host, confirmation_type, url_args=None):
|
||||||
# type: (Union[ContentType, int], str, int) -> str
|
# type: (Union[ContentType, int], str, int, Optional[Dict[str, str]]) -> str
|
||||||
key = generate_key()
|
key = generate_key()
|
||||||
Confirmation.objects.create(content_object=obj, date_sent=timezone_now(), confirmation_key=key,
|
Confirmation.objects.create(content_object=obj, date_sent=timezone_now(), confirmation_key=key,
|
||||||
type=confirmation_type)
|
type=confirmation_type)
|
||||||
return confirmation_url(key, host, confirmation_type)
|
return confirmation_url(key, host, confirmation_type, url_args)
|
||||||
|
|
||||||
def confirmation_url(confirmation_key, host, confirmation_type):
|
def confirmation_url(confirmation_key, host, confirmation_type, url_args=None):
|
||||||
# type: (str, str, int) -> str
|
# type: (str, str, int, Optional[Dict[str, str]]) -> str
|
||||||
return '%s%s%s' % (settings.EXTERNAL_URI_SCHEME,
|
if url_args is None:
|
||||||
host,
|
url_args = {}
|
||||||
reverse(_properties[confirmation_type].url_name,
|
url_args['confirmation_key'] = confirmation_key
|
||||||
kwargs={'confirmation_key': confirmation_key}))
|
return '%s%s%s' % (settings.EXTERNAL_URI_SCHEME, host,
|
||||||
|
reverse(_properties[confirmation_type].url_name, kwargs=url_args))
|
||||||
|
|
||||||
class Confirmation(models.Model):
|
class Confirmation(models.Model):
|
||||||
content_type = models.ForeignKey(ContentType)
|
content_type = models.ForeignKey(ContentType)
|
||||||
@@ -83,6 +84,8 @@ _properties = {
|
|||||||
Confirmation.INVITATION: ConfirmationType('confirmation.views.confirm',
|
Confirmation.INVITATION: ConfirmationType('confirmation.views.confirm',
|
||||||
validity_in_days=settings.INVITATION_LINK_VALIDITY_DAYS),
|
validity_in_days=settings.INVITATION_LINK_VALIDITY_DAYS),
|
||||||
Confirmation.EMAIL_CHANGE: ConfirmationType('zerver.views.user_settings.confirm_email_change'),
|
Confirmation.EMAIL_CHANGE: ConfirmationType('zerver.views.user_settings.confirm_email_change'),
|
||||||
|
Confirmation.UNSUBSCRIBE: ConfirmationType('zerver.views.unsubscribe.email_unsubscribe',
|
||||||
|
validity_in_days=1000000), # should never expire
|
||||||
}
|
}
|
||||||
|
|
||||||
# Conirmation pathways for which there is no content_object that we need to
|
# Conirmation pathways for which there is no content_object that we need to
|
||||||
|
|||||||
@@ -33,22 +33,15 @@ import ujson
|
|||||||
from six.moves import urllib
|
from six.moves import urllib
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
|
||||||
def unsubscribe_token(user_profile):
|
|
||||||
# type: (UserProfile) -> Text
|
|
||||||
# Leverage the Django confirmations framework to generate and track unique
|
|
||||||
# unsubscription tokens.
|
|
||||||
# Will be changed to UNSUBSCRIBE in a few commits. This is the current behavior.
|
|
||||||
return create_confirmation_link(user_profile, 'unused', Confirmation.USER_REGISTRATION).split("/")[-1]
|
|
||||||
|
|
||||||
def one_click_unsubscribe_link(user_profile, email_type):
|
def one_click_unsubscribe_link(user_profile, email_type):
|
||||||
# type: (UserProfile, Text) -> Text
|
# type: (UserProfile, str) -> str
|
||||||
"""
|
"""
|
||||||
Generate a unique link that a logged-out user can visit to unsubscribe from
|
Generate a unique link that a logged-out user can visit to unsubscribe from
|
||||||
Zulip e-mails without having to first log in.
|
Zulip e-mails without having to first log in.
|
||||||
"""
|
"""
|
||||||
token = unsubscribe_token(user_profile)
|
return create_confirmation_link(user_profile, user_profile.realm.host,
|
||||||
resource_path = "accounts/unsubscribe/%s/%s" % (email_type, token)
|
Confirmation.UNSUBSCRIBE,
|
||||||
return "%s/%s" % (user_profile.realm.uri.rstrip("/"), resource_path)
|
url_args = {'email_type': email_type})
|
||||||
|
|
||||||
def hash_util_encode(string):
|
def hash_util_encode(string):
|
||||||
# type: (Text) -> Text
|
# type: (Text) -> Text
|
||||||
|
|||||||
Reference in New Issue
Block a user