using another WS as validation Flask/Rest/Mysql - python

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)

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

Related

I have an Error with python flask cause of an API result (probably cause of my list) and my Database

I use flask, an api and SQLAlchemy with SQLite.
I begin in python and flask and i have problem with the list.
My application work, now i try a news functions.
I need to know if my json informations are in my db.
The function find_current_project_team() get information in the API.
def find_current_project_team():
headers = {"Authorization" : "bearer "+session['token_info']['access_token']}
user = requests.get("https://my.api.com/users/xxxx/", headers = headers)
user = user.json()
ids = [x['id'] for x in user]
return(ids)
I use ids = [x['id'] for x in user] (is the same that) :
ids = []
for x in user:
ids.append(x['id'])
To get ids information. Ids information are id in the api, and i need it.
I have this result :
[2766233, 2766237, 2766256]
I want to check the values ONE by One in my database.
If the values doesn't exist, i want to add it.
If one or all values exists, I want to check and return "impossible sorry, the ids already exists".
For that I write a new function:
def test():
test = find_current_project_team()
for find_team in test:
find_team_db = User.query.filter_by(
login=session['login'], project_session=test
).first()
I have absolutely no idea to how check values one by one.
If someone can help me, thanks you :)
Actually I have this error :
sqlalchemy.exc.InterfaceError: (InterfaceError) Error binding
parameter 1 - probably unsupported type. 'SELECT user.id AS user_id,
user.login AS user_login, user.project_session AS user_project_session
\nFROM user \nWHERE user.login = ? AND user.project_session = ?\n
LIMIT ? OFFSET ?' ('my_tab_login', [2766233, 2766237, 2766256], 1, 0)
It looks to me like you are passing the list directly into the database query:
def test():
test = find_current_project_team()
for find_team in test:
find_team_db = User.query.filter_by(login=session['login'], project_session=test).first()
Instead, you should pass in the ID only:
def test():
test = find_current_project_team()
for find_team in test:
find_team_db = User.query.filter_by(login=session['login'], project_session=find_team).first()
Asides that, I think you can do better with the naming conventions though:
def test():
project_teams = find_current_project_team()
for project_team in project_teams:
project_team_result = User.query.filter_by(login=session['login'], project_session=project_team).first()
All works thanks
My code :
project_teams = find_current_project_team()
for project_team in project_teams:
project_team_result = User.query.filter_by(project_session=project_team).first()
print(project_team_result)
if project_team_result is not None:
print("not none")
else:
project_team_result = User(login=session['login'], project_session=project_team)
db.session.add(project_team_result)
db.session.commit()

Flask: Function is not returning any String and getting Internal Server Error 500

