Why is the below function giving error-('NoneType' object is not subscriptable) 
Snippet:
@app.route('/AddAppointment',methods=['GET','POST'])
def AddAppointment():
    # token=request.args.get('token')

    # if not token:
    #     return jsonify({'MSG':'Token is missing'})
    session=Session()
    try:

        if(request.method == "POST"):

            if('Authorization' in request.headers):
                token= request.headers.get('Authorization')
                BranchId= request.headers.get('branchId')
                if not token:
                    return jsonify({'MSG':'Token is missing'})
                data = jwt.decode(token,app.config['SECRET_KEY'], algorithms=['HS256', ])
                if(data):
            # BranchId= 10
            
                    request_json = request.get_json()
                    PID = request_json.get('pname')
                    

                    fcmToken = session.query(Model.models.Application.M_PatientsDtl.MPD_TokenFCM.label('token')  # Select the MPD_TokenFCM column from M_PatientsDtl
                    ).join(Model.models.Application.M_Patient,  # Specify the first table to join
                        Model.models.Application.M_Patient.MP_Mobile == Model.models.Application.M_PatientsDtl.MPD_MobNoCountryCode  # Join condition
                    ).filter(
                        Model.models.Application.M_Patient.MPID == PID  # Your WHERE clause condition
                    ).first()  # For fetching a single result or None if not found

                    # session.close()
                    #Id = request_json.get('pid')
                    ID = request_json.get('ID')
                    if(ID=='' or ID==None):
                        # request_json = request.get_json()
                        request_json = request.get_json(force = True)
                        pname = request_json.get('pname')
                        procedure = request_json.get('procedure')
                        date = request_json.get('date')
                        time = request_json.get('time')
                        duration = request_json.get('duration')
                        appointmentType = request_json.get('appointmentType')
                        doctor = request_json.get('doctor')
                        notify = request_json.get('notify')
                        status = request_json.get('status')
                        # note = request_json.get('note')
                        currentdate = datetime.datetime.now().strftime('%Y%m%d')
                        currenttime = datetime.datetime.now().strftime('%H%M')
                        appdate1 = datetime.datetime.strptime(date,'%Y-%m-%d')
                        appdate = appdate1.strftime('%Y%m%d')
                        apptime1 = datetime.datetime.strptime(time,'%H:%M')
                        apptime = apptime1.strftime('%H%M')
                        appdatetime = str(appdate)+str(apptime)
                        doctorout = session.query(Model.models.Application.DoctorOutoffice.DOID
                                                    #).filter_by(DO_FromDate=date,DO_ToDate=date,DO_DoctorId=doctor,DO_IsActive=1,DO_IsDeleted=0
                                                    ).filter_by(DO_DoctorId=doctor,DO_IsActive=1,DO_IsDeleted=0
                                                    ).filter(sqlalchemy.func.date_format(Model.models.Application.DoctorOutoffice.DO_FromDate,'%Y%m%d%H%M')>=appdatetime
                                                    ).all()
                        print(doctorout)

                        checkdoctor = session.query(Model.models.Application.M_Appointment.MAID
                                                    ).filter_by(MA_Date=date,M_DoctorDetails_MDDID=doctor,MP_IsActive=1,MP_IsDeleted=0,MP_IsCancelled=0
                                                    ).filter(Model.models.Application.M_Appointment.MA_Time==time).all()
                        if(len(checkdoctor)==0):
                            # if(currentdate == appdate or currentdate < appdate):
                            #     if((currentdate == appdate and (apptime >= currenttime and  apptime <='2000')) or (currentdate <= appdate and ('0800' <= apptime and  apptime <='2000'))):
                            if('0800' <= apptime and  apptime <='2000'):        
                                print('hello')
                                print(currentdate,currenttime)
                                Insert=Model.models.Application.M_Appointment()
                                Insert.M_Patient_MPID=pname
                                Insert.MP_Procedure=procedure
                                Insert.MA_Date=date
                                Insert.MA_Time=time
                                Insert.MP_Duration=duration
                                Insert.MP_AppointmentType=appointmentType
                                Insert.M_DoctorDetails_MDDID=doctor
                                # Insert.M_Organisation_MOID=org
                                Insert.M_Branch_MBID=int(BranchId)
                                # Insert.M_Room_MRID=room
                                Insert.MP_Status=status
                                # Insert.MP_Notes=note
                                Insert.MP_AddIP= flask.request.remote_addr
                                Insert.MP_AddUser = data['id']
                                Insert.MP_AddDate = datetime.datetime.now()
                                session.add(Insert)
                                session.commit()
                                        #return "inserted Successfully"
                                
                                ORGID= session.query(Model.models.Application.M_Appointment.MAID).order_by(Model.models.Application.M_Appointment.MAID.desc()).first()

                                if(notify==True):
                                    PatName = session.query(Model.models.Application.M_Patient.MP_Name,
                                                        Model.models.Application.M_Patient.MP_Mobile,
                                                    ).filter_by(MPID=pname,MP_IsActive=1,MP_IsDeleted=0).all()
                                    CategoryName= session.query(Model.models.Application.M_Service.CategoryName
                                                                ).filter(Model.models.Application.M_Service.MSID==procedure).first()
                                    Name= PatName[0].MP_Name
                                    Mobile= PatName[0].MP_Mobile
                                    date2 = datetime.datetime.strptime(date, '%Y-%m-%d').date()
                                    date3 = date2.strftime('%d-%m-%Y')
                                    time2 = datetime.datetime.strptime(time, '%H:%M').time()
                                    time3 = time2.strftime('%I:%M %p')
                                    catedg = CategoryName[0]
                                    dtime = str(catedg)+'_'+str(date3) +'_'+str(time3)
                                    # dtime = str(date3) +'_'+str(time3)
                                    parentNameQuery = session.query(Model.models.Application.M_PatientsDtl.MPD_Name).filter_by(
                                        MPD_MobNoCountryCode=Mobile, MPD_User=1, MPD_IsActive=1, MPD_IsDeleted=0).first()
                                    parentName = parentNameQuery[0]
                                    
                                    if fcmToken.token != "" and fcmToken.token is not None:
                                        addapoint_notif(fcmToken.token, parentName, Name, ORGID[0])
                                    else:
                                        print("No FCM token available, notification will not be sent.")

                                    if(Mobile!='' and Mobile!=None):
                                        msg = 'https://api.pinnacle.in/index.php/sms/urlsms?sender=CONKID&numbers=91' + str(Mobile) +'&messagetype=TXT&message=Dear member '+' '+', your appointment at Continua Kids for '+str(Name)+' has been created for ' + str(dtime) +'&response=Y&apikey=bb4d93-a1481e-f7c2a2-67d92c-2d3477'
                                        rese = requests.get(msg)
                                        # print(rese)
                                    else:
                                        pass

                                return jsonify({'msg':f'Appointment Added Successfully. Token-{fcmToken}', 'data': {'AppointmentId':ORGID[0]}})
                                # else:
                                #     return jsonify({'err':'Appointment Not allowed at that time'}),200
                            else:
                                return jsonify({'err':'Appointment Not allowed for that date'}),200
                        else:
                            return jsonify({'err':'Doctor is not available at this time'}),200
                    # else:
                    #     return jsonify({'token': data, 'msg':'username is not valid','About':about,'TokenUser':data['user']})
                    else:
                        # request_json = request.get_json()
                        request_json = request.get_json(force = True)
                        pname = request_json.get('pname')
                        procedure = request_json.get('procedure')
                        date = request_json.get('date')
                        time = request_json.get('time')
                        duration = request_json.get('duration')
                        appointmentType = request_json.get('appointmentType')
                        doctor = request_json.get('doctor')
                        notify = request_json.get('notify')
                        status = request_json.get('status')
                        currentdate = datetime.datetime.now().strftime('%Y%m%d')
                        currenttime = datetime.datetime.now().strftime('%H%M')
                        appdate1 = datetime.datetime.strptime(date,'%Y-%m-%d')
                        appdate = appdate1.strftime('%Y%m%d')
                        # apptime1 = datetime.datetime.strptime(time,'%H:%M:%S')
                        apptime1 = parser.parse(time)
                        apptime = apptime1.strftime('%H%M')
                        checkdoctor = session.query(Model.models.Application.M_Appointment.MAID
                                                    ).filter_by(MA_Date=date,M_DoctorDetails_MDDID=doctor,MP_IsActive=1,MP_IsDeleted=0,MP_IsCancelled=0
                                                    ).filter(Model.models.Application.M_Appointment.MA_Time==time).all()
                        if(len(checkdoctor)>0):
                            AppId = checkdoctor[0].MAID
                        else:
                            AppId = 0
                        if(len(checkdoctor)==0 or AppId==ID):
                            # if(currentdate == appdate or currentdate < appdate):
                            #     if((currentdate == appdate and (apptime >= currenttime and  apptime <='2000')) or (currentdate <= appdate and ('0800' <= apptime and  apptime <='2000'))):
                            if('0800' <= apptime and  apptime <='2000'):        
                                print('hello')
                                Insert=session.query(Model.models.Application.M_Appointment).get(ID)
                                Insert.M_Patient_MPID=pname
                                Insert.MP_Procedure=procedure
                                Insert.MA_Date=date
                                Insert.MA_Time=time
                                Insert.MP_Duration=duration
                                Insert.MP_AppointmentType=appointmentType
                                Insert.M_DoctorDetails_MDDID=doctor
                                Insert.M_Branch_MBID=int(BranchId)
                                Insert.MP_Status=status
                                Insert.MP_AddIP= flask.request.remote_addr
                                
                                Insert.MP_ModDate = datetime.datetime.now()
                                session.commit()
                                print("success")
                                if(notify==True):
                                    PatName = session.query(Model.models.Application.M_Patient.MP_Name,
                                                        Model.models.Application.M_Patient.MP_Mobile,
                                                    ).filter_by(MPID=pname,MP_IsActive=1,MP_IsDeleted=0).all()
                                    Name= PatName[0].MP_Name
                                    Mobile= PatName[0].MP_Mobile
                                    date2 = datetime.datetime.strptime(date, '%Y-%m-%d').date()
                                    date3 = date2.strftime('%d-%m-%Y')
                                    time2 = datetime.datetime.strptime(time, '%H:%M').time()
                                    time3 = time2.strftime('%I:%M %p')
                                    dtime = str(date3) +'_'+str(time3)
                                    if(Mobile!='' and Mobile!=None):
                                        msg = 'https://api.pinnacle.in/index.php/sms/urlsms?sender=CONKID&numbers=91' + str(Mobile) +'&messagetype=TXT&message=Your appointment '+str(ID)+' at Continua Kids for '+str(Name)+' has been updated for ' + str(dtime) +'. Thank you!&response=Y&apikey=bb4d93-a1481e-f7c2a2-67d92c-2d3477'
                                        rese = requests.get(msg)
                                        print(rese)
                                    else:
                                        pass

                                return jsonify({'msg':f'Appointment Updated Successfully. Token- {fcmToken}'})
                                # else:
                                #     return jsonify({'err':'Appointment Not allowed at that time'}),200
                            else:
                                return jsonify({'err':'Appointment Not allowed for that date'}),200
                        else:
                            return jsonify({'err':'Doctor is not available at this time'}),200
                else:
                    return jsonify({'err':'Token is expired'})
            else:
                return jsonify({'err':'Please Login'})
    except Exception as e:
        return jsonify({'err':str(e)})
    finally:
        session.close()