Base module API

This is the flask_mongoengine2 main modules API documentation.

flask_mongoengine2.connection module

Module responsible for connection setup.

flask_mongoengine2.connection._get_name(setting_name)[source]

Return known pymongo setting name, or lower case name for unknown.

This problem discovered in issue #451. As mentioned there pymongo settings are not case-sensitive, but mongoengine use exact name of some settings for matching, overwriting pymongo behaviour.

This function address this issue, and potentially address cases when pymongo will become case-sensitive in some settings by same reasons as mongoengine done.

Based on pymongo 4.1.1 settings.

Return type:

str

flask_mongoengine2.connection._sanitize_settings(settings)[source]

Remove MONGODB_ prefix from dict values, to correct bypass to mongoengine.

Return type:

dict

flask_mongoengine2.connection.create_connections(config)[source]

Given Flask application’s config dict, extract relevant config vars out of it and establish MongoEngine connection(s) based on them.

flask_mongoengine2.connection.get_connection_settings(config)[source]

Given a config dict, return a sanitized dict of MongoDB connection settings that we can then use to establish connections. For new applications, settings should exist in a MONGODB_SETTINGS key, but for backward compatibility we also support several config keys prefixed by MONGODB_, e.g. MONGODB_HOST, MONGODB_PORT, etc.

Return type:

List[dict]

flask_mongoengine2.db_fields module

flask_mongoengine2.decorators module

Collection of project wide decorators.

flask_mongoengine2.decorators.orm_deprecated(func)[source]

Warning about usage of deprecated functions, that will be removed in the future.

flask_mongoengine2.documents module

Module responsible for custom pagination.

class flask_mongoengine2.pagination.ListFieldPagination(queryset, doc_id, field_name, page, per_page, total=None)[source]

Bases: Pagination

next(error_out=False)[source]

Returns a Pagination object for the next page.

prev(error_out=False)[source]

Returns a Pagination object for the previous page.

class flask_mongoengine2.pagination.Pagination(iterable, page, per_page)[source]

Bases: object

property has_next

True if a next page exists.

property has_prev

True if a previous page exists

iter_pages(left_edge=2, left_current=2, right_current=5, right_edge=2)[source]

Iterates over the page numbers in the pagination. The four parameters control the thresholds how many numbers should be produced from the sides. Skipped page numbers are represented as None. This is how you could render such a pagination in the templates:

{% macro render_pagination(pagination, endpoint) %}
  <div class=pagination>
  {%- for page in pagination.iter_pages() %}
    {% if page %}
      {% if page != pagination.page %}
        <a href="{{ url_for(endpoint, page=page) }}">{{ page }}</a>
      {% else %}
        <strong>{{ page }}</strong>
      {% endif %}
    {% else %}
      <span class=ellipsis></span>
    {% endif %}
  {%- endfor %}
  </div>
{% endmacro %}
next(error_out=False)[source]

Returns a Pagination object for the next page.

property next_num

Number of the next page

property pages

The total number of pages

prev(error_out=False)[source]

Returns a Pagination object for the previous page.

property prev_num

Number of the previous page.

flask_mongoengine2.panels module

Debug panel views and logic and related mongoDb event listeners.

class flask_mongoengine2.panels.RawQueryEvent(_event, _start_event, _is_query_pass)[source]

Bases: object

Responsible for parsing monitoring events to web panel interface.

Parameters:
_event: Union[CommandSucceededEvent, CommandFailedEvent]
_start_event: CommandStartedEvent
_is_query_pass: bool
property time

Query execution time.

property size

Query object size.

property database

Query database target.

property collection

Query collection target.

property command_name

Query db level operation/command name.

property operation_id

MongoDb operation_id used to match ‘start’ and ‘final’ monitoring events.

property server_command

Raw MongoDb command send to server.

property server_response

Raw MongoDb response received from server.

property request_status

Query execution status.

class flask_mongoengine2.panels.MongoCommandLogger[source]

Bases: CommandListener

Receive point for CommandListener events.

Count and parse incoming events for display in debug panel.

append_raw_query(event, request_status)[source]

Pass ‘unknown’ events to parser and include final result to final list.

failed(event)[source]

Receives ‘failed’ events. Required to track database answer to request.

reset_tracker()[source]

Resets all counters to default, keeping instance itself the same.

started(event)[source]

Receives ‘started’ events. Required to track original request context.

succeeded(event)[source]

Receives ‘succeeded’ events. Required to track database answer to request.

flask_mongoengine2.panels._maybe_patch_jinja_loader(jinja_env)[source]

Extend jinja_env loader with flask_mongoengine2 templates folder.

class flask_mongoengine2.panels.MongoDebugPanel(*args, **kwargs)[source]

Bases: DebugPanel

Panel that shows information about MongoDB operations.

config_error_message = 'Pymongo monitoring configuration error. mongo_command_logger should be registered before database connection.'
name = 'MongoDB'
has_content = True
property _context: dict

Context for rendering, as property for easy testing.

property is_properly_configured: bool

Checks that all required watchers registered before Flask application init.

process_request(request)[source]

Resets logger stats between each request.

nav_title()[source]

Debug toolbar in the bottom right corner.

Return type:

str

nav_subtitle()[source]

Count operations total time.

Return type:

str

title()[source]

Title for ‘opened’ debug panel window.

Return type:

str

url()[source]

Placeholder for internal URLs.

Return type:

str

content()[source]

Gathers all template required variables in one dict.

flask_mongoengine2.sessions module

class flask_mongoengine2.sessions.MongoEngineSession(initial=None, sid=None)[source]

Bases: CallbackDict, SessionMixin

_abc_impl = <_abc._abc_data object>
class flask_mongoengine2.sessions.MongoEngineSessionInterface(db, collection='session')[source]

Bases: SessionInterface

SessionInterface for mongoengine

get_expiration_time(app, session)[source]

A helper method that returns an expiration date for the session or None if the session is linked to the browser session. The default implementation returns now + the permanent session lifetime configured on the application.

Return type:

timedelta

open_session(app, request)[source]

This is called at the beginning of each request, after pushing the request context, before matching the URL.

This must return an object which implements a dictionary-like interface as well as the SessionMixin interface.

This will return None to indicate that loading failed in some way that is not immediately an error. The request context will fall back to using make_null_session() in this case.

save_session(app, session, response)[source]

This is called at the end of each request, after generating a response, before removing the request context. It is skipped if is_null_session() returns True.

Module contents

Note

Parent mongoengine project docs/docstrings has some formatting issues. If class/function/method link not clickable, search on provided parent documentation manually.

class flask_mongoengine2.MongoEngine(app=None, config=None)[source]

Bases: object

Main class used for initialization of Flask-MongoEngine2.

property connection: dict

Return MongoDB connection(s) associated with this MongoEngine instance.

init_app(app, config=None)[source]
flask_mongoengine2.current_mongoengine_instance()[source]

Return a MongoEngine instance associated with current Flask app.