I am trying to set a global variable as in:
# -*- coding: utf8 -*-
from flask import Flask, render_template,redirect,flash, url_for, session, request
from sqlalchemy.sql import text
from sqlalchemy.orm import aliased, Query
from sqlalchemy import exc
from flask_bootstrap import Bootstrap
from .forms import *
import zlib
#from sqlalchemy.orm.query import limit, order_by
from dbmodel import *
app = Flask(__name__)
app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'
Bootstrap(app)
MAX_ITEMS=50
MAX_ZIPS=5
MAX_ORDERS=20
clientMaxSQL="SELECT * FROM CLIENT WHERE 1"
engine, dbsession = dbconnect()
@app.route("/clientRegister",methods=['GET', 'POST'])
def clientRegister():
form = ClientQuery()
global clientMaxSQL
flash('maxSql at start %s ' %(clientMaxSQL))
if form.reset.data == "True":
flash('RESET TABLE FILTERS')
clientMaxSQL="SELECT * FROM CLIENT WHERE 1"
sql = clientMaxSQL
if form.validate_on_submit():
if len(form.compareVal.data.strip()) > 0 and ( any(c.isalpha() for c in form.compareVal.data) or any(c.isdigit() for c in form.compareVal.data)):
sql = sql + " AND " + form.columnSelection.data + form.comparisonSelection.data + "'" +form.compareVal.data+"'"
start=int(request.args.get('start', '0'))
prev=start-MAX_ITEMS
nextStart = start + MAX_ITEMS
tmpSQL = clientMaxSQL
clientMaxSQL = sql
sql = sql + " LIMIT " + str(start)+","+str(nextStart)
try:
clients = engine.execute(sql)
maxStart=engine.execute(clientMaxSQL).rowcount
flash('Attempting Query: %s' %(sql))
except:
flash('Invalid Comparison of %s' %(form.compareVal.data))
sql = tmpSQL + " LIMIT " + str(start)+","+str(nextStart)
clientMaxSQL=tmpSQL
clients = engine.execute(sql)
maxStart=engine.execute(clientMaxSQL)
flash('maxSql at end %s ' %(clientMaxSQL))
return render_template("clientRegister.html",form=form,maxStart=maxStart,clients=clients,start=start, prev=prev,nextStart=nextStart)
I am getting some strange results. It seems as if this statement (declared above all of my function definitions) is executing every once in a while on its own. Is this possible? It is imperative to my web app that this only runs once otherwise I will lose the current state of my query.
Added all the code in the program that ever changes the variable. It is resetting on its own though.