def render_page(template, **kwargs): contact_form = ContactForm() contact_form.setup() slim_subscription_form = SlimSubscriptionForm() if slim_subscription_form.validate_on_submit(): return redirect( url_for('main.subscription', email=slim_subscription_form.slim_subscription_email.data)) if 'error' in session and 'error' not in kwargs: error = session.pop('error') try: kwargs['error'] = error['message']['error'] if isinstance(error, dict) else\ error['message'] if 'message' in error else\ error if isinstance(error, str) else "Unhandled error" except TypeError: current_app.logger.error("Unhandled error %r", error) kwargs['error'] = "Unhandled error" return render_template(template, contact_form=contact_form, slim_subscription_form=slim_subscription_form, latest_magazine=api_client.get_latest_magazine(), **kwargs)
def _send_message(): contact_form = ContactForm() contact_form.setup() if contact_form.validate_on_submit(): current_app.logger.info('send_message: %s, %s, %s, %s', contact_form.contact_name.data, contact_form.contact_email.data, contact_form.contact_reasons.data, contact_form.contact_message.data) try: resp = api_client.send_message( name=contact_form.contact_name.data, email=contact_form.contact_email.data, reason=contact_form.contact_reasons.data, message=contact_form.contact_message.data) if 'error' in session: error = session.pop('error') current_app.logger.error(error) tz = pytz.timezone("Europe/London") return jsonify({ 'error': 'Problem sending message at {}, please try again later'. format(datetime.now(tz).strftime('%H:%M:%S %d/%m/%y')) }) else: return jsonify(resp) except HTTPError as e: return jsonify({'error': e.message}) return jsonify({'errors': contact_form.errors})