Flask: Turn on DEBUG only for web views blueprint - python

In a flask app I'm developing, I have some web views, which return a normal web page content and an API blueprint. For development configuration, I turn on debug, which is handy to see the backtrace in a browser:
DEBUG = True
However, this also applies to the API blueprint. Dumping HTML content into the console when doing a request through httpie is not very nice. Is there a way to have debug only for the web views and not for the APIs?

Related

Using Django to authenticate user from Flask

How would you access the Django authentication framework from a Flask app?
I have a Django app and Flask app running in parallel on a server. Both are hosted behind the same domain, but behind different paths, so they should be able to see each other's cookies.
I'm using Flask to run a simple API microservice, where using Django would be overkill. However, to prevent abuse, I still want Flask to check the request's cookies to see if they're from a user who's still authenticated in the Django application. I don't want to re-implement an authentication framework in Flask.
Access Django settings from inside Flask is relatively simple. I just put something like this at the top of my Flask script to set the path to my Django settings module:
sys.path.insert(0, <path to Django project>)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mydjangoproject.settings")
from django.conf import settings
However, I'm unsure how to update a Flask request handler to pull the correct cookies from a request and verify them with Django's authentication backend. How would I do this?
Digging through the Django interals for the session and authentication middleware, it looks like it's pretty easy to fed Flask's native request instance to them. This seems to do it for me:
from importlib import import_module
from django.conf import settings
from django.contrib.auth.middleware import get_user
engine = import_module(settings.SESSION_ENGINE)
SessionStore = engine.SessionStore
session_key = request.cookies.get(settings.SESSION_COOKIE_NAME)
request.session = SessionStore(session_key)
user = get_user(request)
print(user.is_authenticated)

Conditional flask Login login_view

I have a flask web server, and it uses flask_login for user authentication.
My web server servers both websites and native mobile applications.
I want end-points which serve website (accessed by browsers) to serve login_view when user is not logged-in.
But the end-points which server native applications (android and ios) to respond with HTTP 401 response code.
How can I set LoginManager.login_view conditionally only on few endpoints

Flask building urls for api and view

I am writing a flask application with angularjs frontend. Templates aren't rendered from flask yet, they are delivered as static files. My api endpoint is as following:
#route('/projects', method=['GET'])
def rest_projects(self):
"""TODO: CRUD operation
"""
Project = Model.get('project.project')
return jsonify([project.serialize() for project in Project.search([])])
url_for works perfect with above endpoint but is there any way it can build urls for js templates also?. Say if url has api prefixed ie: /api/tasks, it comes to this handler otherwise deliver template. Right now I am using nginx to achieve that but is there anything wrong in my design which is restricting me to do this from flask only.
Flask is not intended to serve static files and you are better off keeping your files served by nginx for performance reasons. You can serve static files however using send_static_file() or send_from_directory methods.
http://flask.pocoo.org/docs/0.10/api/

Serve static files with Eve

I am running Flask and Eve on localhost at a same time. The Flask app serves static files and makes requests to the Eve app to get some data. I want to run Eve only, without a separate Flask app. How can I serve static files with Eve?
A better approach will be to prefix the /api for all REST APIs. This can be done by adding URL_PREFIX="api" in settings.py.
By doing this whenever there is request to /, Eve(Flask) will not return the resource catalog instead returns the page as given in run.py.
To serve static content add route decorators accordingly in run.py,
#app.route('/')
def index():
return app.send_static_file('index.html')
app.run(host="0.0.0.0", debug=True)
Eve is a Flask application (a subclass) so as a general rule everything that works with Flask works with Eve too. You could register a blueprint, or add new routes.
Also see this answer for a link to a working example: Servicing html requests with Eve
try set import_name arg for Eve:
app = Eve(import_name=__name__)

Having html pages and a flask application on the server

I have a situation where i have a lot of html files and a flask application on other side which will be running on the server.
I want to configure server in such a way that html file is displayed when i go to www.example.com and flask application is called to render pages www.example.com/app
Here i have huge data to display on the website and the other flask app is used for customer specific.
I cannot have #app.route for thousands of html pages to render that particular page. So now i want to configure my web server where html files are accessed when i go to my website and it says away from the flask application and flask application is called when i go to app.
Thanks

Categories