API Reference¶
Application Object¶
- class nereid.Nereid(**config)[source]¶
…
Unlike typical web frameworks and their APIs, nereid depends more on configuration and not direct python modules written along the APIs Most of the functional code will remain on the modules installed on Tryton, and the database configurations.
…
- cache_default_timeout¶
The default timeout to use if the timeout is not explicitly specified in the set or set many argument
- cache_dir¶
The directory where cache files are stored if FileSystemCache is used
- cache_init_kwargs¶
If a custom cache backend unknown to Nereid is used, then the arguments that are needed for the initialisation of the cache could be passed here as a dict
- cache_key_prefix¶
a prefix that is added before all keys. This makes it possible to use the same memcached server for different applications. Applies for: MecachedCache, GAEMemcachedCache If key_prefix is none the value of site is used as key
- cache_memcached_servers¶
a list or tuple of server addresses or alternatively a memcache.Client or a compatible client.
- cache_threshold¶
the maximum number of items the cache stores before it starts deleting some items. Applies for: SimpleCache, FileSystemCache
- cache_type¶
The type of cache to use. The type must be a full specification of the module so that an import can be made. Examples for cachelib backends are given below
NullCache - cachelib.NullCache (default) SimpleCache - cachelib.SimpleCache MemcachedCache - cachelib.MemcachedCache GAEMemcachedCache - cachelib.GAEMemcachedCache FileSystemCache - cachelib.FileSystemCache
- create_jinja_environment()[source]¶
Extend the default jinja environment that is created. Also the environment returned here should be specific to the current website.
- create_url_adapter(request)[source]¶
Creates a URL adapter for the given request. The URL adapter is created at a point where the request context is not yet set up so the request is passed explicitly.
- property database¶
Return connection to Database backend of tryton
- database_name¶
The name of the database to connect to on initialisation
- dispatch_request()[source]¶
Does the request dispatching. Matches the URL and returns the return value of the view or error handler. This does not have to be a response object.
- eager_template_render¶
Load the template eagerly. This would render the template immediately and still return a LazyRenderer. This is useful in debugging issues that may be hard to debug with lazy rendering
- get_context_processors()[source]¶
Returns the method object which wraps context processor methods formed by decorating methods with the
context_processor()
decorator.This method goes through all the models and their methods in the pool of the loaded database and looks for the _context_processor attribute in them and adds to context_processor dict.
- get_template_filters()[source]¶
Returns a list of name, function pairs for template filters registered in the models using
template_filter()
decorator.
- get_urls()[source]¶
Return the URL rules for routes formed by decorating methods with the
route()
decorator.This method goes through all the models and their methods in the pool of the loaded database and looks for the _url_rules attribute in them. If there are URLs defined, it is added to the url map.
- initialise()[source]¶
The application needs initialisation to load the database connection etc. In previous versions this was done with the initialisation of the class in the __init__ method. This is now separated into this function.
- initialised = False¶
boolean attribute to indicate if the initialisation of backend connection and other nereid support features are loaded. The application can work only after the initialisation is done. It is not advisable to set this manually, instead call the
initialise()
- load_backend()[source]¶
This method loads the configuration file if specified and also connects to the backend, initialising the pool on the go
- property pool¶
A proxy to the _pool
- request_class¶
alias of
nereid.wrappers.Request
- request_context(environ)[source]¶
Create a
RequestContext
representing a WSGI environment. Use awith
block to push the context, which will makerequest
point at this request.See /reqcontext.
Typically you should not call this from your own code. A request context is automatically pushed by the
wsgi_app()
when handling a request. Usetest_request_context()
to create an environment and context instead of this method.- Parameters
environ – a WSGI environment
- response_class¶
alias of
nereid.wrappers.Response
- select_jinja_autoescape(filename)[source]¶
Returns True if autoescaping should be active for the given template name.
- session_interface = <nereid.sessions.NereidSessionInterface object>¶
the session interface to use. By default an instance of
NereidSessionInterface
is used here.
- template_prefix_website_name¶
Prefix the name of the website to the template name sutomatically This feature would be deprecated in future in lieu of writing Jinja2 Loaders which could offer this behavior. This is set to False by default. For backward compatibility of loading templates from a template folder which has website names as subfolders, set this to True
New in version 2.8.0.4.
- translations_path¶
The location where the translations of the template are stored
- tryton_configfile¶
Configuration file for Tryton. The path to the configuration file can be specified and will be loaded when the application is initialised
- url_rule_class¶
alias of
nereid.routing.Rule
Templating¶
- class nereid.templating.LazyRenderer(template_name_or_list, context, headers=None, eager=False)[source]¶
A Lazy Rendering object which when called renders the template with the current context.
>>> lazy_render_object = LazyRenderer('template.html', {'a': 1})
You can change the context by setting values to the contex dictionary
>>> lazy_render_object.context['a'] = 100
You can also change the template(s) that should be rendered
>>> lazy_render_object.template_name_or_list = "another-template.html"
or even, change it into an iterable of templates
>>> lazy_render_object.template_name_or_list = ['t1.html', 't2.html']
The template can be rendered and serialized to unicode by calling
>>> unicode(lazy_render_object)
The status code or header can also be set on the lazy renderer
>>> lazy_render_object.sattus = 201 >>> lazy_render_object.headers['X-Some-Header'] = 'header value'
Note
If the template renders objects which depend on the application, request or a tryton transaction context (like an active record), the call must be made within those contexts.
- property value¶
Return the rendered template with the current context
- class nereid.templating.ModuleTemplateLoader(database_name=None, searchpath=None)[source]¶
This loader works like the ChoiceLoader and loads templates from a filesystem path (optional) followed by the template folders in the tryton module path. The template folders are ordered by the same order in which Tryton arranges modules based on the dependencies.
The optional keyword argument searchpath could be used to specify local folder which contains templates which may override the templates bundled into nereid modules.
- Parameters
database_name – The name of the Tryton database. This is required since the modules installed in a database is what matters and not the modules in the site-packages
searchpath – Optional filesystem path where templates that override templates bundled with nereid are located.
New in version 2.8.0.4.
Changed in version 2.8.0.6: Does not accept prefixing of site name anymore
- property loaders¶
Lazy load the loaders
- nereid.templating.render_template(template_name_or_list, **context)[source]¶
Returns a lazy renderer object which renders a template from the template folder with the given context. The returned object is an instance of
LazyRenderer
which has all magic methods implemented to make the object look as close as possible to an unicode object.LazyRenderer objects are automatically converted into response objects by the WSGI dispatcher into a rendered string by the make_response method.
- Parameters
template_name_or_list – the name of the template to be rendered, or an iterable with template names the first one existing will be rendered
context – the variables that should be available in the context of the template.
- nereid.templating.render_email(from_email, to, subject, text_template=None, html_template=None, cc=None, attachments=None, **context)[source]¶
Read the templates for email messages, format them, construct the email from them and return the corresponding email message object.
- Parameters
from_email – Email From
to – Email IDs of direct recepients
subject – Email subject
text_template – <Text email template path>
html_template – <HTML email template path>
cc – Email IDs of Cc recepients
attachments – A dict of filename:string as key value pair [preferable file buffer streams]
context – Context to be sent to template rendering
- Returns
Email multipart instance or Text/HTML part
Helpers¶
- nereid.helpers.url_for(endpoint, **values)[source]¶
Generates a URL to the given endpoint with the method provided. The endpoint is relative to the active module if modules are in use.
The functionality is documented in flask.helpers.url_for
In addition to the arguments provided by flask, nereid allows the locale of the url to be generated to be specified using the locale attribute. The default value of locale is the locale of the current request.
For example:
url_for('nereid.website.home', locale='us')
- nereid.helpers.route(rule, **options)[source]¶
Like
Flask.route()
but for nereid.New in version 3.0.7.0.
Unlike the implementation in flask and flask.blueprint route decorator does not require an existing nereid application or a blueprint instance. Instead the decorator adds an attribute to the method called _url_rules.
from nereid import route class Product: __name__ = 'product.product' @classmethod @route('/product/<uri>') def render_product(cls, uri): ... return 'Product Information'
- nereid.helpers.context_processor(name=None)[source]¶
Makes method available in template context. By default method will be registered by its name.
Decorator adds an attribute to the method called _context_processor.
from nereid import context_processor class Product: __name__ = 'product.product' @classmethod @context_processor('get_sale_price') def get_sale_price(cls): ... return 'Product sale price'
- nereid.helpers.template_filter(name=None)[source]¶
If you want to register your own filters in Jinja2 you have two ways to do that. You can either put them by hand into the jinja_env of the application or use the template_filter() decorator.
The two following examples work the same and both reverse an object:
from nereid import template_filter class MyModel: __name__ = 'product.product' @classmethod @template_filter('reverse') def reverse_filter(cls, s): return s[::-1]
Alternatively you can inject it into the jinja environment in your application.py:
def reverse_filter(s): return s[::-1] app.jinja_env.filters['reverse'] = reverse_filter
In case of the decorator the argument is optional if you want to use the function name as name of the filter. Once registered, you can use the filter in your templates in the same way as Jinja2’s builtin filters, for example if you have a Python list in context called mylist:
{% for x in mylist | reverse %} {% endfor %}
Testing Helpers¶
- class nereid.testing.NereidModuleTestCase(methodName='runTest')[source]¶
Provide a simple Mixin for usage in module tests