Move find_my_team functions out of views/auth.py

The general __init__ file is a more natural home, and where other endpoints
(e.g. create_realm, etc) live.

Also changes forms.ValidationError to django.core.exceptions.ValidationError
to match the rest of the file/codebase.
This commit is contained in:
Rishi Gupta
2017-01-05 18:29:07 -08:00
committed by Tim Abbott
parent 0039689acb
commit 757297b72f
3 changed files with 56 additions and 63 deletions

View File

@@ -32,7 +32,7 @@ from zerver.lib.actions import do_change_password, do_change_full_name, do_chang
do_update_pointer, realm_user_count do_update_pointer, realm_user_count
from zerver.lib.push_notifications import num_push_devices_for_user from zerver.lib.push_notifications import num_push_devices_for_user
from zerver.forms import RegistrationForm, HomepageForm, RealmCreationForm, ToSForm, \ from zerver.forms import RegistrationForm, HomepageForm, RealmCreationForm, ToSForm, \
CreateUserForm CreateUserForm, FindMyTeamForm
from zerver.lib.actions import is_inactive from zerver.lib.actions import is_inactive
from django_auth_ldap.backend import LDAPBackend, _LDAPUser from django_auth_ldap.backend import LDAPBackend, _LDAPUser
from zerver.lib.validator import check_string, check_list from zerver.lib.validator import check_string, check_list
@@ -702,3 +702,56 @@ def json_set_muted_topics(request, user_profile,
def generate_204(request): def generate_204(request):
# type: (HttpRequest) -> HttpResponse # type: (HttpRequest) -> HttpResponse
return HttpResponse(content=None, status=204) return HttpResponse(content=None, status=204)
try:
import mailer
send_mail = mailer.send_mail
except ImportError:
# no mailer app present, stick with default
pass
def send_find_my_team_emails(user_profile):
# type: (UserProfile) -> None
text_template = 'zerver/emails/find_team/find_team_email.txt'
html_template = 'zerver/emails/find_team/find_team_email.html'
context = {'user_profile': user_profile}
text_content = loader.render_to_string(text_template, context)
html_content = loader.render_to_string(html_template, context)
sender = settings.NOREPLY_EMAIL_ADDRESS
recipients = [user_profile.email]
subject = loader.render_to_string('zerver/emails/find_team/find_team_email.subject').strip()
send_mail(subject, text_content, sender, recipients, html_message=html_content)
def find_my_team(request):
# type: (HttpRequest) -> HttpResponse
url = reverse('find-my-team')
emails = [] # type: List[Text]
if request.method == 'POST':
form = FindMyTeamForm(request.POST)
if form.is_valid():
emails = form.cleaned_data['emails']
for user_profile in UserProfile.objects.filter(email__in=emails):
send_find_my_team_emails(user_profile)
# Note: Show all the emails in the result otherwise this
# feature can be used to ascertain which email addresses
# are associated with Zulip.
data = urllib.parse.urlencode({'emails': ','.join(emails)})
return redirect(url + "?" + data)
else:
form = FindMyTeamForm()
result = request.GET.get('emails')
if result:
for email in result.split(','):
try:
validators.validate_email(email)
emails.append(email)
except ValidationError:
pass
return render_to_response('zerver/find_my_team.html',
{'form': form, 'current_url': lambda: url,
'emails': emails},
request=request)

View File

@@ -12,16 +12,12 @@ from django.shortcuts import redirect
from django.views.decorators.csrf import csrf_exempt from django.views.decorators.csrf import csrf_exempt
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from django.core import signing from django.core import signing
from django.template import loader
from django.core.validators import validate_email
from django import forms
from django.core.mail import send_mail
from six.moves import urllib from six.moves import urllib
from typing import Any, Dict, Optional, Tuple, Text from typing import Any, Dict, Optional, Tuple, Text
from confirmation.models import Confirmation from confirmation.models import Confirmation
from zerver.forms import HomepageForm, OurAuthenticationForm, \ from zerver.forms import HomepageForm, OurAuthenticationForm, \
WRONG_SUBDOMAIN_ERROR, FindMyTeamForm WRONG_SUBDOMAIN_ERROR
from zerver.lib.request import REQ, has_request_variables, JsonableError from zerver.lib.request import REQ, has_request_variables, JsonableError
from zerver.lib.response import json_success, json_error from zerver.lib.response import json_success, json_error
@@ -31,7 +27,6 @@ from zerver.views import create_preregistration_user, get_realm_from_request, \
redirect_and_log_into_subdomain redirect_and_log_into_subdomain
from zproject.backends import password_auth_enabled, dev_auth_enabled, google_auth_enabled from zproject.backends import password_auth_enabled, dev_auth_enabled, google_auth_enabled
from zproject.jinja2 import render_to_response from zproject.jinja2 import render_to_response
from zerver.lib.notifications import send_future_email
import hashlib import hashlib
import hmac import hmac
@@ -40,61 +35,6 @@ import logging
import requests import requests
import time import time
import ujson import ujson
import datetime
from typing import Text
try:
import mailer
send_mail = mailer.send_mail
except ImportError:
# no mailer app present, stick with default
pass
def send_find_my_team_emails(user_profile):
# type: (UserProfile) -> None
text_template = 'zerver/emails/find_team/find_team_email.txt'
html_template = 'zerver/emails/find_team/find_team_email.html'
context = {'user_profile': user_profile}
text_content = loader.render_to_string(text_template, context)
html_content = loader.render_to_string(html_template, context)
sender = settings.NOREPLY_EMAIL_ADDRESS
recipients = [user_profile.email]
subject = loader.render_to_string('zerver/emails/find_team/find_team_email.subject').strip()
send_mail(subject, text_content, sender, recipients, html_message=html_content)
def find_my_team(request):
# type: (HttpRequest) -> HttpResponse
url = reverse('find-my-team')
emails = [] # type: List[Text]
if request.method == 'POST':
form = FindMyTeamForm(request.POST)
if form.is_valid():
emails = form.cleaned_data['emails']
for user_profile in UserProfile.objects.filter(email__in=emails):
send_find_my_team_emails(user_profile)
# Note: Show all the emails in the result otherwise this
# feature can be used to ascertain which email addresses
# are associated with Zulip.
data = urllib.parse.urlencode({'emails': ','.join(emails)})
return redirect(url + "?" + data)
else:
form = FindMyTeamForm()
result = request.GET.get('emails')
if result:
for email in result.split(','):
try:
validate_email(email)
emails.append(email)
except forms.ValidationError:
pass
return render_to_response('zerver/find_my_team.html',
{'form': form, 'current_url': lambda: url,
'emails': emails},
request=request)
def maybe_send_to_registration(request, email, full_name=''): def maybe_send_to_registration(request, email, full_name=''):
# type: (HttpRequest, Text, Text) -> HttpResponse # type: (HttpRequest, Text, Text) -> HttpResponse

View File

@@ -131,7 +131,7 @@ i18n_urls = [
name='landing-page'), name='landing-page'),
url(r'^new-user/$', RedirectView.as_view(url='/hello', permanent=True)), url(r'^new-user/$', RedirectView.as_view(url='/hello', permanent=True)),
url(r'^features/$', TemplateView.as_view(template_name='zerver/features.html')), url(r'^features/$', TemplateView.as_view(template_name='zerver/features.html')),
url(r'^find-my-team/$', zerver.views.auth.find_my_team, name='find-my-team'), url(r'^find-my-team/$', zerver.views.find_my_team, name='find-my-team'),
] ]
# If a Terms of Service is supplied, add that route # If a Terms of Service is supplied, add that route