Annotate confirmation/models.py.

This commit is contained in:
Umair Khan 2016-09-12 11:05:36 +05:00 committed by Tim Abbott
parent e2f29054a4
commit 3f7f79f4b5
1 changed files with 11 additions and 0 deletions

View File

@ -19,6 +19,9 @@ from django.utils.timezone import now
from confirmation.util import get_status_field
from zerver.lib.utils import generate_random_token
from zerver.models import PreregistrationUser
from typing import Optional, Union, Any
from six import text_type
try:
import mailer
@ -31,6 +34,7 @@ except ImportError:
B16_RE = re.compile('^[a-f0-9]{40}$')
def check_key_is_valid(creation_key):
# type: (text_type) -> bool
if not RealmCreationKey.objects.filter(creation_key=creation_key).exists():
return False
days_sofar = (now() - RealmCreationKey.objects.get(creation_key=creation_key).date_created).days
@ -40,9 +44,11 @@ def check_key_is_valid(creation_key):
return False
def generate_key():
# type: () -> text_type
return generate_random_token(40)
def generate_activation_url(key, host=None):
# type: (text_type, Optional[str]) -> text_type
if host is None:
host = settings.EXTERNAL_HOST
return u'%s%s%s' % (settings.EXTERNAL_URI_SCHEME,
@ -51,6 +57,7 @@ def generate_activation_url(key, host=None):
kwargs={'confirmation_key': key}))
def generate_realm_creation_url():
# type: () -> text_type
key = generate_key()
RealmCreationKey.objects.create(creation_key=key, date_created=now())
return u'%s%s%s' % (settings.EXTERNAL_URI_SCHEME,
@ -61,6 +68,7 @@ def generate_realm_creation_url():
class ConfirmationManager(models.Manager):
def confirm(self, confirmation_key):
# type: (str) -> Union[bool, PreregistrationUser]
if B16_RE.search(confirmation_key):
try:
confirmation = self.get(confirmation_key=confirmation_key)
@ -74,6 +82,7 @@ class ConfirmationManager(models.Manager):
return False
def get_link_for_object(self, obj, host=None):
# type: (Union[ContentType, int], Optional[str]) -> text_type
key = generate_key()
self.create(content_object=obj, date_sent=now(), confirmation_key=key)
return generate_activation_url(key, host=host)
@ -81,6 +90,7 @@ class ConfirmationManager(models.Manager):
def send_confirmation(self, obj, email_address, additional_context=None,
subject_template_path=None, body_template_path=None,
host=None):
# type: (ContentType, text_type, Optional[Dict[str, Any]], Optional[str], Optional[str], Optional[str]) -> Confirmation
confirmation_key = generate_key()
current_site = Site.objects.get_current()
activate_url = generate_activation_url(confirmation_key, host=host)
@ -133,6 +143,7 @@ class Confirmation(models.Model):
verbose_name_plural = _('confirmation emails')
def __unicode__(self):
# type: () -> text_type
return _('confirmation email for %s') % (self.content_object,)
class RealmCreationKey(models.Model):