示例#1
0
def post_change_thread_category(thread_id):  # noqa: C901
    thread = message_controllers.get_conversation(thread_id)
    form = ChangeThreadCategoryForm(request.form)

    if form.validate():
        category = form.category.data

        # If the category is SURVEY then we need to go to another page to set the business_id for the thread also.
        if category == "SURVEY":
            session["secure-message-change-survey-shortname"] = form.select_survey.data
            return redirect(url_for("messages_bp.get_change_reporting_unit", thread_id=thread_id))

        payload = {"category": category}
        try:
            message_controllers.patch_thread(thread_id, payload)
        except ApiError:
            flash("Something went wrong updating the category. Please try again.", category="error")
            return redirect(url_for("messages_bp.get_change_thread_category", thread_id=thread_id))

        flash("The category has been successfully updated")
        return redirect(url_for("messages_bp.view_select_survey"))

    breadcrumbs = [{"text": "Messages", "url": "/messages"}, {"text": "Filter by survey"}]
    survey_list = get_business_survey_shortname_list()

    return render_template(
        "secure-message/change-thread-category.html",
        thread=thread,
        thread_id=thread_id,
        breadcrumbs=breadcrumbs,
        survey_list=survey_list,
        form=form,
    )
示例#2
0
def get_change_reporting_unit(thread_id):
    thread = message_controllers.get_conversation(thread_id)
    reporting_units = get_respondent_enrolments_from_thread(thread)

    breadcrumbs = [{"text": "Messages", "url": "/messages"}, {"text": "Filter by survey"}]

    return render_template(
        "secure-message/change-reporting-unit.html",
        thread=thread,
        thread_id=thread_id,
        breadcrumbs=breadcrumbs,
        reporting_units=reporting_units,
    )
示例#3
0
def close_conversation(thread_id):
    conversation_tab = request.args.get("conversation_tab")
    page = request.args.get("page")
    ru_ref_filter = request.args.get("ru_ref_filter")
    business_id_filter = request.args.get("business_id_filter")
    category = request.args.get("category")

    if request.method == "POST":
        payload = {"is_closed": True}
        message_controllers.patch_thread(thread_id, payload)
        thread_url = (
            url_for(
                "messages_bp.view_conversation",
                thread_id=thread_id,
                conversation_tab=conversation_tab,
                page=page,
                ru_ref_filter=ru_ref_filter,
                business_id_filter=business_id_filter,
            )
            + "#latest-message"
        )

        flash(Markup(f"Conversation closed. <a href={thread_url}>View conversation</a>"))
        return redirect(
            url_for(
                "messages_bp.view_select_survey",
                page=request.args.get("page"),
                conversation_tab=conversation_tab,
                ru_ref_filter=ru_ref_filter,
                business_id_filter=business_id_filter,
            )
        )

    thread_conversation = message_controllers.get_conversation(thread_id)
    refined_thread = [_refine(message) for message in reversed(thread_conversation["messages"])]

    return render_template(
        "close-conversation.html",
        subject=refined_thread[0]["subject"],
        business=refined_thread[0]["business_name"],
        ru_ref=refined_thread[0]["ru_ref"],
        respondent=refined_thread[0]["to"] if refined_thread[0]["internal"] is True else refined_thread[0]["from"],
        thread_id=thread_id,
        page=page,
        conversation_tab=conversation_tab,
        ru_ref_filter=ru_ref_filter,
        business_id_filter=business_id_filter,
        category=category,
    )
示例#4
0
def post_change_reporting_unit(thread_id):  # noqa: C901
    thread = message_controllers.get_conversation(thread_id)
    reporting_unit = request.form.get("reporting-units")
    if reporting_unit:
        message_patch_payload = {"business_id": reporting_unit}

        # You can come to this page after changing a thread category to SURVEY, so we'll add the survey_id to
        # the message patch
        survey_shortname = session.get("secure-message-change-survey-shortname")
        if survey_shortname:
            survey_id = survey_controllers.get_survey_id_by_short_name(survey_shortname)
            message_patch_payload["survey_id"] = survey_id

        # Patch messages first. It's the most 'risky' as it does multiple calls
        for message in thread["messages"]:
            message_id = message["msg_id"]
            try:
                message_controllers.patch_message(message_id, message_patch_payload)
            except ApiError:
                flash("Something went wrong updating the survey.  Please try again.", category="error")
                return redirect(url_for("messages_bp.get_change_reporting_unit", thread_id=thread_id))

        flash("The survey has been successfully updated.")

        # Next, if there a survey_id in the session, patch the category to SURVEY as that's the only thing
        # it can be
        if survey_shortname:
            payload = {"category": "SURVEY"}
            try:
                message_controllers.patch_thread(thread_id, payload)
            except ApiError:
                flash("Something went wrong updating the category. Please try again.", category="error")
                return redirect(url_for("messages_bp.get_change_thread_category", thread_id=thread_id))
            flash("The category has been successfully updated")
            del session["secure-message-change-survey-shortname"]

        return redirect(url_for("messages_bp.view_select_survey"))

    flash("An option must be selected", category="error")
    reporting_units = get_respondent_enrolments_from_thread(thread)
    breadcrumbs = [{"text": "Messages", "url": "/messages"}, {"text": "Filter by survey"}]

    return render_template(
        "secure-message/change-reporting-unit.html",
        thread=thread,
        thread_id=thread_id,
        breadcrumbs=breadcrumbs,
        reporting_units=reporting_units,
    )
