Skip to content

octoprint.server.util.flask#

LessSimpleCache(threshold = 500, default_timeout = 300) #

Bases: BaseCache

Slightly improved version of :class:SimpleCache.

Setting default_timeout or timeout to -1 will have no timeout be applied at all.

OctoPrintFlaskRequest(environ, *args, **kwargs) #

Bases: flask.Request

server_name() #

Short cut to the request's server name header

server_port() #

Short cut to the request's server port header

check_lastmodified(lastmodified: Union[int, float, datetime]) -> bool #

Compares the provided lastmodified value with the value of the If-Modified-Since header.

If lastmodified is an int or float, it's assumed to be a Unix timestamp and converted to a timezone aware datetime instance in UTC.

If lastmodified is a datetime instance, it needs to be timezone aware or the result will always be False.

Parameters:

Raises:

  • ValueError

    If anything but an int, float or datetime instance is passed

Returns:

  • bool( bool ) –

    true if the values indicate that the document is still up to date

firstrun_only_access(func) #

If you decorate a view with this, it will ensure that first setup has not been done for OctoPrint's Access Control. Otherwise it will cause a HTTP 403 status code to be returned by the decorated resource.

Request specific suffix for set and read cookies

We need this because cookies are not port-specific and we don't want to overwrite our session and other cookies from one OctoPrint instance on our machine with those of another one who happens to listen on the same address albeit a different port or script root.

get_flask_user_from_request(request) #

Retrieves the current flask user from the request context. Uses API key if available, otherwise the current user session if available.

:param request: flask request from which to retrieve the current user :return: the user (might be an anonymous user)

make_api_error(message, status) #

Helper to generate API error responses in JSON format.

Turns something like make_api_error("Not Found", 404) into a JSON response with body {"error": "Not Found"}.

Parameters:

  • message

    The error message to put into the response

  • status

    The HTTP status code

make_text_response(message, status) #

Helper to generate basic text responses.

Response will have the provided message as body, the provided status code, and a content type of "text/plain".

Parameters:

  • message

    The message in the response body

  • status

    The HTTP status code

no_firstrun_access(func) #

If you decorate a view with this, it will ensure that first setup has been done for OctoPrint's Access Control.

If OctoPrint's Access Control has not been setup yet (indicated by the userManager not reporting that its user database has been customized from default), the decorator will cause a HTTP 403 status code to be returned by the decorated resource.

permission_validator(request, permission) #

Validates that the given request is made by an authorized user, identified either by API key or existing Flask session.

Must be executed in an existing Flask request context!

:param request: The Flask request object :param request: The required permission

redirect_to_tornado(request, target, code = 302) #

Redirects from flask to tornado, flask request context must exist.

:param request: :param target: :param code: :return:

restricted_access(func) #

This combines 🇵🇾func:no_firstrun_access and login_required.