<s> from __future__ import absolute_import <EOL> from typing import Any <EOL> from django . conf import settings <EOL> from django . contrib . auth import authenticate , login , get_backends <EOL> from django . core . urlresolvers import reverse <EOL> from django . http import HttpResponseRedirect , HttpResponseForbidden , HttpResponse <EOL> from django . shortcuts import render_to_response , redirect <EOL> from django . template import RequestContext , loader <EOL> from django . utils . timezone import now <EOL> from django . utils . cache import patch_cache_control <EOL> from django . core . exceptions import ValidationError <EOL> from django . core import validators <EOL> from django . contrib . auth . views import login as django_login_page , logout_then_login as django_logout_then_login <EOL> from django . forms . models import model_to_dict <EOL> from django . core . mail import send_mail <EOL> from django . middleware . csrf import get_token <EOL> from zerver . models import Message , UserProfile , Stream , Subscription , Huddle , Recipient , Realm , UserMessage , DefaultStream , RealmEmoji , RealmAlias , RealmFilter , PreregistrationUser , get_client , MitUser , UserActivity , PushDeviceToken , get_stream , UserPresence , get_recipient , split_email_to_domain , resolve_email_to_domain , email_to_username , get_realm , completely_open , get_unique_open_realm , remote_user_to_email , email_allowed_for_realm <EOL> from zerver . lib . actions import do_change_password , do_change_full_name , do_change_is_admin , do_activate_user , do_create_user , internal_send_message , update_user_presence , do_events_register , get_status_dict , do_change_enable_offline_email_notifications , do_change_enable_digest_emails , do_set_realm_name , do_set_realm_restricted_to_domain , do_set_realm_invite_required , do_set_realm_invite_by_admins_only , get_default_subs , user_email_is_unique , do_invite_users , do_refer_friend , compute_mit_user_fullname , do_set_muted_topics , clear_followup_emails_queue , do_update_pointer , realm_user_count <EOL> from zerver . lib . push_notifications import num_push_devices_for_user <EOL> from zerver . forms import RegistrationForm , HomepageForm , ToSForm , CreateUserForm , is_inactive , OurAuthenticationForm <EOL> from django . views . decorators . csrf import csrf_exempt <EOL> from django_auth_ldap . backend import LDAPBackend , _LDAPUser <EOL> from zerver . lib import bugdown <EOL> from zerver . lib . validator import check_string , check_list , check_bool <EOL> from zerver . decorator import require_post , authenticated_json_post_view , has_request_variables , authenticated_json_view , to_non_negative_int , JsonableError , get_user_profile_by_email , REQ , require_realm_admin , zulip_login_required <EOL> from zerver . lib . avatar import avatar_url <EOL> from zerver . lib . upload import upload_message_image_through_web_client , get_signed_upload_url , get_realm_for_filename <EOL> from zerver . lib . response import json_success , json_error <EOL> from zerver . lib . utils import statsd , generate_random_token <EOL> from zproject . backends import password_auth_enabled , dev_auth_enabled <EOL> from confirmation . models import Confirmation <EOL> import requests <EOL> import subprocess <EOL> import calendar <EOL> import datetime <EOL> import ujson <EOL> import simplejson <EOL> import re <EOL> from six . moves import urllib <EOL> import base64 <EOL> import time <EOL> import logging <EOL> import jwt <EOL> import hashlib <EOL> import hmac <EOL> from zerver . lib . rest import rest_dispatch as _rest_dispatch <EOL> rest_dispatch = csrf_exempt ( ( lambda request , * args , ** kwargs : _rest_dispatch ( request , globals ( ) , * args , ** kwargs ) ) ) <EOL> def name_changes_disabled ( realm ) : <EOL> return settings . NAME_CHANGES_DISABLED or realm . name_changes_disabled <EOL> @ require_post <EOL> def accounts_register ( request ) : <EOL> key = request . POST [ '<STR_LIT:key>' ] <EOL> confirmation = Confirmation . objects . get ( confirmation_key = key ) <EOL> prereg_user = confirmation . content_object <EOL> email = prereg_user . email <EOL> mit_beta_user = isinstance ( confirmation . content_object , MitUser ) <EOL> try : <EOL> existing_user_profile = get_user_profile_by_email ( email ) <EOL> except UserProfile . DoesNotExist : <EOL> existing_user_profile = None <EOL> validators . validate_email ( email ) <EOL> unique_open_realm = get_unique_open_realm ( ) <EOL> if unique_open_realm : <EOL> realm = unique_open_realm <EOL> domain = realm . domain <EOL> elif not mit_beta_user and prereg_user . referred_by : <EOL> realm = prereg_user . referred_by . realm <EOL> domain = realm . domain <EOL> if not email_allowed_for_realm ( email , realm ) : <EOL> return render_to_response ( "<STR_LIT>" , { "<STR_LIT>" : realm . name } ) <EOL> elif not mit_beta_user and prereg_user . realm : <EOL> domain = prereg_user . realm . domain <EOL> realm = get_realm ( domain ) <EOL> else : <EOL> domain = resolve_email_to_domain ( email ) <EOL> realm = get_realm ( domain ) <EOL> if realm and realm . deactivated : <EOL> return render_to_response ( "<STR_LIT>" , <EOL> { "<STR_LIT>" : realm . name , <EOL> "<STR_LIT>" : settings . ZULIP_ADMINISTRATOR } ) <EOL> try : <EOL> if existing_user_profile is not None and existing_user_profile . is_mirror_dummy : <EOL> is_inactive ( email ) <EOL> else : <EOL> user_email_is_unique ( email ) <EOL> except ValidationError : <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' ) + '<STR_LIT>' + urllib . parse . quote_plus ( email ) ) <EOL> name_validated = False <EOL> full_name = None <EOL> if request . POST . get ( '<STR_LIT>' ) : <EOL> try : <EOL> del request . session [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> pass <EOL> if domain == "<STR_LIT>" : <EOL> hesiod_name = compute_mit_user_fullname ( email ) <EOL> form = RegistrationForm ( <EOL> initial = { '<STR_LIT>' : hesiod_name if "<STR_LIT:@>" not in hesiod_name else "<STR_LIT>" } ) <EOL> name_validated = True <EOL> elif settings . POPULATE_PROFILE_VIA_LDAP : <EOL> for backend in get_backends ( ) : <EOL> if isinstance ( backend , LDAPBackend ) : <EOL> ldap_attrs = _LDAPUser ( backend , backend . django_to_ldap_username ( email ) ) . attrs <EOL> try : <EOL> request . session [ '<STR_LIT>' ] = ldap_attrs [ settings . AUTH_LDAP_USER_ATTR_MAP [ '<STR_LIT>' ] ] [ <NUM_LIT:0> ] <EOL> name_validated = True <EOL> form = RegistrationForm ( <EOL> { '<STR_LIT>' : request . session [ '<STR_LIT>' ] } ) <EOL> break <EOL> except TypeError : <EOL> form = RegistrationForm ( ) <EOL> elif '<STR_LIT>' in request . POST : <EOL> form = RegistrationForm ( <EOL> initial = { '<STR_LIT>' : request . POST . get ( '<STR_LIT>' ) } <EOL> ) <EOL> else : <EOL> form = RegistrationForm ( ) <EOL> else : <EOL> postdata = request . POST . copy ( ) <EOL> if name_changes_disabled ( realm ) : <EOL> try : <EOL> postdata . update ( { '<STR_LIT>' : request . session [ '<STR_LIT>' ] } ) <EOL> name_validated = True <EOL> except KeyError : <EOL> pass <EOL> form = RegistrationForm ( postdata ) <EOL> if not password_auth_enabled ( realm ) : <EOL> form [ '<STR_LIT:password>' ] . field . required = False <EOL> if form . is_valid ( ) : <EOL> if password_auth_enabled ( realm ) : <EOL> password = form . cleaned_data [ '<STR_LIT:password>' ] <EOL> else : <EOL> password = None <EOL> full_name = form . cleaned_data [ '<STR_LIT>' ] <EOL> short_name = email_to_username ( email ) <EOL> first_in_realm = len ( UserProfile . objects . filter ( realm = realm , is_bot = False ) ) == <NUM_LIT:0> <EOL> if existing_user_profile is not None and existing_user_profile . is_mirror_dummy : <EOL> try : <EOL> user_profile = existing_user_profile <EOL> do_activate_user ( user_profile ) <EOL> do_change_password ( user_profile , password ) <EOL> do_change_full_name ( user_profile , full_name ) <EOL> except UserProfile . DoesNotExist : <EOL> user_profile = do_create_user ( email , password , realm , full_name , short_name , <EOL> prereg_user = prereg_user , <EOL> newsletter_data = { "<STR_LIT>" : request . META [ '<STR_LIT>' ] } ) <EOL> else : <EOL> user_profile = do_create_user ( email , password , realm , full_name , short_name , <EOL> prereg_user = prereg_user , <EOL> newsletter_data = { "<STR_LIT>" : request . META [ '<STR_LIT>' ] } ) <EOL> login ( request , authenticate ( username = user_profile . email , use_dummy_backend = True ) ) <EOL> if first_in_realm : <EOL> do_change_is_admin ( user_profile , True ) <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' ) ) <EOL> else : <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' ) ) <EOL> return render_to_response ( '<STR_LIT>' , <EOL> { '<STR_LIT>' : form , <EOL> '<STR_LIT>' : domain , <EOL> '<STR_LIT:email>' : email , <EOL> '<STR_LIT:key>' : key , <EOL> '<STR_LIT>' : request . session . get ( '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : name_validated and name_changes_disabled ( realm ) , <EOL> '<STR_LIT>' : password_auth_enabled ( realm ) , <EOL> } , <EOL> context_instance = RequestContext ( request ) ) <EOL> @ zulip_login_required <EOL> def accounts_accept_terms ( request ) : <EOL> email = request . user . email <EOL> domain = resolve_email_to_domain ( email ) <EOL> if request . method == "<STR_LIT:POST>" : <EOL> form = ToSForm ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> full_name = form . cleaned_data [ '<STR_LIT>' ] <EOL> send_mail ( '<STR_LIT>' + full_name , <EOL> loader . render_to_string ( '<STR_LIT>' , <EOL> { '<STR_LIT:name>' : full_name , <EOL> '<STR_LIT:email>' : email , <EOL> '<STR_LIT>' : request . META [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : request . META . get ( '<STR_LIT>' , "<STR_LIT>" ) } ) , <EOL> settings . EMAIL_HOST_USER , <EOL> [ "<STR_LIT>" ] ) <EOL> do_change_full_name ( request . user , full_name ) <EOL> return redirect ( home ) <EOL> else : <EOL> form = ToSForm ( ) <EOL> return render_to_response ( '<STR_LIT>' , <EOL> { '<STR_LIT>' : form , '<STR_LIT>' : domain , '<STR_LIT:email>' : email } , <EOL> context_instance = RequestContext ( request ) ) <EOL> from zerver . lib . ccache import make_ccache <EOL> @ authenticated_json_view <EOL> @ has_request_variables <EOL> def webathena_kerberos_login ( request , user_profile , <EOL> cred = REQ ( default = None ) ) : <EOL> if cred is None : <EOL> return json_error ( "<STR_LIT>" ) <EOL> if not user_profile . realm . domain == "<STR_LIT>" : <EOL> return json_error ( "<STR_LIT>" ) <EOL> try : <EOL> parsed_cred = ujson . loads ( cred ) <EOL> user = parsed_cred [ "<STR_LIT>" ] [ "<STR_LIT>" ] [ <NUM_LIT:0> ] <EOL> if user == "<STR_LIT>" : <EOL> user = "<STR_LIT>" <EOL> assert ( user == user_profile . email . split ( "<STR_LIT:@>" ) [ <NUM_LIT:0> ] ) <EOL> ccache = make_ccache ( parsed_cred ) <EOL> except Exception : <EOL> return json_error ( "<STR_LIT>" ) <EOL> try : <EOL> subprocess . check_call ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> user , <EOL> user_profile . api_key , <EOL> base64 . b64encode ( ccache ) ] ) <EOL> except Exception : <EOL> logging . exception ( "<STR_LIT>" ) <EOL> return json_error ( "<STR_LIT>" ) <EOL> return json_success ( ) <EOL> def api_endpoint_docs ( request ) : <EOL> raw_calls = open ( '<STR_LIT>' , '<STR_LIT:r>' ) . read ( ) <EOL> calls = ujson . loads ( raw_calls ) <EOL> langs = set ( ) <EOL> for call in calls : <EOL> call [ "<STR_LIT>" ] = "<STR_LIT>" % ( settings . EXTERNAL_API_URI , call [ "<STR_LIT>" ] ) <EOL> call [ "<STR_LIT>" ] [ "<STR_LIT>" ] = call [ "<STR_LIT>" ] [ "<STR_LIT>" ] . replace ( "<STR_LIT>" , settings . EXTERNAL_API_URI ) <EOL> response = call [ '<STR_LIT>' ] <EOL> if not '<STR_LIT:\n>' in response : <EOL> extended_response = response . replace ( "<STR_LIT:U+002CU+0020>" , "<STR_LIT>" ) <EOL> else : <EOL> extended_response = response <EOL> call [ '<STR_LIT>' ] = bugdown . convert ( "<STR_LIT>" + extended_response + "<STR_LIT>" , "<STR_LIT:default>" ) <EOL> for example_type in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> for lang in call . get ( '<STR_LIT>' + example_type , [ ] ) : <EOL> langs . add ( lang ) <EOL> return render_to_response ( <EOL> '<STR_LIT>' , { <EOL> '<STR_LIT:content>' : calls , <EOL> '<STR_LIT>' : langs , <EOL> } , <EOL> context_instance = RequestContext ( request ) ) <EOL> @ authenticated_json_post_view <EOL> @ has_request_variables <EOL> def json_invite_users ( request , user_profile , invitee_emails = REQ ) : <EOL> if not invitee_emails : <EOL> return json_error ( "<STR_LIT>" ) <EOL> invitee_emails = set ( re . split ( r'<STR_LIT>' , invitee_emails ) ) <EOL> stream_names = request . POST . getlist ( '<STR_LIT>' ) <EOL> if not stream_names : <EOL> return json_error ( "<STR_LIT>" ) <EOL> notifications_stream = user_profile . realm . notifications_stream <EOL> if notifications_stream and not notifications_stream . invite_only : <EOL> stream_names . append ( notifications_stream . name ) <EOL> streams = [ ] <EOL> for stream_name in stream_names : <EOL> stream = get_stream ( stream_name , user_profile . realm ) <EOL> if stream is None : <EOL> return json_error ( "<STR_LIT>" % ( stream_name , ) ) <EOL> streams . append ( stream ) <EOL> ret_error , error_data = do_invite_users ( user_profile , invitee_emails , streams ) <EOL> if ret_error is not None : <EOL> return json_error ( data = error_data , msg = ret_error ) <EOL> else : <EOL> return json_success ( ) <EOL> def create_homepage_form ( request , user_info = None ) : <EOL> if user_info : <EOL> return HomepageForm ( user_info , domain = request . session . get ( "<STR_LIT>" ) ) <EOL> return HomepageForm ( domain = request . session . get ( "<STR_LIT>" ) ) <EOL> def maybe_send_to_registration ( request , email , full_name = '<STR_LIT>' ) : <EOL> form = create_homepage_form ( request , user_info = { '<STR_LIT:email>' : email } ) <EOL> request . verified_email = None <EOL> if form . is_valid ( ) : <EOL> prereg_user = None <EOL> if settings . ONLY_SSO : <EOL> try : <EOL> prereg_user = PreregistrationUser . objects . filter ( email__iexact = email ) . latest ( "<STR_LIT>" ) <EOL> except PreregistrationUser . DoesNotExist : <EOL> prereg_user = create_preregistration_user ( email , request ) <EOL> else : <EOL> prereg_user = create_preregistration_user ( email , request ) <EOL> return redirect ( "<STR_LIT>" . join ( ( <EOL> settings . EXTERNAL_URI_SCHEME , <EOL> request . get_host ( ) , <EOL> "<STR_LIT:/>" , <EOL> Confirmation . objects . get_link_for_object ( prereg_user ) . split ( "<STR_LIT:/>" , <NUM_LIT:3> ) [ <NUM_LIT:3> ] , <EOL> '<STR_LIT>' , <EOL> urllib . parse . quote_plus ( full_name . encode ( '<STR_LIT:utf8>' ) ) ) ) ) <EOL> else : <EOL> return render_to_response ( '<STR_LIT>' , { '<STR_LIT>' : form } , <EOL> context_instance = RequestContext ( request ) ) <EOL> def login_or_register_remote_user ( request , remote_username , user_profile , full_name = '<STR_LIT>' ) : <EOL> if user_profile is None or user_profile . is_mirror_dummy : <EOL> return maybe_send_to_registration ( request , remote_user_to_email ( remote_username ) , full_name ) <EOL> else : <EOL> login ( request , user_profile ) <EOL> return HttpResponseRedirect ( "<STR_LIT>" % ( settings . EXTERNAL_URI_SCHEME , <EOL> request . get_host ( ) ) ) <EOL> def remote_user_sso ( request ) : <EOL> try : <EOL> remote_user = request . META [ "<STR_LIT>" ] <EOL> except KeyError : <EOL> raise JsonableError ( "<STR_LIT>" ) <EOL> user_profile = authenticate ( remote_user = remote_user ) <EOL> return login_or_register_remote_user ( request , remote_user , user_profile ) <EOL> @ csrf_exempt <EOL> def remote_user_jwt ( request ) : <EOL> try : <EOL> json_web_token = request . POST [ "<STR_LIT>" ] <EOL> payload , signing_input , header , signature = jwt . load ( json_web_token ) <EOL> except KeyError : <EOL> raise JsonableError ( "<STR_LIT>" ) <EOL> except jwt . DecodeError : <EOL> raise JsonableError ( "<STR_LIT>" ) <EOL> remote_user = payload . get ( "<STR_LIT:user>" , None ) <EOL> if remote_user is None : <EOL> raise JsonableError ( "<STR_LIT>" ) <EOL> domain = payload . get ( '<STR_LIT>' , None ) <EOL> if domain is None : <EOL> raise JsonableError ( "<STR_LIT>" ) <EOL> email = "<STR_LIT>" % ( remote_user , domain ) <EOL> try : <EOL> jwt . verify_signature ( payload , signing_input , header , signature , <EOL> settings . JWT_AUTH_KEYS [ domain ] ) <EOL> user_profile = authenticate ( username = email , use_dummy_backend = True ) <EOL> except ( jwt . DecodeError , jwt . ExpiredSignature ) : <EOL> raise JsonableError ( "<STR_LIT>" ) <EOL> except KeyError : <EOL> raise JsonableError ( "<STR_LIT>" ) <EOL> except UserProfile . DoesNotExist : <EOL> user_profile = None <EOL> return login_or_register_remote_user ( request , email , user_profile , remote_user ) <EOL> def google_oauth2_csrf ( request , value ) : <EOL> return hmac . new ( get_token ( request ) . encode ( '<STR_LIT:utf-8>' ) , value , hashlib . sha256 ) . hexdigest ( ) <EOL> def start_google_oauth2 ( request ) : <EOL> uri = '<STR_LIT>' <EOL> cur_time = str ( int ( time . time ( ) ) ) <EOL> csrf_state = '<STR_LIT>' . format ( <EOL> cur_time , <EOL> google_oauth2_csrf ( request , cur_time ) , <EOL> ) <EOL> prams = { <EOL> '<STR_LIT>' : '<STR_LIT:code>' , <EOL> '<STR_LIT>' : settings . GOOGLE_OAUTH2_CLIENT_ID , <EOL> '<STR_LIT>' : '<STR_LIT>' . join ( ( <EOL> settings . EXTERNAL_URI_SCHEME , <EOL> request . get_host ( ) , <EOL> reverse ( '<STR_LIT>' ) , <EOL> ) ) , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:state>' : csrf_state , <EOL> } <EOL> return redirect ( uri + urllib . parse . urlencode ( prams ) ) <EOL> requests_json_is_function = callable ( requests . Response . json ) <EOL> def extract_json_response ( resp ) : <EOL> if requests_json_is_function : <EOL> return resp . json ( ) <EOL> else : <EOL> return resp . json <EOL> def finish_google_oauth2 ( request ) : <EOL> error = request . GET . get ( '<STR_LIT:error>' ) <EOL> if error == '<STR_LIT>' : <EOL> return redirect ( '<STR_LIT:/>' ) <EOL> elif error is not None : <EOL> logging . warning ( '<STR_LIT>' , request . GET ) <EOL> return HttpResponse ( status = <NUM_LIT> ) <EOL> value , hmac_value = request . GET . get ( '<STR_LIT:state>' ) . split ( '<STR_LIT::>' ) <EOL> if hmac_value != google_oauth2_csrf ( request , value ) : <EOL> logging . warning ( '<STR_LIT>' ) <EOL> return HttpResponse ( status = <NUM_LIT> ) <EOL> resp = requests . post ( <EOL> '<STR_LIT>' , <EOL> data = { <EOL> '<STR_LIT:code>' : request . GET . get ( '<STR_LIT:code>' ) , <EOL> '<STR_LIT>' : settings . GOOGLE_OAUTH2_CLIENT_ID , <EOL> '<STR_LIT>' : settings . GOOGLE_OAUTH2_CLIENT_SECRET , <EOL> '<STR_LIT>' : '<STR_LIT>' . join ( ( <EOL> settings . EXTERNAL_URI_SCHEME , <EOL> request . get_host ( ) , <EOL> reverse ( '<STR_LIT>' ) , <EOL> ) ) , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> ) <EOL> if resp . status_code == <NUM_LIT> : <EOL> logging . warning ( '<STR_LIT>' % ( resp . text , ) ) <EOL> return HttpResponse ( status = <NUM_LIT> ) <EOL> elif resp . status_code != <NUM_LIT:200> : <EOL> raise Exception ( '<STR_LIT>' % ( resp . text , ) ) <EOL> access_token = extract_json_response ( resp ) [ '<STR_LIT>' ] <EOL> resp = requests . get ( <EOL> '<STR_LIT>' , <EOL> params = { '<STR_LIT>' : access_token } <EOL> ) <EOL> if resp . status_code == <NUM_LIT> : <EOL> logging . warning ( '<STR_LIT>' % ( resp . text , ) ) <EOL> return HttpResponse ( status = <NUM_LIT> ) <EOL> elif resp . status_code != <NUM_LIT:200> : <EOL> raise Exception ( '<STR_LIT>' % ( resp . text , ) ) <EOL> body = extract_json_response ( resp ) <EOL> try : <EOL> full_name = body [ '<STR_LIT:name>' ] [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> full_name = u'<STR_LIT>' . format ( <EOL> body [ '<STR_LIT:name>' ] [ '<STR_LIT>' ] , body [ '<STR_LIT:name>' ] [ '<STR_LIT>' ] <EOL> ) <EOL> for email in body [ '<STR_LIT>' ] : <EOL> if email [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> break <EOL> else : <EOL> raise Exception ( '<STR_LIT>' % ( body , ) ) <EOL> email_address = email [ '<STR_LIT:value>' ] <EOL> user_profile = authenticate ( username = email_address , use_dummy_backend = True ) <EOL> return login_or_register_remote_user ( request , email_address , user_profile , full_name ) <EOL> def login_page ( request , ** kwargs ) : <EOL> extra_context = kwargs . pop ( '<STR_LIT>' , { } ) <EOL> if dev_auth_enabled ( ) : <EOL> MAX_DEV_BACKEND_USERS = <NUM_LIT:100> <EOL> users_query = UserProfile . objects . select_related ( ) . filter ( is_bot = False , is_active = True ) <EOL> users = users_query . order_by ( '<STR_LIT:email>' ) [ <NUM_LIT:0> : MAX_DEV_BACKEND_USERS ] <EOL> extra_context [ '<STR_LIT>' ] = [ u . email for u in users if u . is_realm_admin ] <EOL> extra_context [ '<STR_LIT>' ] = [ u . email for u in users if not u . is_realm_admin ] <EOL> template_response = django_login_page ( <EOL> request , authentication_form = OurAuthenticationForm , <EOL> extra_context = extra_context , ** kwargs ) <EOL> try : <EOL> template_response . context_data [ '<STR_LIT:email>' ] = request . GET [ '<STR_LIT:email>' ] <EOL> except KeyError : <EOL> pass <EOL> return template_response <EOL> def dev_direct_login ( request , ** kwargs ) : <EOL> if ( not dev_auth_enabled ( ) ) or settings . PRODUCTION : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> email = request . POST [ '<STR_LIT>' ] <EOL> user_profile = authenticate ( username = email ) <EOL> login ( request , user_profile ) <EOL> return HttpResponseRedirect ( "<STR_LIT>" % ( settings . EXTERNAL_URI_SCHEME , <EOL> request . get_host ( ) ) ) <EOL> @ authenticated_json_post_view <EOL> @ has_request_variables <EOL> def json_bulk_invite_users ( request , user_profile , <EOL> invitee_emails = REQ ( validator = check_list ( check_string ) ) ) : <EOL> invitee_emails = set ( invitee_emails ) <EOL> streams = get_default_subs ( user_profile ) <EOL> ret_error , error_data = do_invite_users ( user_profile , invitee_emails , streams ) <EOL> if ret_error is not None : <EOL> return json_error ( data = error_data , msg = ret_error ) <EOL> else : <EOL> invited = PreregistrationUser . objects . filter ( referred_by = user_profile ) <EOL> internal_message = "<STR_LIT>" % ( <EOL> user_profile . full_name , user_profile . email , invited . count ( ) ) <EOL> internal_send_message ( settings . NEW_USER_BOT , "<STR_LIT>" , "<STR_LIT>" , <EOL> user_profile . realm . domain , internal_message ) <EOL> return json_success ( ) <EOL> @ zulip_login_required <EOL> def initial_invite_page ( request ) : <EOL> user = request . user <EOL> domain_count = len ( UserProfile . objects . filter ( realm = user . realm ) ) <EOL> if domain_count > <NUM_LIT:1> : <EOL> return redirect ( '<STR_LIT>' ) <EOL> params = { '<STR_LIT>' : user . realm . domain } <EOL> if ( user . realm . restricted_to_domain ) : <EOL> params [ '<STR_LIT>' ] = user . realm . domain <EOL> return render_to_response ( '<STR_LIT>' , params , <EOL> context_instance = RequestContext ( request ) ) <EOL> @ require_post <EOL> def logout_then_login ( request , ** kwargs ) : <EOL> return django_logout_then_login ( request , kwargs ) <EOL> def create_preregistration_user ( email , request ) : <EOL> domain = request . session . get ( "<STR_LIT>" ) <EOL> if completely_open ( domain ) : <EOL> request . session [ "<STR_LIT>" ] = None <EOL> return PreregistrationUser . objects . create ( email = email , <EOL> realm = get_realm ( domain ) ) <EOL> if split_email_to_domain ( email ) == "<STR_LIT>" : <EOL> return MitUser . objects . get_or_create ( email = email ) [ <NUM_LIT:0> ] <EOL> return PreregistrationUser . objects . create ( email = email ) <EOL> def accounts_home_with_domain ( request , domain ) : <EOL> if completely_open ( domain ) : <EOL> request . session [ "<STR_LIT>" ] = domain <EOL> return accounts_home ( request ) <EOL> else : <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' ) ) <EOL> def send_registration_completion_email ( email , request ) : <EOL> """<STR_LIT>""" <EOL> prereg_user = create_preregistration_user ( email , request ) <EOL> context = { '<STR_LIT>' : settings . ZULIP_ADMINISTRATOR , <EOL> '<STR_LIT>' : settings . VOYAGER } <EOL> Confirmation . objects . send_confirmation ( prereg_user , email , <EOL> additional_context = context ) <EOL> def accounts_home ( request ) : <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = create_homepage_form ( request , user_info = request . POST ) <EOL> if form . is_valid ( ) : <EOL> email = form . cleaned_data [ '<STR_LIT:email>' ] <EOL> send_registration_completion_email ( email , request ) <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT:email>' : email } ) ) <EOL> try : <EOL> email = request . POST [ '<STR_LIT:email>' ] <EOL> is_inactive ( email ) <EOL> except ValidationError : <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' ) + '<STR_LIT>' + urllib . parse . quote_plus ( email ) ) <EOL> else : <EOL> form = create_homepage_form ( request ) <EOL> return render_to_response ( '<STR_LIT>' , <EOL> { '<STR_LIT>' : form , '<STR_LIT>' : request . get_full_path } , <EOL> context_instance = RequestContext ( request ) ) <EOL> def approximate_unread_count ( user_profile ) : <EOL> not_in_home_view_recipients = [ sub . recipient . id for sub in Subscription . objects . filter ( <EOL> user_profile = user_profile , in_home_view = False ) ] <EOL> muted_topics = ujson . loads ( user_profile . muted_topics ) <EOL> if muted_topics : <EOL> muted_topics = muted_topics [ <NUM_LIT:0> ] <EOL> return UserMessage . objects . filter ( <EOL> user_profile = user_profile , message_id__gt = user_profile . pointer ) . exclude ( <EOL> message__recipient__type = Recipient . STREAM , <EOL> message__recipient__id__in = not_in_home_view_recipients ) . exclude ( <EOL> message__subject__in = muted_topics ) . exclude ( <EOL> flags = UserMessage . flags . read ) . count ( ) <EOL> def sent_time_in_epoch_seconds ( user_message ) : <EOL> if not user_message : <EOL> return None <EOL> return calendar . timegm ( user_message . message . pub_date . utctimetuple ( ) ) <EOL> @ zulip_login_required <EOL> def home ( request ) : <EOL> request . session . modified = True <EOL> user_profile = request . user <EOL> request . _email = request . user . email <EOL> request . client = get_client ( "<STR_LIT>" ) <EOL> narrow = [ ] <EOL> narrow_stream = None <EOL> narrow_topic = request . GET . get ( "<STR_LIT>" ) <EOL> if request . GET . get ( "<STR_LIT>" ) : <EOL> try : <EOL> narrow_stream = get_stream ( request . GET . get ( "<STR_LIT>" ) , user_profile . realm ) <EOL> assert ( narrow_stream is not None ) <EOL> assert ( narrow_stream . is_public ( ) ) <EOL> narrow = [ [ "<STR_LIT>" , narrow_stream . name ] ] <EOL> except Exception : <EOL> logging . exception ( "<STR_LIT>" ) <EOL> if narrow_topic is not None : <EOL> narrow . append ( [ "<STR_LIT>" , narrow_topic ] ) <EOL> register_ret = do_events_register ( user_profile , request . client , <EOL> apply_markdown = True , narrow = narrow ) <EOL> user_has_messages = ( register_ret [ '<STR_LIT>' ] != - <NUM_LIT:1> ) <EOL> if not user_profile . last_reminder is None : <EOL> user_profile . last_reminder = None <EOL> user_profile . save ( update_fields = [ "<STR_LIT>" ] ) <EOL> needs_tutorial = settings . TUTORIAL_ENABLED and user_profile . tutorial_status != UserProfile . TUTORIAL_FINISHED <EOL> first_in_realm = realm_user_count ( user_profile . realm ) == <NUM_LIT:1> <EOL> prompt_for_invites = first_in_realm and not PreregistrationUser . objects . filter ( referred_by = user_profile ) . count ( ) <EOL> if user_profile . pointer == - <NUM_LIT:1> and user_has_messages : <EOL> register_ret [ '<STR_LIT>' ] = register_ret [ '<STR_LIT>' ] <EOL> user_profile . last_pointer_updater = request . session . session_key <EOL> if user_profile . pointer == - <NUM_LIT:1> : <EOL> latest_read = None <EOL> else : <EOL> try : <EOL> latest_read = UserMessage . objects . get ( user_profile = user_profile , <EOL> message__id = user_profile . pointer ) <EOL> except UserMessage . DoesNotExist : <EOL> logging . warning ( "<STR_LIT>" % ( user_profile . email , user_profile . pointer ) ) <EOL> latest_read = None <EOL> desktop_notifications_enabled = user_profile . enable_desktop_notifications <EOL> if narrow_stream is not None : <EOL> desktop_notifications_enabled = False <EOL> if user_profile . realm . notifications_stream : <EOL> notifications_stream = user_profile . realm . notifications_stream . name <EOL> else : <EOL> notifications_stream = "<STR_LIT>" <EOL> page_params = dict ( <EOL> voyager = settings . VOYAGER , <EOL> debug_mode = settings . DEBUG , <EOL> test_suite = settings . TEST_SUITE , <EOL> poll_timeout = settings . POLL_TIMEOUT , <EOL> login_page = settings . HOME_NOT_LOGGED_IN , <EOL> maxfilesize = settings . MAX_FILE_UPLOAD_SIZE , <EOL> password_auth_enabled = password_auth_enabled ( user_profile . realm ) , <EOL> have_initial_messages = user_has_messages , <EOL> subbed_info = register_ret [ '<STR_LIT>' ] , <EOL> unsubbed_info = register_ret [ '<STR_LIT>' ] , <EOL> email_dict = register_ret [ '<STR_LIT>' ] , <EOL> people_list = register_ret [ '<STR_LIT>' ] , <EOL> bot_list = register_ret [ '<STR_LIT>' ] , <EOL> initial_pointer = register_ret [ '<STR_LIT>' ] , <EOL> initial_presences = register_ret [ '<STR_LIT>' ] , <EOL> initial_servertime = time . time ( ) , <EOL> fullname = user_profile . full_name , <EOL> email = user_profile . email , <EOL> domain = user_profile . realm . domain , <EOL> realm_name = register_ret [ '<STR_LIT>' ] , <EOL> realm_invite_required = register_ret [ '<STR_LIT>' ] , <EOL> realm_invite_by_admins_only = register_ret [ '<STR_LIT>' ] , <EOL> realm_restricted_to_domain = register_ret [ '<STR_LIT>' ] , <EOL> enter_sends = user_profile . enter_sends , <EOL> left_side_userlist = register_ret [ '<STR_LIT>' ] , <EOL> referrals = register_ret [ '<STR_LIT>' ] , <EOL> realm_emoji = register_ret [ '<STR_LIT>' ] , <EOL> needs_tutorial = needs_tutorial , <EOL> first_in_realm = first_in_realm , <EOL> prompt_for_invites = prompt_for_invites , <EOL> notifications_stream = notifications_stream , <EOL> stream_desktop_notifications_enabled = <EOL> user_profile . enable_stream_desktop_notifications , <EOL> stream_sounds_enabled = user_profile . enable_stream_sounds , <EOL> desktop_notifications_enabled = desktop_notifications_enabled , <EOL> sounds_enabled = <EOL> user_profile . enable_sounds , <EOL> enable_offline_email_notifications = <EOL> user_profile . enable_offline_email_notifications , <EOL> enable_offline_push_notifications = <EOL> user_profile . enable_offline_push_notifications , <EOL> twenty_four_hour_time = register_ret [ '<STR_LIT>' ] , <EOL> enable_digest_emails = user_profile . enable_digest_emails , <EOL> event_queue_id = register_ret [ '<STR_LIT>' ] , <EOL> last_event_id = register_ret [ '<STR_LIT>' ] , <EOL> max_message_id = register_ret [ '<STR_LIT>' ] , <EOL> unread_count = approximate_unread_count ( user_profile ) , <EOL> furthest_read_time = sent_time_in_epoch_seconds ( latest_read ) , <EOL> staging = settings . ZULIP_COM_STAGING or settings . DEVELOPMENT , <EOL> alert_words = register_ret [ '<STR_LIT>' ] , <EOL> muted_topics = register_ret [ '<STR_LIT>' ] , <EOL> realm_filters = register_ret [ '<STR_LIT>' ] , <EOL> is_admin = user_profile . is_realm_admin , <EOL> can_create_streams = user_profile . can_create_streams ( ) , <EOL> name_changes_disabled = name_changes_disabled ( user_profile . realm ) , <EOL> has_mobile_devices = num_push_devices_for_user ( user_profile ) > <NUM_LIT:0> , <EOL> autoscroll_forever = user_profile . autoscroll_forever , <EOL> default_desktop_notifications = user_profile . default_desktop_notifications , <EOL> avatar_url = avatar_url ( user_profile ) , <EOL> mandatory_topics = user_profile . realm . mandatory_topics , <EOL> show_digest_email = user_profile . realm . show_digest_email , <EOL> ) <EOL> if narrow_stream is not None : <EOL> recipient = get_recipient ( Recipient . STREAM , narrow_stream . id ) <EOL> try : <EOL> initial_pointer = Message . objects . filter ( recipient = recipient ) . order_by ( '<STR_LIT:id>' ) . reverse ( ) [ <NUM_LIT:0> ] . id <EOL> except IndexError : <EOL> initial_pointer = - <NUM_LIT:1> <EOL> page_params [ "<STR_LIT>" ] = narrow_stream . name <EOL> if narrow_topic is not None : <EOL> page_params [ "<STR_LIT>" ] = narrow_topic <EOL> page_params [ "<STR_LIT>" ] = [ dict ( operator = term [ <NUM_LIT:0> ] , operand = term [ <NUM_LIT:1> ] ) for term in narrow ] <EOL> page_params [ "<STR_LIT>" ] = initial_pointer <EOL> page_params [ "<STR_LIT>" ] = initial_pointer <EOL> page_params [ "<STR_LIT>" ] = ( initial_pointer != - <NUM_LIT:1> ) <EOL> statsd . incr ( '<STR_LIT>' ) <EOL> show_invites = True <EOL> if user_profile . realm . invite_by_admins_only and not user_profile . is_realm_admin : <EOL> show_invites = False <EOL> product_name = "<STR_LIT>" <EOL> page_params [ '<STR_LIT>' ] = product_name <EOL> request . _log_data [ '<STR_LIT>' ] = "<STR_LIT>" % ( register_ret [ "<STR_LIT>" ] , ) <EOL> response = render_to_response ( '<STR_LIT>' , <EOL> { '<STR_LIT>' : user_profile , <EOL> '<STR_LIT>' : simplejson . encoder . JSONEncoderForHTML ( ) . encode ( page_params ) , <EOL> '<STR_LIT>' : is_buggy_ua ( request . META . get ( "<STR_LIT>" , "<STR_LIT>" ) ) , <EOL> '<STR_LIT>' : avatar_url ( user_profile ) , <EOL> '<STR_LIT>' : <EOL> settings . DEBUG and ( '<STR_LIT>' in request . GET ) , <EOL> '<STR_LIT>' : settings . PIPELINE , <EOL> '<STR_LIT>' : show_invites , <EOL> '<STR_LIT>' : user_profile . is_realm_admin , <EOL> '<STR_LIT>' : user_profile . realm . domain == "<STR_LIT>" , <EOL> '<STR_LIT>' : settings . ENABLE_FEEDBACK , <EOL> '<STR_LIT>' : narrow_stream is not None , <EOL> '<STR_LIT>' : product_name <EOL> } , <EOL> context_instance = RequestContext ( request ) ) <EOL> patch_cache_control ( response , no_cache = True , no_store = True , must_revalidate = True ) <EOL> return response <EOL> @ zulip_login_required <EOL> def desktop_home ( request ) : <EOL> return HttpResponseRedirect ( reverse ( '<STR_LIT>' ) ) <EOL> def is_buggy_ua ( agent ) : <EOL> """<STR_LIT>""" <EOL> return ( "<STR_LIT>" in agent or "<STR_LIT>" in agent or "<STR_LIT>" in agent ) and not "<STR_LIT>" in agent <EOL> def get_pointer_backend ( request , user_profile ) : <EOL> return json_success ( { '<STR_LIT>' : user_profile . pointer } ) <EOL> @ has_request_variables <EOL> def update_pointer_backend ( request , user_profile , <EOL> pointer = REQ ( converter = to_non_negative_int ) ) : <EOL> if pointer <= user_profile . pointer : <EOL> return json_success ( ) <EOL> try : <EOL> UserMessage . objects . get ( <EOL> user_profile = user_profile , <EOL> message__id = pointer <EOL> ) <EOL> except UserMessage . DoesNotExist : <EOL> raise JsonableError ( "<STR_LIT>" ) <EOL> request . _log_data [ "<STR_LIT>" ] = "<STR_LIT>" % ( pointer , ) <EOL> update_flags = ( request . client . name . lower ( ) in [ '<STR_LIT>' , "<STR_LIT>" ] ) <EOL> do_update_pointer ( user_profile , pointer , update_flags = update_flags ) <EOL> return json_success ( ) <EOL> def generate_client_id ( ) : <EOL> return generate_random_token ( <NUM_LIT:32> ) <EOL> @ require_realm_admin <EOL> def export ( request , user_profile ) : <EOL> if ( Message . objects . filter ( sender__realm = user_profile . realm ) . count ( ) > <NUM_LIT> or <EOL> UserMessage . objects . filter ( user_profile__realm = user_profile . realm ) . count ( ) > <NUM_LIT> ) : <EOL> return json_error ( "<STR_LIT>" ) <EOL> response = { } <EOL> response [ '<STR_LIT>' ] = [ model_to_dict ( x ) <EOL> for x in Realm . objects . select_related ( ) . filter ( id = user_profile . realm . id ) ] <EOL> response [ '<STR_LIT>' ] = [ model_to_dict ( x , exclude = [ "<STR_LIT:password>" , "<STR_LIT>" ] ) <EOL> for x in UserProfile . objects . select_related ( ) . filter ( realm = user_profile . realm ) ] <EOL> userprofile_ids = set ( userprofile [ "<STR_LIT:id>" ] for userprofile in response [ '<STR_LIT>' ] ) <EOL> response [ '<STR_LIT>' ] = [ model_to_dict ( x , exclude = [ "<STR_LIT>" ] ) <EOL> for x in Stream . objects . select_related ( ) . filter ( realm = user_profile . realm , invite_only = False ) ] <EOL> stream_ids = set ( x [ "<STR_LIT:id>" ] for x in response [ '<STR_LIT>' ] ) <EOL> response [ '<STR_LIT>' ] = [ model_to_dict ( x ) for x in UserMessage . objects . select_related ( ) <EOL> if x . user_profile_id in userprofile_ids ] <EOL> user_recipients = [ model_to_dict ( x ) <EOL> for x in Recipient . objects . select_related ( ) . filter ( type = <NUM_LIT:1> ) <EOL> if x . type_id in userprofile_ids ] <EOL> stream_recipients = [ model_to_dict ( x ) <EOL> for x in Recipient . objects . select_related ( ) . filter ( type = <NUM_LIT:2> ) <EOL> if x . type_id in stream_ids ] <EOL> stream_recipient_ids = set ( x [ "<STR_LIT:id>" ] for x in stream_recipients ) <EOL> response [ '<STR_LIT>' ] = [ model_to_dict ( x ) for x in Subscription . objects . select_related ( ) <EOL> if x . user_profile_id in userprofile_ids <EOL> and x . recipient_id in stream_recipient_ids ] <EOL> subscription_recipient_ids = set ( x [ "<STR_LIT>" ] for x in response [ '<STR_LIT>' ] ) <EOL> huddle_recipients = [ model_to_dict ( r ) <EOL> for r in Recipient . objects . select_related ( ) . filter ( type = <NUM_LIT:3> ) <EOL> if r . type_id in subscription_recipient_ids ] <EOL> huddle_ids = set ( x [ "<STR_LIT>" ] for x in huddle_recipients ) <EOL> response [ "<STR_LIT>" ] = user_recipients + stream_recipients + huddle_recipients <EOL> response [ '<STR_LIT>' ] = [ model_to_dict ( h ) <EOL> for h in Huddle . objects . select_related ( ) <EOL> if h . id in huddle_ids ] <EOL> recipient_ids = set ( x [ "<STR_LIT:id>" ] for x in response [ '<STR_LIT>' ] ) <EOL> response [ "<STR_LIT>" ] = [ model_to_dict ( m ) for m in Message . objects . select_related ( ) <EOL> if m . recipient_id in recipient_ids <EOL> and m . sender_id in userprofile_ids ] <EOL> for ( table , model ) in [ ( "<STR_LIT>" , DefaultStream ) , <EOL> ( "<STR_LIT>" , RealmEmoji ) , <EOL> ( "<STR_LIT>" , RealmAlias ) , <EOL> ( "<STR_LIT>" , RealmFilter ) ] : <EOL> response [ "<STR_LIT>" + table ] = [ model_to_dict ( x ) for x in <EOL> model . objects . select_related ( ) . filter ( realm_id = user_profile . realm . id ) ] <EOL> return json_success ( response ) <EOL> def get_profile_backend ( request , user_profile ) : <EOL> result = dict ( pointer = user_profile . pointer , <EOL> client_id = generate_client_id ( ) , <EOL> max_message_id = - <NUM_LIT:1> ) <EOL> messages = Message . objects . filter ( usermessage__user_profile = user_profile ) . order_by ( '<STR_LIT>' ) [ : <NUM_LIT:1> ] <EOL> if messages : <EOL> result [ '<STR_LIT>' ] = messages [ <NUM_LIT:0> ] . id <EOL> return json_success ( result ) <EOL> @ require_realm_admin <EOL> @ has_request_variables <EOL> def update_realm ( request , user_profile , name = REQ ( validator = check_string , default = None ) , <EOL> restricted_to_domain = REQ ( validator = check_bool , default = None ) , <EOL> invite_required = REQ ( validator = check_bool , default = None ) , <EOL> invite_by_admins_only = REQ ( validator = check_bool , default = None ) ) : <EOL> realm = user_profile . realm <EOL> data = { } <EOL> if name is not None and realm . name != name : <EOL> do_set_realm_name ( realm , name ) <EOL> data [ '<STR_LIT:name>' ] = '<STR_LIT>' <EOL> if restricted_to_domain is not None and realm . restricted_to_domain != restricted_to_domain : <EOL> do_set_realm_restricted_to_domain ( realm , restricted_to_domain ) <EOL> data [ '<STR_LIT>' ] = restricted_to_domain <EOL> if invite_required is not None and realm . invite_required != invite_required : <EOL> do_set_realm_invite_required ( realm , invite_required ) <EOL> data [ '<STR_LIT>' ] = invite_required <EOL> if invite_by_admins_only is not None and realm . invite_by_admins_only != invite_by_admins_only : <EOL> do_set_realm_invite_by_admins_only ( realm , invite_by_admins_only ) <EOL> data [ '<STR_LIT>' ] = invite_by_admins_only <EOL> return json_success ( data ) <EOL> @ authenticated_json_post_view <EOL> @ has_request_variables <EOL> def json_upload_file ( request , user_profile ) : <EOL> if len ( request . FILES ) == <NUM_LIT:0> : <EOL> return json_error ( "<STR_LIT>" ) <EOL> if len ( request . FILES ) != <NUM_LIT:1> : <EOL> return json_error ( "<STR_LIT>" ) <EOL> user_file = list ( request . FILES . values ( ) ) [ <NUM_LIT:0> ] <EOL> if ( ( settings . MAX_FILE_UPLOAD_SIZE * <NUM_LIT> * <NUM_LIT> ) < user_file . _get_size ( ) ) : <EOL> return json_error ( "<STR_LIT>" ) <EOL> uri = upload_message_image_through_web_client ( request , user_file , user_profile ) <EOL> return json_success ( { '<STR_LIT>' : uri } ) <EOL> @ zulip_login_required <EOL> @ has_request_variables <EOL> def get_uploaded_file ( request , realm_id , filename , <EOL> redir = REQ ( validator = check_bool , default = True ) ) : <EOL> if settings . LOCAL_UPLOADS_DIR is not None : <EOL> return HttpResponseForbidden ( ) <EOL> user_profile = request . user <EOL> url_path = "<STR_LIT>" % ( realm_id , filename ) <EOL> if realm_id == "<STR_LIT>" : <EOL> realm_id = get_realm_for_filename ( url_path ) <EOL> if realm_id is None : <EOL> return json_error ( "<STR_LIT>" , status = <NUM_LIT> ) <EOL> if user_profile . realm . id == int ( realm_id ) or user_profile . realm . domain == '<STR_LIT>' : <EOL> uri = get_signed_upload_url ( url_path ) <EOL> if redir : <EOL> return redirect ( uri ) <EOL> else : <EOL> return json_success ( { '<STR_LIT>' : uri } ) <EOL> else : <EOL> return HttpResponseForbidden ( ) <EOL> @ csrf_exempt <EOL> @ require_post <EOL> @ has_request_variables <EOL> def api_fetch_api_key ( request , username = REQ , password = REQ ) : <EOL> return_data = { } <EOL> if username == "<STR_LIT>" : <EOL> user_profile = authenticate ( google_oauth2_token = password , return_data = return_data ) <EOL> else : <EOL> user_profile = authenticate ( username = username , password = password ) <EOL> if user_profile is None : <EOL> if return_data . get ( "<STR_LIT>" ) == True : <EOL> return json_error ( "<STR_LIT>" , data = { "<STR_LIT>" : "<STR_LIT>" } , status = <NUM_LIT> ) <EOL> return json_error ( "<STR_LIT>" , data = { "<STR_LIT>" : "<STR_LIT>" } , status = <NUM_LIT> ) <EOL> if not user_profile . is_active : <EOL> return json_error ( "<STR_LIT>" , data = { "<STR_LIT>" : "<STR_LIT>" } , status = <NUM_LIT> ) <EOL> return json_success ( { "<STR_LIT>" : user_profile . api_key , "<STR_LIT:email>" : user_profile . email } ) <EOL> @ authenticated_json_post_view <EOL> @ has_request_variables <EOL> def json_fetch_api_key ( request , user_profile , password = REQ ( default = '<STR_LIT>' ) ) : <EOL> if password_auth_enabled ( user_profile . realm ) and not user_profile . check_password ( password ) : <EOL> return json_error ( "<STR_LIT>" ) <EOL> return json_success ( { "<STR_LIT>" : user_profile . api_key } ) <EOL> @ csrf_exempt <EOL> def api_fetch_google_client_id ( request ) : <EOL> if not settings . GOOGLE_CLIENT_ID : <EOL> return json_error ( "<STR_LIT>" , status = <NUM_LIT> ) <EOL> return json_success ( { "<STR_LIT>" : settings . GOOGLE_CLIENT_ID } ) <EOL> def get_status_list ( requesting_user_profile ) : <EOL> return { '<STR_LIT>' : get_status_dict ( requesting_user_profile ) , <EOL> '<STR_LIT>' : time . time ( ) } <EOL> @ has_request_variables <EOL> def update_active_status_backend ( request , user_profile , status = REQ , <EOL> new_user_input = REQ ( validator = check_bool , default = False ) ) : <EOL> status_val = UserPresence . status_from_string ( status ) <EOL> if status_val is None : <EOL> raise JsonableError ( "<STR_LIT>" % ( status , ) ) <EOL> else : <EOL> update_user_presence ( user_profile , request . client , now ( ) , status_val , <EOL> new_user_input ) <EOL> ret = get_status_list ( user_profile ) <EOL> if user_profile . realm . domain == "<STR_LIT>" : <EOL> try : <EOL> activity = UserActivity . objects . get ( user_profile = user_profile , <EOL> query = "<STR_LIT>" , <EOL> client__name = "<STR_LIT>" ) <EOL> ret [ '<STR_LIT>' ] = ( activity . last_visit . replace ( tzinfo = None ) > <EOL> datetime . datetime . utcnow ( ) - datetime . timedelta ( minutes = <NUM_LIT:5> ) ) <EOL> except UserActivity . DoesNotExist : <EOL> ret [ '<STR_LIT>' ] = False <EOL> return json_success ( ret ) <EOL> @ authenticated_json_post_view <EOL> def json_get_active_statuses ( request , user_profile ) : <EOL> return json_success ( get_status_list ( user_profile ) ) <EOL> @ has_request_variables <EOL> def api_events_register ( request , user_profile , <EOL> apply_markdown = REQ ( default = False , validator = check_bool ) , <EOL> all_public_streams = REQ ( default = None , validator = check_bool ) ) : <EOL> return events_register_backend ( request , user_profile , <EOL> apply_markdown = apply_markdown , <EOL> all_public_streams = all_public_streams ) <EOL> def _default_all_public_streams ( user_profile , all_public_streams ) : <EOL> if all_public_streams is not None : <EOL> return all_public_streams <EOL> else : <EOL> return user_profile . default_all_public_streams <EOL> def _default_narrow ( user_profile , narrow ) : <EOL> default_stream = user_profile . default_events_register_stream <EOL> if not narrow and user_profile . default_events_register_stream is not None : <EOL> narrow = [ ( '<STR_LIT>' , default_stream . name ) ] <EOL> return narrow <EOL> @ has_request_variables <EOL> def events_register_backend ( request , user_profile , apply_markdown = True , <EOL> all_public_streams = None , <EOL> event_types = REQ ( validator = check_list ( check_string ) , default = None ) , <EOL> narrow = REQ ( validator = check_list ( check_list ( check_string , length = <NUM_LIT:2> ) ) , default = [ ] ) , <EOL> queue_lifespan_secs = REQ ( converter = int , default = <NUM_LIT:0> ) ) : <EOL> all_public_streams = _default_all_public_streams ( user_profile , all_public_streams ) <EOL> narrow = _default_narrow ( user_profile , narrow ) <EOL> ret = do_events_register ( user_profile , request . client , apply_markdown , <EOL> event_types , queue_lifespan_secs , all_public_streams , <EOL> narrow = narrow ) <EOL> return json_success ( ret ) <EOL> @ authenticated_json_post_view <EOL> @ has_request_variables <EOL> def json_refer_friend ( request , user_profile , email = REQ ) : <EOL> if not email : <EOL> return json_error ( "<STR_LIT>" ) <EOL> if user_profile . invites_granted - user_profile . invites_used <= <NUM_LIT:0> : <EOL> return json_error ( "<STR_LIT>" ) <EOL> do_refer_friend ( user_profile , email ) ; <EOL> return json_success ( ) <EOL> @ authenticated_json_post_view <EOL> @ has_request_variables <EOL> def json_set_muted_topics ( request , user_profile , <EOL> muted_topics = REQ ( validator = check_list ( check_list ( check_string , length = <NUM_LIT:2> ) ) , default = [ ] ) ) : <EOL> do_set_muted_topics ( user_profile , muted_topics ) <EOL> return json_success ( ) <EOL> def add_push_device_token ( request , user_profile , token , kind , ios_app_id = None ) : <EOL> if token == '<STR_LIT>' or len ( token ) > <NUM_LIT> : <EOL> return json_error ( '<STR_LIT>' ) <EOL> PushDeviceToken . objects . filter ( token = token ) . delete ( ) <EOL> token , created = PushDeviceToken . objects . get_or_create ( user = user_profile , <EOL> token = token , <EOL> kind = kind , <EOL> ios_app_id = ios_app_id ) <EOL> if not created : <EOL> token . last_updated = now ( ) <EOL> token . save ( update_fields = [ '<STR_LIT>' ] ) <EOL> return json_success ( ) <EOL> @ has_request_variables <EOL> def add_apns_device_token ( request , user_profile , token = REQ , appid = REQ ( default = settings . ZULIP_IOS_APP_ID ) ) : <EOL> return add_push_device_token ( request , user_profile , token , PushDeviceToken . APNS , ios_app_id = appid ) <EOL> @ has_request_variables <EOL> def add_android_reg_id ( request , user_profile , token = REQ ) : <EOL> return add_push_device_token ( request , user_profile , token , PushDeviceToken . GCM ) <EOL> def remove_push_device_token ( request , user_profile , token , kind ) : <EOL> if token == '<STR_LIT>' or len ( token ) > <NUM_LIT> : <EOL> return json_error ( '<STR_LIT>' ) <EOL> try : <EOL> token = PushDeviceToken . objects . get ( token = token , kind = kind ) <EOL> token . delete ( ) <EOL> except PushDeviceToken . DoesNotExist : <EOL> return json_error ( "<STR_LIT>" ) <EOL> return json_success ( ) <EOL> @ has_request_variables <EOL> def remove_apns_device_token ( request , user_profile , token = REQ ) : <EOL> return remove_push_device_token ( request , user_profile , token , PushDeviceToken . APNS ) <EOL> @ has_request_variables <EOL> def remove_android_reg_id ( request , user_profile , token = REQ ) : <EOL> return remove_push_device_token ( request , user_profile , token , PushDeviceToken . GCM ) <EOL> def generate_204 ( request ) : <EOL> return HttpResponse ( content = None , status = <NUM_LIT> ) <EOL> def process_unsubscribe ( token , type , unsubscribe_function ) : <EOL> try : <EOL> confirmation = Confirmation . objects . get ( confirmation_key = token ) <EOL> except Confirmation . DoesNotExist : <EOL> return render_to_response ( '<STR_LIT>' ) <EOL> user_profile = confirmation . content_object <EOL> unsubscribe_function ( user_profile ) <EOL> return render_to_response ( '<STR_LIT>' , <EOL> { "<STR_LIT>" : type , <EOL> "<STR_LIT>" : settings . EXTERNAL_HOST } ) <EOL> def do_missedmessage_unsubscribe ( user_profile ) : <EOL> do_change_enable_offline_email_notifications ( user_profile , False ) <EOL> def do_welcome_unsubscribe ( user_profile ) : <EOL> clear_followup_emails_queue ( user_profile . email ) <EOL> def do_digest_unsubscribe ( user_profile ) : <EOL> do_change_enable_digest_emails ( user_profile , False ) <EOL> email_unsubscribers = { <EOL> "<STR_LIT>" : ( "<STR_LIT>" , do_missedmessage_unsubscribe ) , <EOL> "<STR_LIT>" : ( "<STR_LIT>" , do_welcome_unsubscribe ) , <EOL> "<STR_LIT>" : ( "<STR_LIT>" , do_digest_unsubscribe ) <EOL> } <EOL> def email_unsubscribe ( request , type , token ) : <EOL> if type in email_unsubscribers : <EOL> display_name , unsubscribe_function = email_unsubscribers [ type ] <EOL> return process_unsubscribe ( token , display_name , unsubscribe_function ) <EOL> return render_to_response ( '<STR_LIT>' , { } , <EOL> context_instance = RequestContext ( request ) ) </s>
