def setup_cm_account(password=''): # check if already setup client_id = getattr(settings, 'CAMPAIGNMONITOR_API_CLIENT_ID', '') if not client_id or client_id == '[CAMPAIGNMONITOR_API_CLIENT_ID]': cs = CreateSend() company_name = get_setting('site', 'global', 'sitedisplayname') #contact_name = get_setting('site', 'global', 'admincontactname') #contact_email = get_setting('site', 'global', 'admincontactemail') contact_name = "Schipul Client" contact_email = get_contact_email() if not contact_email: raise ValueError("Invalid Email address.") #timezone = get_setting('site', 'global', 'defaulttimezone') # country - must exist on campaign monitor country = 'United States of America' # timezone - must use the format specified by campaign monitor timezone = "(GMT-06:00) Central Time (US & Canada)" api_key = getattr(settings, 'CAMPAIGNMONITOR_API_KEY', None) CreateSend.api_key = api_key # check if this company already exists on campaign monitor # if it does, raise an error clients = cs.clients() cm_client_id = None for cl in clients: if cl.Name == company_name: #cm_client_id = cl.ClientID #break raise ValueError('Company name "%s" already exists on campaign monitor.' % company_name) if not cm_client_id: # 1) Create an account cl = Client() cm_client_id = cl.create(company_name, contact_name, contact_email, timezone, country) cl = Client(cm_client_id) # 2) Set access with username and password # access level = 63 full access username = company_name.replace(' ', '') if not password: # generate a random password with 6 in length allowed_chars='abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789()#$%' password = User.objects.make_random_password(length=6, allowed_chars=allowed_chars) cl.set_access(username, password, 63) # 3) Set up billing - client will pay monthly #cl.set_payg_billing('USD', True, True, 0) cl.set_monthly_billing('USD', True, 0) # send an email to [email protected] now = datetime.now() now_str = now.strftime('%m/%d/%Y %H:%M') sender = get_setting('site', 'global', 'siteemailnoreplyaddress') recipient = '*****@*****.**' subject = 'Campaign Monitor New client Account "%s" Created' % company_name email_body = """Company Name: %s \n\nContact Name: %s \n\nContact Email: %s \n\n\nThanks,\n%s\n """ % (company_name, contact_name, contact_email, now_str) send_mail(subject, email_body, sender, [recipient], fail_silently=True) # add/update the client_id in the local_settings.py local_setting_file = os.path.join(getattr(settings, 'PROJECT_ROOT'), 'settings.py') f = open(local_setting_file, 'r') content = f.read() if client_id == '[CAMPAIGNMONITOR_API_CLIENT_ID]': content = content.replace('[CAMPAIGNMONITOR_API_CLIENT_ID]', cm_client_id) else: content = "%s\nCAMPAIGNMONITOR_API_CLIENT_ID='%s'\n" % (content, cm_client_id) f.close() f = open(local_setting_file, 'w') f.write(content) f.close() print('Success!') else: print('Already has a campaign monitor account')
def setup_cm_account(password=''): # check if already setup client_id = getattr(settings, 'CAMPAIGNMONITOR_API_CLIENT_ID', '') if not client_id or client_id == '[CAMPAIGNMONITOR_API_CLIENT_ID]': cs = CreateSend() company_name = get_setting('site', 'global', 'sitedisplayname') #contact_name = get_setting('site', 'global', 'admincontactname') #contact_email = get_setting('site', 'global', 'admincontactemail') contact_name = "Schipul Client" contact_email = get_contact_email() if not contact_email: raise ValueError("Invalid Email address.") #timezone = get_setting('site', 'global', 'defaulttimezone') # country - must exist on campaign monitor country = 'United States of America' # timezone - must use the format specified by campaign monitor timezone = "(GMT-06:00) Central Time (US & Canada)" api_key = getattr(settings, 'CAMPAIGNMONITOR_API_KEY', None) CreateSend.api_key = api_key # check if this company already exists on campaign monitor # if it does, raise an error clients = cs.clients() cm_client_id = None for cl in clients: if cl.Name == company_name: #cm_client_id = cl.ClientID #break raise ValueError( 'Company name "%s" already exists on campaign monitor.' % company_name) if not cm_client_id: # 1) Create an account cl = Client() cm_client_id = cl.create(company_name, contact_name, contact_email, timezone, country) cl = Client(cm_client_id) # 2) Set access with username and password # access level = 63 full access username = company_name.replace(' ', '') if not password: # generate a random password with 6 in length allowed_chars = 'abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789()#$%' password = User.objects.make_random_password( length=6, allowed_chars=allowed_chars) cl.set_access(username, password, 63) # 3) Set up billing - client will pay monthly #cl.set_payg_billing('USD', True, True, 0) cl.set_monthly_billing('USD', True, 0) # send an email to [email protected] now = datetime.now() now_str = now.strftime('%m/%d/%Y %H:%M') sender = get_setting('site', 'global', 'siteemailnoreplyaddress' ) or settings.DEFAULT_FROM_EMAIL recipient = '*****@*****.**' subject = 'Campaign Monitor New client Account "%s" Created' % company_name email_body = """Company Name: %s \n\nContact Name: %s \n\nContact Email: %s \n\n\nThanks,\n%s\n """ % (company_name, contact_name, contact_email, now_str) send_mail(subject, email_body, sender, [recipient], fail_silently=True) # add/update the client_id in the local_settings.py local_setting_file = os.path.join(settings.PROJECT_ROOT, 'settings.py') f = open(local_setting_file, 'r') content = f.read() if client_id == '[CAMPAIGNMONITOR_API_CLIENT_ID]': content = content.replace( '[CAMPAIGNMONITOR_API_CLIENT_ID]', cm_client_id) else: content = "%s\nCAMPAIGNMONITOR_API_CLIENT_ID='%s'\n" % ( content, cm_client_id) f.close() f = open(local_setting_file, 'w') f.write(content) f.close() print('Success!') else: print('Already has a campaign monitor account')