confirmation: Liberate confirm from ConfirmationManager.

This commit is contained in:
Rishi Gupta 2017-07-07 19:18:58 -07:00 committed by Tim Abbott
parent 3bc74113ad
commit 3b97262647
3 changed files with 22 additions and 22 deletions

View File

@ -25,27 +25,27 @@ def generate_key():
# type: () -> str
return generate_random_token(40)
def get_object_from_key(confirmation_key):
# type: (str) -> Union[bool, PreregistrationUser, EmailChangeStatus]
if B16_RE.search(confirmation_key):
try:
confirmation = Confirmation.objects.get(confirmation_key=confirmation_key)
except Confirmation.DoesNotExist:
return False
time_elapsed = timezone_now() - confirmation.date_sent
if time_elapsed.total_seconds() > settings.EMAIL_CONFIRMATION_DAYS * 24 * 3600:
return False
obj = confirmation.content_object
obj.status = getattr(settings, 'STATUS_ACTIVE', 1)
obj.save(update_fields=['status'])
return obj
return False
class ConfirmationManager(models.Manager):
url_pattern_name = 'confirmation.views.confirm'
def confirm(self, confirmation_key):
# type: (str) -> Union[bool, PreregistrationUser, EmailChangeStatus]
if B16_RE.search(confirmation_key):
try:
confirmation = self.get(confirmation_key=confirmation_key)
except self.model.DoesNotExist:
return False
time_elapsed = timezone_now() - confirmation.date_sent
if time_elapsed.total_seconds() > settings.EMAIL_CONFIRMATION_DAYS * 24 * 3600:
return False
obj = confirmation.content_object
obj.status = getattr(settings, 'STATUS_ACTIVE', 1)
obj.save(update_fields=['status'])
return obj
return False
def get_link_for_object(self, obj, host):
# type: (Union[ContentType, int], str) -> str
key = generate_key()

View File

@ -10,7 +10,7 @@ from django.template import RequestContext
from django.conf import settings
from django.http import HttpRequest, HttpResponse
from confirmation.models import Confirmation
from confirmation.models import Confirmation, get_object_from_key
from zerver.models import PreregistrationUser
from typing import Any, Dict
@ -19,7 +19,7 @@ from typing import Any, Dict
# Do not add other confirmation paths here.
def confirm(request, confirmation_key):
# type: (HttpRequest, str) -> HttpResponse
obj = Confirmation.objects.confirm(confirmation_key)
obj = get_object_from_key(confirmation_key)
if obj:
return render(request, 'confirmation/confirm_preregistrationuser.html',
context={

View File

@ -26,7 +26,7 @@ from zerver.lib.users import check_change_full_name
from zerver.lib.timezone import get_all_timezones
from zerver.models import UserProfile, Realm, name_changes_disabled, \
EmailChangeStatus
from confirmation.models import EmailChangeConfirmation
from confirmation.models import EmailChangeConfirmation, get_object_from_key
@zulip_login_required
def confirm_email_change(request, confirmation_key):
@ -36,7 +36,7 @@ def confirm_email_change(request, confirmation_key):
raise JsonableError(_("Email address changes are disabled in this organization."))
confirmation_key = confirmation_key.lower()
obj = EmailChangeConfirmation.objects.confirm(confirmation_key)
obj = get_object_from_key(confirmation_key)
confirmed = False
new_email = old_email = None # type: Text
if obj: