Esempio n. 1
0
def user_campaign_new(request, param_username):
	if current_user(request) and request.user.username == param_username:
		page_vars = {"page_title": "New Campaign"}

		if request.method == 'POST':

			completed_form = CampaignForm(request.POST)

			if completed_form.is_valid():

				campaign = completed_form.save(commit=False)
				campaign.user = request.user
				campaign.active = True
				campaign.save()

				messages.add_message(request, messages.SUCCESS, 'Created New Campaign')

				return HttpResponseRedirect('/' + request.user.username + '/campaign-' + str(campaign.pk))
			else:
				generate_form_errors(request, completed_form)
				page_vars['form'] = completed_form
				return render(request, 'campaign/new.html', page_vars)
				
		elif request.method == "GET":
			page_vars['form'] = CampaignForm()
			csrfContext = RequestContext(request, page_vars)
			return render(request, 'campaign/new.html', csrfContext)
	else:
		raise Http404
Esempio n. 2
0
def add_user(request):

	# If current user is authenticated and it staff.
	if current_staff(request):

		page_vars = {"page_title": 'Add User'}

		# User is requesting form, build it!
		if request.method == "GET":

			# Build form object.
			page_vars['form'] = AdminCreateUserForm()

			# Generating CSRF Context.
			csrfContext = RequestContext(request, page_vars)

			# Render page for user.
			return render(request, 'administrative/add_user.html', csrfContext)

		# User is submitting form, process it!
		elif request.method == "POST":

			# Build form object from post data.
			completed_form = AdminCreateUserForm(request.POST)

			# If form validates correctly, save.
			if completed_form.is_valid():

				# Saving new user to database.
				new_user = completed_form.save(commit=False)
				new_user.save()

				# We want to create a new Profile for the user too!
				new_profile = Profile(user=new_user, time_zone='UTC')
				new_profile.save()

				# Generate message for user.
				messages.add_message(request, messages.SUCCESS, 'Success: New User Added')

				# Redirect back to admin page.
				return HttpResponseRedirect('/admin')

			# Form is NOT valid.
			else:

				# Form is not valid, re-render the page with the form.
				generate_form_errors(request, completed_form)
				page_vars['form'] = completed_form

				# Generate CSRF Context.
				csrfContext = RequestContext(request, page_vars)

				# Render page for user.
				return render(request, 'administrative/add_user.html', csrfContext)

	else:
		raise Http404
Esempio n. 3
0
def user_campaign_edit(request, param_username, param_campaign_pk):
	page_vars = {"page_title": "Edit Campaign"}

	if current_user(request) and param_username == request.user.username:
		try:
			user_campaign = Campaign.objects.get(pk = param_campaign_pk)
		except Campaign.DoesNotExist:
			raise Http404

		if user_campaign.user != request.user:
			raise Http404

		page_vars['campaign'] = user_campaign

		if user_campaign.user == request.user:

			if request.method == "GET":
				action = request.GET.get('action')

				if action == "delete":
					# We want to delete the campaign
					Campaign.objects.filter(pk=param_campaign_pk).delete()
					messages.add_message(request, messages.SUCCESS, 'Successfully deleted Campaign')
					return HttpResponseRedirect('/' + request.user.username + '/campaigns')

				else:
					# We want to edit the campaign, render form				
					page_vars['form'] = CampaignForm(instance=user_campaign)

					# Generate warning is campaign is disabled
					if user_campaign.active == False:
						messages.add_message(request, messages.ERROR, 'Campaign disabled by MailPrime Administrator')

					# Generate CSRF Context
					csrfContext = RequestContext(request, page_vars)

					# Render page for user
					return render(request, 'campaign/edit.html', csrfContext)

			elif request.method == "POST":

				# Check if campaign is disabled
				if user_campaign.active == False:
					return HttpResponseRedirect('/{0}/campaign-{1}'.format(request.user.username, user_campaign.pk))

				# It's safe to assume that the campaign is active, process form
				completed_form = CampaignForm(request.POST, instance=user_campaign)

				if completed_form.is_valid():
					completed_form.save()
					messages.add_message(request, messages.SUCCESS, 'Successfully Updated Campaign')
					return HttpResponseRedirect('/' + request.user.username + '/campaign-' + str(user_campaign.pk))
				else:
					generate_form_errors(request, completed_form)
					page_vars['form'] = completed_form
					return render(request, 'campaign/edit.html', page_vars)