I am returning a bunch of strings to Front End using Flask RESTful API. The string is generated after a ML algo runs on the input string and classify this to a predefined Answer. The back-end is in MongoDB.
The code given below was working fine earlier. Ever since I have inserted the following lines (marked as *) it is not returning any value. After debugging I found the error as built-in error: The view function could not return a list.
Below is my code (only relevant part)
app = Flask(__name__)
#app.route('/')
def index():
return render_template('index.html')
#app.route('/response/',methods = ['GET','POST'])
def response():
if request.method=='POST':
text_org = request.json['foo']
text = json.loads(json.dumps(text_org))
text = text.lower()
if len(text.split())==0:
return 'Please ask a question'
elif len(text.split())<=3:
resp = ' '.join(['What more would you like to know about', clean_text(text), '? Please be little more specific..'])
return resp
else:
df_to_match = pd.DataFrame({'Slot_Value':tbl_df['slot_value'],'User_In':[clean_text(text)]*len(tbl_df['slot_value'])})
is_match = [process.extract(i, df_to_match['Slot_Value'], limit=3) for i in df_to_match['User_In']]
is_match_ratio = [i for w in is_match for i in w]
if list(is_match_ratio[0]).pop(1) > 65:
#tup = [w for (w,i,n) in is_match_ratio]
x = model.predict([text])
x_t = le.inverse_transform(x) '# *new line
x_t = x_t.tolist() '# *new line
x_f = ' '.join(x_t)
x_f = re.sub('[^A-Za-z0-9,]',' ',x_f) # * new line
y=x_f.replace(' ','').split(',')
slot_value tbl_df.loc[tbl_df.Combi.str.contains(y),'slot_value']
text_clean = clean_text(text) #User Defined Function for Text preprocessing
df_exact = pd.DataFrame({'Slot_Value':slot_value,'User_input':[text_clean]*len(slot_value)})
slot_exact = [process.extract(i, df_exact['Slot_Value'], limit=1) for i in df_exact['User_input']]
slot_exact = ' '.join([n[0] for n in slot_exact[0]])
for i in db_col.find({"slot_value":slot_exact},{"Answer":1,"_id":0}): # querying mongodb with only 'Answer' shall be retrieved
m = json.dumps(i).split('Answer',1)[-1]
resp = m.replace('\\n','<br>').replace('"','').replace(':','').replace('}','')
resp_final = email_url(resp) # * new line
return resp_final
else:
resp = ' '.join(['Sorry I have not understood this, please rephrase the question or'])
return resp
else:
resp = [random.choices(msg)]
return resp
if __name__ == "__main__":
print("**Starting Server...")
app.run(host='0.0.0.0',port=5002, debug=True)
email_url is a UDF that performs some regex and return the variable resp with HTML tags. What I have figured out that all the exceptional cases like text.split()<=3, ' '.join(['Sorry I have not understood ..... ']) are working fine. So this means problem is with new lines as marked *, may be I am missing out anything?
Edit: Few More Information
le: LabelEncoder
model: MultinomialNB()
The above model is used to predict the class for new input as received via text_org = request.json['foo']
I got the solution. I the below line
slot_value tbl_df.loc[tbl_df.Combi.str.contains(y),'slot_value']
instead of y which is a list, I have changed it to ','.join(y)
To enlighten others: le.inverse_transform always produce an array which needs to be converted to list. In the above code snippet y is that list. Hence such error was occurring.

Zapier Action Code: Python will not run with input_data variable

