def post(self, request, *args, **kwargs): """Check if the form is valid and then pass it on to our form handling functions.""" form = NewsletterSignupForm(request.POST) if not form.is_valid(): return self.form_invalid(request, form) else: return self.form_valid(request, form)
def test_post_view(self, mock_subscribe): """Posting an email to the list should add that email to our MailChimp list.""" form = NewsletterSignupForm({ 'email': '*****@*****.**', 'list': settings.MAILCHIMP_LIST_DEFAULT }) ok_(form.is_valid(), 'The form should validate.') response = http_post_response(self.url, self.view, form.data) mock_subscribe.assert_called_with(form.data['email'], form.data['list']) eq_(response.status_code, 302, 'Should redirect upon successful submission.')
def test_post_other_list(self, mock_subscribe): """Posting to a list other than the default should optionally subscribe to the default.""" form = NewsletterSignupForm({ 'email': '*****@*****.**', 'default': True, 'list': 'other' }) ok_(form.is_valid(), 'The form should validate.') response = http_post_response(self.url, self.view, form.data) mock_subscribe.assert_any_call(form.data['email'], form.data['list']) mock_subscribe.assert_any_call(form.data['email'], settings.MAILCHIMP_LIST_DEFAULT) eq_(response.status_code, 302, 'Should redirect upon successful submission.')
def get(self, request, *args, **kwargs): """Returns a signup form""" template = 'forms/newsletter.html' context = { 'form': NewsletterSignupForm( initial={'list': settings.MAILCHIMP_LIST_DEFAULT}) } return render(request, template, context)
def newsletter(context, list_id=None, label=None, cta=None): """Template tag to insert a newsletter signup form.""" list_id = settings.MAILCHIMP_LIST_DEFAULT if list_id is None else list_id label = 'Newsletter' if label is None else label cta = 'Want the latest investigative and FOIA news?' if cta is None else cta is_default = list_id == settings.MAILCHIMP_LIST_DEFAULT request = context['request'] initial_data = {'list': list_id} if request.user.is_authenticated(): initial_data['email'] = request.user.email newsletter_form = NewsletterSignupForm(initial=initial_data) return { 'request': request, 'label': label, 'cta': cta, 'is_default': is_default, 'newsletter_form': newsletter_form }