langdev.web — LangDev web services

LangDev uses Flask as framework for web frontend. It depends on Werkzeug and Jinja2 also.

See also

Blueprint langdev.web.home
Website home.
Blueprint langdev.web.user
User authentications, personal pages, and so on.
Blueprint langdev.web.forum
Forum post pages and comments.
Blueprint langdev.web.thirdparty
Third-party applications.
Module langdev.web.pager
Pager for long length web application.
Module langdev.web.serializers
Serializers for various content types.
Module langdev.web.wsgi
Custom WSGI middlewares for LangDev web application.
flask.g.session

(langdev.orm.Session) The global variable that stores the SQLAlchemy session.

flask.g.database_engine

(sqlalchemy.engine.base.Engine) The global variable that stores SQLAlchemy dtabase engine.

langdev.web.blueprints = {'langdev.web.forum:forum': {'url_prefix': '/posts'}, 'langdev.web.thirdparty:thirdparty': {'url_prefix': '/apps'}, 'langdev.web.user:user': {'url_prefix': '/users'}, 'langdev.web.home:home': {}}

The dict of blueprints to be registered by default. Keys are import names in string, and values are keyword arguments for flask.Flask.register_blueprint() method.

blueprints = {'module.name:blueprint': {'url_prefix': '/path'},
              'module.name2:blueprint2': {}}

It can be extended by BLUEPRINTS configuration also:

BLUEPRINTS = {'module.name:var': {}}

See also

Function werkzeug.utils.import_string()
The function that imports an object based on a string, provided by Werkzeug.
Flask — Modular Applications with Blueprints
Flask provides ‘blueprint’ facilities for large applications.
langdev.web.wsgi_middlewares = ['langdev.web.wsgi:MethodRewriteMiddleware']

The list of WSGI middlewares to hook in. Its elements are import names in string.

wsgi_middlewares = ['langdev.web.wsgi:MethodRewriteMiddleware']

It can be extended by WSGI_MIDDLEWARES configuration as well.

WSGI_MIDDLEWARES = ['werkzeug.contrib.fixers.ProxyFix']

See also

Function werkzeug.utils.import_string()
The function that imports an object based on a string, provided by Werkzeug.
Module langdev.web.wsgi
Custom WSGI middlewares for LangDev web application.
Flask — Hooking in WSGI Middlewares
Flask provides a way to hook in WSGI middlewares.
langdev.web.before_request_funcs = [<function define_session at 0x34f4de8>, <function define_current_user at 0x369e6e0>]

Similar to flask.Flask.before_request_funcs attribute. It is for lazy loading of global before_request_funcs list.

langdev.web.after_request_funcs = []

Similar to flask.Flask.after_request_funcs attribute. It is for lazy loading of global after_request_funcs list.

langdev.web.error_handlers = {403: <function forbidden at 0x369ea28>}

It is for lazy loading of global error_handlers list.

langdev.web.template_filters = {'order_by': <function query_order_by at 0x34f4ed8>}

Internal storage dictionary for template_filter() decorator.

langdev.web.content_types = {'text/xml': '.xml', 'text/html': '.html', 'application/xhtml+xml': '.html', 'application/plist+xml': 'langdev.web.serializers:plist', 'application/json': 'langdev.web.serializers:json', 'application/x-plist': 'langdev.web.serializers:plist'}

The dict of serializers for content types. Keys are MIME types like application/json, and values are functions that encode a value into the paired type, or a string which is a postfix of the template filename e.g. '.html', '.xml'. If value is a string that doesn’t start with period (.), it will be interpreted as import name.

content_types = {'application/json': json.dumps,
                 'text/yaml': 'langdev.web.serializers:yaml',
                 'text/html': '.html',
                 'text/xml': '.xml'}

See also

Function render()
The generic content type version of flask.render_template().
Function werkzeug.utils.import_string()
The function that imports an object based on a string, provided by Werkzeug.
langdev.web.default_content_type = 'text/html'

The default content type (MIME type) that is used for */*.

langdev.web.create_app(modifier=None, config_filename=None)

An application factory. It sets up the application then returns the application.

app = create_app(config_filename='prod.cfg')

Instead you pass an argument config_filename, it can be used as decorator-style as well:

@create_app
def app(app):
    app.debug = True
    app.config['MAGIC_NUMBER'] = 1234
Parameters:
  • modifier (callable object) – a function, for decorator-style use
  • config_filename (basestring) – a configuration file name
Returns:

a WSGI application

Return type:

flask.Flask

langdev.web.before_request(function)

The decorator that registers function into before_request_funcs.

langdev.web.after_request(function)

The decorator that registers function into after_request_funcs.

langdev.web.errorhandler(code)

The decorator that registers a function into error_handlers.

@errorhandler(404)
def not_found(error):
    return 'This page does not exist', 404
Parameters:code (int) – an error status code to associate

See also

Decorator Method flask.Flask.errorhandler()

langdev.web.template_filter(name=None)

A decorator that is used to register custom template filter. You can specify a name for the filter, otherwise the function name will be used. Example:

@template_filter
def reverse(s):
    return s[::-1]

@template_filter('order_by')
def query_order_by(query, column):
    return query.order_by(column)
Parameters:name (basestring) – the optional name of the filter, otherwise the function name will be used
langdev.web.method_for(endpoint)

Gets the available method for endpoint. It is useful for generating <form> tags.

<form action="{{ url_for('endpoint') }}"
      method="{{ method_for('endpoint') }}">
Parameters:endpoint (basestring) – an endpoint name to find the available method
Returns:an available method
Return type:basestring
langdev.web.render(template_name, value, **context)

The generic content type version of flask.render_template() function. Unlike flask.render_template(), it takes one more required parameter, value, for generic serialization to JSON-like formats. And template_name doesn’t include its postfix.

render('user/profile', user, user=user)
Parameters:
  • template_name (basestring) – the name of the template to be rendered, but postfix excluded
  • **context – the variables that should be available in the context of the template

See also

Constant content_types

langdev.web.get_database_engine(config)

Gets SQLAlchemy Engine object from the config.

Parameters:config (flask.Config, dict) – the configuration that contains 'DATABASE_URL' or 'ENGINE'
Returns:SQLAlchemy database engine
Return type:sqlalchemy.engine.base.Engine

See also

SQLAlchemy — Engine Configuration

langdev.web.define_session()

Sets the g.session and g.database_engine global variables before every request.

langdev.web.query_order_by(query, column)

Orders a query by column.

{% for post in user.posts|order_by('-created_at') %}
  - {{ post }}
{% endfor %}
Parameters:column (basestring) – a column name. if - character has prepended, it becomes descending
Returns:an ordered query
Return type:sqlalchemy.orm.query.Query

Project Versions

Previous topic

langdev.objsimplify — Object simplifier for generic serialization

Next topic

langdev.web.home — Website home

This Page