Search code examples
pythonmysqlrestflaskflask-restful

using another WS as validation Flask/Rest/Mysql


I am trying to build a simple web application with 3 web services. Two of my web services are supposed to validate if a student exist in a course or not. This is done by a simple SELECT-query. My third web service should add a student into a database, but only if the student do exist in the specific course.

This is my validation WS which should return a true/false.

@app.route('/checkStudOnCourse/<string:AppCode>/<string:ideal>', methods= ["GET"])
def checkStudOnCourseWS(AppCode, ideal):

myCursor3 = mydb.cursor()
query3 = ("SELECT studentID FROM Ideal.course WHERE applicationCode = " + "'" + AppCode + "' AND Ideal = " + "'" + ideal + "'")
myCursor3.execute(query3)
myresult3 = myCursor3.fetchall()

if len(myresult3) == 0:
    return render_template('Invalid.html')
else:
    return jsonify({'Student in course ': True})

Below is regResult which should do a SQL insert into a database. I only want the submit to work if the above result is "True", how can I do that? I know I have not done the INSERT query, but that is not a problem. What I am unsure about is: How can I only let the submit be be INSERTED if the validation WS is "True".

@app.route('/register', methods=["POST", "GET"])
def regResultat():


if request.method == "POST":

    Period = request.form['period']
    #ProvNr = request.form['provNr']
    Grade = request.form['grade']
    Applicationcode = request.form['applicationcode']
    #Datum = request.form['datum']
    Ideal = request.form['ideal']

CheckStudOnCourse = 'http://127.0.0.1:5000/checkAppCodeWS/'+Applicationcode+'/'+Ideal
CheckStudOnResp = requests.get(CheckStudOnCourse)

Solution

  • At first, such syntax:

    if len(myresult3) == 0, can be simplified by if myresult3, because Python evaluates that implicitly to bool.

    Secondly, if you once returned from function, there is no need to write an else statement:

        if len(myresult3) == 0:
            return render_template('Invalid.html')  #  < -- in case 'True',
                                                    #  it returns here, otherwise 
                                                    #  function keeps going"""
    
         return jsonify({'Student in course ': True})  # < -- in case 'False', it is returned here
    

    Focusing on your issue, you could do that:

    Get your value from ws

    CheckStudOnCourse = 'http://127.0.0.1:5000/checkAppCodeWS/'+Applicationcode+'/'+Ideal
    CheckStudOnResp = requests.get(CheckStudOnCourse)
    

    Extract json from it:

    if result_as_json.status == 200:
        result_as_json = CheckStudOnResp.json()  #  < -- it is now a dict
    

    Do some checks:

    if result_as_json.get('Student in course', False):  #  I highly suggest to use other 
                                                       #  convention to name json keys 
                                                       #  e.g. Student in course -> 
                                                       #  student_exists_in_course
        #  do your code here