def govdelivery_subscribe(request): """ View that checks to see if the request is AJAX, attempts to subscribe the user, then either redirects to an error/success page (non-AJAX) or in the case of AJAX, returns some JSON to tell the front-end. """ is_ajax = request.is_ajax() if is_ajax: passing_response = JsonResponse({'result': 'pass'}) failing_response = JsonResponse({'result': 'fail'}) else: passing_response = redirect('govdelivery:success') failing_response = redirect('govdelivery:server_error') for required_param in REQUIRED_PARAMS_GOVDELIVERY: if (required_param not in request.POST or not request.POST.get(required_param)): return failing_response if is_ajax else \ redirect('govdelivery:user_error') email_address = request.POST['email'] codes = request.POST.getlist('code') gd = get_govdelivery_api() try: subscription_response = gd.set_subscriber_topics( email_address, codes, send_notifications=True) if subscription_response.status_code != 200: return failing_response except Exception: return failing_response answers = extract_answers_from_request(request) for question_id, answer_text in answers: gd.set_subscriber_answers_to_question(email_address, question_id, answer_text) return passing_response
def govdelivery_subscribe(self, email, code): govdelivery = get_govdelivery_api() subscription_response = govdelivery.set_subscriber_topics( contact_details=email, topic_codes=[code], send_notifications=True ) subscription_response.raise_for_status()
def test_uses_govdelivery_account_code_for_account_code(self): govdelivery = get_govdelivery_api() self.assertEqual(govdelivery.account_code, 'my-account-code')
def test_uses_govdelivery_api_setting_for_class(self): self.assertIsInstance(get_govdelivery_api(), UnitTestGovDelivery)
def govdelivery_subscribe(self, email, code): govdelivery = get_govdelivery_api() subscription_response = govdelivery.set_subscriber_topics( contact_details=email, topic_codes=[code], send_notifications=True) subscription_response.raise_for_status()
def govdelivery_api(self): return get_govdelivery_api()