Meph-
Meph-

Reputation: 677

Override send_static_file() in Flask

I'm trying use this answer to my problem, but I don't know how to it use correctly.

My code:

app = Flask(name)

app = Flask(__name__, static_folder="static", static_path="")

class SecuredStaticFlask(app):
    def send_static_file(self, filename):
        # Get user from session
        if current_user.is_authenticated():
            return super(SecuredStaticFlask, self).send_static_file(filename)

        else:
            abort(403)

When I visit http://localhost:5000/some_existing_file in the browser, Flask give me an error:

Traceback (most recent call last):
  File "/home/honza/Stažené/pycharm-community-3.0/helpers/pydev/pydevd.py", line 1498, in <module>
    debugger.run(setup['file'], None, None)
  File "/home/honza/Stažené/pycharm-community-3.0/helpers/pydev/pydevd.py", line 1139, in run
    pydev_imports.execfile(file, globals, locals) #execute the script
  File "/home/honza/workspace/web_development/login/toneid_web_api_jk.py", line 37, in <module>
    class SecuredStaticFlask(app):
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 507, in __init__
    self.add_url_rule(self.static_url_path + '/<path:filename>',
TypeError: Error when calling the metaclass bases
    can only concatenate tuple (not "str") to tuple

Instance of flask.Flask is app, right? So, I think subclass of Flask is subclass_name(parent_class).

Can anybody help me? Thanks.

Upvotes: 2

Views: 1204

Answers (1)

Paweł Pogorzelski
Paweł Pogorzelski

Reputation: 651

In my opinion it should be in lines of :

class SecuredStaticFlask(Flask):
    def send_static_file(self, filename):
        # Get user from session
        if current_user.is_authenticated():
            return super(SecuredStaticFlask, self).send_static_file(filename)

        else:
            abort(403)

and

app = SecuredStaticFlask(__name__, static_folder="static", static_path="")

Upvotes: 3

Related Questions