Esempio n. 4
0
def add_template(request):

	# If current user is authenticated and it staff.
	if current_staff(request):

		page_vars = {'page_title': 'Add Template'}

		# User is requesting for a new form
		if request.method == "GET":

			# Creating form instance
			page_vars['form'] = AdminTemplateForm()

			# CSRF context
			csrfContext = RequestContext(request, page_vars)

			# Render page for user
			return render(request, 'administrative/add_template.html', csrfContext)

		# User is uploading new template, process it!
		elif request.method == "POST":

			# Create form instance from post data
			completed_form = AdminTemplateForm(request.POST)

			# Check to see if form is valid
			if completed_form.is_valid():

				# Save it!
				completed_form.save()

				# Generate Message for user
				messages.add_message(request, messages.SUCCESS, 'Success: New Template Added')

				# Redirect
				return HttpResponseRedirect('/admin/all_templates')

			# Form is not valid!
			else:
				
				# Generate form errors.
				generate_form_errors(request, completed_form)

				# Resend form.
				page_vars['form'] = completed_form

				# CSRF context
				csrfContext = RequestContext(request, page_vars)

				# Render Page
				return render(request, 'administrative/add_template.html', csrfContext)

	else:
		raise Http404
Esempio n. 5
0
def user_account(request, param_username):
	page_vars = { "page_title": "Account: " + request.user.username }

	if current_user(request) and param_username == request.user.username:

		try:
			requested_user = User.objects.get(username=param_username)
		except User.DoesNotExist:
			raise Http404

		if request.method == "GET":
			user_profile = Profile.objects.get(user = request.user)
			init_values = {'time_zone': user_profile.time_zone, 'email': request.user.email}
			page_vars['form'] = UserProfileForm(initial=init_values)
			csrfContext = RequestContext(request, page_vars)
			
			return render(request, 'auth/user_account.html', csrfContext)

		elif request.method == "POST":
			if current_user(request):
				completed_form = UserProfileForm(request.POST)

				if completed_form.is_valid():

					user_profile = Profile.objects.get(user=request.user)
					user_obj = User.objects.get(username=request.user.username)

					# Updating User and Profile Objects with form data
					user_profile.time_zone = completed_form.cleaned_data['time_zone']

					request.session['user_timezone'] = completed_form.cleaned_data['time_zone']

					user_obj.email = completed_form.cleaned_data['email']

					# Only update the user's password if it's not empty
					if completed_form.cleaned_data['password'] != '':
						user_obj.set_password(completed_form.cleaned_data['password'])

					# Commiting the edited objects to the database
					user_profile.save()
					user_obj.save()

					messages.add_message(request, messages.SUCCESS, 'Success: Your Account Has Been Updated')
					return HttpResponseRedirect('/' + request.user.username + '/account')
				else:
					generate_form_errors(request, completed_form)
					return HttpResponseRedirect('/' + request.user.username + '/account')
			else:
				raise Http404
		
	else:
		raise Http404
Esempio n. 6
0
def add_recipient(request, param_username, param_campaign_pk):
	if current_user(request) and request.user.username == param_username:
		page_vars = {'page_title': 'Add Recipient'}

		try:
			page_vars['campaign'] = Campaign.objects.get(pk=param_campaign_pk, user=request.user)
		except Campaign.DoesNotExist:
			raise Http404

		# User is requesting page
		if request.method == "GET":
			page_vars['form'] = RecipientForm()

			csrfContext = RequestContext(request, page_vars)
			return render(request, 'recipient/add.html', csrfContext)

		# User is uploading from the form
		elif request.method == "POST":

			# Check to see if the campaign is disabled
			if page_vars['campaign'].active:
				completed_form = RecipientForm(request.POST)

				if completed_form.is_valid():
					recipient = completed_form.save(commit=False)

					recipient.campaign = page_vars['campaign']
					recipient.save()

					# Send out invitation!!!
					os.system("python /var/www/mailprime/manage.py deploy_invitations {0} &".format( recipient.campaign.pk ))
					
					return HttpResponseRedirect('/' + request.user.username + '/campaign-' + str(page_vars['campaign'].pk) + '/recipients')
				else:
					generate_form_errors(request, completed_form)
					page_vars['form'] = completed_form
					return render(request, 'recipient/add.html', page_vars)

			else:

				# Campaign is deactivated, generate message.
				messages.add_message(request, messages.SUCCESS, 'Campaign disabled by MailPrime Administrator')
				return HttpResponseRedirect('/{0}/campaign-{1}/recipients/add'.format(request.user.username, page_vars['campaign'].pk))

	else:
		raise Http404