示例#5
0
def get_change_thread_category(thread_id):
    thread = message_controllers.get_conversation(thread_id)
    form = ChangeThreadCategoryForm()
    breadcrumbs = [{"text": "Messages", "url": "/messages"}, {"text": "Filter by survey"}]

    survey_list = get_business_survey_shortname_list()

    return render_template(
        "secure-message/change-thread-category.html",
        thread=thread,
        thread_id=thread_id,
        breadcrumbs=breadcrumbs,
        survey_list=survey_list,
        form=form,
    )
示例#6
0
def close_conversation(thread_id):
    conversation_tab = request.args.get('conversation_tab')
    page = request.args.get('page')

    ru_ref_filter = request.args.get('ru_ref_filter')

    business_id_filter = request.args.get('business_id_filter')

    if request.method == 'POST':
        message_controllers.update_close_conversation_status(
            thread_id=thread_id, status=True)
        thread_url = url_for(
            "messages_bp.view_conversation",
            thread_id=thread_id,
            conversation_tab=conversation_tab,
            page=page,
            ru_ref_filter=ru_ref_filter,
            business_id_filter=business_id_filter) + "#latest-message"

        flash(
            Markup(
                f'Conversation closed. <a href={thread_url}>View conversation</a>'
            ))
        return redirect(
            url_for('messages_bp.view_select_survey',
                    page=request.args.get('page'),
                    conversation_tab=conversation_tab,
                    ru_ref_filter=ru_ref_filter,
                    business_id_filter=business_id_filter))

    thread_conversation = message_controllers.get_conversation(thread_id)
    refined_thread = [
        _refine(message)
        for message in reversed(thread_conversation['messages'])
    ]

    return render_template('close-conversation.html',
                           subject=refined_thread[0]['subject'],
                           business=refined_thread[0]['business_name'],
                           ru_ref=refined_thread[0]['ru_ref'],
                           respondent=refined_thread[0]['to'],
                           thread_id=thread_id,
                           page=page,
                           conversation_tab=conversation_tab,
                           ru_ref_filter=ru_ref_filter,
                           business_id_filter=business_id_filter)
示例#7
0
def view_conversation(thread_id):
    conversation_tab = request.args.get('conversation_tab')
    page = request.args.get('page')

    ru_ref_filter = request.args.get('ru_ref_filter')
    business_id_filter = request.args.get('business_id_filter')

    if request.method == 'POST' and request.form.get('reopen'):
        message_controllers.update_close_conversation_status(
            thread_id=thread_id, status=False)
        thread_url = url_for(
            "messages_bp.view_conversation",
            thread_id=thread_id,
            conversation_tab=conversation_tab,
            page=page,
            ru_ref_filter=ru_ref_filter,
            business_id_filter=business_id_filter) + "#latest-message"
        flash(
            Markup(
                f'Conversation re-opened. <a href={thread_url}>View conversation</a>'
            ))
        return redirect(
            url_for('messages_bp.view_select_survey',
                    conversation_tab=conversation_tab,
                    page=page,
                    ru_ref_filter=ru_ref_filter,
                    business_id_filter=business_id_filter))

    thread_conversation = message_controllers.get_conversation(thread_id)
    refined_thread = [
        _refine(message)
        for message in reversed(thread_conversation['messages'])
    ]
    latest_message = refined_thread[-1]
    closed_at = _format_closed_at(thread_conversation)
    breadcrumbs = _get_conversation_breadcrumbs(
        thread_conversation['messages'])
    respondent_is_deleted = False

    for message in refined_thread:
        if 'Deleted respondent' in message['username']:
            respondent_is_deleted = True

    if latest_message['unread'] and _can_mark_as_unread(latest_message):
        message_controllers.remove_unread_label(latest_message['message_id'])

    form = SecureMessageForm(request.form)

    if form.validate_on_submit():
        form = _populate_form_details_from_hidden_fields(form)
        g.form_subject_data = form.subject.data
        g.form_body_data = form.body.data

        try:
            message_controllers.send_message(
                _get_message_json(form,
                                  thread_id=refined_thread[0]['thread_id']))
            thread_url = url_for(
                "messages_bp.view_conversation",
                thread_id=thread_id,
                page=page,
                conversation_tab=conversation_tab,
                ru_ref_filter=ru_ref_filter,
                business_id_filter=business_id_filter) + "#latest-message"
            flash(
                Markup(f'Message sent. <a href={thread_url}>View Message</a>'))
            return redirect(
                url_for('messages_bp.view_selected_survey',
                        selected_survey=refined_thread[0]['survey'],
                        page=page,
                        conversation_tab=conversation_tab,
                        ru_ref_filter=ru_ref_filter,
                        business_id_filter=business_id_filter))

        except (ApiError, InternalError) as e:
            error = "Message failed to send, something has gone wrong with the website."
            if e.status_code == 404 and "Respondent not found" in e.message:
                error = "Cannot send message to respondent as they have been deleted"

            form = _repopulate_form_with_submitted_data(form)
            form.errors['sending'] = [error]
            return render_template('conversation-view/conversation-view.html',
                                   form=form,
                                   breadcrumbs=breadcrumbs,
                                   messages=refined_thread,
                                   respondent_is_deleted=respondent_is_deleted,
                                   thread_data=thread_conversation)

    return render_template(
        "conversation-view/conversation-view.html",
        breadcrumbs=breadcrumbs,
        messages=refined_thread,
        respondent_is_deleted=respondent_is_deleted,
        form=form,
        selected_survey=refined_thread[0]['survey'],
        page=page,
        closed_at=closed_at,
        thread_data=thread_conversation,
        show_mark_unread=_can_mark_as_unread(latest_message),
        conversation_tab=conversation_tab,
        ru_ref_filter=ru_ref_filter,
        business_id_filter=business_id_filter)
