Can't call a function to create a object on #route function even tough it works on python interpreter - database

I'm creating a database for a school project and doing the backend between it and the app.(I'm using Flask and SQLAlchemy for it)
So the problem is that i can't call a function User(entrances), the function User() is a construction function used to create and object ,well it's what it looks like for me at least. But the function itself works when i do it in the interpreter.
WHY can i do it in my interpreter and can't do it on the flask tiny web framework???
here's the github link:BackEndRepository
Before taking a look at the samples here's the output form the interpreter that works:
Interperter
here's the code for my route:
from app import app,db
from models import User
from flask import render_template,Flask,request,redirect,url_for
#app.route('/index')
def index():
return "Hello, World!"
#app.route('/teste')
def teste():
users = db.session.query(User).all()
return u"<br>".join([u"{0}: {1}".format(user.name, user.email) for user in users])
#app.route('/teste2')
def teste2():
teste=User(name='susan',email='susan#example.com')
#u = User(name=request.args.get('1'), email=request.args.get('2'))
return teste
And here is the code for my model:
from app import db
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(64), index=True, unique=True)
email = db.Column(db.String(120), index=True, unique=True)
def __init__(self,name,email):
self.name = name
self.email= email
def __repr__(self):
return '<User {0} {1}>'.format(self.name,self.email)
so i run in my terminal "flask run" and get the error in response:(theese is all the log i had after doing what i explained)
#Arthur:~/Public/ProjetoBackEnd(Original)$ flask run
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [21/Nov/2018 14:24:41] "GET / HTTP/1.1" 404 -
127.0.0.1 - - [21/Nov/2018 14:24:41] "GET /favicon.ico HTTP/1.1" 404 -
127.0.0.1 - - [21/Nov/2018 14:24:46] "GET /index HTTP/1.1" 200 -
127.0.0.1 - - [21/Nov/2018 14:24:46] "GET /favicon.ico HTTP/1.1" 404 -
127.0.0.1 - - [21/Nov/2018 14:24:52] "GET /teste HTTP/1.1" 200 -
127.0.0.1 - - [21/Nov/2018 14:24:53] "GET /favicon.ico HTTP/1.1" 404 -
[2018-11-21 14:24:57,004] ERROR in app: Exception on /teste2 [GET]
Traceback (most recent call last):
File "/home/arthur/.local/lib/python2.7/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/home/arthur/.local/lib/python2.7/site-packages/flask/app.py", line 1816, in full_dispatch_request
return self.finalize_request(rv)
File "/home/arthur/.local/lib/python2.7/site-packages/flask/app.py", line 1831, in finalize_request
response = self.make_response(rv)
File "/home/arthur/.local/lib/python2.7/site-packages/flask/app.py", line 1982, in make_response
reraise(TypeError, new_error, sys.exc_info()[2])
File "/home/arthur/.local/lib/python2.7/site-packages/flask/app.py", line 1974, in make_response
rv = self.response_class.force_type(rv, request.environ)
File "/home/arthur/.local/lib/python2.7/site-packages/werkzeug/wrappers.py", line 921, in force_type
response = BaseResponse(*_run_wsgi_app(response, environ))
File "/home/arthur/.local/lib/python2.7/site-packages/werkzeug/test.py", line 923, in run_wsgi_app
app_rv = app(environ, start_response)
TypeError: 'User' object is not callable
The view function did not return a valid response. The return type must be a string, tuple, Response instance, or WSGI callable, but it was a User.
127.0.0.1 - - [21/Nov/2018 14:24:57] "GET /teste2 HTTP/1.1" 500 -
127.0.0.1 - - [21/Nov/2018 14:24:57] "GET /favicon.ico HTTP/1.1" 404 -
JUST highlighting the part that's bugging me out the most here:
TypeError: 'User' object is not callable

The problem is here:
#app.route('/teste2')
def teste2():
teste=User(name='susan',email='susan#example.com')
#u = User(name=request.args.get('1'), email=request.args.get('2'))
return teste
You are returning teste which is a User. That's not a valid return type here as the error tells you:
The return type must be a string, tuple, Response instance, or WSGI callable, but it was a User.
It's fine to return a string for example, so this should work:
#app.route('/teste2')
def teste2():
teste=User(name='susan',email='susan#example.com')
#u = User(name=request.args.get('1'), email=request.args.get('2'))
return teste.name

