def campaign_control(request): action = request.GET['action'] if action == 'delete': """Valid Types to delete are uid, cid, tid""" object_type = request.GET['type'] if object_type == 'uid': recipient_uid = request.GET['uid'] campaign_id = request.GET['cid'] if len(recipient_uid) == 8: result = Recipient().delete_recipient(recipient_uid) if result: return redirect( '/campaign/{0}/#tracking'.format(campaign_id)) else: error_line = "Unable to delete user." return render(request, 'error.html', {'error': error_line}) # Start a Campaign if action == 'start': campaign_id = request.GET['cid'] # get campaign and check if its already started try: campaign_details = Campaign.objects.get(id=campaign_id) except: return render(request, 'error.html', {'error': 'Not a valid Campaign ID'}) if campaign_details.state == 'Running': return render(request, 'error.html', {'error': 'Campaign has already started'}) elif campaign_details.state == 'Completed': return render(request, 'error.html', {'error': 'Campaign has already Finished'}) elif campaign_details.state == 'Pending': # Get recipients and templates recipient_list = campaign_details.recipient_set.all() template_id = campaign_details.template_id template_details = Template.objects.get(id=template_id) # pass the email templates over to the email function email_status = emailfunctions.create_email(recipient_list, template_details) # Set the Status to Started and set the Start Date campaign_details.start_date = timezone.now() campaign_details.state = 'Running' campaign_details.save() return redirect('/campaign/{0}/'.format(campaign_id)) else: return render(request, 'error.html', {'error': 'Not a Valid Campaign Action'}) # Stop a Campaign # Does not stop the portal just sets the stop clock if action == 'stop': campaign_id = request.GET['cid'] # get campaign and check if its already started try: campaign_details = Campaign.objects.get(id=campaign_id) except: return render(request, 'error.html', {'error': 'Not a valid Campaign ID'}) if campaign_details.state == 'Completed': return render(request, 'error.html', {'error': 'Campaign has already Finished'}) elif campaign_details.state == 'Pending': return render(request, 'error.html', {'error': 'Campaign has not been started'}) elif campaign_details.state == 'Running': campaign_details.end_date = timezone.now() campaign_details.state = 'Completed' campaign_details.save() return redirect('/campaign/{0}/'.format(campaign_id)) else: return render(request, 'error.html', {'error': 'Not a Valid Campaign Action'})
def add_post_data(request, add_type): """ Add Type is passed via the URI Valid Types and actions are: campaign - new, update template - new, update recipient - new, update """ # Add Campaign if add_type == 'campaign': action = request.POST['action'] campaign_name = request.POST['c_name'] campaign_description = request.POST['c_desc'] campaign_template = request.POST['c_template'] if action == 'new': campaign = Campaign() elif action == 'update': campaign = Campaign.objects.get(pk=campaign_id) else: error_line = "Not a Valid Campaign Action" campaign.name = campaign_name campaign.description = campaign_description campaign.template_id = campaign_template campaign.created = timezone.now() campaign.save() campaign_id = campaign.id return redirect('/campaign/{0}'.format(campaign_id)) # Add Recipients if add_type == 'recipient': action = request.POST['action'] campaign_id = request.POST['c_id'] campaign = Campaign.objects.get(pk=campaign_id) csv_file = request.FILES real_name = request.POST['real_name'] email_add = request.POST['email_add'] # check for CSV File - Override single email add if csv_file and action == 'new': csv_file = csv_file['css_import'] recipients = [] try: csv_data = csv_file.read() csv_date = csv_data.replace('\r', '') lines = csv_data.split('\n') with transaction.commit_on_success(): for line in lines: if len(line) > 10: uid = ''.join([ random.choice(string.ascii_letters + string.digits) for n in xrange(8) ]) real_name, email_add = line.split(',') recipient = Recipient(real_name=real_name, email_address=email_add, campaign=campaign, uid=uid) recipient.save() except Exception as e: error_line = "Unable to parse CSV File - {0}".format(e) return render(request, 'error.html', {'error': error_line}) # Check for single Entry elif real_name and email_add: uid = ''.join([ random.choice(string.ascii_letters + string.digits) for n in xrange(8) ]) recipient = Recipient(real_name=real_name, email_address=email_add, campaign=campaign, uid=uid) recipient.save() else: error = "Unable to Add recipients" return render(request, 'error.html', {'error': error_line}) return redirect('/campaign/{0}/#tracking'.format(campaign_id)) # Add / Update Templates if add_type == 'template': # Details template_id = request.POST['id'] title = request.POST['title'] description = request.POST['description'] # Email display_name = request.POST['display_name'] email_address = request.POST['email_address'] subject_line = request.POST['subject_line'] smtp_id = request.POST['smtp_id'] email_design = request.POST['email_design'] # Word Document document_name = request.POST['document_name'] try: document_enable = request.POST['document_enable'] document_enable = 1 except: document_enable = 0 document_design = request.POST['document_design'] # Portal portal_uri = request.POST['portal_uri'] try: portal_plugins = request.POST['portal_plugin'] portal_plugins = 1 except: portal_plugins = 0 portal_design = request.POST['portal_design'] portal_redirect = request.POST['portal_redirect'] action = request.POST['action'] # Process The Post Data if action == 'delete': # return here pass if action == 'new': template = Template() if action == 'update': template = Template.objects.get(pk=template_id) smtp_server = SMTPServer.objects.get(pk=smtp_id) template.title = title template.description = description template.display_name = display_name template.email_address = email_address template.subject_line = subject_line template.smtpServer = smtp_server template.email_design = email_design template.portal_uri = portal_uri template.portal_plugins = portal_plugins template.portal_design = portal_design template.portal_redirect = portal_redirect template.document_enable = document_enable template.document_name = document_name template.document_design = document_design template.save() template_id = template.id return redirect('/template/{0}'.format(template_id)) error_line = "Not a Valid POST Request" return render(request, 'error.html', {'error': error_line})
campaign.name = 'This is another test campaign' campaign.description = 'This is another test description' campaign.template_id = '2' campaign.created = fake.date_time_between(start_date="-3d", end_date="-2d") campaign.start_data = fake.date_time_between(start_date="-3d", end_date="-2d") campaign.save() # Create Recipients for i in range(company_size): full_name = fake.name() email_add = '{0}@{1}'.format(full_name.replace(' ', '.'), company_domain) uid = ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(8)]) # Create a recipient recipient = Recipient() recipient.campaign = campaign recipient.real_name = full_name recipient.email_address = email_add recipient.uid = uid # Choose to enter details or not portal_choice = random.choice([True, False]) document_choice = random.choice([True, False]) webbug_choice = random.choice([True, False]) # Fill as per choices if portal_choice: recipient.portal_open = fake.date_time_between(start_date="-2d", end_date="now") recipient.os_system = random.choice(os_list)
campaign.template_id = '2' campaign.created = fake.date_time_between(start_date="-3d", end_date="-2d") campaign.start_data = fake.date_time_between(start_date="-3d", end_date="-2d") campaign.save() # Create Recipients for i in range(company_size): full_name = fake.name() email_add = '{0}@{1}'.format(full_name.replace(' ', '.'), company_domain) uid = ''.join([ random.choice(string.ascii_letters + string.digits) for n in xrange(8) ]) # Create a recipient recipient = Recipient() recipient.campaign = campaign recipient.real_name = full_name recipient.email_address = email_add recipient.uid = uid # Choose to enter details or not portal_choice = random.choice([True, False]) document_choice = random.choice([True, False]) webbug_choice = random.choice([True, False]) # Fill as per choices if portal_choice: recipient.portal_open = fake.date_time_between(start_date="-2d", end_date="now")
def add_post_data(request, add_type): """ Add Type is passed via the URI Valid Types and actions are: campaign - new, update template - new, update recipient - new, update """ # Add Campaign if add_type == 'campaign': action = request.POST['action'] campaign_name = request.POST['c_name'] campaign_description = request.POST['c_desc'] campaign_template = request.POST['c_template'] if action == 'new': campaign = Campaign() elif action == 'update': campaign = Campaign.objects.get(pk=campaign_id) else: error_line = "Not a Valid Campaign Action" campaign.name = campaign_name campaign.description = campaign_description campaign.template_id = campaign_template campaign.created = timezone.now() campaign.save() campaign_id = campaign.id return redirect('/campaign/{0}'.format(campaign_id)) # Add Recipients if add_type == 'recipient': action = request.POST['action'] campaign_id = request.POST['c_id'] campaign = Campaign.objects.get(pk=campaign_id) csv_file = request.FILES real_name = request.POST['real_name'] email_add = request.POST['email_add'] # check for CSV File - Override single email add if csv_file and action == 'new': csv_file = csv_file['css_import'] recipients = [] try: csv_data = csv_file.read() csv_date = csv_data.replace('\r', '') lines = csv_data.split('\n') with transaction.commit_on_success(): for line in lines: if len(line) > 10: uid = ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(8)]) real_name, email_add = line.split(',') recipient = Recipient(real_name=real_name, email_address=email_add, campaign=campaign, uid=uid) recipient.save() except Exception as e: error_line = "Unable to parse CSV File - {0}".format(e) return render(request, 'error.html', {'error':error_line}) # Check for single Entry elif real_name and email_add: uid = ''.join([random.choice(string.ascii_letters + string.digits) for n in xrange(8)]) recipient = Recipient(real_name=real_name, email_address=email_add, campaign=campaign, uid=uid) recipient.save() else: error = "Unable to Add recipients" return render(request, 'error.html', {'error':error_line}) return redirect('/campaign/{0}/#tracking'.format(campaign_id)) # Add / Update Templates if add_type == 'template': # Details template_id = request.POST['id'] title = request.POST['title'] description = request.POST['description'] # Email display_name = request.POST['display_name'] email_address = request.POST['email_address'] subject_line = request.POST['subject_line'] smtp_id = request.POST['smtp_id'] email_design = request.POST['email_design'] # Word Document document_name = request.POST['document_name'] try: document_enable = request.POST['document_enable'] document_enable = 1 except: document_enable = 0 document_design = request.POST['document_design'] # Portal portal_uri = request.POST['portal_uri'] try: portal_plugins = request.POST['portal_plugin'] portal_plugins = 1 except: portal_plugins = 0 portal_design = request.POST['portal_design'] portal_redirect = request.POST['portal_redirect'] action = request.POST['action'] # Process The Post Data if action == 'delete': # return here pass if action == 'new': template = Template() if action == 'update': template = Template.objects.get(pk=template_id) smtp_server = SMTPServer.objects.get(pk=smtp_id) template.title = title template.description = description template.display_name = display_name template.email_address = email_address template.subject_line = subject_line template.smtpServer = smtp_server template.email_design = email_design template.portal_uri = portal_uri template.portal_plugins = portal_plugins template.portal_design = portal_design template.portal_redirect = portal_redirect template.document_enable = document_enable template.document_name = document_name template.document_design = document_design template.save() template_id = template.id return redirect('/template/{0}'.format(template_id)) error_line = "Not a Valid POST Request" return render(request, 'error.html', {'error':error_line})