Showing flash messages before redirect in bootstrap modal

Viewed 24

I have two routes each of them , I would like to show flash messages before redirect from one to another (ScoreDecision to AverageDecision). In Score Decision page when you click into submit button with average True modal pops out empty and redirect to average decision with filled forms. At this route when you click submit, modal shows the first message that I would like to show. I have read almost every post related with flash messaging in modal but could not find any suggestions for this. I read that flask holds the messages session based is this problem somehow because session ? any help would be highly appreciated Thanks.

ScoreDecision


@app.route('/decision', methods=['GET', 'POST'])
def scoreDecision():
    formDecision = Decisionform()
    if formDecision.average_active.data == True and request.method == 'POST':
        flash('Are you sure')
        return redirect(url_for('averageDecision'))

    res = {
        "inputs": [
            {"name": "JOB_",
                     "value": ''},
            {"name": "LOAN_",
                     "value": formDecision.loan.data},
            {"name": "MORTDUE_",
                     "value": formDecision.mortdue.data},
            {"name": "REASON_",
                     "value": formDecision.reason.data},
            {"name": "YOJ_",
                     "value": formDecision.yoj.data},
            {"name": "DEBTINC_",
                     "value": ''},
            {"name": "DELINQ_",
                     "value": formDecision.delinq.data},
            {"name": "DEROG_",
                     "value": formDecision.derog.data},
            {"name": "CLAGE_",
                     "value": formDecision.clage.data},
            {"name": "NINQ_",
                     "value": formDecision.delinq.data},
            {"name": "VALUE_",
                     "value": formDecision.value.data},
            {"name": "CLNO_",
                     "value": formDecision.clno.data}
        ]}
    if request.method == 'POST' and formDecision.validate_on_submit:
        debtinc = .3
        # if formDecision.reason.data == 'Debt Consolidation':
        #     res['inputs'][3] = {'name': 'REASON_', 'value': 'DebtCon'}
        # elif formDecision.reason.data == 'Home Improvement':
        #     res['inputs'][3] = {'name': 'REASON_', 'value': 'HomeImp'}
        # else:
        #     res['inputs'][3] = {'name': 'REASON_', 'value': 'Other'}
        # res['inputs'][5] = {'name': 'DEBTINC_', 'value': debtinc}
        if formDecision.job.data == 'Sales' and 41000 <= formDecision.income.data <= 89000 and debtinc < .5:
            res['inputs'][0] = {'name': 'JOB_', 'value': formDecision.job.data}
            make_call(res)
        elif formDecision.job.data == 'Sales' and formDecision.income.data < 40000:
            flash('Your income and debt looks odd are you sure you want to continue ? ')
            make_call(res)

        elif formDecision.job.data == 'Manager' and 44000 <= formDecision.income.data <= 107000 and debtinc < .5:
            res['inputs'][0] = {'name': 'JOB_', 'value': 'Mgr'}
            flash('Your income and debt looks odd are you sure you want to continue ? ')
            make_call(res)
        elif formDecision.job.data == 'Manager' and formDecision.income.data < 40000:
            flash('Your income and debt looks odd are you sure you want to continue ? ')
            make_call(res)
        elif formDecision.job.data in ['Office', 'Other'] and 18000 <= formDecision.income.data <= 71000 and debtinc < .5:
            res['inputs'][0] = {'name': 'JOB_', 'value': formDecision.job.data}
            flash('Your income and debt looks odd are you sure you want to continue ? ')
            make_call(res)
        elif formDecision.job.data == 'Executive Professor' and 69000 <= formDecision.income.data <= 200000 and debtinc < .5:
            res['inputs'][0] = {'name': 'JOB_', 'value': 'ProfExe'}
            flash('Your income and debt looks odd are you sure you want to continue ? ')
            make_call(res)
        elif formDecision.job.data == 'Self' and 30000 <= formDecision.income.data <= 350000 and debtinc < .5:
            res['inputs'][0] = {'name': 'JOB_', 'value': formDecision.job.data}
            flash('Your income and debt looks odd are you sure you want to continue ? ')
            make_call(res)
        elif debtinc > .5:
            flash('Your income and debt looks odd are you sure you want to continue ? ')
            return redirect(url_for('odd-input'))
        # else:
        #     return render_template('404.html')
    return render_template('inputs.html', form=formDecision)

AverageDecision