Esempio n. 7
0
def user_campaign_message_new(request, param_username, param_campaign_pk):
	if current_user(request) and request.user.username == param_username:

		try:
			campaign = Campaign.objects.get(pk = param_campaign_pk)
		except Campaign.DoesNotExist:
			raise Http404

		if campaign.user == request.user:
			page_vars = {"page_title": "New Message"}
			page_vars['campaign'] = campaign

			if request.method == "POST":
				
				completed_form = MessageForm(request.POST)

				if completed_form.is_valid():
					message = completed_form.save(commit=False)
					message.campaign_id = param_campaign_pk
					message.save()
					messages.add_message(request, messages.SUCCESS, 'Successfully Created Message')

					return HttpResponseRedirect('/'+ request.user.username + "/campaign-" + str(message.campaign.id) + "/messages")
				else:
					generate_form_errors(request, completed_form)
					page_vars['form'] = completed_form

					return render(request, 'message/new.html', page_vars)
			else:
				# Process GET DATA...
				page_vars['form'] = MessageForm()
				csrfContext = RequestContext(request, page_vars)
				
				return render(request, 'message/new.html', csrfContext)
	else:
		raise Http404
Esempio n. 8
0
def upload_recipients(request, param_username, param_campaign_pk):
	page_vars = {"page_title": 'Upload Recipients'}

	if current_user(request) and request.user.username == param_username:

		# Capturing Campaign that will be used with the recipient entries
		try:
			campaign = Campaign.objects.get(pk=param_campaign_pk, user=request.user)
		except Campaign.DoesNotExist:
			raise Http404

		# User is requesting page, render form
		if request.method == 'GET':

			page_vars['campaign'] = campaign
			page_vars['form'] = ContactUploadForm()
			
			csrfContext = RequestContext(request, page_vars)
			return render(request, 'recipient/upload.html', csrfContext)

		# User is uploading from form, parse data
		elif request.method == 'POST':

			# Check to see if campaign is disabled
			if campaign.active == False:

				#Generate warning, redirect.
				messages.add_message(request, messages.SUCCESS, 'Campaign disabled by MailPrime Administrator')
				return HttpResponseRedirect('/{0}/campaign-{1}/recipients/upload'.format(request.user.username, campaign.pk))

			# Can be assumed that the campaign is not disabled now

			# Page Vars
			page_vars = { 'page_title': 'Upload Report'}

			# Array that will contain invalid/valid emails
			invalid_emails = {}
			valid_emails = {}

			# Valid email flag
			valid = True
			valid_recip = True
			valid_email_count = 0
			invalid_email_count = 0

			form = ContactUploadForm(request.POST, request.FILES)

			if form.is_valid():

				uploaded_file = request.FILES['contact_file']

				# Splitting file into chunks so it doesn't eat up system resources
				for chunk in uploaded_file.chunks():

					# Iterate through each line
					for line in chunk.rstrip().split('\n'):

						try:
							validate_email(line)
						except forms.ValidationError:
							valid = False

						if valid:

							# Save new Recipient to database
							try:
								Recipient(email=line, campaign=campaign).save()
							except IntegrityError:
								valid_recip = False


							# Increment counter
							if (valid_recip):

								valid_email_count += 1
								valid_emails[line] = 'Success'

							else:

								invalid_email_count += 1
								invalid_emails[line] = 'Repeated'

						else:

							# Increment counter
							invalid_email_count += 1
							invalid_emails[line] = 'Invalid'

						# Reset valid flag
						valid = True
						valid_recip = True

				# Giving campaign to template for URL reasons
				page_vars['campaign'] = campaign

				# Generate messages for user
				if (valid_email_count > 0):
					messages.add_message(request, messages.SUCCESS, "{0} Successful Entries".format(valid_email_count))
					page_vars['valid_emails'] = valid_emails

				if (invalid_email_count > 0):
					messages.add_message(request, messages.SUCCESS, "{0} Entries Failed".format(invalid_email_count))
					page_vars['invalid_emails'] = invalid_emails

				# Send out Invitations if necessary
				if valid_email_count > 0:
					os.system("python /var/www/mailprime/manage.py deploy_invitations {0} &".format(campaign.pk))

				return render(request, 'recipient/upload_report.html', page_vars)

			# Form is NOT valid
			else:
				generate_form_errors(request, form)
				return HttpResponseRedirect('/{0}/campaign-{1}/recipients/upload'.format( request.user.username, campaign.pk ))
				
	else:
		raise Http404