Related

Multiple Request with Flask API and Reactjs

Below is the request that I did. Everything works except when I try the put /stats-batch which shows this error. I don't know what is the options that is being displayed, I only noticed it today.
The /stats-batch also just prints hello when access
27.0.0.1 - - [25/May/2021 16:20:46] "OPTIONS /user HTTP/1.1" 200 -
127.0.0.1 - - [25/May/2021 16:20:46] "POST /user HTTP/1.1" 200 -
127.0.0.1 - - [25/May/2021 16:20:48] "OPTIONS /product HTTP/1.1" 200 -
127.0.0.1 - - [25/May/2021 16:20:48] "GET /product HTTP/1.1" 200 -
127.0.0.1 - - [25/May/2021 16:20:48] "OPTIONS /new-batch HTTP/1.1" 200 -
127.0.0.1 - - [25/May/2021 16:20:48] "GET /new-batch HTTP/1.1" 200 -
127.0.0.1 - - [25/May/2021 16:21:21] "OPTIONS /new-batch HTTP/1.1" 200 -
oks
127.0.0.1 - - [25/May/2021 16:21:21] "PUT /new-batch HTTP/1.1" 200 -
127.0.0.1 - - [25/May/2021 16:21:21] "GET /new-batch HTTP/1.1" 200 -
127.0.0.1 - - [25/May/2021 16:21:43] "OPTIONS /stats-batch HTTP/1.1" 200 -
127.0.0.1 - - [25/May/2021 16:21:44] "PUT /stats-batch HTTP/1.1" 500 -
Traceback (most recent call last):
File "C:\Users\USER\Desktop\Allen\santeh\env\Lib\site-packages\flask\app.py", line 2464, in __call__
return self.wsgi_app(environ, start_response)
.......
.......
File "C:\Users\USER\AppData\Local\Programs\Python\Python39\Lib\json\encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type function is not JSON serializable
I call this functions with a button click using axios like this. config has the authorization header just like all the other request
const startBatch = async () => {
const status = 'start'
await axios
.put('http://127.0.0.1:5000/stats-batch', status, config)
.then((res) => {
console.log(res)
})
.catch((err) => {
console.log(err)
})
}
The console shows the error that No Access-Control-Allow-Origin header is present on the requested resource.
I also have flask_cors installed and the other axios request is working. I don't know what cause this error if its the backend or the frontend.
I see an issue; "/new-batch" endpoint is throwing 500 exceptions, which could be related to "Object of type function is not JSON serializable".
Based on the description / Error "No Access-Control-Allow-Origin header is present on the requested resource" message what I understand is
CORS configuration has been missed for PUT VERB.
Because you wrote that rest of the endpoints work without issues, only PUT on this specific "/new-batch" endpoint fails.
Options: In CORS, a preflight request is sent with the OPTIONS method so that the server can respond if it is acceptable to send the request.
URL: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS

python flask + Server Sent Events(SSE) in Google App Engine(GAE)