I am using Zapier to catch a webhook and use that info for an API post. The action code runs perfectly fine with "4111111111111111" in place of Ccnum in doSale. But when I use the input_data variable and place it in doSale it errors.
Zapier Input Variable:
Zapier Error:
Python code:
import pycurl
import urllib
import urlparse
import StringIO
class gwapi():
def __init__(self):
self.login= dict()
self.order = dict()
self.billing = dict()
self.shipping = dict()
self.responses = dict()
def setLogin(self,username,password):
self.login['password'] = password
self.login['username'] = username
def setOrder(self, orderid, orderdescription, tax, shipping, ponumber,ipadress):
self.order['orderid'] = orderid;
self.order['orderdescription'] = orderdescription
self.order['shipping'] = '{0:.2f}'.format(float(shipping))
self.order['ipaddress'] = ipadress
self.order['tax'] = '{0:.2f}'.format(float(tax))
self.order['ponumber'] = ponumber
def setBilling(self,
firstname,
lastname,
company,
address1,
address2,
city,
state,
zip,
country,
phone,
fax,
email,
website):
self.billing['firstname'] = firstname
self.billing['lastname'] = lastname
self.billing['company'] = company
self.billing['address1'] = address1
self.billing['address2'] = address2
self.billing['city'] = city
self.billing['state'] = state
self.billing['zip'] = zip
self.billing['country'] = country
self.billing['phone'] = phone
self.billing['fax'] = fax
self.billing['email'] = email
self.billing['website'] = website
def setShipping(self,firstname,
lastname,
company,
address1,
address2,
city,
state,
zipcode,
country,
email):
self.shipping['firstname'] = firstname
self.shipping['lastname'] = lastname
self.shipping['company'] = company
self.shipping['address1'] = address1
self.shipping['address2'] = address2
self.shipping['city'] = city
self.shipping['state'] = state
self.shipping['zip'] = zipcode
self.shipping['country'] = country
self.shipping['email'] = email
def doSale(self,amount, ccnumber, ccexp, cvv=''):
query = ""
# Login Information
query = query + "username=" + urllib.quote(self.login['username']) + "&"
query += "password=" + urllib.quote(self.login['password']) + "&"
# Sales Information
query += "ccnumber=" + urllib.quote(ccnumber) + "&"
query += "ccexp=" + urllib.quote(ccexp) + "&"
query += "amount=" + urllib.quote('{0:.2f}'.format(float(amount))) + "&"
if (cvv!=''):
query += "cvv=" + urllib.quote(cvv) + "&"
# Order Information
for key,value in self.order.iteritems():
query += key +"=" + urllib.quote(str(value)) + "&"
# Billing Information
for key,value in self.billing.iteritems():
query += key +"=" + urllib.quote(str(value)) + "&"
# Shipping Information
for key,value in self.shipping.iteritems():
query += key +"=" + urllib.quote(str(value)) + "&"
query += "type=sale"
return self.doPost(query)
def doPost(self,query):
responseIO = StringIO.StringIO()
curlObj = pycurl.Curl()
curlObj.setopt(pycurl.POST,1)
curlObj.setopt(pycurl.CONNECTTIMEOUT,30)
curlObj.setopt(pycurl.TIMEOUT,30)
curlObj.setopt(pycurl.HEADER,0)
curlObj.setopt(pycurl.SSL_VERIFYPEER,0)
curlObj.setopt(pycurl.WRITEFUNCTION,responseIO.write);
curlObj.setopt(pycurl.URL,"https://secure.merchantonegateway.com/api/transact.php")
curlObj.setopt(pycurl.POSTFIELDS,query)
curlObj.perform()
data = responseIO.getvalue()
temp = urlparse.parse_qs(data)
for key,value in temp.iteritems():
self.responses[key] = value[0]
return self.responses['response']
# NOTE: your username and password should replace the ones below
Ccnum = input_data['Ccnum'] #this variable I would like to use in
#the gw.doSale below
gw = gwapi()
gw.setLogin("demo", "password");
gw.setBilling("John","Smith","Acme, Inc.","123 Main St","Suite 200", "Beverly Hills",
"CA","90210","US","555-555-5555","555-555-5556","support#example.com",
"www.example.com")
r = gw.doSale("5.00",Ccnum,"1212",'999')
print gw.responses['response']
if (int(gw.responses['response']) == 1) :
print "Approved"
elif (int(gw.responses['response']) == 2) :
print "Declined"
elif (int(gw.responses['response']) == 3) :
print "Error"
Towards the end is where the problems are. How can I pass the variables from Zapier into the python code?
David here, from the Zapier Platform team. A few things.
First, I think your issue is the one described here. Namely, I believe input_data's values are unicode. So you'll want to call str(input_data['Ccnum']) instead.
Alternatively, if you want to use Requests, it's also supported and is a lot less finicky.
All that said, I would be remiss if I didn't mention that everything in Zapier code steps gets logged in plain text internally. For that reason, I'd strongly recommend against putting credit card numbers, your password for this service, and any other sensitive data through a Code step. A private server that you control is a much safer option.
​Let me know if you've got any other questions!

How can I use Geoalchemy2 to update spatial data (like point)?

