<s> import logging <EOL> from django . utils . translation import ugettext <EOL> import uuid <EOL> from datetime import datetime , date , timedelta <EOL> from django . template . loader import render_to_string <EOL> from corehq . apps . accounting . models import ( <EOL> SoftwarePlanEdition , DefaultProductPlan , BillingAccount , <EOL> BillingAccountType , Subscription , SubscriptionAdjustmentMethod , Currency , <EOL> SubscriptionType , PreOrPostPay <EOL> ) <EOL> from corehq . apps . registration . models import RegistrationRequest <EOL> from dimagi . utils . couch import CriticalSection <EOL> from dimagi . utils . name_to_url import name_to_url <EOL> from dimagi . utils . web import get_ip , get_url_base , get_site_domain <EOL> from django . conf import settings <EOL> from django . core . urlresolvers import reverse <EOL> from corehq . apps . domain . models import Domain <EOL> from corehq . apps . users . models import WebUser , CouchUser , UserRole <EOL> from corehq . apps . hqwebapp . tasks import send_html_email_async <EOL> from dimagi . utils . couch . database import get_safe_write_kwargs <EOL> from corehq . apps . hqwebapp . tasks import send_mail_async <EOL> from corehq . apps . analytics . tasks import track_created_new_project_space_on_hubspot <EOL> from corehq . apps . analytics . utils import get_meta <EOL> def activate_new_user ( form , is_domain_admin = True , domain = None , ip = None ) : <EOL> username = form . cleaned_data [ '<STR_LIT:email>' ] <EOL> password = form . cleaned_data [ '<STR_LIT:password>' ] <EOL> full_name = form . cleaned_data [ '<STR_LIT>' ] <EOL> now = datetime . utcnow ( ) <EOL> new_user = WebUser . create ( domain , username , password , is_admin = is_domain_admin ) <EOL> new_user . first_name = full_name [ <NUM_LIT:0> ] <EOL> new_user . last_name = full_name [ <NUM_LIT:1> ] <EOL> new_user . email = username <EOL> new_user . email_opt_out = False <EOL> new_user . subscribed_to_commcare_users = False <EOL> new_user . eula . signed = True <EOL> new_user . eula . date = now <EOL> new_user . eula . type = '<STR_LIT>' <EOL> if ip : <EOL> new_user . eula . user_ip = ip <EOL> new_user . is_staff = False <EOL> new_user . is_active = True <EOL> new_user . is_superuser = False <EOL> new_user . last_login = now <EOL> new_user . date_joined = now <EOL> new_user . last_password_set = now <EOL> new_user . save ( ) <EOL> return new_user <EOL> def request_new_domain ( request , form , is_new_user = True ) : <EOL> now = datetime . utcnow ( ) <EOL> current_user = CouchUser . from_django_user ( request . user ) <EOL> dom_req = RegistrationRequest ( ) <EOL> if is_new_user : <EOL> dom_req . request_time = now <EOL> dom_req . request_ip = get_ip ( request ) <EOL> dom_req . activation_guid = uuid . uuid1 ( ) . hex <EOL> name = name_to_url ( form . cleaned_data [ '<STR_LIT>' ] , "<STR_LIT>" ) <EOL> with CriticalSection ( [ '<STR_LIT>' . format ( name ) ] ) : <EOL> name = Domain . generate_name ( name ) <EOL> new_domain = Domain ( <EOL> name = name , <EOL> hr_name = form . cleaned_data [ '<STR_LIT>' ] , <EOL> is_active = False , <EOL> date_created = datetime . utcnow ( ) , <EOL> creating_user = current_user . username , <EOL> secure_submissions = True , <EOL> ) <EOL> if form . cleaned_data . get ( '<STR_LIT>' ) : <EOL> new_domain . default_timezone = form . cleaned_data [ '<STR_LIT>' ] <EOL> if not is_new_user : <EOL> new_domain . is_active = True <EOL> new_domain . save ( ** get_safe_write_kwargs ( ) ) <EOL> if not new_domain . name : <EOL> new_domain . name = new_domain . _id <EOL> new_domain . save ( ) <EOL> if is_new_user : <EOL> create_30_day_advanced_trial ( new_domain ) <EOL> UserRole . init_domain_with_presets ( new_domain . name ) <EOL> dom_req . domain = new_domain . name <EOL> if request . user . is_authenticated ( ) : <EOL> if not current_user : <EOL> current_user = WebUser ( ) <EOL> current_user . sync_from_django_user ( request . user ) <EOL> current_user . save ( ) <EOL> current_user . add_domain_membership ( new_domain . name , is_admin = True ) <EOL> current_user . save ( ) <EOL> dom_req . requesting_user_username = request . user . username <EOL> dom_req . new_user_username = request . user . username <EOL> if is_new_user : <EOL> dom_req . save ( ) <EOL> send_domain_registration_email ( request . user . email , <EOL> dom_req . domain , <EOL> dom_req . activation_guid , <EOL> request . user . get_full_name ( ) ) <EOL> else : <EOL> send_global_domain_registration_email ( request . user , new_domain . name ) <EOL> send_new_request_update_email ( request . user , get_ip ( request ) , new_domain . name , is_new_user = is_new_user ) <EOL> meta = get_meta ( request ) <EOL> track_created_new_project_space_on_hubspot . delay ( current_user , request . COOKIES , meta ) <EOL> return new_domain . name <EOL> REGISTRATION_EMAIL_BODY_HTML = u"""<STR_LIT>""" <EOL> REGISTRATION_EMAIL_BODY_PLAINTEXT = u"""<STR_LIT>""" <EOL> WIKI_LINK = '<STR_LIT>' <EOL> USERS_LINK = '<STR_LIT>' <EOL> PRICING_LINK = '<STR_LIT>' <EOL> def send_domain_registration_email ( recipient , domain_name , guid , full_name ) : <EOL> DNS_name = get_site_domain ( ) <EOL> registration_link = '<STR_LIT>' + DNS_name + reverse ( '<STR_LIT>' ) + guid + '<STR_LIT:/>' <EOL> params = { <EOL> "<STR_LIT>" : domain_name , <EOL> "<STR_LIT>" : PRICING_LINK , <EOL> "<STR_LIT>" : registration_link , <EOL> "<STR_LIT>" : full_name , <EOL> "<STR_LIT>" : USERS_LINK , <EOL> "<STR_LIT>" : WIKI_LINK , <EOL> '<STR_LIT>' : '<STR_LIT>' if settings . STATIC_CDN else '<STR_LIT>' + DNS_name , <EOL> } <EOL> message_plaintext = render_to_string ( '<STR_LIT>' , params ) <EOL> message_html = render_to_string ( '<STR_LIT>' , params ) <EOL> subject = ugettext ( '<STR_LIT>' ) <EOL> try : <EOL> send_html_email_async . delay ( subject , recipient , message_html , <EOL> text_content = message_plaintext , <EOL> email_from = settings . DEFAULT_FROM_EMAIL , ga_track = True ) <EOL> except Exception : <EOL> logging . warning ( "<STR_LIT>" % message_plaintext ) <EOL> def send_global_domain_registration_email ( requesting_user , domain_name ) : <EOL> DNS_name = get_site_domain ( ) <EOL> registration_link = '<STR_LIT>' + DNS_name + reverse ( "<STR_LIT>" , args = [ domain_name ] ) <EOL> message_plaintext = u"""<STR_LIT>""" + REGISTRATION_EMAIL_BODY_PLAINTEXT <EOL> message_html = u"""<STR_LIT>""" + REGISTRATION_EMAIL_BODY_HTML <EOL> params = { <EOL> "<STR_LIT:name>" : requesting_user . first_name , <EOL> "<STR_LIT>" : domain_name , <EOL> "<STR_LIT>" : PRICING_LINK , <EOL> "<STR_LIT>" : registration_link , <EOL> "<STR_LIT:username>" : requesting_user . email , <EOL> "<STR_LIT>" : USERS_LINK , <EOL> "<STR_LIT>" : WIKI_LINK , <EOL> } <EOL> message_plaintext = message_plaintext . format ( ** params ) <EOL> message_html = message_html . format ( ** params ) <EOL> subject = '<STR_LIT>' . format ( ** locals ( ) ) <EOL> try : <EOL> send_html_email_async . delay ( subject , requesting_user . email , message_html , <EOL> text_content = message_plaintext , <EOL> email_from = settings . DEFAULT_FROM_EMAIL ) <EOL> except Exception : <EOL> logging . warning ( "<STR_LIT>" % message_plaintext ) <EOL> def send_new_request_update_email ( user , requesting_ip , entity_name , entity_type = "<STR_LIT>" , is_new_user = False , is_confirming = False ) : <EOL> entity_texts = { "<STR_LIT>" : [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> "<STR_LIT>" : [ "<STR_LIT>" , "<STR_LIT>" ] } [ entity_type ] <EOL> if is_confirming : <EOL> message = "<STR_LIT>" % ( entity_texts [ <NUM_LIT:0> ] , entity_name ) <EOL> elif is_new_user : <EOL> message = "<STR_LIT>" % ( entity_texts [ <NUM_LIT:0> ] , entity_name ) <EOL> else : <EOL> message = "<STR_LIT>" % ( entity_texts [ <NUM_LIT:0> ] , entity_name ) <EOL> message = u"""<STR_LIT>""" % ( <EOL> message , <EOL> user . username , <EOL> requesting_ip , <EOL> entity_texts [ <NUM_LIT:0> ] , <EOL> get_url_base ( ) + "<STR_LIT>" % ( "<STR_LIT:o>" if entity_type == "<STR_LIT>" else "<STR_LIT:a>" , entity_name ) ) <EOL> try : <EOL> recipients = settings . NEW_DOMAIN_RECIPIENTS <EOL> send_mail_async . delay ( <EOL> u"<STR_LIT>" % ( entity_texts [ <NUM_LIT:0> ] , entity_name ) , <EOL> message , settings . SERVER_EMAIL , recipients <EOL> ) <EOL> except Exception : <EOL> logging . warning ( "<STR_LIT>" % message ) <EOL> def create_30_day_advanced_trial ( domain_obj ) : <EOL> advanced_plan_version = DefaultProductPlan . get_default_plan_by_domain ( <EOL> domain_obj , edition = SoftwarePlanEdition . ADVANCED , is_trial = True <EOL> ) <EOL> expiration_date = date . today ( ) + timedelta ( days = <NUM_LIT:30> ) <EOL> trial_account = BillingAccount . objects . get_or_create ( <EOL> name = "<STR_LIT>" % domain_obj . name , <EOL> currency = Currency . get_default ( ) , <EOL> created_by_domain = domain_obj . name , <EOL> account_type = BillingAccountType . TRIAL , <EOL> pre_or_post_pay = PreOrPostPay . POSTPAY , <EOL> ) [ <NUM_LIT:0> ] <EOL> trial_subscription = Subscription . new_domain_subscription ( <EOL> trial_account , domain_obj . name , advanced_plan_version , <EOL> date_end = expiration_date , <EOL> adjustment_method = SubscriptionAdjustmentMethod . TRIAL , <EOL> is_trial = True , <EOL> service_type = SubscriptionType . TRIAL <EOL> ) <EOL> trial_subscription . is_active = True <EOL> trial_subscription . save ( ) </s>
