2016-06-23 11:32:45 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2013-04-23 18:51:17 +02:00
|
|
|
from __future__ import absolute_import
|
2016-10-12 05:18:50 +02:00
|
|
|
from typing import Any, List, Dict, Optional, Tuple, Iterable, Sequence
|
2013-04-23 18:51:17 +02:00
|
|
|
|
2016-06-23 11:32:45 +02:00
|
|
|
from django.utils import translation
|
2016-05-25 15:02:02 +02:00
|
|
|
from django.utils.translation import ugettext as _
|
2012-09-19 19:39:34 +02:00
|
|
|
from django.conf import settings
|
2013-11-21 01:30:20 +01:00
|
|
|
from django.contrib.auth import authenticate, login, get_backends
|
2012-08-28 18:44:51 +02:00
|
|
|
from django.core.urlresolvers import reverse
|
2016-06-05 04:58:33 +02:00
|
|
|
from django.http import HttpResponseRedirect, HttpResponseForbidden, HttpResponse, HttpRequest
|
2016-04-21 08:48:33 +02:00
|
|
|
from django.shortcuts import redirect
|
2013-01-08 23:26:40 +01:00
|
|
|
from django.template import RequestContext, loader
|
2013-11-04 17:22:58 +01:00
|
|
|
from django.utils.timezone import now
|
2013-08-16 22:55:50 +02:00
|
|
|
from django.utils.cache import patch_cache_control
|
2012-09-29 00:49:34 +02:00
|
|
|
from django.core.exceptions import ValidationError
|
2012-12-11 23:42:32 +01:00
|
|
|
from django.core import validators
|
2015-08-21 01:59:39 +02:00
|
|
|
from django.core.mail import send_mail
|
2015-08-18 20:32:56 +02:00
|
|
|
from zerver.models import Message, UserProfile, Stream, Subscription, Huddle, \
|
|
|
|
Recipient, Realm, UserMessage, DefaultStream, RealmEmoji, RealmAlias, \
|
2015-11-23 14:35:16 +01:00
|
|
|
RealmFilter, \
|
2016-07-27 02:56:36 +02:00
|
|
|
PreregistrationUser, get_client, UserActivity, \
|
2016-10-06 09:18:51 +02:00
|
|
|
get_stream, UserPresence, get_recipient, name_changes_disabled, \
|
2013-12-12 18:36:32 +01:00
|
|
|
split_email_to_domain, resolve_email_to_domain, email_to_username, get_realm, \
|
2016-10-12 04:50:38 +02:00
|
|
|
completely_open, get_unique_open_realm, email_allowed_for_realm, \
|
2016-07-19 14:35:08 +02:00
|
|
|
get_cross_realm_users, resolve_subdomain_to_realm
|
2015-11-23 17:09:21 +01:00
|
|
|
from zerver.lib.actions import do_change_password, do_change_full_name, do_change_is_admin, \
|
2016-06-03 01:02:58 +02:00
|
|
|
do_activate_user, do_create_user, do_create_realm, set_default_streams, \
|
2016-10-12 05:13:32 +02:00
|
|
|
update_user_presence, do_events_register, \
|
2016-10-12 05:18:50 +02:00
|
|
|
do_change_tos_version, \
|
2016-10-12 05:15:02 +02:00
|
|
|
user_email_is_unique, \
|
2016-10-12 05:18:50 +02:00
|
|
|
compute_mit_user_fullname, do_set_muted_topics, \
|
2016-06-21 21:34:41 +02:00
|
|
|
do_update_pointer, realm_user_count
|
2013-10-16 17:24:52 +02:00
|
|
|
from zerver.lib.push_notifications import num_push_devices_for_user
|
2016-06-03 01:02:58 +02:00
|
|
|
from zerver.forms import RegistrationForm, HomepageForm, RealmCreationForm, ToSForm, \
|
2016-10-12 04:50:38 +02:00
|
|
|
CreateUserForm
|
2016-07-22 13:44:53 +02:00
|
|
|
from zerver.lib.actions import is_inactive
|
2013-11-21 01:30:20 +01:00
|
|
|
from django_auth_ldap.backend import LDAPBackend, _LDAPUser
|
2015-11-23 17:29:37 +01:00
|
|
|
from zerver.lib.validator import check_string, check_list, check_bool
|
2015-11-23 14:35:16 +01:00
|
|
|
from zerver.decorator import require_post, authenticated_json_post_view, \
|
2016-09-14 02:20:36 +02:00
|
|
|
has_request_variables, \
|
2016-07-26 23:16:20 +02:00
|
|
|
JsonableError, get_user_profile_by_email, REQ, \
|
2016-04-21 23:48:34 +02:00
|
|
|
zulip_login_required
|
2015-11-24 05:26:33 +01:00
|
|
|
from zerver.lib.avatar import avatar_url
|
2016-08-02 14:34:12 +02:00
|
|
|
from zerver.lib.i18n import get_language_list, get_language_name, \
|
|
|
|
get_language_list_for_templates
|
2015-11-23 14:35:16 +01:00
|
|
|
from zerver.lib.response import json_success, json_error
|
2016-07-19 14:35:08 +02:00
|
|
|
from zerver.lib.utils import statsd, get_subdomain
|
2016-09-08 07:35:50 +02:00
|
|
|
from version import ZULIP_VERSION
|
2016-10-12 04:50:38 +02:00
|
|
|
from zproject.backends import password_auth_enabled
|
2013-01-23 23:24:44 +01:00
|
|
|
|
2016-06-22 21:16:02 +02:00
|
|
|
from confirmation.models import Confirmation, RealmCreationKey, check_key_is_valid
|
2012-09-28 22:47:05 +02:00
|
|
|
|
2015-01-29 08:59:41 +01:00
|
|
|
import requests
|
|
|
|
|
2013-08-09 20:26:35 +02:00
|
|
|
import calendar
|
2012-08-28 18:44:51 +02:00
|
|
|
import datetime
|
|
|
|
import simplejson
|
2012-09-07 19:20:04 +02:00
|
|
|
import re
|
2016-06-05 04:58:33 +02:00
|
|
|
from six import text_type
|
2016-08-02 14:34:12 +02:00
|
|
|
from six.moves import urllib, zip_longest, zip, range
|
2013-05-06 17:14:59 +02:00
|
|
|
import time
|
2013-05-20 19:09:18 +02:00
|
|
|
import logging
|
2012-10-16 21:15:01 +02:00
|
|
|
|
2016-04-21 08:48:33 +02:00
|
|
|
from zproject.jinja2 import render_to_response
|
2013-10-17 19:21:18 +02:00
|
|
|
|
2012-09-28 22:47:05 +02:00
|
|
|
@require_post
|
2012-10-16 21:42:40 +02:00
|
|
|
def accounts_register(request):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (HttpRequest) -> HttpResponse
|
2012-09-28 22:47:05 +02:00
|
|
|
key = request.POST['key']
|
2012-10-29 19:08:18 +01:00
|
|
|
confirmation = Confirmation.objects.get(confirmation_key=key)
|
2012-12-11 23:42:32 +01:00
|
|
|
prereg_user = confirmation.content_object
|
|
|
|
email = prereg_user.email
|
2016-06-03 01:02:58 +02:00
|
|
|
realm_creation = prereg_user.realm_creation
|
2014-01-07 19:13:45 +01:00
|
|
|
try:
|
|
|
|
existing_user_profile = get_user_profile_by_email(email)
|
|
|
|
except UserProfile.DoesNotExist:
|
|
|
|
existing_user_profile = None
|
2012-10-29 19:08:18 +01:00
|
|
|
|
2013-07-18 18:48:56 +02:00
|
|
|
validators.validate_email(email)
|
2016-06-03 01:02:58 +02:00
|
|
|
# If OPEN_REALM_CREATION is enabled all user sign ups should go through the
|
|
|
|
# special URL with domain name so that REALM can be identified if multiple realms exist
|
2015-12-28 21:37:24 +01:00
|
|
|
unique_open_realm = get_unique_open_realm()
|
2016-06-03 01:02:58 +02:00
|
|
|
if unique_open_realm is not None:
|
2015-12-28 21:37:24 +01:00
|
|
|
realm = unique_open_realm
|
|
|
|
domain = realm.domain
|
2016-07-27 02:56:36 +02:00
|
|
|
elif prereg_user.referred_by:
|
2015-12-28 21:37:24 +01:00
|
|
|
# If someone invited you, you are joining their realm regardless
|
|
|
|
# of your e-mail address.
|
2015-08-20 02:38:32 +02:00
|
|
|
realm = prereg_user.referred_by.realm
|
|
|
|
domain = realm.domain
|
2016-01-12 16:24:34 +01:00
|
|
|
if not email_allowed_for_realm(email, realm):
|
2015-08-20 02:38:32 +02:00
|
|
|
return render_to_response("zerver/closed_realm.html", {"closed_domain_name": realm.name})
|
2016-07-27 02:56:36 +02:00
|
|
|
elif prereg_user.realm:
|
2013-08-02 20:32:56 +02:00
|
|
|
# You have a realm set, even though nobody referred you. This
|
|
|
|
# happens if you sign up through a special URL for an open
|
|
|
|
# realm.
|
|
|
|
domain = prereg_user.realm.domain
|
2015-12-28 21:37:24 +01:00
|
|
|
realm = get_realm(domain)
|
2013-02-06 22:26:04 +01:00
|
|
|
else:
|
2013-11-22 23:48:00 +01:00
|
|
|
domain = resolve_email_to_domain(email)
|
2015-12-28 21:37:24 +01:00
|
|
|
realm = get_realm(domain)
|
2012-09-25 22:58:59 +02:00
|
|
|
|
2014-01-07 19:04:26 +01:00
|
|
|
if realm and realm.deactivated:
|
|
|
|
# The user is trying to register for a deactivated realm. Advise them to
|
|
|
|
# contact support.
|
|
|
|
return render_to_response("zerver/deactivated.html",
|
2015-09-30 02:58:50 +02:00
|
|
|
{"deactivated_domain_name": realm.name,
|
|
|
|
"zulip_administrator": settings.ZULIP_ADMINISTRATOR})
|
2014-01-07 19:04:26 +01:00
|
|
|
|
2012-09-29 00:49:34 +02:00
|
|
|
try:
|
2014-01-07 19:13:45 +01:00
|
|
|
if existing_user_profile is not None and existing_user_profile.is_mirror_dummy:
|
|
|
|
# Mirror dummy users to be activated must be inactive
|
2013-02-12 20:52:42 +01:00
|
|
|
is_inactive(email)
|
2012-10-29 19:08:18 +01:00
|
|
|
else:
|
|
|
|
# Other users should not already exist at all.
|
2013-07-08 17:57:04 +02:00
|
|
|
user_email_is_unique(email)
|
2012-09-29 00:49:34 +02:00
|
|
|
except ValidationError:
|
2016-07-08 18:57:01 +02:00
|
|
|
return HttpResponseRedirect(reverse('django.contrib.auth.views.login') + '?email=' +
|
|
|
|
urllib.parse.quote_plus(email))
|
2012-09-29 00:49:34 +02:00
|
|
|
|
2013-11-21 04:57:23 +01:00
|
|
|
name_validated = False
|
|
|
|
full_name = None
|
|
|
|
|
2012-10-02 22:20:07 +02:00
|
|
|
if request.POST.get('from_confirmation'):
|
2013-11-21 04:57:23 +01:00
|
|
|
try:
|
|
|
|
del request.session['authenticated_full_name']
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2016-07-27 01:45:29 +02:00
|
|
|
if realm is not None and realm.is_zephyr_mirror_realm and domain == "mit.edu":
|
|
|
|
# for MIT users, we can get an authoritative name from Hesiod
|
2013-08-15 18:43:42 +02:00
|
|
|
hesiod_name = compute_mit_user_fullname(email)
|
|
|
|
form = RegistrationForm(
|
|
|
|
initial={'full_name': hesiod_name if "@" not in hesiod_name else ""})
|
2013-11-21 04:57:23 +01:00
|
|
|
name_validated = True
|
2013-11-21 01:30:20 +01:00
|
|
|
elif settings.POPULATE_PROFILE_VIA_LDAP:
|
|
|
|
for backend in get_backends():
|
|
|
|
if isinstance(backend, LDAPBackend):
|
|
|
|
ldap_attrs = _LDAPUser(backend, backend.django_to_ldap_username(email)).attrs
|
2013-11-21 04:57:23 +01:00
|
|
|
try:
|
2016-07-17 19:40:12 +02:00
|
|
|
ldap_full_name = ldap_attrs[settings.AUTH_LDAP_USER_ATTR_MAP['full_name']][0]
|
|
|
|
request.session['authenticated_full_name'] = ldap_full_name
|
2013-11-21 04:57:23 +01:00
|
|
|
name_validated = True
|
|
|
|
# We don't use initial= here, because if the form is
|
|
|
|
# complete (that is, no additional fields need to be
|
|
|
|
# filled out by the user) we want the form to validate,
|
|
|
|
# so they can be directly registered without having to
|
|
|
|
# go through this interstitial.
|
2016-07-17 19:40:12 +02:00
|
|
|
form = RegistrationForm({'full_name': ldap_full_name})
|
2013-11-21 04:57:23 +01:00
|
|
|
# FIXME: This will result in the user getting
|
|
|
|
# validation errors if they have to enter a password.
|
|
|
|
# Not relevant for ONLY_SSO, though.
|
|
|
|
break
|
|
|
|
except TypeError:
|
|
|
|
# Let the user fill out a name and/or try another backend
|
|
|
|
form = RegistrationForm()
|
2015-01-29 09:00:23 +01:00
|
|
|
elif 'full_name' in request.POST:
|
|
|
|
form = RegistrationForm(
|
|
|
|
initial={'full_name': request.POST.get('full_name')}
|
|
|
|
)
|
2013-08-15 18:43:42 +02:00
|
|
|
else:
|
|
|
|
form = RegistrationForm()
|
2012-10-02 22:20:07 +02:00
|
|
|
else:
|
2013-11-21 04:57:23 +01:00
|
|
|
postdata = request.POST.copy()
|
2014-03-27 23:50:37 +01:00
|
|
|
if name_changes_disabled(realm):
|
2013-11-21 04:57:23 +01:00
|
|
|
# If we populate profile information via LDAP and we have a
|
|
|
|
# verified name from you on file, use that. Otherwise, fall
|
|
|
|
# back to the full name in the request.
|
|
|
|
try:
|
|
|
|
postdata.update({'full_name': request.session['authenticated_full_name']})
|
|
|
|
name_validated = True
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
form = RegistrationForm(postdata)
|
2014-03-28 00:45:03 +01:00
|
|
|
if not password_auth_enabled(realm):
|
|
|
|
form['password'].field.required = False
|
2013-11-21 04:57:23 +01:00
|
|
|
|
|
|
|
if form.is_valid():
|
2014-03-28 00:45:03 +01:00
|
|
|
if password_auth_enabled(realm):
|
2013-11-21 04:57:23 +01:00
|
|
|
password = form.cleaned_data['password']
|
|
|
|
else:
|
|
|
|
# SSO users don't need no passwords
|
|
|
|
password = None
|
|
|
|
|
2016-06-03 01:02:58 +02:00
|
|
|
if realm_creation:
|
|
|
|
domain = split_email_to_domain(email)
|
2016-09-16 19:05:14 +02:00
|
|
|
realm_name = form.cleaned_data['realm_name']
|
|
|
|
org_type = int(form.cleaned_data['realm_org_type'])
|
2016-07-19 14:35:08 +02:00
|
|
|
if settings.REALMS_HAVE_SUBDOMAINS:
|
2016-09-16 19:05:14 +02:00
|
|
|
realm = do_create_realm(domain, realm_name, org_type=org_type,
|
2016-07-19 14:35:08 +02:00
|
|
|
subdomain=form.cleaned_data['realm_subdomain'])[0]
|
|
|
|
else:
|
2016-09-16 19:05:14 +02:00
|
|
|
realm = do_create_realm(domain, realm_name, org_type=org_type)[0]
|
2016-07-19 14:35:08 +02:00
|
|
|
|
2016-06-03 01:02:58 +02:00
|
|
|
set_default_streams(realm, settings.DEFAULT_NEW_REALM_STREAMS)
|
|
|
|
|
2013-11-21 04:57:23 +01:00
|
|
|
full_name = form.cleaned_data['full_name']
|
|
|
|
short_name = email_to_username(email)
|
|
|
|
first_in_realm = len(UserProfile.objects.filter(realm=realm, is_bot=False)) == 0
|
|
|
|
|
|
|
|
# FIXME: sanitize email addresses and fullname
|
2014-01-07 19:13:45 +01:00
|
|
|
if existing_user_profile is not None and existing_user_profile.is_mirror_dummy:
|
2013-11-21 04:57:23 +01:00
|
|
|
try:
|
2014-01-07 19:13:45 +01:00
|
|
|
user_profile = existing_user_profile
|
2013-12-09 22:37:04 +01:00
|
|
|
do_activate_user(user_profile)
|
|
|
|
do_change_password(user_profile, password)
|
|
|
|
do_change_full_name(user_profile, full_name)
|
2013-11-21 04:57:23 +01:00
|
|
|
except UserProfile.DoesNotExist:
|
2015-10-13 21:33:54 +02:00
|
|
|
user_profile = do_create_user(email, password, realm, full_name, short_name,
|
|
|
|
prereg_user=prereg_user,
|
2016-08-14 03:44:07 +02:00
|
|
|
tos_version=settings.TOS_VERSION,
|
2015-10-13 21:33:54 +02:00
|
|
|
newsletter_data={"IP": request.META['REMOTE_ADDR']})
|
2013-11-21 04:57:23 +01:00
|
|
|
else:
|
2015-10-13 21:33:54 +02:00
|
|
|
user_profile = do_create_user(email, password, realm, full_name, short_name,
|
|
|
|
prereg_user=prereg_user,
|
2016-08-14 03:44:07 +02:00
|
|
|
tos_version=settings.TOS_VERSION,
|
2015-10-13 21:33:54 +02:00
|
|
|
newsletter_data={"IP": request.META['REMOTE_ADDR']})
|
2012-11-17 03:57:46 +01:00
|
|
|
|
2013-11-21 04:57:23 +01:00
|
|
|
# This logs you in using the ZulipDummyBackend, since honestly nothing
|
|
|
|
# more fancy than this is required.
|
2016-08-14 04:44:55 +02:00
|
|
|
return_data = {} # type: Dict[str, bool]
|
|
|
|
auth_result = authenticate(username=user_profile.email,
|
|
|
|
realm_subdomain=realm.subdomain,
|
|
|
|
return_data=return_data,
|
|
|
|
use_dummy_backend=True)
|
|
|
|
if return_data.get('invalid_subdomain'):
|
|
|
|
# By construction, this should never happen.
|
|
|
|
logging.error("Subdomain mismatch in registration %s: %s" % (
|
|
|
|
realm.subdomain, user_profile.email,))
|
|
|
|
return redirect('/')
|
|
|
|
login(request, auth_result)
|
2013-07-11 21:40:52 +02:00
|
|
|
|
2013-11-21 04:57:23 +01:00
|
|
|
if first_in_realm:
|
2014-01-22 20:04:34 +01:00
|
|
|
do_change_is_admin(user_profile, True)
|
2016-10-06 01:23:27 +02:00
|
|
|
return HttpResponseRedirect(realm.uri + reverse('zerver.views.home'))
|
2012-08-28 18:44:51 +02:00
|
|
|
|
2013-07-29 23:03:31 +02:00
|
|
|
return render_to_response('zerver/register.html',
|
2013-04-23 23:46:12 +02:00
|
|
|
{'form': form,
|
|
|
|
'company_name': domain,
|
|
|
|
'email': email,
|
|
|
|
'key': key,
|
2013-11-21 04:57:23 +01:00
|
|
|
'full_name': request.session.get('authenticated_full_name', None),
|
2014-03-28 00:45:03 +01:00
|
|
|
'lock_name': name_validated and name_changes_disabled(realm),
|
|
|
|
# password_auth_enabled is normally set via our context processor,
|
|
|
|
# but for the registration form, there is no logged in user yet, so
|
|
|
|
# we have to set it here.
|
2016-06-03 01:02:58 +02:00
|
|
|
'creating_new_team': realm_creation,
|
2016-07-19 14:35:08 +02:00
|
|
|
'realms_have_subdomains': settings.REALMS_HAVE_SUBDOMAINS,
|
2014-03-28 00:45:03 +01:00
|
|
|
'password_auth_enabled': password_auth_enabled(realm),
|
2013-04-23 23:46:12 +02:00
|
|
|
},
|
2016-04-21 08:48:33 +02:00
|
|
|
request=request)
|
2012-08-28 18:44:51 +02:00
|
|
|
|
2016-04-21 23:48:34 +02:00
|
|
|
@zulip_login_required
|
2013-01-08 23:26:40 +01:00
|
|
|
def accounts_accept_terms(request):
|
2016-07-30 05:20:05 +02:00
|
|
|
# type: (HttpRequest) -> HttpResponse
|
2013-01-08 23:26:40 +01:00
|
|
|
if request.method == "POST":
|
|
|
|
form = ToSForm(request.POST)
|
|
|
|
if form.is_valid():
|
2016-08-10 03:05:26 +02:00
|
|
|
do_change_tos_version(request.user, settings.TOS_VERSION)
|
2013-01-08 23:26:40 +01:00
|
|
|
return redirect(home)
|
|
|
|
else:
|
|
|
|
form = ToSForm()
|
2016-08-10 03:05:26 +02:00
|
|
|
|
|
|
|
email = request.user.email
|
|
|
|
domain = resolve_email_to_domain(email)
|
|
|
|
special_message_template = None
|
|
|
|
if request.user.tos_version is None and settings.FIRST_TIME_TOS_TEMPLATE is not None:
|
|
|
|
special_message_template = 'zerver/' + settings.FIRST_TIME_TOS_TEMPLATE
|
2013-07-29 23:03:31 +02:00
|
|
|
return render_to_response('zerver/accounts_accept_terms.html',
|
2016-08-10 03:05:26 +02:00
|
|
|
{ 'form': form, 'company_name': domain, 'email': email, \
|
|
|
|
'special_message_template' : special_message_template },
|
2016-04-21 08:48:33 +02:00
|
|
|
request=request)
|
2013-01-08 23:26:40 +01:00
|
|
|
|
2013-08-07 17:59:45 +02:00
|
|
|
def create_homepage_form(request, user_info=None):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (HttpRequest, Optional[Dict[str, Any]]) -> HomepageForm
|
2013-08-07 17:59:45 +02:00
|
|
|
if user_info:
|
2016-07-19 14:35:08 +02:00
|
|
|
return HomepageForm(user_info, domain=request.session.get("domain"),
|
|
|
|
subdomain=get_subdomain(request))
|
2013-08-07 17:59:45 +02:00
|
|
|
# An empty fields dict is not treated the same way as not
|
|
|
|
# providing it.
|
2016-07-19 14:35:08 +02:00
|
|
|
return HomepageForm(domain=request.session.get("domain"), subdomain=get_subdomain(request))
|
2013-08-07 17:59:45 +02:00
|
|
|
|
2016-06-03 01:02:58 +02:00
|
|
|
def create_preregistration_user(email, request, realm_creation=False):
|
|
|
|
# type: (text_type, HttpRequest, bool) -> HttpResponse
|
2013-08-02 20:32:56 +02:00
|
|
|
domain = request.session.get("domain")
|
2013-12-09 19:40:48 +01:00
|
|
|
if completely_open(domain):
|
|
|
|
# Clear the "domain" from the session object; it's no longer needed
|
|
|
|
request.session["domain"] = None
|
|
|
|
|
|
|
|
# The user is trying to sign up for a completely open realm,
|
|
|
|
# so create them a PreregistrationUser for that realm
|
|
|
|
return PreregistrationUser.objects.create(email=email,
|
2016-06-03 01:02:58 +02:00
|
|
|
realm=get_realm(domain),
|
|
|
|
realm_creation=realm_creation)
|
2013-12-09 19:40:48 +01:00
|
|
|
|
2016-06-03 01:02:58 +02:00
|
|
|
return PreregistrationUser.objects.create(email=email, realm_creation=realm_creation)
|
2013-08-02 20:32:56 +02:00
|
|
|
|
|
|
|
def accounts_home_with_domain(request, domain):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (HttpRequest, str) -> HttpResponse
|
2013-08-02 20:32:56 +02:00
|
|
|
if completely_open(domain):
|
|
|
|
# You can sign up for a completely open realm through a
|
|
|
|
# special registration path that contains the domain in the
|
|
|
|
# URL. We store this information in the session rather than
|
|
|
|
# elsewhere because we don't have control over URL or form
|
|
|
|
# data for folks registering through OpenID.
|
|
|
|
request.session["domain"] = domain
|
|
|
|
return accounts_home(request)
|
|
|
|
else:
|
2013-07-29 23:03:31 +02:00
|
|
|
return HttpResponseRedirect(reverse('zerver.views.accounts_home'))
|
2013-08-02 20:32:56 +02:00
|
|
|
|
2016-06-03 01:02:58 +02:00
|
|
|
def send_registration_completion_email(email, request, realm_creation=False):
|
2016-06-25 19:04:36 +02:00
|
|
|
# type: (str, HttpRequest, bool) -> Confirmation
|
2014-01-31 20:46:45 +01:00
|
|
|
"""
|
|
|
|
Send an email with a confirmation link to the provided e-mail so the user
|
|
|
|
can complete their registration.
|
|
|
|
"""
|
2016-06-03 01:02:58 +02:00
|
|
|
prereg_user = create_preregistration_user(email, request, realm_creation)
|
2014-01-31 20:46:45 +01:00
|
|
|
context = {'support_email': settings.ZULIP_ADMINISTRATOR,
|
2016-07-19 07:31:59 +02:00
|
|
|
'verbose_support_offers': settings.VERBOSE_SUPPORT_OFFERS}
|
2016-06-23 21:53:46 +02:00
|
|
|
return Confirmation.objects.send_confirmation(prereg_user, email,
|
2016-07-19 14:35:08 +02:00
|
|
|
additional_context=context,
|
|
|
|
host=request.get_host())
|
2014-01-31 20:46:45 +01:00
|
|
|
|
2016-07-17 19:40:12 +02:00
|
|
|
def redirect_to_email_login_url(email):
|
2016-08-08 13:41:50 +02:00
|
|
|
# type: (str) -> HttpResponseRedirect
|
2016-07-17 19:40:12 +02:00
|
|
|
login_url = reverse('django.contrib.auth.views.login')
|
|
|
|
redirect_url = login_url + '?email=' + urllib.parse.quote_plus(email)
|
|
|
|
return HttpResponseRedirect(redirect_url)
|
|
|
|
|
2016-06-03 01:02:58 +02:00
|
|
|
"""
|
|
|
|
When settings.OPEN_REALM_CREATION is enabled public users can create new realm. For creating the realm the user should
|
|
|
|
not be the member of any current realm. The realm is created with domain same as the that of the user's email.
|
|
|
|
When there is no unique_open_realm user registrations are made by visiting /register/domain_of_the_realm.
|
|
|
|
"""
|
2016-06-22 21:16:02 +02:00
|
|
|
def create_realm(request, creation_key=None):
|
|
|
|
# type: (HttpRequest, Optional[text_type]) -> HttpResponse
|
2016-06-03 01:02:58 +02:00
|
|
|
if not settings.OPEN_REALM_CREATION:
|
2016-06-22 21:16:02 +02:00
|
|
|
if creation_key is None:
|
2016-07-08 18:57:01 +02:00
|
|
|
return render_to_response("zerver/realm_creation_failed.html",
|
|
|
|
{'message': _('New organization creation disabled.')})
|
2016-06-22 21:16:02 +02:00
|
|
|
elif not check_key_is_valid(creation_key):
|
2016-07-17 19:40:12 +02:00
|
|
|
return render_to_response("zerver/realm_creation_failed.html",
|
|
|
|
{'message': _('The organization creation link has been expired'
|
|
|
|
' or is not valid.')})
|
2016-06-03 01:02:58 +02:00
|
|
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
form = RealmCreationForm(request.POST, domain=request.session.get("domain"))
|
|
|
|
if form.is_valid():
|
|
|
|
email = form.cleaned_data['email']
|
2016-06-25 19:04:36 +02:00
|
|
|
confirmation_key = send_registration_completion_email(email, request, realm_creation=True).confirmation_key
|
|
|
|
if settings.DEVELOPMENT:
|
|
|
|
request.session['confirmation_key'] = {'confirmation_key': confirmation_key}
|
2016-06-22 21:16:02 +02:00
|
|
|
if (creation_key is not None and check_key_is_valid(creation_key)):
|
|
|
|
RealmCreationKey.objects.get(creation_key=creation_key).delete()
|
2016-06-03 01:02:58 +02:00
|
|
|
return HttpResponseRedirect(reverse('send_confirm', kwargs={'email': email}))
|
|
|
|
try:
|
|
|
|
email = request.POST['email']
|
|
|
|
user_email_is_unique(email)
|
|
|
|
except ValidationError:
|
|
|
|
# if the user user is already registered he can't create a new realm as a realm
|
|
|
|
# with the same domain as user's email already exists
|
2016-07-17 19:40:12 +02:00
|
|
|
return redirect_to_email_login_url(email)
|
2016-06-03 01:02:58 +02:00
|
|
|
else:
|
|
|
|
form = RealmCreationForm(domain=request.session.get("domain"))
|
|
|
|
return render_to_response('zerver/create_realm.html',
|
|
|
|
{'form': form, 'current_url': request.get_full_path},
|
|
|
|
request=request)
|
|
|
|
|
2016-06-25 19:04:36 +02:00
|
|
|
def confirmation_key(request):
|
2016-08-08 13:41:50 +02:00
|
|
|
# type: (HttpRequest) -> HttpResponse
|
2016-06-25 19:04:36 +02:00
|
|
|
return json_success(request.session.get('confirmation_key'))
|
2016-06-03 01:02:58 +02:00
|
|
|
|
2012-08-28 18:44:51 +02:00
|
|
|
def accounts_home(request):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (HttpRequest) -> HttpResponse
|
2012-09-28 22:47:05 +02:00
|
|
|
if request.method == 'POST':
|
2013-08-07 17:59:45 +02:00
|
|
|
form = create_homepage_form(request, user_info=request.POST)
|
2012-09-28 22:47:05 +02:00
|
|
|
if form.is_valid():
|
2013-01-08 20:24:47 +01:00
|
|
|
email = form.cleaned_data['email']
|
2014-01-31 20:46:45 +01:00
|
|
|
send_registration_completion_email(email, request)
|
2013-03-28 19:21:29 +01:00
|
|
|
return HttpResponseRedirect(reverse('send_confirm', kwargs={'email': email}))
|
2012-09-29 00:49:34 +02:00
|
|
|
try:
|
|
|
|
email = request.POST['email']
|
2013-02-11 19:37:31 +01:00
|
|
|
# Note: We don't check for uniqueness
|
2013-02-12 20:52:42 +01:00
|
|
|
is_inactive(email)
|
2012-09-29 00:49:34 +02:00
|
|
|
except ValidationError:
|
2016-07-17 19:40:12 +02:00
|
|
|
return redirect_to_email_login_url(email)
|
2012-11-21 21:14:55 +01:00
|
|
|
else:
|
2013-08-07 17:59:45 +02:00
|
|
|
form = create_homepage_form(request)
|
2013-07-29 23:03:31 +02:00
|
|
|
return render_to_response('zerver/accounts_home.html',
|
2013-08-02 20:32:56 +02:00
|
|
|
{'form': form, 'current_url': request.get_full_path},
|
2016-04-21 08:48:33 +02:00
|
|
|
request=request)
|
2012-09-04 23:21:30 +02:00
|
|
|
|
2013-09-04 21:02:11 +02:00
|
|
|
def approximate_unread_count(user_profile):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (UserProfile) -> int
|
2013-09-10 10:50:07 +02:00
|
|
|
not_in_home_view_recipients = [sub.recipient.id for sub in \
|
|
|
|
Subscription.objects.filter(
|
|
|
|
user_profile=user_profile, in_home_view=False)]
|
|
|
|
|
2016-07-16 15:09:10 +02:00
|
|
|
# TODO: We may want to exclude muted messages from this count.
|
|
|
|
# It was attempted in the past, but the original attempt
|
|
|
|
# was broken. When we re-architect muting, we may
|
|
|
|
# want to to revisit this (see git issue #1019).
|
2013-09-10 10:50:07 +02:00
|
|
|
return UserMessage.objects.filter(
|
|
|
|
user_profile=user_profile, message_id__gt=user_profile.pointer).exclude(
|
|
|
|
message__recipient__type=Recipient.STREAM,
|
2013-11-21 17:26:17 +01:00
|
|
|
message__recipient__id__in=not_in_home_view_recipients).exclude(
|
|
|
|
flags=UserMessage.flags.read).count()
|
2013-08-09 20:26:35 +02:00
|
|
|
|
|
|
|
def sent_time_in_epoch_seconds(user_message):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (UserMessage) -> float
|
2013-08-09 20:26:35 +02:00
|
|
|
# user_message is a UserMessage object.
|
|
|
|
if not user_message:
|
|
|
|
return None
|
|
|
|
# We have USE_TZ = True, so our datetime objects are timezone-aware.
|
|
|
|
# Return the epoch seconds in UTC.
|
|
|
|
return calendar.timegm(user_message.message.pub_date.utctimetuple())
|
|
|
|
|
2016-04-21 23:48:34 +02:00
|
|
|
@zulip_login_required
|
2012-08-28 18:44:51 +02:00
|
|
|
def home(request):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (HttpRequest) -> HttpResponse
|
2012-11-21 00:42:16 +01:00
|
|
|
# We need to modify the session object every two weeks or it will expire.
|
|
|
|
# This line makes reloading the page a sufficient action to keep the
|
|
|
|
# session alive.
|
|
|
|
request.session.modified = True
|
|
|
|
|
2013-03-29 17:39:53 +01:00
|
|
|
user_profile = request.user
|
2013-09-30 22:34:08 +02:00
|
|
|
request._email = request.user.email
|
2013-10-18 21:19:34 +02:00
|
|
|
request.client = get_client("website")
|
2012-08-28 18:44:51 +02:00
|
|
|
|
2016-08-10 03:05:26 +02:00
|
|
|
# If a user hasn't signed the current Terms of Service, send them there
|
|
|
|
if settings.TERMS_OF_SERVICE is not None and settings.TOS_VERSION is not None and \
|
|
|
|
int(settings.TOS_VERSION.split('.')[0]) > user_profile.major_tos_version():
|
|
|
|
return accounts_accept_terms(request)
|
|
|
|
|
2016-06-13 10:14:33 +02:00
|
|
|
narrow = [] # type: List[List[text_type]]
|
2013-12-13 20:20:28 +01:00
|
|
|
narrow_stream = None
|
2014-01-17 00:05:35 +01:00
|
|
|
narrow_topic = request.GET.get("topic")
|
2014-01-17 00:06:30 +01:00
|
|
|
if request.GET.get("stream"):
|
2013-12-13 20:20:28 +01:00
|
|
|
try:
|
2014-01-17 00:06:30 +01:00
|
|
|
narrow_stream = get_stream(request.GET.get("stream"), user_profile.realm)
|
2013-12-13 20:20:28 +01:00
|
|
|
assert(narrow_stream is not None)
|
|
|
|
assert(narrow_stream.is_public())
|
|
|
|
narrow = [["stream", narrow_stream.name]]
|
|
|
|
except Exception:
|
|
|
|
logging.exception("Narrow parsing")
|
2014-01-17 00:05:35 +01:00
|
|
|
if narrow_topic is not None:
|
|
|
|
narrow.append(["topic", narrow_topic])
|
2013-12-13 20:20:28 +01:00
|
|
|
|
2013-10-18 21:19:34 +02:00
|
|
|
register_ret = do_events_register(user_profile, request.client,
|
2013-12-13 20:20:28 +01:00
|
|
|
apply_markdown=True, narrow=narrow)
|
2013-03-28 18:09:27 +01:00
|
|
|
user_has_messages = (register_ret['max_message_id'] != -1)
|
2013-02-06 00:33:45 +01:00
|
|
|
|
2013-05-17 21:28:51 +02:00
|
|
|
# Reset our don't-spam-users-with-email counter since the
|
|
|
|
# user has since logged in
|
|
|
|
if not user_profile.last_reminder is None:
|
|
|
|
user_profile.last_reminder = None
|
2013-07-23 22:01:38 +02:00
|
|
|
user_profile.save(update_fields=["last_reminder"])
|
2013-05-17 21:28:51 +02:00
|
|
|
|
2013-04-04 22:30:28 +02:00
|
|
|
# Brand new users get the tutorial
|
|
|
|
needs_tutorial = settings.TUTORIAL_ENABLED and \
|
2013-06-27 16:41:58 +02:00
|
|
|
user_profile.tutorial_status != UserProfile.TUTORIAL_FINISHED
|
2013-03-12 04:54:17 +01:00
|
|
|
|
2014-01-14 23:53:35 +01:00
|
|
|
first_in_realm = realm_user_count(user_profile.realm) == 1
|
2014-01-09 22:53:18 +01:00
|
|
|
# If you are the only person in the realm and you didn't invite
|
|
|
|
# anyone, we'll continue to encourage you to do so on the frontend.
|
2014-01-14 23:53:35 +01:00
|
|
|
prompt_for_invites = first_in_realm and \
|
2014-01-09 22:53:18 +01:00
|
|
|
not PreregistrationUser.objects.filter(referred_by=user_profile).count()
|
|
|
|
|
2013-03-28 18:09:27 +01:00
|
|
|
if user_profile.pointer == -1 and user_has_messages:
|
2013-02-06 00:33:45 +01:00
|
|
|
# Put the new user's pointer at the bottom
|
|
|
|
#
|
|
|
|
# This improves performance, because we limit backfilling of messages
|
|
|
|
# before the pointer. It's also likely that someone joining an
|
|
|
|
# organization is interested in recent messages more than the very
|
|
|
|
# first messages on the system.
|
|
|
|
|
2013-08-09 23:01:37 +02:00
|
|
|
register_ret['pointer'] = register_ret['max_message_id']
|
2013-02-06 00:33:45 +01:00
|
|
|
user_profile.last_pointer_updater = request.session.session_key
|
2012-09-12 22:55:37 +02:00
|
|
|
|
2013-08-09 20:26:35 +02:00
|
|
|
if user_profile.pointer == -1:
|
|
|
|
latest_read = None
|
|
|
|
else:
|
2013-08-19 18:10:32 +02:00
|
|
|
try:
|
|
|
|
latest_read = UserMessage.objects.get(user_profile=user_profile,
|
|
|
|
message__id=user_profile.pointer)
|
|
|
|
except UserMessage.DoesNotExist:
|
|
|
|
# Don't completely fail if your saved pointer ID is invalid
|
|
|
|
logging.warning("%s has invalid pointer %s" % (user_profile.email, user_profile.pointer))
|
|
|
|
latest_read = None
|
|
|
|
|
2014-01-14 17:06:32 +01:00
|
|
|
desktop_notifications_enabled = user_profile.enable_desktop_notifications
|
|
|
|
if narrow_stream is not None:
|
|
|
|
desktop_notifications_enabled = False
|
|
|
|
|
2014-01-15 22:21:43 +01:00
|
|
|
if user_profile.realm.notifications_stream:
|
|
|
|
notifications_stream = user_profile.realm.notifications_stream.name
|
|
|
|
else:
|
|
|
|
notifications_stream = ""
|
|
|
|
|
2016-06-23 11:32:45 +02:00
|
|
|
# Set default language and make it persist
|
|
|
|
default_language = register_ret['default_language']
|
|
|
|
url_lang = '/{}'.format(request.LANGUAGE_CODE)
|
|
|
|
if not request.path.startswith(url_lang):
|
|
|
|
translation.activate(default_language)
|
|
|
|
|
|
|
|
request.session[translation.LANGUAGE_SESSION_KEY] = default_language
|
|
|
|
|
2013-03-25 23:49:38 +01:00
|
|
|
# Pass parameters to the client-side JavaScript code.
|
|
|
|
# These end up in a global JavaScript Object named 'page_params'.
|
2013-12-14 00:00:32 +01:00
|
|
|
page_params = dict(
|
2016-09-08 07:35:50 +02:00
|
|
|
zulip_version = ZULIP_VERSION,
|
2016-07-19 06:31:33 +02:00
|
|
|
share_the_love = settings.SHARE_THE_LOVE,
|
2016-06-20 21:58:59 +02:00
|
|
|
development_environment = settings.DEVELOPMENT,
|
2013-03-25 23:49:38 +01:00
|
|
|
debug_mode = settings.DEBUG,
|
2013-10-11 21:36:49 +02:00
|
|
|
test_suite = settings.TEST_SUITE,
|
2013-03-25 23:49:38 +01:00
|
|
|
poll_timeout = settings.POLL_TIMEOUT,
|
2013-11-13 21:31:09 +01:00
|
|
|
login_page = settings.HOME_NOT_LOGGED_IN,
|
2016-08-14 01:15:12 +02:00
|
|
|
server_uri = settings.SERVER_URI,
|
|
|
|
realm_uri = user_profile.realm.uri,
|
2015-11-21 04:08:53 +01:00
|
|
|
maxfilesize = settings.MAX_FILE_UPLOAD_SIZE,
|
2016-07-05 13:05:51 +02:00
|
|
|
server_generation = settings.SERVER_GENERATION,
|
2014-03-28 00:45:03 +01:00
|
|
|
password_auth_enabled = password_auth_enabled(user_profile.realm),
|
2013-03-28 18:09:27 +01:00
|
|
|
have_initial_messages = user_has_messages,
|
2014-01-23 21:04:46 +01:00
|
|
|
subbed_info = register_ret['subscriptions'],
|
2013-06-12 21:15:32 +02:00
|
|
|
unsubbed_info = register_ret['unsubscribed'],
|
2016-07-12 23:57:16 +02:00
|
|
|
neversubbed_info = register_ret['never_subscribed'],
|
2013-10-21 19:37:52 +02:00
|
|
|
email_dict = register_ret['email_dict'],
|
2013-03-29 15:35:37 +01:00
|
|
|
people_list = register_ret['realm_users'],
|
2014-02-26 00:12:14 +01:00
|
|
|
bot_list = register_ret['realm_bots'],
|
2013-03-28 18:09:27 +01:00
|
|
|
initial_pointer = register_ret['pointer'],
|
2013-04-03 22:00:02 +02:00
|
|
|
initial_presences = register_ret['presences'],
|
2013-05-06 17:14:59 +02:00
|
|
|
initial_servertime = time.time(), # Used for calculating relative presence age
|
2013-03-25 23:49:38 +01:00
|
|
|
fullname = user_profile.full_name,
|
2013-03-28 20:43:34 +01:00
|
|
|
email = user_profile.email,
|
2013-03-25 23:49:38 +01:00
|
|
|
domain = user_profile.realm.domain,
|
2014-01-29 19:08:09 +01:00
|
|
|
realm_name = register_ret['realm_name'],
|
2015-08-20 02:38:32 +02:00
|
|
|
realm_invite_required = register_ret['realm_invite_required'],
|
2015-08-20 21:25:30 +02:00
|
|
|
realm_invite_by_admins_only = register_ret['realm_invite_by_admins_only'],
|
2016-05-12 10:28:00 +02:00
|
|
|
realm_create_stream_by_admins_only = register_ret['realm_create_stream_by_admins_only'],
|
2016-06-21 21:34:41 +02:00
|
|
|
realm_allow_message_editing = register_ret['realm_allow_message_editing'],
|
2016-07-08 02:25:55 +02:00
|
|
|
realm_message_content_edit_limit_seconds = register_ret['realm_message_content_edit_limit_seconds'],
|
2015-08-20 02:38:32 +02:00
|
|
|
realm_restricted_to_domain = register_ret['realm_restricted_to_domain'],
|
2016-08-04 17:32:41 +02:00
|
|
|
realm_default_language = register_ret['realm_default_language'],
|
2013-03-25 23:49:38 +01:00
|
|
|
enter_sends = user_profile.enter_sends,
|
2016-08-19 00:28:28 +02:00
|
|
|
user_id = user_profile.id,
|
2015-08-20 23:59:44 +02:00
|
|
|
left_side_userlist = register_ret['left_side_userlist'],
|
2016-06-23 11:32:45 +02:00
|
|
|
default_language = register_ret['default_language'],
|
2016-08-02 14:33:13 +02:00
|
|
|
default_language_name = get_language_name(register_ret['default_language']),
|
2016-08-02 14:34:12 +02:00
|
|
|
language_list_dbl_col = get_language_list_for_templates(register_ret['default_language']),
|
|
|
|
language_list = get_language_list(),
|
2013-07-26 16:51:02 +02:00
|
|
|
referrals = register_ret['referrals'],
|
2013-08-22 19:54:35 +02:00
|
|
|
realm_emoji = register_ret['realm_emoji'],
|
2013-03-25 23:49:38 +01:00
|
|
|
needs_tutorial = needs_tutorial,
|
2014-01-14 23:53:35 +01:00
|
|
|
first_in_realm = first_in_realm,
|
2014-01-09 22:53:18 +01:00
|
|
|
prompt_for_invites = prompt_for_invites,
|
2014-01-15 22:21:43 +01:00
|
|
|
notifications_stream = notifications_stream,
|
2016-06-10 19:46:53 +02:00
|
|
|
cross_realm_user_emails = list(get_cross_realm_users()),
|
2014-02-05 21:41:01 +01:00
|
|
|
|
|
|
|
# Stream message notification settings:
|
|
|
|
stream_desktop_notifications_enabled =
|
|
|
|
user_profile.enable_stream_desktop_notifications,
|
|
|
|
stream_sounds_enabled = user_profile.enable_stream_sounds,
|
|
|
|
|
|
|
|
# Private message and @-mention notification settings:
|
|
|
|
desktop_notifications_enabled = desktop_notifications_enabled,
|
2013-05-03 21:49:01 +02:00
|
|
|
sounds_enabled =
|
|
|
|
user_profile.enable_sounds,
|
2013-05-07 23:19:52 +02:00
|
|
|
enable_offline_email_notifications =
|
|
|
|
user_profile.enable_offline_email_notifications,
|
2013-10-16 17:24:52 +02:00
|
|
|
enable_offline_push_notifications =
|
|
|
|
user_profile.enable_offline_push_notifications,
|
2015-08-19 22:35:46 +02:00
|
|
|
twenty_four_hour_time = register_ret['twenty_four_hour_time'],
|
2014-02-05 21:41:01 +01:00
|
|
|
|
2013-12-02 02:00:23 +01:00
|
|
|
enable_digest_emails = user_profile.enable_digest_emails,
|
2013-03-28 18:09:27 +01:00
|
|
|
event_queue_id = register_ret['queue_id'],
|
|
|
|
last_event_id = register_ret['last_event_id'],
|
2013-05-08 15:47:37 +02:00
|
|
|
max_message_id = register_ret['max_message_id'],
|
2013-09-04 21:02:11 +02:00
|
|
|
unread_count = approximate_unread_count(user_profile),
|
2013-08-09 20:26:35 +02:00
|
|
|
furthest_read_time = sent_time_in_epoch_seconds(latest_read),
|
2016-07-19 06:44:48 +02:00
|
|
|
save_stacktraces = settings.SAVE_FRONTEND_STACKTRACES,
|
2013-09-10 11:46:18 +02:00
|
|
|
alert_words = register_ret['alert_words'],
|
2013-09-27 19:38:56 +02:00
|
|
|
muted_topics = register_ret['muted_topics'],
|
2014-01-06 23:42:02 +01:00
|
|
|
realm_filters = register_ret['realm_filters'],
|
2016-05-20 22:08:42 +02:00
|
|
|
realm_default_streams = register_ret['realm_default_streams'],
|
2016-02-08 03:59:38 +01:00
|
|
|
is_admin = user_profile.is_realm_admin,
|
2014-01-15 23:30:36 +01:00
|
|
|
can_create_streams = user_profile.can_create_streams(),
|
2014-03-27 23:50:37 +01:00
|
|
|
name_changes_disabled = name_changes_disabled(user_profile.realm),
|
2013-12-03 21:01:37 +01:00
|
|
|
has_mobile_devices = num_push_devices_for_user(user_profile) > 0,
|
2013-12-12 22:36:16 +01:00
|
|
|
autoscroll_forever = user_profile.autoscroll_forever,
|
2014-02-13 23:47:57 +01:00
|
|
|
default_desktop_notifications = user_profile.default_desktop_notifications,
|
2015-08-20 08:41:50 +02:00
|
|
|
avatar_url = avatar_url(user_profile),
|
|
|
|
mandatory_topics = user_profile.realm.mandatory_topics,
|
2015-08-20 09:11:11 +02:00
|
|
|
show_digest_email = user_profile.realm.show_digest_email,
|
2016-07-27 02:09:10 +02:00
|
|
|
presence_disabled = user_profile.realm.presence_disabled,
|
2016-07-27 01:45:29 +02:00
|
|
|
is_zephyr_mirror_realm = user_profile.realm.is_zephyr_mirror_realm,
|
2013-12-12 22:36:16 +01:00
|
|
|
)
|
2016-08-04 17:32:41 +02:00
|
|
|
|
2013-12-13 20:20:28 +01:00
|
|
|
if narrow_stream is not None:
|
|
|
|
# In narrow_stream context, initial pointer is just latest message
|
|
|
|
recipient = get_recipient(Recipient.STREAM, narrow_stream.id)
|
|
|
|
try:
|
|
|
|
initial_pointer = Message.objects.filter(recipient=recipient).order_by('id').reverse()[0].id
|
|
|
|
except IndexError:
|
|
|
|
initial_pointer = -1
|
|
|
|
page_params["narrow_stream"] = narrow_stream.name
|
2014-01-17 00:05:35 +01:00
|
|
|
if narrow_topic is not None:
|
|
|
|
page_params["narrow_topic"] = narrow_topic
|
2014-02-11 17:10:44 +01:00
|
|
|
page_params["narrow"] = [dict(operator=term[0], operand=term[1]) for term in narrow]
|
2013-12-13 20:20:28 +01:00
|
|
|
page_params["max_message_id"] = initial_pointer
|
|
|
|
page_params["initial_pointer"] = initial_pointer
|
|
|
|
page_params["have_initial_messages"] = (initial_pointer != -1)
|
2013-01-09 00:10:37 +01:00
|
|
|
|
2013-04-16 22:58:21 +02:00
|
|
|
statsd.incr('views.home')
|
2013-09-19 23:04:27 +02:00
|
|
|
show_invites = True
|
2013-01-03 00:19:35 +01:00
|
|
|
|
2015-08-20 21:25:30 +02:00
|
|
|
# Some realms only allow admins to invite users
|
2016-02-08 03:59:38 +01:00
|
|
|
if user_profile.realm.invite_by_admins_only and not user_profile.is_realm_admin:
|
2013-06-02 19:23:02 +02:00
|
|
|
show_invites = False
|
|
|
|
|
2014-07-15 23:21:03 +02:00
|
|
|
product_name = "Zulip"
|
|
|
|
page_params['product_name'] = product_name
|
2013-11-08 20:18:02 +01:00
|
|
|
request._log_data['extra'] = "[%s]" % (register_ret["queue_id"],)
|
2013-08-16 22:55:50 +02:00
|
|
|
response = render_to_response('zerver/index.html',
|
|
|
|
{'user_profile': user_profile,
|
2013-12-14 00:00:32 +01:00
|
|
|
'page_params' : simplejson.encoder.JSONEncoderForHTML().encode(page_params),
|
2015-12-15 03:47:39 +01:00
|
|
|
'nofontface': is_buggy_ua(request.META.get("HTTP_USER_AGENT", "Unspecified")),
|
2013-12-04 21:48:18 +01:00
|
|
|
'avatar_url': avatar_url(user_profile),
|
2013-08-16 22:55:50 +02:00
|
|
|
'show_debug':
|
|
|
|
settings.DEBUG and ('show_debug' in request.GET),
|
2016-07-04 09:33:57 +02:00
|
|
|
'pipeline': settings.PIPELINE_ENABLED,
|
2013-08-16 22:55:50 +02:00
|
|
|
'show_invites': show_invites,
|
2016-02-08 03:59:38 +01:00
|
|
|
'is_admin': user_profile.is_realm_admin,
|
2016-07-27 01:45:29 +02:00
|
|
|
'show_webathena': user_profile.realm.webathena_enabled,
|
2013-12-12 22:36:16 +01:00
|
|
|
'enable_feedback': settings.ENABLE_FEEDBACK,
|
2013-12-13 22:10:06 +01:00
|
|
|
'embedded': narrow_stream is not None,
|
2014-07-15 23:21:03 +02:00
|
|
|
'product_name': product_name
|
2013-08-16 22:55:50 +02:00
|
|
|
},
|
2016-04-21 08:48:33 +02:00
|
|
|
request=request)
|
2013-08-16 22:55:50 +02:00
|
|
|
patch_cache_control(response, no_cache=True, no_store=True, must_revalidate=True)
|
|
|
|
return response
|
2012-08-28 18:44:51 +02:00
|
|
|
|
2016-04-21 23:48:34 +02:00
|
|
|
@zulip_login_required
|
2013-11-12 21:01:55 +01:00
|
|
|
def desktop_home(request):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (HttpRequest) -> HttpResponse
|
2013-11-12 21:01:55 +01:00
|
|
|
return HttpResponseRedirect(reverse('zerver.views.home'))
|
|
|
|
|
2013-07-01 20:13:26 +02:00
|
|
|
def is_buggy_ua(agent):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (str) -> bool
|
2013-07-01 20:13:26 +02:00
|
|
|
"""Discrimiate CSS served to clients based on User Agent
|
|
|
|
|
|
|
|
Due to QTBUG-3467, @font-face is not supported in QtWebKit.
|
|
|
|
This may get fixed in the future, but for right now we can
|
|
|
|
just serve the more conservative CSS to all our desktop apps.
|
|
|
|
"""
|
2013-12-19 20:29:10 +01:00
|
|
|
return ("Humbug Desktop/" in agent or "Zulip Desktop/" in agent or "ZulipDesktop/" in agent) and \
|
2016-05-10 01:55:43 +02:00
|
|
|
"Mac" not in agent
|
2012-10-21 03:53:03 +02:00
|
|
|
|
2013-04-02 22:42:51 +02:00
|
|
|
# Does not need to be authenticated because it's called from rest_dispatch
|
2013-03-14 23:21:53 +01:00
|
|
|
@has_request_variables
|
|
|
|
def api_events_register(request, user_profile,
|
2014-02-14 15:48:42 +01:00
|
|
|
apply_markdown=REQ(default=False, validator=check_bool),
|
2014-02-07 01:22:19 +01:00
|
|
|
all_public_streams=REQ(default=None, validator=check_bool)):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (HttpRequest, UserProfile, bool, Optional[bool]) -> HttpResponse
|
2013-03-14 23:21:53 +01:00
|
|
|
return events_register_backend(request, user_profile,
|
2013-10-17 23:51:25 +02:00
|
|
|
apply_markdown=apply_markdown,
|
|
|
|
all_public_streams=all_public_streams)
|
2013-03-14 23:21:53 +01:00
|
|
|
|
2014-02-07 01:22:19 +01:00
|
|
|
def _default_all_public_streams(user_profile, all_public_streams):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (UserProfile, Optional[bool]) -> bool
|
2014-02-07 01:22:19 +01:00
|
|
|
if all_public_streams is not None:
|
|
|
|
return all_public_streams
|
|
|
|
else:
|
|
|
|
return user_profile.default_all_public_streams
|
|
|
|
|
|
|
|
def _default_narrow(user_profile, narrow):
|
2016-07-03 19:13:25 +02:00
|
|
|
# type: (UserProfile, Iterable[Sequence[text_type]]) -> Iterable[Sequence[text_type]]
|
2014-02-07 01:22:19 +01:00
|
|
|
default_stream = user_profile.default_events_register_stream
|
|
|
|
if not narrow and user_profile.default_events_register_stream is not None:
|
2016-06-05 04:58:33 +02:00
|
|
|
narrow = [['stream', default_stream.name]]
|
2014-02-07 01:22:19 +01:00
|
|
|
return narrow
|
|
|
|
|
2013-03-14 23:21:53 +01:00
|
|
|
@has_request_variables
|
|
|
|
def events_register_backend(request, user_profile, apply_markdown=True,
|
2014-02-07 01:22:19 +01:00
|
|
|
all_public_streams=None,
|
2013-12-13 20:51:53 +01:00
|
|
|
event_types=REQ(validator=check_list(check_string), default=None),
|
2013-12-17 23:25:36 +01:00
|
|
|
narrow=REQ(validator=check_list(check_list(check_string, length=2)), default=[]),
|
2013-08-05 22:09:12 +02:00
|
|
|
queue_lifespan_secs=REQ(converter=int, default=0)):
|
2016-07-03 19:13:25 +02:00
|
|
|
# type: (HttpRequest, UserProfile, bool, Optional[bool], Optional[Iterable[str]], Iterable[Sequence[text_type]], int) -> HttpResponse
|
2014-02-07 01:22:19 +01:00
|
|
|
all_public_streams = _default_all_public_streams(user_profile, all_public_streams)
|
|
|
|
narrow = _default_narrow(user_profile, narrow)
|
|
|
|
|
2013-05-07 17:25:25 +02:00
|
|
|
ret = do_events_register(user_profile, request.client, apply_markdown,
|
2013-12-10 16:28:16 +01:00
|
|
|
event_types, queue_lifespan_secs, all_public_streams,
|
|
|
|
narrow=narrow)
|
2013-03-21 22:43:53 +01:00
|
|
|
return json_success(ret)
|
2013-04-24 17:18:49 +02:00
|
|
|
|
2013-09-10 00:06:24 +02:00
|
|
|
@authenticated_json_post_view
|
|
|
|
@has_request_variables
|
|
|
|
def json_set_muted_topics(request, user_profile,
|
2013-12-18 18:02:10 +01:00
|
|
|
muted_topics=REQ(validator=check_list(check_list(check_string, length=2)), default=[])):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (HttpRequest, UserProfile, List[List[text_type]]) -> HttpResponse
|
2013-09-10 00:06:24 +02:00
|
|
|
do_set_muted_topics(user_profile, muted_topics)
|
|
|
|
return json_success()
|
2013-10-15 20:19:41 +02:00
|
|
|
|
2013-11-08 17:53:41 +01:00
|
|
|
def generate_204(request):
|
2016-06-05 04:58:33 +02:00
|
|
|
# type: (HttpRequest) -> HttpResponse
|
2013-11-08 17:53:41 +01:00
|
|
|
return HttpResponse(content=None, status=204)
|