Esempio n. 9
0
def edit_user(request, param_user_pk):

	# If current user is authenticated and it staff.
	if current_staff(request):

		page_vars = {"page_title": 'Alter User'}

		# Getting Requested User object from database.
		try:
			requested_user = User.objects.get(pk=param_user_pk)
		except User.DoesNotExist:
			raise Http404

		# User is requesting the form, build it!
		if request.method == "GET":

			# Get profile object from database.
			try:
				user_profile = Profile.objects.get(user=requested_user)
			except Profile.DoesNotExist:
				raise Http404

			# Build initial values for form.
			init_values = {'time_zone': user_profile.time_zone, 'email': requested_user.email}

			# Create Form object with initial values.
			page_vars['form'] = UserProfileForm(initial=init_values)

			# Getting requested_user so we can create the post link via pk.
			page_vars['requested_user'] = requested_user

			# Generating the CSRF context.
			csrfContext = RequestContext(request, page_vars)

			# Render page with form.
			return render(request, 'administrative/edit_user.html', csrfContext)

		# Okay so the user is submitting changes, apply them!
		elif request.method == "POST":

			# Build form obj from POST data.
			completed_form = UserProfileForm(request.POST)

			if completed_form.is_valid():

				# We need to get the profile obj out of the db.
				try:
					user_profile = Profile.objects.get(user=requested_user)
				except Profile.DoesNotExist:
					raise Http404

				# Update User and Profile objs in db with validated form-data.
				user_profile.time_zone = completed_form.cleaned_data['time_zone']
				requested_user.email = completed_form.cleaned_data['email']

				# Only update the password if it's not empty.
				if completed_form.cleaned_data['password'] != '':
					requested_user.set_password(completed_form.cleaned_data['password'])

				# Save the changes to the database.
				user_profile.save()
				requested_user.save()

				# Generate message for the user
				messages.add_message(request, messages.SUCCESS, 'Success: Changes Applied to User')

				# Redirect back to user-edit page.
				return HttpResponseRedirect('/admin/edit_user/{0}'.format(requested_user.pk))

			# Form data is NOT valid. Generate messages.
			else:

				# Generate error messages from form
				generate_form_errors(request, completed_form)

				# Update page_vars
				page_vars['form'] = completed_form

				# Generate CSRF Context.
				csrfContext = RequestContext(request, page_vars)

				# Render page with errors, old form.
				return render(request, 'administrative/edit_user.html', csrfContext)

	else:
		raise Http404
Esempio n. 10
0
def edit_template(request, param_template_pk):

	# If current user is authenticated and it staff.
	if current_staff(request):

		page_vars = {"page_title": "Edit Template"}

		# Get the desired template out of DB
		try:
			template = Template.objects.get(pk=param_template_pk)
		except Template.DoesNotExist:
			raise Http404

		# Okay, we have the template now.
		# Method is GET, load form object.
		if request.method == "GET":

			# Check for 'delete' action
			action = request.GET.get('action')

			# If action == 'delete' delete template from system
			if action == 'delete':

				# Generate message for user.
				messages.add_message(request, messages.SUCCESS, 'Success: Template Deleted')

				# Delete template from db.
				template.delete()

				# Redirect
				return HttpResponseRedirect('/admin/all_templates')

			# We can assume that the delete action was not called now

			page_vars['form'] = AdminTemplateForm(instance=template)
			page_vars['template'] = template

			# Generating CSRF context.
			csrfContext = RequestContext(request, page_vars)

			# Render page w/ form for user.
			return render(request, 'administrative/edit_template.html', csrfContext)

		# User is posting the form, update the template obj.
		elif request.method == "POST":

			# Build templateform obj.
			completed_form = AdminTemplateForm(request.POST, instance=template)

			# Save template if valid
			if completed_form.is_valid():

				# Save it to DB
				completed_form.save()

				# Generate message for user
				messages.add_message(request, messages.SUCCESS, 'Success: Changes Applied to Template')

				# Redirect back to edit page
				return HttpResponseRedirect('/admin/edit_template/{0}'.format(template.pk))

			# Form is not valid
			else:

				# Generate form errors.
				generate_form_errors(request, completed_form)

				# Resend form.
				page_vars['form'] = completed_form
				page_vars['template'] = template

				# CSRF context
				csrfContext = RequestContext(request, page_vars)

				# Render Page
				return render(request, 'administrative/edit_template.html', csrfContext)
	else:
		raise Http404