示例#8
0
def view_conversation(thread_id):
    conversation_tab = request.args.get("conversation_tab")
    page = request.args.get("page")
    ru_ref_filter = request.args.get("ru_ref_filter")
    business_id_filter = request.args.get("business_id_filter")

    if request.method == "POST" and request.form.get("reopen"):
        payload = {"is_closed": False}
        message_controllers.patch_thread(thread_id, payload)
        thread_url = (
            url_for(
                "messages_bp.view_conversation",
                thread_id=thread_id,
                conversation_tab=conversation_tab,
                page=page,
                ru_ref_filter=ru_ref_filter,
                business_id_filter=business_id_filter,
            )
            + "#latest-message"
        )
        flash(Markup(f"Conversation re-opened. <a href={thread_url}>View conversation</a>"))
        return redirect(
            url_for(
                "messages_bp.view_select_survey",
                conversation_tab=conversation_tab,
                page=page,
                ru_ref_filter=ru_ref_filter,
                business_id_filter=business_id_filter,
            )
        )

    thread_conversation = message_controllers.get_conversation(thread_id)
    category = thread_conversation["category"]
    refined_thread = [_refine(message, category) for message in reversed(thread_conversation["messages"])]
    latest_message = refined_thread[-1]
    closed_at = _format_closed_at(thread_conversation)
    breadcrumbs = _get_conversation_breadcrumbs(thread_conversation["messages"])
    respondent_is_deleted = False
    for message in refined_thread:
        if "Deleted respondent" in message["username"]:
            respondent_is_deleted = True
    if latest_message["unread"] and _can_mark_as_unread(latest_message):
        message_controllers.remove_unread_label(latest_message["message_id"])

    form = SecureMessageForm(request.form)

    if form.validate_on_submit():
        form = _populate_form_details_from_hidden_fields(form)
        g.form_subject_data = form.subject.data
        g.form_body_data = form.body.data

        try:
            if category != "SURVEY":
                message_controllers.send_message(
                    _get_non_survey_message_json(
                        form, thread_id=refined_thread[0]["thread_id"], category=thread_conversation["category"]
                    )
                )
            else:
                message_controllers.send_message(_get_message_json(form, thread_id=refined_thread[0]["thread_id"]))
            thread_url = (
                url_for(
                    "messages_bp.view_conversation",
                    thread_id=thread_id,
                    page=page,
                    conversation_tab=conversation_tab,
                    ru_ref_filter=ru_ref_filter,
                    business_id_filter=business_id_filter,
                )
                + "#latest-message"
            )
            flash(Markup(f"Message sent. <a href={thread_url}>View Message</a>"))
            return redirect(
                url_for(
                    "messages_bp.view_select_survey",
                    page=page,
                    conversation_tab=conversation_tab,
                    ru_ref_filter=ru_ref_filter,
                    business_id_filter=business_id_filter,
                )
            )

        except (ApiError, InternalError) as e:
            error = "Message failed to send, something has gone wrong with the website."
            if e.status_code == 404 and "Respondent not found" in e.message:
                error = "Cannot send message to respondent as they have been deleted"

            form = _repopulate_form_with_submitted_data(form)
            flash(error, "sending")
            return render_template(
                "conversation-view/conversation-view.html",
                form=form,
                breadcrumbs=breadcrumbs,
                messages=refined_thread,
                respondent_is_deleted=respondent_is_deleted,
                thread_data=thread_conversation,
            )

    return render_template(
        "conversation-view/conversation-view.html",
        breadcrumbs=breadcrumbs,
        messages=refined_thread,
        respondent_is_deleted=respondent_is_deleted,
        form=form,
        selected_survey=refined_thread[0]["survey"],
        page=page,
        closed_at=closed_at,
        thread_data=thread_conversation,
        show_mark_unread=_can_mark_as_unread(latest_message),
        conversation_tab=conversation_tab,
        ru_ref_filter=ru_ref_filter,
        business_id_filter=business_id_filter,
    )