Hi I am trying to do SSE(Server-Sent Events) with python flask quite similar to this question
I am using SSE to plot a real-time graph in the web app.
My codes are working fine when applied in local, but when I deploy it in GAE(Google App Engine), the data does not seem to be returned(yield)
I found that I should make the response header as following
X-Accel-Buffering: no
in this guide here
So, I tried this code on "main.py"
# render "plot.html" and plot the real-time graph
# plot.html gets the value from /chart-data event stream, and update the graph
#app.route('/plot_graph', methods=["GET", "POST"])
def plot_graph():
return render_template('plot.html')
#app.route('/chart-data')
def chart_data():
def generate_random_data():
while True:
# generating random data
json_data = json.dumps(
{'time': datetime.now().strftime('%Y-%m-%d %H:%M:%S'), 'value': random() * 100})
yield f"data:{json_data}\n\n"
time.sleep(0.5)
resp = Response(generate_random_data(), mimetype='text/event-stream')
resp.headers["X-Accel-Buffering"] = "no"
return resp
Also, in "plot.html" file, it is getting values from /chart-data like this
const source = new EventSource("/chart-data");
source.onmessage = function (event) {
const data = JSON.parse(event.data);
...
}
and this also works well when executed in local machine, but not working on GAE..
for the logs and error messages, when I tried the code on local machine,
and went to the root dir, /plot_graph, /chart-data, the log looks like
127.0.0.1 - - [03/Jul/2020 14:14:19] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [03/Jul/2020 14:14:20] "GET /favicon.ico HTTP/1.1" 404 -
127.0.0.1 - - [03/Jul/2020 14:14:23] "GET /plot_graph HTTP/1.1" 200 -
127.0.0.1 - - [03/Jul/2020 14:14:24] "GET /chart-data HTTP/1.1" 200 -
127.0.0.1 - - [03/Jul/2020 14:15:04] "GET /chart-data HTTP/1.1" 200 -
when I went into /plot_graph, I see a GET request from /chart-data and it works fine(/plot_graph shows a real-time graph). Also, when I go into /chart-data, I can see yielded values being displayed on the web.
In the GAE logs,
2020-07-03 05:22:23 default[20200703t141524] "GET / HTTP/1.1" 200
2020-07-03 05:22:23 default[20200703t141524] "GET /favicon.ico HTTP/1.1" 404
2020-07-03 05:22:38 default[20200703t141524] "GET /plot_graph HTTP/1.1" 200
for the GAE, even if I entered /plot_graph, the GET request does not seemed to be happening. and since it is not getting any values, the graph is not plotted(only the frame for the graph is displayed).
Also, I tried to go into /chart-data of GAE web server, but I could not enter and cannot see any GET request in the logs as I saw on the server on local machine.
Please can you help me with this problem?

symfony 3.4 - vuejs, logged user data under vuejs unavailable

Like in the title. I have a problem with recieving logged user data under vuejs.
I use
- FOS User - to login
- Fos Rest - to api
- Jms Serializer
This is my function to take data from database
public function getUser()
{
$userId = $this->container->get('security.token_storage')->getToken()->getUser('id');
return $this->repository->FindOneBy(['id' => $userId]);
}
Now, when it is in form like above, console.log return an empty object, in vuejs. However, when I change $userId to 5 for example -
$this->repository->FindOneBy(['id' => 5]);
object is available with data.
now. I checked api addres in both cases - works. i also return a dump in both cases. everything in both cases is identical.
this is my log
when $userId
127.0.0.1 - - [07/Apr/2018:03:55:24 +0200] "GET /ekopanel2/web/app_dev.php/api/v1/greenker/user HTTP/1.1" 204 380 "http://localhost:8080/greenker" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:59.0) Gecko/20100101 Firefox/59.0"
and this is when 5
127.0.0.1 - - [07/Apr/2018:03:55:34 +0200] "GET /ekopanel2/web/app_dev.php/api/v1/greenker/user HTTP/1.1" 200 895 "http://localhost:8080/greenker" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:59.0) Gecko/20100101 Firefox/59.0"
i noticed that status code is different, when 5 it is 200 and working, when $userId, status code is 204, so it looks like it gets empty data.
Can you help please?
Let's suppose you are logged in.
If not, $this->container->get('security.token_storage')->getToken()->getUser() would return the string "anon."
Have you tried with this :
$userId = $this->container->get('security.token_storage')->getToken()->getUser()->getId();
Be carefull to check if the user is logged in. Otherwise, it will throw an exception like "Call to a member function getId() on string".

Google AppEngine: Python 2.7: How to split request handler up into several files?

I have an application in which I would like to move some of the request handlers into separate files. I've reduced the problem to this simple app which demonstrates it.
If I browse the main page, e.g.:
http://localhost:12082/
I see
'Hello World...'
displayed
if I try to go to the init page, e.g.:
http://localhost:12082/init/
I see
'404 Not Found'
The resource could not be found.
and in the log I see this:
(init2 message is never logged, the helper method is never called.)
What am I doing wrong?
Thank you for any clues
log contents:
...
INFO 2014-01-15 20:58:23,384 admin_server.py:117] Starting admin server at: http://localhost:8005
INFO 2014-01-16 04:58:34,398 initter.py:6] init1
INFO 2014-01-16 04:58:34,398 initter.py:17] init3
INFO 2014-01-15 20:58:34,421 module.py:617] default: "GET / HTTP/1.1" 200 14
INFO 2014-01-15 20:58:34,473 module.py:617] default: "GET / HTTP/1.1" 200 14
INFO 2014-01-15 20:58:39,555 module.py:617] default: "GET /init/ HTTP/1.1" 404 154
app.yaml:
application: helloworld
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
script: helloworld.application
helloworld.py
import webapp2
import initter
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('Hello World...')
application = webapp2.WSGIApplication([
('/', MainPage),
('/init', initter.LoadPeople),
], debug=True)
initter.py
import webapp2
import logging
logging.getLogger().setLevel(logging.DEBUG)
logging.info("init1")
class LoadPeople(webapp2.RequestHandler):
def get(self):
logging.info("init2")
names = [ 'Joe', 'Jill', 'George', 'John', 'Dave' ]
for name in names:
logging.debug("name is %s", str(name))
logging.info("init3")
http handler expressions could be tricky. There is a difference between '/init' and '/init/' (with an extra slash at the end). Try changing your WSGI app initialization to this:
application = webapp2.WSGIApplication([
('/', MainPage),
('/init*', initter.LoadPeople),
], debug=True)
This will also take care of any query-strings you provide. Example: "....localhost/init?someKey=someValue"