Esempio n. 11
0
def user_campaign_message(request, param_username, param_campaign_pk, param_message_pk):
	page_vars = {"page_title": 'Campaign Message'}

	if current_user(request) and request.user.username == param_username:

		try:
			message = Message.objects.get(campaign__pk = param_campaign_pk, campaign__user__username = request.user.username, pk = param_message_pk)
		except Message.DoesNotExist:
			raise Http404

		action = request.GET.get('action')

		# Check to see if the campaign is disabled.
		if message.campaign.active == False:

			# Campaign is locked, generate message for user, return.
			messages.add_message(request, messages.SUCCESS, 'Campaign disabled by MailPrime Administrator')

			page_vars['templates'] = Template.objects.all()
			page_vars['sample_link'] = "/api/{0}/c-{1}/m-{2}/sample_message.html".format(
																						request.user.username,
																						message.campaign.pk,
																						message.pk)
			page_vars['message'] = message
			page_vars['form'] = MessageForm(instance=message)
			page_vars['campaign'] = message.campaign
			page_vars['slick_present'] = True
			
			csrfContext = RequestContext(request, page_vars)
			return render(request, 'message/show.html', csrfContext)

		# Deploy message if deploy GET param is set
		if (action is not None) and (action == 'deploy'):

			if message.deployed == False:
				os.system('python /var/www/mailprime/manage.py deploy_messages ' + str(message.pk) + '&')
				messages.add_message(request, messages.SUCCESS, 'SUCCESS: Your Message Has Been Deployed')
			else:
				messages.add_message(request, messages.SUCCESS, 'ERROR: Message Has Already Been Deployed')

			return HttpResponseRedirect('/' + request.user.username + '/campaign-' + str(message.campaign.pk) + '/messages')

		elif (action is not None) and (action == 'delete'):

			# Storing the campaign in a var so we can delete the message.
			campaign_pk = message.campaign.pk

			# Add message for user.
			messages.add_message(request, messages.SUCCESS, 'SUCCESS: Deleted Message')

			# Delete the message from the database.
			message.delete()

			# Redirect back to the campaign homepage.
			return HttpResponseRedirect('/{0}/campaign-{1}'.format(request.user.username, campaign_pk))

		# Else, GET param not set, render regular page
		else:

			# Request method is GET, generate new form
			if request.method == "GET":

				page_vars['templates'] = Template.objects.all()
				page_vars['sample_link'] = "/api/{0}/c-{1}/m-{2}/sample_message.html".format(
																						request.user.username,
																						message.campaign.pk,
																						message.pk)
				page_vars['message'] = message
				page_vars['form'] = MessageForm(instance=message)
				page_vars['campaign'] = message.campaign
				page_vars['slick_present'] = True
			
				csrfContext = RequestContext(request, page_vars)
				return render(request, 'message/show.html', csrfContext)

			# Request method is POST, process form data to model
			elif request.method == "POST":

				# Creating Message instance from modelform
				completed_form = MessageForm(request.POST, instance=message)

				# Running validation w/ clean(), if valid
				if completed_form.is_valid():

					new_message = completed_form.save(commit=False)

					if new_message.can_update():

						new_message.save()
						messages.add_message(request, messages.SUCCESS, 'Successfully Updated Message')
						return HttpResponseRedirect('/{0}/campaign-{1}/message-{2}'.format(request.user.username, message.campaign.pk, message.pk))

					else:
						messages.add_message(request, messages.SUCCESS, 'ERROR: Message has already been deployed')
						return HttpResponseRedirect('/{0}/campaign-{1}/message-{2}'.format(request.user.username, message.campaign.pk, message.pk))

				# Validation errors occurred, regenerate form with vars
				else:
					generate_form_errors(request, completed_form)
					page_vars['form'] = completed_form
					page_vars['campaign'] = message.campaign
					page_vars['message'] = message
					page_vars['templates'] = Template.objects.all()
					page_vars['slick_present'] = True
					page_vars['sample_link'] = "/api/{0}/c-{1}/m-{2}/sample_message.html".format(
																							request.user.username,
																							message.campaign.pk,
																							message.pk)

					csrfContext = RequestContext(request, page_vars)
					return render(request, 'message/show.html', csrfContext)
	else:
		raise Http404