def send_help_message(session, short_name, business_id): """Sends secure message for the help pages""" form = SecureMessagingForm(request.form) option = request.args['option'] sub_option = request.args['sub_option'] if not form.validate(): flash(form.errors['body'][0]) if sub_option == 'not_defined': return redirect( url_for('surveys_bp.get_send_help_message', short_name=short_name, business_id=business_id, option=option)) else: return redirect( url_for('surveys_bp.get_send_help_message_page', short_name=short_name, business_id=business_id, option=option, sub_option=sub_option)) else: subject = request.args['subject'] party_id = session.get_party_id() survey = survey_controller.get_survey_by_short_name(short_name) business_id = business_id logger.info("Form validation successful", party_id=party_id) sent_message = _send_new_message(subject, party_id, survey['id'], business_id) thread_url = url_for( "secure_message_bp.view_conversation", thread_id=sent_message['thread_id']) + "#latest-message" flash(Markup(f'Message sent. <a href={thread_url}>View Message</a>')) return redirect(url_for('surveys_bp.get_survey_list', tag='todo'))
def create_message(session): """Creates and sends a message outside of the context of an existing conversation""" survey_id = request.args["survey"] ru_ref = request.args["ru_ref"] party_id = session.get_party_id() form = SecureMessagingForm(request.form) if request.method == "POST" and form.validate(): logger.info("Form validation successful", party_id=party_id) sent_message = _send_new_message(party_id, survey_id, ru_ref) thread_url = (url_for("secure_message_bp.view_conversation", thread_id=sent_message["thread_id"]) + "#latest-message") flash(Markup(f"Message sent. <a href={thread_url}>View Message</a>")) return redirect(url_for("secure_message_bp.view_conversation_list")) else: unread_message_count = { "unread_message_count": conversation_controller.try_message_count_from_session(session) } return render_template( "secure-messages/secure-messages-view.html", ru_ref=ru_ref, survey_id=survey_id, form=form, errors=form.errors, message={}, unread_message_count=unread_message_count, )
def something_else_post(session): """Sends secure message for the something else pages""" form = SecureMessagingForm(request.form) if not form.validate(): flash(form.errors["body"][0]) return redirect(url_for("account_bp.something_else")) subject = "My account" party_id = session.get_party_id() logger.info("Form validation successful", party_id=party_id) sent_message = _send_new_message(subject, party_id, category="TECHNICAL") thread_url = url_for("secure_message_bp.view_conversation", thread_id=sent_message["thread_id"]) + "#latest-message" flash(Markup(f"Message sent. <a href={thread_url}>View Message</a>")) return redirect(url_for("surveys_bp.get_survey_list", tag="todo"))
def view_conversation(session, thread_id): party_id = session.get('party_id') logger.info("Getting conversation", thread_id=thread_id, party_id=party_id) # TODO, do we really want to do a GET every time, even if we're POSTing? Rops does it this # way so we can get it working, then get it right. conversation = get_conversation(thread_id) logger.info('Successfully retrieved conversation', thread_id=thread_id, party_id=party_id) try: refined_conversation = [ refine(message) for message in reversed(conversation['messages']) ] except KeyError as e: logger.error('Message is missing important data', thread_id=thread_id, party_id=party_id) raise e if refined_conversation[-1]['unread']: remove_unread_label(refined_conversation[-1]['message_id']) form = SecureMessagingForm(request.form) form.subject.data = refined_conversation[0].get('subject') if not conversation['is_closed']: if form.validate_on_submit(): logger.info("Sending message", thread_id=thread_id, party_id=party_id) send_message( _get_message_json(form, refined_conversation[0], party_id=session['party_id'])) logger.info("Successfully sent message", thread_id=thread_id, party_id=party_id) thread_url = url_for("secure_message_bp.view_conversation", thread_id=thread_id) + "#latest-message" flash( Markup('Message sent. <a href={}>View Message</a>'.format( thread_url))) return redirect( url_for('secure_message_bp.view_conversation_list')) return render_template('secure-messages/conversation-view.html', form=form, conversation=refined_conversation, conversation_data=conversation)
def _send_new_message(party_id, survey_id, business_id): logger.info("Attempting to send message", party_id=party_id, business_id=business_id) form = SecureMessagingForm(request.form) subject = form["subject"].data if form["subject"].data else form[ "hidden_subject"].data message_json = { "msg_from": party_id, "msg_to": ["GROUP"], "subject": subject, "body": form["body"].data, "thread_id": form["thread_id"].data, "business_id": business_id, "survey_id": survey_id, } response = conversation_controller.send_message(json.dumps(message_json)) logger.info("Secure message sent successfully", message_id=response["msg_id"], party_id=party_id, business_id=business_id) return response
def post_help_option_select(session, short_name, business_id, option): """Provides additional options once sub options are selected""" if option == 'help-completing-this-survey': form = HelpCompletingThisSurveyForm(request.values) form_valid = form.validate() breadcrumbs_title = help_completing_this_survey_title if form_valid: sub_option = form.data['option'] if sub_option == 'answer-survey-question': return redirect( url_for('surveys_bp.get_send_help_message', short_name=short_name, option=option, business_id=business_id)) if sub_option == 'do-not-have-specific-figures' or sub_option == 'unable-to-return-by-deadline': return redirect( url_for('surveys_bp.get_help_option_sub_option_select', short_name=short_name, option=option, sub_option=sub_option, business_id=business_id)) if form.data['option'] == 'something-else': return render_template( 'secure-messages/help/secure-message-send-messages-view.html', short_name=short_name, option=option, form=SecureMessagingForm(), subject=help_completing_this_survey_title, text_one=breadcrumbs_title, business_id=business_id) else: flash('You need to choose an option') return redirect( url_for('surveys_bp.get_help_option_select', short_name=short_name, business_id=business_id, option=option)) if option == 'info-about-this-survey': form = HelpInfoAboutThisSurveyForm(request.values) form_valid = form.validate() if form_valid: sub_option = form.data['option'] return redirect( url_for('surveys_bp.get_help_option_sub_option_select', short_name=short_name, option=option, business_id=business_id, sub_option=sub_option)) else: flash('You need to choose an option') return redirect( url_for('surveys_bp.get_help_option_select', short_name=short_name, business_id=business_id, option=option)) else: abort(404)
def send_help_technical_message(session): """Sends secure message for the help pages""" form = SecureMessagingForm(request.form) option = request.args.get("option", None) if not form.validate(): flash(form.errors["body"][0]) return redirect( url_for("surveys_bp.get_send_help_technical_message_page", option=option)) else: subject = subject_text_mapping.get(option) party_id = session.get_party_id() logger.info("Form validation successful", party_id=party_id) sent_message = _send_new_message(subject, party_id) thread_url = (url_for("secure_message_bp.view_conversation", thread_id=sent_message["thread_id"]) + "#latest-message") flash(Markup(f"Message sent. <a href={thread_url}>View Message</a>")) return redirect(url_for("surveys_bp.get_survey_list", tag="todo"))
def send_help_message(session, option, sub_option): """Sends secure message for the help pages""" form = SecureMessagingForm(request.form) if not form.validate(): flash(form.errors["body"][0]) return redirect(url_for("surveys_bp.get_send_help_message_page", option=option, sub_option=sub_option)) else: abort_help_if_session_not_set() business_id, ru_ref, short_name, survey, survey_ref = get_selected_survey_business_details() subject = subject_text_mapping.get(sub_option) party_id = session.get_party_id() business_id = business_id logger.info("Form validation successful", party_id=party_id) category = "SURVEY" if option == "info-about-the-ons" or option == "something-else": category = "TECHNICAL" sent_message = _send_new_message(subject, party_id, survey["id"], business_id, category) thread_url = ( url_for("secure_message_bp.view_conversation", thread_id=sent_message["thread_id"]) + "#latest-message" ) flash(Markup(f"Message sent. <a href={thread_url}>View Message</a>")) return redirect(url_for("surveys_bp.get_survey_list", tag="todo"))
def get_send_help_message(session, short_name, business_id, option): """Gets the send message page once the option is selected""" if option == 'help-completing-this-survey': breadcrumbs_title = help_completing_this_survey_title return render_template( 'secure-messages/help/secure-message-send-messages-view.html', short_name=short_name, option=option, form=SecureMessagingForm(), subject='Help answering a survey question', breadcrumb_title_one=breadcrumbs_title, business_id=business_id)
def create_message(session): survey = request.args['survey'] ru_ref = request.args['ru_ref'] party_id = session['party_id'] form = SecureMessagingForm(request.form) if request.method == 'POST' and form.validate(): logger.info("Form validation successful", party_id=party_id) sent_message = send_message(party_id, survey, ru_ref) thread_url = url_for( "secure_message_bp.view_conversation", thread_id=sent_message['thread_id']) + "#latest-message" flash( Markup('Message sent. <a href={}>View Message</a>'.format( thread_url))) return redirect(url_for('secure_message_bp.view_conversation_list')) else: return render_template('secure-messages/secure-messages-view.html', ru_ref=ru_ref, survey=survey, form=form, errors=form.errors, message={})
def _send_new_message(subject, party_id, category): logger.info("Attempting to send message", party_id=party_id) form = SecureMessagingForm(request.form) message_json = { "msg_from": party_id, "msg_to": ["GROUP"], "subject": subject, "body": form["body"].data, "thread_id": form["thread_id"].data, "category": category, } response = conversation_controller.send_message(json.dumps(message_json)) logger.info("Secure message sent successfully", message_id=response["msg_id"], party_id=party_id) return response
def get_send_help_message_page(session, short_name, business_id, option, sub_option): """Gets the send message page once the option and sub option is selected""" subject = subject_text_mapping.get(sub_option) text = breadcrumb_text_mapping.get(sub_option) return render_template( 'secure-messages/help/secure-message-send-messages-view.html', short_name=short_name, option=option, sub_option=sub_option, form=SecureMessagingForm(), subject=subject, breadcrumb_title_one=text[0], breadcrumb_title_two=text[1], business_id=business_id)
def get_send_help_message_page(session, option, sub_option): """Gets the send message page once the option and sub option is selected""" abort_help_if_session_not_set() business_id, ru_ref, short_name, survey, survey_ref = get_selected_survey_business_details() subject = subject_text_mapping.get(sub_option) breadcrumb_text = breadcrumb_text_mapping.get(sub_option, None) breadcrumb_title_one = breadcrumb_text[0] if len(breadcrumb_text) > 0 else None breadcrumb_title_two = breadcrumb_text[1] if len(breadcrumb_text) > 1 else None return render_template( "secure-messages/help/secure-message-send-messages-view.html", short_name=short_name, option=option, sub_option=sub_option, form=SecureMessagingForm(), subject=subject, breadcrumb_title_one=breadcrumb_title_one, breadcrumb_title_two=breadcrumb_title_two, business_id=business_id, survey_ref=survey_ref, ru_ref=ru_ref, )
def send_message(party_id, survey, ru_ref): logger.info('Attempting to send message', party_id=party_id) form = SecureMessagingForm(request.form) subject = form['subject'].data if form['subject'].data else form[ 'hidden_subject'].data message_json = { "msg_from": party_id, "msg_to": ['GROUP'], "subject": subject, "body": form['body'].data, "thread_id": form['thread_id'].data, "ru_id": ru_ref, "survey": survey, } response = conversation_controller.send_message(json.dumps(message_json)) logger.info('Secure message sent successfully', message_id=response['msg_id'], party_id=party_id) return response
def _send_new_message(subject, party_id, survey, business_id): logger.info('Attempting to send message', party_id=party_id, business_id=business_id) form = SecureMessagingForm(request.form) message_json = { "msg_from": party_id, "msg_to": ['GROUP'], "subject": subject, "body": form['body'].data, "thread_id": form['thread_id'].data, "business_id": business_id, "survey": survey, } response = conversation_controller.send_message(json.dumps(message_json)) logger.info('Secure message sent successfully', message_id=response['msg_id'], party_id=party_id, business_id=business_id) return response
def view_conversation(session, thread_id): """Endpoint to view conversations by thread_id""" party_id = session.get_party_id() logger.info("Getting conversation", thread_id=thread_id, party_id=party_id) conversation = get_conversation(thread_id) # secure message will send category in case the conversation is technical or miscellaneous is_survey_category = ( False if "category" in conversation and conversation["category"] in ["TECHNICAL", "MISC"] else True) # sets appropriate message category category = "SURVEY" if is_survey_category else conversation["category"] logger.info("Successfully retrieved conversation", thread_id=thread_id, party_id=party_id) try: refined_conversation = [ refine(message) for message in reversed(conversation["messages"]) ] except KeyError: logger.error("Message is missing important data", thread_id=thread_id, party_id=party_id) raise if refined_conversation[-1]["unread"]: remove_unread_label(refined_conversation[-1]["message_id"]) form = SecureMessagingForm(request.form) form.subject.data = refined_conversation[0].get("subject") if not conversation["is_closed"]: if form.validate_on_submit(): logger.info("Sending message", thread_id=thread_id, party_id=party_id) msg_to = get_msg_to(refined_conversation) if is_survey_category: send_message( _get_message_json(form, refined_conversation[0], msg_to=msg_to, msg_from=party_id)) else: send_message( _get_non_survey_message_json(form, refined_conversation[0], msg_to=msg_to, msg_from=party_id, category=category)) logger.info("Successfully sent message", thread_id=thread_id, party_id=party_id) thread_url = url_for("secure_message_bp.view_conversation", thread_id=thread_id) + "#latest-message" flash( Markup(f"Message sent. <a href={thread_url}>View Message</a>")) return redirect( url_for("secure_message_bp.view_conversation_list")) unread_message_count = { "unread_message_count": get_message_count_from_api(session) } survey_name = None business_name = None if is_survey_category: try: survey_name = get_survey( app.config["SURVEY_URL"], app.config["BASIC_AUTH"], refined_conversation[-1]["survey_id"]).get("longName") except ApiError as exc: logger.info("Failed to get survey name, setting to None", status_code=exc.status_code) try: business_name = conversation["messages"][-1]["@business_details"][ "name"] except (KeyError, TypeError): logger.info("Failed to get business name, setting to None") return render_template( "secure-messages/conversation-view.html", form=form, conversation=refined_conversation, conversation_data=conversation, unread_message_count=unread_message_count, survey_name=survey_name, business_name=business_name, category=category, )
def something_else(session): """Gets the something else once the option is selected""" return render_template("account/account-something-else.html", form=SecureMessagingForm())
def view_conversation(session, thread_id): """Endpoint to view conversations by thread_id""" party_id = session.get_party_id() logger.info("Getting conversation", thread_id=thread_id, party_id=party_id) conversation = get_conversation(thread_id) logger.info('Successfully retrieved conversation', thread_id=thread_id, party_id=party_id) try: refined_conversation = [ refine(message) for message in reversed(conversation['messages']) ] except KeyError: logger.error('Message is missing important data', thread_id=thread_id, party_id=party_id) raise if refined_conversation[-1]['unread']: remove_unread_label(refined_conversation[-1]['message_id']) form = SecureMessagingForm(request.form) form.subject.data = refined_conversation[0].get('subject') if not conversation['is_closed']: if form.validate_on_submit(): logger.info("Sending message", thread_id=thread_id, party_id=party_id) msg_to = get_msg_to(refined_conversation) send_message( _get_message_json(form, refined_conversation[0], msg_to=msg_to, msg_from=party_id)) logger.info("Successfully sent message", thread_id=thread_id, party_id=party_id) thread_url = url_for("secure_message_bp.view_conversation", thread_id=thread_id) + "#latest-message" flash( Markup(f'Message sent. <a href={thread_url}>View Message</a>')) return redirect( url_for('secure_message_bp.view_conversation_list')) unread_message_count = { 'unread_message_count': get_message_count_from_api(session) } survey_name = None try: survey_name = get_survey( app.config['SURVEY_URL'], app.config['BASIC_AUTH'], refined_conversation[-1]['survey_id']).get('longName') except ApiError as exc: logger.info('Failed to get survey name, setting to None', status_code=exc.status_code) business_name = None try: business_name = conversation['messages'][-1]['@business_details'][ 'name'] except KeyError: logger.info('Failed to get business name, setting to None') return render_template('secure-messages/conversation-view.html', form=form, conversation=refined_conversation, conversation_data=conversation, unread_message_count=unread_message_count, survey_name=survey_name, business_name=business_name)