コード例 #1
0
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)
コード例 #2
0
 def test_valid_form_with_email(self):
     form = CreateContactForm(data={
         'message': 'a message',
         'email': '*****@*****.**'
     })
     self.assertTrue(form.is_valid())
コード例 #3
0
 def test_valid_form_no_email(self):
     form = CreateContactForm(data={'message': 'a message'})
     self.assertTrue(form.is_valid())
コード例 #4
0
 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."
     )
コード例 #5
0
 def test_message_help_text(self):
     form = CreateContactForm()
     self.assertEqual(form.fields['message'].help_text,
                      "Enter your message (max 500 characters).")
コード例 #6
0
 def test_email_field_label(self):
     form = CreateContactForm()
     self.assertTrue(form.fields['email'].label == None
                     or form.fields['email'].label == 'email')
コード例 #7
0
 def test_message_field_label(self):
     form = CreateContactForm()
     self.assertTrue(form.fields['message'].label == None
                     or form.fields['message'].label == 'message')