<s> from datetime import datetime <EOL> from django . conf import settings <EOL> from django . contrib import messages <EOL> from django . contrib . auth import authenticate , login <EOL> from django . core . urlresolvers import reverse <EOL> from django . db import transaction <EOL> from django . http import HttpResponseRedirect , Http404 <EOL> from django . shortcuts import redirect , render <EOL> from django . utils . decorators import method_decorator <EOL> from django . utils . translation import ugettext as _ <EOL> import sys <EOL> from django . views . generic . base import TemplateView <EOL> from corehq . apps . analytics . tasks import ( <EOL> track_workflow , <EOL> track_confirmed_account_on_hubspot , <EOL> track_clicked_signup_on_hubspot <EOL> ) <EOL> from corehq . apps . analytics . utils import get_meta <EOL> from corehq . apps . domain . decorators import login_required <EOL> from corehq . apps . domain . models import Domain <EOL> from corehq . apps . domain . exceptions import NameUnavailableException <EOL> from corehq . apps . registration . models import RegistrationRequest <EOL> from corehq . apps . registration . forms import NewWebUserRegistrationForm , DomainRegistrationForm <EOL> from corehq . apps . registration . utils import activate_new_user , send_new_request_update_email , request_new_domain , send_domain_registration_email <EOL> from corehq . apps . style . decorators import use_bootstrap3 <EOL> from corehq . apps . users . models import WebUser , CouchUser <EOL> from dimagi . utils . couch . resource_conflict import retry_resource <EOL> from dimagi . utils . decorators . memoized import memoized <EOL> from dimagi . utils . web import get_ip <EOL> from corehq . util . context_processors import get_per_domain_context <EOL> def get_domain_context ( ) : <EOL> return get_per_domain_context ( Domain ( ) ) <EOL> def registration_default ( request ) : <EOL> return redirect ( register_user ) <EOL> @ transaction . atomic <EOL> def register_user ( request ) : <EOL> prefilled_email = request . GET . get ( '<STR_LIT:e>' , '<STR_LIT>' ) <EOL> context = get_domain_context ( ) <EOL> if request . user . is_authenticated ( ) : <EOL> domains_for_user = Domain . active_for_user ( request . user ) <EOL> if len ( domains_for_user ) == <NUM_LIT:0> : <EOL> return redirect ( "<STR_LIT>" ) <EOL> else : <EOL> return redirect ( "<STR_LIT>" ) <EOL> else : <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = NewWebUserRegistrationForm ( request . POST ) <EOL> if form . is_valid ( ) : <EOL> activate_new_user ( form , ip = get_ip ( request ) ) <EOL> new_user = authenticate ( username = form . cleaned_data [ '<STR_LIT:email>' ] , <EOL> password = form . cleaned_data [ '<STR_LIT:password>' ] ) <EOL> track_workflow ( new_user . email , "<STR_LIT>" ) <EOL> login ( request , new_user ) <EOL> requested_domain = form . cleaned_data [ '<STR_LIT>' ] <EOL> if form . cleaned_data [ '<STR_LIT>' ] : <EOL> try : <EOL> requested_domain = request_new_domain ( <EOL> request , form , is_new_user = True ) <EOL> except NameUnavailableException : <EOL> context . update ( { <EOL> '<STR_LIT>' : { '<STR_LIT>' : _ ( '<STR_LIT>' ) } , <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : <NUM_LIT:1> <EOL> } ) <EOL> return render ( request , '<STR_LIT>' , context ) <EOL> context . update ( { <EOL> '<STR_LIT>' : requested_domain , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : { '<STR_LIT>' : _ ( '<STR_LIT>' ) } , <EOL> } ) <EOL> return render ( request , '<STR_LIT>' , context ) <EOL> context . update ( { '<STR_LIT>' : form . cleaned_data [ '<STR_LIT>' ] } ) <EOL> else : <EOL> form = NewWebUserRegistrationForm ( <EOL> initial = { '<STR_LIT:email>' : prefilled_email , '<STR_LIT>' : True } ) <EOL> context . update ( { '<STR_LIT>' : True } ) <EOL> meta = get_meta ( request ) <EOL> track_clicked_signup_on_hubspot ( prefilled_email , request . COOKIES , meta ) <EOL> context . update ( { <EOL> '<STR_LIT>' : form , <EOL> '<STR_LIT>' : { '<STR_LIT>' : _ ( '<STR_LIT>' ) } , <EOL> '<STR_LIT>' : settings . ENABLE_DRACONIAN_SECURITY_FEATURES , <EOL> '<STR_LIT>' : True , <EOL> } ) <EOL> return render ( request , '<STR_LIT>' , context ) <EOL> class RegisterDomainView ( TemplateView ) : <EOL> template_name = '<STR_LIT>' <EOL> @ method_decorator ( login_required ) <EOL> @ use_bootstrap3 <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> return super ( RegisterDomainView , self ) . dispatch ( request , * args , ** kwargs ) <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> if self . is_new_user : <EOL> pending_domains = Domain . active_for_user ( request . user , is_active = False ) <EOL> if len ( pending_domains ) > <NUM_LIT:0> : <EOL> context = get_domain_context ( ) <EOL> context . update ( { <EOL> '<STR_LIT>' : pending_domains [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' : { '<STR_LIT>' : _ ( '<STR_LIT>' ) } , <EOL> } ) <EOL> return render ( request , '<STR_LIT>' , context ) <EOL> return super ( RegisterDomainView , self ) . get ( request , * args , ** kwargs ) <EOL> @ property <EOL> @ memoized <EOL> def is_new_user ( self ) : <EOL> user = self . request . user <EOL> return not ( Domain . active_for_user ( user ) or user . is_superuser ) <EOL> @ transaction . atomic <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> referer_url = request . GET . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> nextpage = request . POST . get ( '<STR_LIT>' ) <EOL> form = DomainRegistrationForm ( request . POST ) <EOL> context = self . get_context_data ( form = form ) <EOL> if form . is_valid ( ) : <EOL> reqs_today = RegistrationRequest . get_requests_today ( ) <EOL> max_req = settings . DOMAIN_MAX_REGISTRATION_REQUESTS_PER_DAY <EOL> if reqs_today >= max_req : <EOL> context . update ( { <EOL> '<STR_LIT>' : { '<STR_LIT>' : _ ( '<STR_LIT>' ) } , <EOL> '<STR_LIT>' : _ ( <EOL> '<STR_LIT>' <EOL> ) % max_req , <EOL> '<STR_LIT>' : <NUM_LIT:1> <EOL> } ) <EOL> return render ( request , '<STR_LIT>' , context ) <EOL> try : <EOL> domain_name = request_new_domain ( <EOL> request , form , is_new_user = self . is_new_user ) <EOL> except NameUnavailableException : <EOL> context . update ( { <EOL> '<STR_LIT>' : { '<STR_LIT>' : _ ( '<STR_LIT>' ) } , <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : <NUM_LIT:1> <EOL> } ) <EOL> return render ( request , '<STR_LIT>' , context ) <EOL> if self . is_new_user : <EOL> context . update ( { <EOL> '<STR_LIT>' : domain_name , <EOL> '<STR_LIT>' : True , <EOL> '<STR_LIT>' : { '<STR_LIT>' : _ ( '<STR_LIT>' ) } , <EOL> } ) <EOL> return render ( request , '<STR_LIT>' , context ) <EOL> else : <EOL> if nextpage : <EOL> return HttpResponseRedirect ( nextpage ) <EOL> if referer_url : <EOL> return redirect ( referer_url ) <EOL> return HttpResponseRedirect ( reverse ( "<STR_LIT>" , args = [ domain_name ] ) ) <EOL> return self . render_to_response ( context ) <EOL> def get_context_data ( self , ** kwargs ) : <EOL> request = self . request <EOL> if ( not request . couch_user ) or request . couch_user . is_commcare_user ( ) : <EOL> raise Http404 ( ) <EOL> context = super ( RegisterDomainView , self ) . get_context_data ( ** kwargs ) <EOL> context . update ( get_domain_context ( ) ) <EOL> context . update ( { <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' ) or DomainRegistrationForm ( ) , <EOL> '<STR_LIT>' : self . is_new_user , <EOL> } ) <EOL> return context <EOL> @ transaction . atomic <EOL> @ login_required <EOL> def resend_confirmation ( request ) : <EOL> try : <EOL> dom_req = RegistrationRequest . get_request_for_username ( request . user . username ) <EOL> except Exception : <EOL> dom_req = None <EOL> if not dom_req : <EOL> inactive_domains_for_user = Domain . active_for_user ( request . user , is_active = False ) <EOL> if len ( inactive_domains_for_user ) > <NUM_LIT:0> : <EOL> for domain in inactive_domains_for_user : <EOL> domain . is_active = True <EOL> domain . save ( ) <EOL> return redirect ( '<STR_LIT>' ) <EOL> context = get_domain_context ( ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> try : <EOL> send_domain_registration_email ( dom_req . new_user_username , <EOL> dom_req . domain , dom_req . activation_guid , <EOL> request . user . get_full_name ( ) ) <EOL> except Exception : <EOL> context . update ( { <EOL> '<STR_LIT>' : { '<STR_LIT>' : _ ( '<STR_LIT>' ) } , <EOL> '<STR_LIT>' : _ ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : sys . exc_info ( ) , <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> } ) <EOL> return render ( request , '<STR_LIT>' , context ) <EOL> else : <EOL> context . update ( { <EOL> '<STR_LIT>' : dom_req . domain , <EOL> '<STR_LIT>' : { '<STR_LIT>' : ( '<STR_LIT>' ) } , <EOL> } ) <EOL> return render ( request , '<STR_LIT>' , <EOL> context ) <EOL> context . update ( { <EOL> '<STR_LIT>' : dom_req . domain , <EOL> '<STR_LIT>' : { '<STR_LIT>' : _ ( '<STR_LIT>' ) } , <EOL> } ) <EOL> return render ( request , '<STR_LIT>' , context ) <EOL> @ transaction . atomic <EOL> def confirm_domain ( request , guid = None ) : <EOL> error = None <EOL> if guid is None : <EOL> error = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> else : <EOL> req = RegistrationRequest . get_by_guid ( guid ) <EOL> if not req : <EOL> error = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) % guid <EOL> if error is not None : <EOL> context = { <EOL> '<STR_LIT>' : error , <EOL> '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' } , <EOL> } <EOL> return render ( request , '<STR_LIT>' , context ) <EOL> requested_domain = Domain . get_by_name ( req . domain ) <EOL> if requested_domain . is_active : <EOL> assert ( req . confirm_time is not None and req . confirm_ip is not None ) <EOL> messages . success ( request , '<STR_LIT>' <EOL> '<STR_LIT>' % req . new_user_username ) <EOL> return HttpResponseRedirect ( reverse ( "<STR_LIT>" , args = [ requested_domain ] ) ) <EOL> req . confirm_time = datetime . utcnow ( ) <EOL> req . confirm_ip = get_ip ( request ) <EOL> req . save ( ) <EOL> requested_domain . is_active = True <EOL> requested_domain . save ( ) <EOL> requesting_user = WebUser . get_by_username ( req . new_user_username ) <EOL> send_new_request_update_email ( requesting_user , get_ip ( request ) , requested_domain . name , is_confirming = True ) <EOL> messages . success ( request , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> % ( requesting_user . username ) ) <EOL> track_workflow ( requesting_user . email , "<STR_LIT>" ) <EOL> track_confirmed_account_on_hubspot . delay ( requesting_user ) <EOL> return HttpResponseRedirect ( reverse ( "<STR_LIT>" , args = [ requested_domain ] ) ) <EOL> @ retry_resource ( <NUM_LIT:3> ) <EOL> def eula_agreement ( request ) : <EOL> if request . method == '<STR_LIT:POST>' : <EOL> current_user = CouchUser . from_django_user ( request . user ) <EOL> current_user . eula . signed = True <EOL> current_user . eula . date = datetime . utcnow ( ) <EOL> current_user . eula . user_ip = get_ip ( request ) <EOL> current_user . save ( ) <EOL> return HttpResponseRedirect ( request . POST . get ( '<STR_LIT>' , '<STR_LIT:/>' ) ) </s>
