confirmation: Support more models as ConfirmationObjT.

Signed-off-by: Zixuan James Li <p359101898@gmail.com>
This commit is contained in:
Zixuan James Li 2022-06-28 15:52:24 -04:00 committed by Tim Abbott
parent 710cf9647e
commit 0017f7a025
5 changed files with 13 additions and 5 deletions

View File

@ -54,7 +54,7 @@ def generate_key() -> str:
return b32encode(secrets.token_bytes(15)).decode().lower() return b32encode(secrets.token_bytes(15)).decode().lower()
ConfirmationObjT = Union[MultiuseInvite, PreregistrationUser, EmailChangeStatus] ConfirmationObjT = Union[MultiuseInvite, PreregistrationUser, EmailChangeStatus, UserProfile, Realm]
def get_object_from_key( def get_object_from_key(

View File

@ -190,11 +190,14 @@ def maybe_send_to_registration(
if multiuse_object_key: if multiuse_object_key:
from_multiuse_invite = True from_multiuse_invite = True
try: try:
multiuse_obj = get_object_from_key(multiuse_object_key, [Confirmation.MULTIUSE_INVITE]) confirmation_obj = get_object_from_key(
multiuse_object_key, [Confirmation.MULTIUSE_INVITE]
)
except ConfirmationKeyException as exception: except ConfirmationKeyException as exception:
return render_confirmation_key_error(request, exception) return render_confirmation_key_error(request, exception)
assert multiuse_obj is not None assert isinstance(confirmation_obj, MultiuseInvite)
multiuse_obj = confirmation_obj
if realm != multiuse_obj.realm: if realm != multiuse_obj.realm:
return render(request, "confirmation/link_does_not_exist.html", status=404) return render(request, "confirmation/link_does_not_exist.html", status=404)

View File

@ -330,6 +330,7 @@ def realm_reactivation(request: HttpRequest, confirmation_key: str) -> HttpRespo
realm = get_object_from_key(confirmation_key, [Confirmation.REALM_REACTIVATION]) realm = get_object_from_key(confirmation_key, [Confirmation.REALM_REACTIVATION])
except ConfirmationKeyException: except ConfirmationKeyException:
return render(request, "zerver/realm_reactivation_link_error.html") return render(request, "zerver/realm_reactivation_link_error.html")
assert isinstance(realm, Realm)
do_reactivate_realm(realm) do_reactivate_realm(realm)
context = {"realm": realm} context = {"realm": realm}
return render(request, "zerver/realm_reactivation.html", context) return render(request, "zerver/realm_reactivation.html", context)

View File

@ -137,6 +137,7 @@ def check_prereg_key(request: HttpRequest, confirmation_key: str) -> Preregistra
] ]
prereg_user = get_object_from_key(confirmation_key, confirmation_types, activate_object=False) prereg_user = get_object_from_key(confirmation_key, confirmation_types, activate_object=False)
assert isinstance(prereg_user, PreregistrationUser)
if prereg_user.status == confirmation_settings.STATUS_REVOKED: if prereg_user.status == confirmation_settings.STATUS_REVOKED:
raise ConfirmationKeyException(ConfirmationKeyException.EXPIRED) raise ConfirmationKeyException(ConfirmationKeyException.EXPIRED)
@ -741,9 +742,11 @@ def accounts_home(
def accounts_home_from_multiuse_invite(request: HttpRequest, confirmation_key: str) -> HttpResponse: def accounts_home_from_multiuse_invite(request: HttpRequest, confirmation_key: str) -> HttpResponse:
realm = get_realm_from_request(request) realm = get_realm_from_request(request)
multiuse_object = None multiuse_object: Optional[MultiuseInvite] = None
try: try:
multiuse_object = get_object_from_key(confirmation_key, [Confirmation.MULTIUSE_INVITE]) confirmation_obj = get_object_from_key(confirmation_key, [Confirmation.MULTIUSE_INVITE])
assert isinstance(confirmation_obj, MultiuseInvite)
multiuse_object = confirmation_obj
if realm != multiuse_object.realm: if realm != multiuse_object.realm:
return render(request, "confirmation/link_does_not_exist.html", status=404) return render(request, "confirmation/link_does_not_exist.html", status=404)
# Required for OAuth 2 # Required for OAuth 2

View File

@ -22,6 +22,7 @@ def process_unsubscribe(
except ConfirmationKeyException: except ConfirmationKeyException:
return render(request, "zerver/unsubscribe_link_error.html") return render(request, "zerver/unsubscribe_link_error.html")
assert isinstance(user_profile, UserProfile)
unsubscribe_function(user_profile) unsubscribe_function(user_profile)
context = common_context(user_profile) context = common_context(user_profile)
context.update(subscription_type=subscription_type) context.update(subscription_type=subscription_type)