Quorum Extensions for Flask¶
A small extension framework for Flask to easy a series of simple tasks.
Changelog¶
List the complete set of changes to the quorum project since it’s creation.
Current Versions¶
0.8.2¶
- Support for multiple items in the sort field
- Support for id as a fallback secondary sorter
- Changed structure of the ReadTheDocs configuration
0.8.1¶
- Fixed Incompatibilities with Flask 2.3+
0.8.0¶
- Support for the basic_auth() method
- Support for the date.py module and the format_delta function
- Renamed repository to flask-quorum
0.7.0¶
- Safer check for integer value in Excel
0.6.4¶
- Fixed support for count_documents in PyMongo>=4
0.6.3¶
- Fixed issue related with PyMongo version parsing
0.6.2¶
- Made client side sessions more stable, by patching some global session related configurations
0.6.1¶
- Support for version printing at startup
0.6.0¶
- Major 0.6.0 release
- Fixed critical issues with deep nested relations
0.5.36¶
- Fixed issue with pypy
0.5.35¶
- Added compatibility with TinyDB 4
0.5.34¶
- Dynamic template loading for SMTP
0.5.33¶
- Small improvements
0.5.32¶
- Improved pyMongo integration
- Support for context in configuration
- Better verify functions
0.5.31¶
- Support for the default field in the to_locale method
0.5.30¶
- Small set of fixes
0.5.28¶
- Bug fixes
0.5.27¶
- Improved quality of the HTTP client
- Generators as input for HTTP requests
- Support for Flask 1.0.x
- Bug fixes
0.5.26¶
- Support for secure headers
- Bug fixes
0.5.25¶
- Support for clusters in Pusher
0.5.24¶
- Fixed some bugs related with data structures
0.5.23¶
- Fixed some bugs
0.5.22¶
- Fixed some bugs, related with HTTP client
0.5.21¶
- Fixed some bugs
0.5.20¶
- Fixed some bugs
- Support for better OrderedDict structure
0.5.19¶
- Fixed problem with the SERVER_NAME config value generation
0.5.18¶
- Fixed issue with default port and URL generation
0.5.17¶
- Support for BASE_URL and _external URL generation
0.5.16¶
- Small context ensure fixes
0.5.15¶
- Simplified ensure_context decorator
0.5.14¶
- New support for minute and hour based work
0.5.13¶
- New ensure_context() function
0.5.12¶
- Fixed AMQP issues
0.5.11¶
- Small legacy fixes
0.5.10¶
- Small fixes in form loading
0.5.9¶
- Better ACL structure
0.5.8¶
- ACL security fix
0.5.7¶
- Small Mime bug fixes
0.5.6¶
- Some ACL bug fixes
0.5.5¶
- New namespace based ACL
0.5.4¶
- New compatibility layer with models
0.5.3¶
- Fixed issue with new version of Jinja 2
0.5.2¶
- Support for multiple indexes
0.5.1¶
- Some bug fixes for indexes
0.5.0¶
- Improved overall stability
- Added support for more indexes in Mongo
0.4.15¶
- Small set of fixes
0.4.14¶
- Small set of fixes in RabbitMQ to AMQP migration
0.4.13¶
- Renamed RabbitMQ to AMQP
0.4.12¶
- Removed extra print statements
0.4.11¶
- New model structure
- Fixed issues with locales
0.4.10¶
- New configuration infra-structure
0.4.9¶
- Fixed another build issue
0.4.8¶
- Fixed issue with deployment
0.4.7¶
- New dump all support in typesf
0.4.6¶
- Fixed issue related with locales
0.4.5¶
- Support for locales in exceptions
0.4.4¶
- Fixed major bug with file type
0.4.3¶
- Lots of bug fixes
- Better export of database
0.4.2¶
- Better structure for map based models
0.4.1¶
- Better resolution of models
0.4.0¶
- Small set of issue fixes
0.3.22¶
- Fixed major issue
0.3.21¶
- Major changes in data layer
0.3.20¶
- Fixed memory leak
0.3.19¶
- Fixed issue in xls conversion
0.3.18¶
- Better xls conversion
- Minor bug fixes
0.3.17¶
- Better persistence model
- Minor bug fixes
0.3.16¶
- New map like access support for models
0.3.15¶
- Fixed issue with filtering
0.3.14¶
- New support for travis
0.3.13¶
- Fixed bug related with http client
0.3.12¶
- Fixed bug related with email sending
0.3.11¶
- Bug fix related with async based redirection
0.3.10¶
- Compatibility fixes
0.3.9¶
- Compatibility fixes
- Support for new pymongo interface
0.3.8¶
- Better email address support with format
0.3.7¶
- Support for model duplicate attribute validation
0.3.6¶
- New support for session file path definition
0.3.5¶
- Better configuration overriding
0.3.4¶
- Fixed problem in http naming collision
0.3.3¶
- New handler retrieval function
0.3.2¶
- Refactor of the configuration infra-structure
0.3.1¶
- Fix in legacy support
0.3.0¶
- Major code re-structure
- New Apache based license
0.2.6¶
- New set of bug fixes
- Fixed issue in memory based log
0.2.5¶
- Support for new HTTP client
0.2.4¶
- Major bug fix with
count
fixed
0.2.3¶
- Improved overall stability of the system
0.2.2¶
- Improved the email structure
0.2.1¶
- Minimal stability improvements
0.2.0¶
- Initial support for
Python 3.0+
- More stability in the infra-structure
0.1.8¶
- New support for
quorum.exists_amazon_key()
andquorum.clear_amazon_bucket()
calls- Better unit testing for
amazon.py
- Support for the SERVER_* environment variables
0.1.7¶
- Better signature for
quorum.send_mail()
- Improved asynchronous mode under
quorum.send_mail_a()
- New support for
quorum.delete_amazon_key()
calls
Configuration Variables¶
Global¶
-
.. NAME::
string (default = None)
¶ String that is going to be used to identify the app for the internal structures a fallback value should be defined by the developer of the app.
-
.. INSTANCE::
string (default = None)
¶ The descriptive value of the instance that is going to be executed. This value should provide the required mechanisms to correctly separate to virtually different running instance (eg: databases, log files, etc.).
-
.. LEVEL::
string (default = "INFO")
¶ Defines the verbosity level that is going to be used while running the application’s logger. It also provides a way of defining if the application is running for production where production is INFO or more verbose levels.
-
.. DEBUG::
boolean (default = False)
¶ If the application should be ran under the run mode.
Note
This variable is considered to be deprecated and the
LEVEL
variable should be used instead with the DEBUG level set.
Mail / SMTP¶
-
.. SMTP_URL::
string
¶ URL that describes the multiple SMTP configurations to be used as gateway for sending SMTP messages (e-mails) under quorum.
-
.. SMTP_HOST::
string
¶ Hostname or IP address of the server to be used as gateway for sending SMTP messages (e-mails) under quorum.
This value may contain an optional port value separated by a ‘:’ character.
-
.. SMTP_USER::
string
¶ Username to be used in the authentication process on the SMTP connections used for sending email messages.
Note
Most of the times the username is an email address and as such it’s also used as the default fallback value for the sender value for outgoing emails.
-
.. SMTP_PASSWORD::
string
¶ Password to be used in the authentication process on the SMTP connections used for sending email messages.
MongoDB¶
-
.. MONGOHQ_URL::
string
¶ The url to be used for the establishment of connection to the MongoDB server. It must contain authentication information, host, port and optionally the default database to be used.
Note
An example url for mongo would be something like mongodb://root:root@db.hive:27017.
Redis¶
-
.. REDISTOGO_URL::
string
¶ The URL that described the connection to be used with the REDIS key value database, this URL is going to be used under the redis-py infra-structure.
Note
An example url for rabbit would be something like redis://root:root@db.hive.
RabbitMQ / AMQP¶
-
.. AMQP_URL::
string
¶ URL used by the AMQP library (pika) to create the connection with the server that is going to be used in the session. It should contain both authentication and location information.
Note
An example url for amqp would be something like amqp://root:root@amqp.hive.
Amazon Web Services¶
-
.. AMAZON_ID::
string
¶ Identifier of the Amazon S3 account to be sued for the connection, this should comply with the expected string values.
-
.. AMAZON_SECRET::
string
¶ The secret value of the account to be used, this value should be kept secret from any external person to avoid security problems.
-
.. AMAZON_BUCKET::
string
¶ The name of the bucket where file of the current application are going to be stored. Currently there’s no support for multiple buckets per one application scope.
API Reference¶
quorum¶
Base¶
-
quorum.
APP
= None¶ The reference to the top level application object that is being handled by quorum. This value is used across the quorum infra-structure to access flask data and capabilities.
Note
Changing this value directly should be done with care as it may create undesired results. To set/start this value use the
quorum.load()
function instead.
-
quorum.
load
(app=None, name=None, locales=('en_us', ), secret_key=None, execution=True, redis_session=False, mongo_database=None, logger=None, models=None, safe=False, **kwargs)¶ Initial loader function responsible for the overriding of the flask loading system and for the loading of configuration.
Note
This function should be called inside you main app file failure to do so may result in unexpected behavior.
Parameters: - app (Application) – The optional flask application object to be used in the loading of quorum (useful for self managed apps).
- name (String) – The name to be used to describe the application for the management of internal values.
- locales (List) – The list containing the various locale strings for the locales available for the application to be loaded.
- secret_key (String) – The secret seed value to be used for cryptographic operations under flask (eg: client side sessions) this value should be shared among all the pre-fork instances.
- execution (bool) – Flag indicating if the (background) execution thread should be started providing the required support for background tasks.
- redis_session (bool) – If the session management for the flask infra-structure should be managed using a server side session with support from redis.
- mongo_database (String) – The default name of the database to be used when using the MongoDB infra-structure.
- logger (String) – The name to be used as identification for possible logger files created by the logging sub-module.
- models (Module) – The module containing the complete set of model classes to be used by the data infra-structure (eg:
mongo
). - safe (bool) – If the application should be run in a safe mode meaning that extra validations will be done to ensure proper execution, typically these kind of validations have a performance impacts (not recommended).
Return type: Application
Returns: The application that is used by the loaded quorum environment in case one was provided that is retrieved, otherwise the newly created one is returned instead.
-
quorum.
unload
()¶ Unloads the current quorum instance, after this call no more access to the quorum facilities is allowed.
A normal setup of the application would never require this function to be called directly.
Warning
Use this function with care as it may result in unexpected behavior from a developer point of view.
Note
After the call to this method most of the functionally of quorum will become unavailable until further call to
quorum.load()
.
-
quorum.
run_back
(callable, args=[], kwargs={}, target_time=None, callback=None)¶ Runs the provided callable (function, method, etc) in a separated thread context under submission of a queue system. It’s possible to control the runtime for the execution with the
target_time
argument and it’s also possible to be notified of the end of the execution providing a callable to thecallback
parameter.Warning
The execution is not guaranteed as the system process may be interrupted and resuming of the execution would not be possible.
Parameters: - callable (Function) – The callable object to be called in a separated execution environment.
- args (Dictionary) – The list of unnamed argument values to be send to the callable upon execution.
- args – The dictionary of named argument values to be send to the callable upon execution.
- target_time (float) – The target timestamp value for execution, in case it’s not provided the current time is used as the target one.
- callback (Function) – The callback function to be called upon finishing the execution of the callable, in case an error (exception) on executing the callback the error is passed as error argument.
-
quorum.
run_background
(callable, args=[], kwargs={}, target_time=None, callback=None)¶ Runs the provided callable (function, method, etc) in a separated thread context under submission of a queue system. It’s possible to control the runtime for the execution with the
target_time
argument and it’s also possible to be notified of the end of the execution providing a callable to thecallback
parameter.Warning
The execution is not guaranteed as the system process may be interrupted and resuming of the execution would not be possible.
Parameters: - callable (Function) – The callable object to be called in a separated execution environment.
- args (Dictionary) – The list of unnamed argument values to be send to the callable upon execution.
- args – The dictionary of named argument values to be send to the callable upon execution.
- target_time (float) – The target timestamp value for execution, in case it’s not provided the current time is used as the target one.
- callback (Function) – The callback function to be called upon finishing the execution of the callable, in case an error (exception) on executing the callback the error is passed as error argument.
Mail / SMTP¶
-
quorum.
send_mail
(app=None, subject='', sender=None, receivers=[], data=None, plain=None, rich=None, context={}, encoding='utf-8', host=None, port=None, username=None, password=None, stls=False, safe=True)¶ Sends an email message using the provided
SMTP_HOST
,SMTP_USER
andSMTP_PASSWORD
configurations.The email message is sent under the
alternative
mime type so that both the plain text and the rich text (HTML) parts are sent in the same message.The
plain
andrich
arguments allow the user to process a template with the context provided by thecontext
map.Warning
This is a blocking call and as such the control flow may block for more that a second, if you want a non blocking (asynchronous) call please use
quorum.send_mail_a()
.Parameters: - app (Application) – Optional application object to be used for the rendering operation as the main object for flask. In case this value is not provided the global
quorum.APP
value is used instead (fallback). - subject (String) – The mime subject to be sent with this message, note that if this value is not set many spam filters will consider the message as spam.
- sender (String) – The email (and name) of the sender of the email, in case this value is not set the
SMTP_USER
variable is used instead. - receivers (List) – The list of receivers (with email and name) for which the email will be sent.
- data (String) – The buffer containing the data to be used for both the plain and the rich text parts in case the template associated arguments are not set or in case the rendering is not successful.
- plain (String) – Relative path to the plain text template to be used for the rendering of the email, this path must be relative to the templates folder.
- rich (String) – Relative path to the rich text template to be used for the rendering of the email, this path must be relative to the templates folder.
- context (Dictionary) – The map containing the complete set of variables that are going to be “exposed” to the template rendering engine for both the
plain
and therich
. - encoding (String) – The text encoding name that is going to be used in the mail that is going to be sent, should be used with care.
- host (String) – The hostname for the connection of the SMTP server that is going to be used, this may be wither a domain of an address.
- port (int) – The TCP port number that is going to be used for the client connection with the server.
- username (String) – Username value that is used for the authentication part of the connection with the SMTP server.
- password (String) – Secret password to be used as part of the authentication process of the created SMTP connection.
- stls (bool) – If the connection with the target SMTP server should be made using a secure mechanism of a plain text one.
- safe (bool) – If the current email message should be used using a safe strategy, meaning that newline sequences will be made standard.
- app (Application) – Optional application object to be used for the rendering operation as the main object for flask. In case this value is not provided the global
-
quorum.
send_mail_a
(*args, **kwargs)¶ Asynchronous call to the
quorum.send_mail()
function that is executed in a different thread from the current one. The currently loaded queue system is used for the sending of the email, for more information check onquorum.run_background()
.Note
The arguments to be send for this function are the same as the one present in the original
quorum.send_mail()
function.
Glosary¶
- BIOS
- A set of computer instructions in firmware that control input and output operations.
- EPEL
- Common acronym for Extra Packages for Enterprise Linux. These are typically included in Fedora Linux, and provided for RedHat, CentOS, and other RPM-based distributions. The project’s homepage is here: FedoraProject:EPEL.
- MAC
- Pronounced as “mack” and often used as a noun referring to a network device’s Media Access Controller (MAC) address. A MAC address is a globally unique number assigned to each interface in an Ethernet network and used to direct Ethernet frames between source and destination devices.
- OSI
- Open Systems Interconnection (OSI) is an effort to develop standards-based computer networking protocols in order to allow networking equipment from different vendors to interoperate without relying on implementation of competing proprietary protocols. The OSI is best known for the development of the standard seven-layer OSI model for describing layers of abstraction into which the various networking protocols are categorized.
- POSIX
- An acronym for “Portable Operating System Interface”, is a family of standards specified by the IEEE for maintaining compatibility between operating systems. POSIX defines the application programming interface (API), along with command line shells and utility interfaces, for software compatibility with variants of Unix and other operating systems. Read more here: Wikipedia:POSIX.
- RFC
- The RFC documents (Request for Comments) are a series of Internet standards, best common practices, and related documents describing how networked computers communicate. This document series provides the standards for how the Internet and many other related technologies interrelate and interoperate.
Example¶
import flask
import quorum
app = quorum.load(
name = __name__
)
@app.route("/", methods = ("GET",))
def index():
return flask.render_template("index.html.tpl")
if __name__ == "__main__":
quorum.run()