globals Module

This file is part of the web2py Web Framework
Copyrighted by Massimo Di Pierro <mdipierro@cs.depaul.edu>

Contains the classes for the global used variables:

  • Request
  • Response
  • Session
class gluon.globals.Request(env)[source]

Bases: gluon.storage.Storage

Defines the request object and the default values of its members

  • env: environment variables, by gluon.main.wsgibase()
  • cookies
  • get_vars
  • post_vars
  • vars
  • folder
  • application
  • function
  • args
  • extension
  • now: datetime.datetime.now()
  • utcnow : datetime.datetime.utcnow()
  • is_local
  • is_https
  • restful()
body
compute_uuid()[source]
get_vars

Lazily parses the query string into get_vars

parse_all_vars()[source]

Merges get_vars and post_vars to vars

parse_get_vars()[source]

Takes the QUERY_STRING and unpacks it to get_vars

parse_post_vars()[source]

Takes the body of the request and unpacks it into post_vars. application/json is also automatically parsed

post_vars

Lazily parse the body into post_vars

requires_https()[source]

If request comes in over HTTP, redirects it to HTTPS and secures the session.

restful()[source]
user_agent()[source]
uuid

Lazily uuid

vars

Lazily parses all get_vars and post_vars to fill vars

class gluon.globals.Response[source]

Bases: gluon.storage.Storage

Defines the response object and the default values of its members response.write( ) can be used to write in the output html

download(request, db, chunk_size=65536, attachment=True, download_filename=None)[source]

Example of usage in controller:

def download():
    return response.download(request, db)

Downloads from http://…./download/filename

include_files(extensions=None)[source]

Includes files (usually in the head). Can minify and cache local files By default, caches in ram for 5 minutes. To change, response.cache_includes = (cache_method, time_expire). Example: (cache.disk, 60) # caches to disk for 1 minute.

include_meta()[source]
json(data, default=None)[source]
render(*a, **b)[source]
stream(stream, chunk_size=65536, request=None, attachment=False, filename=None)[source]

If in a controller function:

return response.stream(file, 100)

the file content will be streamed at 100 bytes at the time

Parameters:
  • stream – filename or read()able content
  • chunk_size (int) – Buffer size
  • request – the request object
  • attachment (bool) – prepares the correct headers to download the file as an attachment. Usually creates a pop-up download window on browsers
  • filename (str) – the name for the attachment

Note

for using the stream name (filename) with attachments the option must be explicitly set as function parameter (will default to the last request argument otherwise)

toolbar()[source]
write(data, escape=True)[source]
xmlrpc(request, methods)[source]

assuming:

def add(a, b):
    return a+b

if a controller function “func”:

return response.xmlrpc(request, [add])

the controller will be able to handle xmlrpc requests for the add function. Example:

import xmlrpclib
connection = xmlrpclib.ServerProxy(
    'http://hostname/app/contr/func')
print connection.add(3, 4)
class gluon.globals.Session[source]

Bases: gluon.storage.Storage

Defines the session object and the default values of its members (None)

  • session_storage_type : ‘file’, ‘db’, or ‘cookie’
  • session_cookie_compression_level :
  • session_cookie_expires : cookie expiration
  • session_cookie_key : for encrypted sessions in cookies
  • session_id : a number or None if no session
  • session_id_name :
  • session_locked :
  • session_masterapp :
  • session_new : a new session obj is being created
  • session_hash : hash of the pickled loaded session
  • session_pickled : picked session

if session in cookie:

  • session_data_name : name of the cookie for session data

if session in db:

  • session_db_record_id
  • session_db_table
  • session_db_unique_key

if session in file:

  • session_file
  • session_filename
clear()[source]
clear_session_cookies()[source]
connect(request=None, response=None, db=None, tablename='web2py_session', masterapp=None, migrate=True, separate=None, check_client=False, cookie_key=None, cookie_expires=None, compression_level=None)[source]

Used in models, allows to customize Session handling

Parameters:
  • request – the request object
  • response – the response object
  • db – to store/retrieve sessions in db (a table is created)
  • tablename (str) – table name
  • masterapp (str) – points to another’s app sessions. This enables a “SSO” environment among apps
  • migrate – passed to the underlying db
  • separate

    with True, creates a folder with the 2 initials of the session id. Can also be a function, e.g.

    separate=lambda(session_name): session_name[-2:]
    
  • check_client – if True, sessions can only come from the same ip
  • cookie_key (str) – secret for cookie encryption
  • cookie_expires – sets the expiration of the cookie
  • compression_level (int) – 0-9, sets zlib compression on the data before the encryption
forget(response=None)[source]
is_expired(seconds=3600)[source]
is_new()[source]
renew(clear_session=False)[source]
secure()[source]