def contact(request): """View function for creating contact forms.""" context = gen_alerts(request) # adds contact form into the database if request.method == "POST": form = CreateContactForm(request.POST) if form.is_valid(): model_instance = form.save(commit=True) if (model_instance.email): subject = f'New Contact Request Made by {model_instance.email}' else: subject = f'New Anonymous Contact Request' # sends an email concerning the contact form to selected club officers recipients = ContactNotificant.objects.all().values_list('email', flat=True) send_mail(subject=subject, message=model_instance.message, from_email='Game Night Notifications', recipient_list=list(recipients)) request.session['notify'] = "Contact Successfully Sent!" return HttpResponseRedirect(reverse('contact_submit')) else: form = CreateContactForm() context['form'] = form return render(request, 'main/create_contact.html', context)
def test_valid_form_with_email(self): form = CreateContactForm(data={ 'message': 'a message', 'email': '*****@*****.**' }) self.assertTrue(form.is_valid())
def test_valid_form_no_email(self): form = CreateContactForm(data={'message': 'a message'}) self.assertTrue(form.is_valid())
def test_email_help_text(self): form = CreateContactForm() self.assertEqual( form.fields['email'].help_text, "Enter your email address (optional). This is best used if you want us to contact you back." )
def test_message_help_text(self): form = CreateContactForm() self.assertEqual(form.fields['message'].help_text, "Enter your message (max 500 characters).")
def test_email_field_label(self): form = CreateContactForm() self.assertTrue(form.fields['email'].label == None or form.fields['email'].label == 'email')
def test_message_field_label(self): form = CreateContactForm() self.assertTrue(form.fields['message'].label == None or form.fields['message'].label == 'message')