@app.route('/decision-average', methods=['GET', 'POST'])
def averageDecision():
    averageDecisionForm = Decisionform()
    averageDecisionForm.loan.data = str(randrange(1800,1900))
    averageDecisionForm.mortdue.data = str(randrange(72000,73769))
    averageDecisionForm.reason.data = "Reason"
    averageDecisionForm.yoj.data = str(randrange(5,10))
    averageDecisionForm.delinq.data = "0"
    averageDecisionForm.derog.data = "0"
    averageDecisionForm.clage.data = str(randrange(170,190))
    averageDecisionForm.delinq.data = "0"
    averageDecisionForm.value.data = str(randrange(101500,102000))
    averageDecisionForm.clno.data = str(randrange(18,22))
    averageDecisionForm.ninq.data = "1"
    averageDecisionForm.income.data = str(randrange(63000,67000))
    averageDecisionForm.currentDebt.data = str(randrange(1900,2100))
    averageDecisionForm.job.data = "Self"
    averageDecisionForm.reason.data = "Home Improvement"

    avg_req = {
        "inputs": [
            {"name": "JOB_",
                     "value": averageDecisionForm.job.data},
            {"name": "LOAN_",
                     "value": averageDecisionForm.loan.data},
            {"name": "MORTDUE_",
                     "value": averageDecisionForm.mortdue.data},
            {"name": "REASON_",
                     "value": "HomeImp"},
            {"name": "YOJ_",
                     "value": averageDecisionForm.yoj.data},
            {"name": "DEBTINC_",
                     "value": "33"},
            {"name": "DELINQ_",
                     "value": averageDecisionForm.delinq.data},
            {"name": "DEROG_",
                     "value": averageDecisionForm.derog.data},
            {"name": "CLAGE_",
                     "value": averageDecisionForm.clage.data},
            {"name": "NINQ_",
                     "value": averageDecisionForm.delinq.data},
            {"name": "VALUE_",
                     "value": averageDecisionForm.value.data},
            {"name": "CLNO_",
                     "value": averageDecisionForm.clno.data}
        ]}
    if request.method == 'POST' and averageDecisionForm.validate_on_submit:
        flash('you are about to submit inputs are you sure')
        make_call(avg_req)

    return render_template('averageInputs.html', form=averageDecisionForm)

Finally my templates

ScoreDecision

    <div class="container">
      <div id="confirm">
        <button type="button" class="btn btn-danger" data-toggle="modal" data-target="#MyFancyModal">Submit
        </button>
        <div id="MyFancyModal" class="modal fade" role="dialog">
          <div class="modal-dialog">
            <div class="modal-content">
              <div class="modal-header">Please confirm</div>
              <div class="modal-body">
                <ul class="flashes">
                  {% with messages = get_flashed_messages() %} {% if messages %}
                      {% for message in messages %}
                      <li>{{ message }}</li>
                      {% endfor %}
                  {% endif %} {% endwith %}
                </ul>
              </div>
              <div class="modal-footer">
                <form action="{{ decision }}" method="post">
                  <button type="submit" class="btn btn-danger">Yes</button>
                  <button class="btn btn-default" data-dismiss="modal">
                    No
                  </button>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </form>
  {% block scripts %}
  {{ super() }}
  <script>$(document).ready(function() {
    var messages = "{{ get_flashed_messages() }}";

    if (typeof messages != 'undefined' || messages != '[]') {
        $("#MyFancyModal").modal();
    };
});
  </script>
  {% endblock %}
  {% endblock %}
</body>

Average

        <div class="container">
            <div id="confirm">
                <button type="button" class="btn btn-danger" data-toggle="modal" data-target="#MyFancyModal">Submit
                </button>
                <div id="MyFancyModal" class="modal fade" role="dialog">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-header">Please confirm</div>
                            <div class="modal-body" id="flashes">
                                {% with messages = get_flashed_messages() %} {% if messages %}
                                <ul>
                                    {% for message in messages %}
                                    <li>{{ message }}</li>
                                    {% endfor %}
                                </ul>
                                {% endif %}{% endwith %}
                            </div>
                            <div class="modal-footer">
                                <form action="{{ averageDecision }}" method="post">
                                    <button type="submit" class="btn btn-danger">Yes</button>
                                    <button class="btn btn-default" data-dismiss="modal">
                                        No
                                    </button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>
    {% block javascript %}
    <script>
        $(document).ready(function() {
    var messages = "{{ get_flashed_messages() }}";

    if (typeof messages != 'undefined' || messages != '[]') {
        $("#MyFancyModal").modal();
    };
});
    </script>
    {% endblock %}
    {% endblock %}
</body>

0 Answers
Related