App Engine 401/403 Status Codes Not Working on WebApp2

I am trying to raise a 401/403 status for when a user attempts to access something they don't have privileges to. I used the Webapp2 Exceptions example which generates the proper error codes for 404/500 "natural" events. Such as going to http://localhost:8080/nourl generates the proper 404 and messing up code generates a 500. But when I use the method such as below to set the code using self.error(XXX) I see the code in the console but it does not show up in the browser. EG If you leave self.error() empty it generates the proper 500 code. If you use self.error(500) the console outputs:
INFO 2012-05-09 18:25:29,549 dev_appserver.py:2891] "GET / HTTP/1.1" 500 -
But the browser is completely blank. Below is an example app that exhibits this behaviour. Simply change the self.error() line to the desired code and run. The expected result would be that it generates the proper response to the browser based on the code supplied, not just when a "natural" event occurs such as 404.
main.py
import webapp2
import wsgiref.handlers
import logging
from google.appengine.api import users
class HomeHandler(webapp2.RequestHandler):
def get(self):
user = users.get_current_user()
if user:
self.response.out.write("Hi :1", user.nickname)
else:
self.error(401)
app = webapp2.WSGIApplication([
(r'/', HomeHandler),
], debug=True)
def handle_401(request, response, exception):
logging.exception(exception)
response.write("401 Error")
response.set_status(401)
def handle_403(request, response, exception):
logging.exception(exception)
response.write("403 Error")
response.set_status(403)
def handle_404(request, response, exception):
logging.exception(exception)
response.write("404 Error")
response.set_status(404)
def handle_500(request, response, exception):
logging.exception(exception)
response.write("500 Error")
response.set_status(500)
app.error_handlers[401] = handle_401
app.error_handlers[403] = handle_403
app.error_handlers[404] = handle_404
app.error_handlers[500] = handle_500
# Run the application
def main():
app.run()
app.yaml
application: 401test
version: 1
runtime: python27
api_version: 1
threadsafe: yes
libraries:
- name: webapp2
version: latest
handlers:
- url: /.*
script: main.app
you are using py27 with threadsafe environment.
in app.yaml you set script: main.app so the code after app is defined is not executed.
didn't test it but this should work:
# create a bare app
bare_app = webapp2.WSGIApplication(debug=True)
#define the error handlers
def handle_401(request, response, exception):
logging.exception(exception)
response.write("401 Error")
response.set_status(401)
def handle_403(request, response, exception):
logging.exception(exception)
response.write("403 Error")
response.set_status(403)
def handle_404(request, response, exception):
logging.exception(exception)
response.write("404 Error")
response.set_status(404)
def handle_500(request, response, exception):
logging.exception(exception)
response.write("500 Error")
response.set_status(500)
# add the error handlers
bare_app.error_handlers[401] = handle_401
bare_app.error_handlers[403] = handle_403
bare_app.error_handlers[404] = handle_404
bare_app.error_handlers[500] = handle_500
bare_app.router.add((r'/', HomeHandler))
app = bare_app
edit:
use self.abort() instead of self.error()
http://webapp-improved.appspot.com/guide/exceptions.html#abort
you can see the difference in the source code:
http://code.google.com/p/webapp-improved/source/browse/webapp2.py#574
while self.error() sets the status code but clears the response, self.abort() executes the function that takes care of the error handling.

Resources