I just write a small website to show some spatial data using flask and Geoalchemy2. Now I can insert some new spatial records (for example, points) into my postgresql database but having some troubles when I want to update them.
my code is as below.
Model.py:
class Geopoint(db.Model):
"""point class"""
__tablename__ = 'geo_point'
ptid = db.Column(db.Integer, primary_key=True)
quiztime = db.Column(db.Numeric)
geopt = db.Column(Geography(geometry_type='POINT', srid=4326))
init.py:
db = SQLAlchemy()
geo_engine = create_engine('postgresql://postgres:password#localhost/database', echo=True)
view.py:
geo_session_class = sessionmaker(bind=geo_engine)
geo_session = geo_session_class()
if request.method == 'POST':
if geo_type == 'POINT':
pt_res = geo_session.query(
Geopoint.ptid,
Geopoint.quiztime,
Geopoint.geopt.ST_AsText()
).filter_by(ptid=geo_id).first()
if pt_res:
print pt_res
else:
geo_session.add(
Geopoint(
quiztime=time.time(),
geopt=geo_type + '(' + geo_coord.encode('utf-8') + ')'
)
)
geo_session.commit()
My code works when I add a new point record.
When I edit the existed point my code of update part returns the printed result (I just want to know how to write it.):
(4L, Decimal('1508430387.581'), u'POINT(120.057, 30.262)')
that does not look like a class but a tuple so I can not update it with
Geopoint.geopt=.....
db.session.add(Geopoint)
db.session.commit()
In the official document there are only examples to add new objects into the database so I am really confused.
Is there any MAGIC sentence to update the data or is there any other geography orm libraries to use?
Appreciation for any answer.
emmmmmm……
Finally I figure out it myself.
Actually the change is very simple. I just change the query object so it return a Geopoint object to me that can be updated.
geo_session_class = sessionmaker(bind=geo_engine)
geo_session = geo_session_class()
if request.method == 'POST':
if geo_type == 'POINT':
pt_res = geo_session.query(
Geopoint.ptid,
Geopoint.quiztime,
Geopoint.geopt.ST_AsText()
).filter_by(ptid=geo_id).first()
if pt_res:
print pt_res
else:
geo_session.add(
Geopoint(
quiztime=time.time(),
geopt=geo_type + '(' + geo_coord.encode('utf-8') + ')'
)
)
geo_session.commit()
change to this.
if request.method == 'POST':
if geo_type == 'POINT':
pt_res = geo_session.query(Geopoint).filter_by(ptid=geo_id).first()
if pt_res:
print pt_res
else:
geo_session.add(
Geopoint(
quiztime=time.time(),
geopt=geo_type + '(' + geo_coord.encode('utf-8') + ')'
)
)
geo_session.commit()
Then my update code is possible to write.
=。=

Why can't I pickle this list?

The purpose of this form is to let users enter a lot of places (comma separated) and it'll retrieve the phone, name, website. Have it working in a python IDE, no problem, but having issues putting it into my webapp.
I'm getting the error Exception Value: Can't pickle local object 'GetNums.<locals>.get_data' at the line where a is assigned. I checked the type of inputText and verified that it is indeed a list. So, I'm not sure why it won't pickle.
def GetNums(request):
form = GetNumsForm(request.POST or None)
if form.is_valid():
inputText = form.cleaned_data.get('getnums')
# all experimental
inputText = inputText.split(',')
def get_data(i):
#DON'T FORGET TO MOVE THE PRIMARY KEY LATER TO SETTINGS
r1 = requests.get('https://maps.googleapis.com/maps/api/place/textsearch/json?query=' + i + '&key=GET_YOUR_OWN')
a = r1.json()
pid = a['results'][0]['place_id']
r2 = requests.get('https://maps.googleapis.com/maps/api/place/details/json?placeid=' + pid + '&key=GET_YOUR_OWN')
b = r2.json()
phone = b['result']['formatted_phone_number']
name = b['result']['name']
try:
website = b['result']['website']
except:
website ='No website found'
return ' '.join((phone, name, website))
v = str(type(inputText))
with Pool(5) as p:
a = (p.map(get_data, inputText))
# for line in p.map(get_data, inputText):
# print(line)
#code assist by http://stackoverflow.com/a/34512870/5037442
#end experimental
return render(request, 'about.html', {'v': a})
It's actually barfing when trying to pickle get_data, which is a nested function/closure.
Move get_data out of GetNums (and agh rename it to snake_case please) and it should work.

Categories