Logo Search packages:      
Sourcecode: python-openid version File versions

util.py

00001 
"""
Utility code for the Django example consumer and server.
"""

from django.db import connection
from django.template.context import RequestContext
from django.template import loader
from django import http
from django.core.exceptions import ImproperlyConfigured

from django.conf import settings

from openid.store.filestore import FileOpenIDStore
from openid.store import sqlstore

00017 def getOpenIDStore(filestore_path, table_prefix):
    """
    Returns an OpenID association store object based on the database
    engine chosen for this Django application.

    * If no database engine is chosen, a filesystem-based store will
      be used whose path is filestore_path.

    * If a database engine is chosen, a store object for that database
      type will be returned.

    * If the chosen engine is not supported by the OpenID library,
      raise ImproperlyConfigured.

    * If a database store is used, this will create the tables
      necessary to use it.  The table names will be prefixed with
      table_prefix.  DO NOT use the same table prefix for both an
      OpenID consumer and an OpenID server in the same database.

    The result of this function should be passed to the Consumer
    constructor as the store parameter.
    """
    if not settings.DATABASE_ENGINE:
        return FileOpenIDStore(filestore_path)

    # Possible side-effect: create a database connection if one isn't
    # already open.
    connection.cursor()

    # Create table names to specify for SQL-backed stores.
    tablenames = {
        'settings_table': table_prefix + 'openid_settings',
        'associations_table': table_prefix + 'openid_associations',
        'nonces_table': table_prefix + 'openid_nonces',
        }

    types = {
        'postgresql': sqlstore.PostgreSQLStore,
        'mysql': sqlstore.MySQLStore,
        'sqlite3': sqlstore.SQLiteStore,
        }

    try:
        s = types[settings.DATABASE_ENGINE](connection.connection,
                                            **tablenames)
    except KeyError:
        raise ImproperlyConfigured, \
              "Database engine %s not supported by OpenID library" % \
              (settings.DATABASE_ENGINE,)

    try:
        s.createTables()
    except (SystemExit, KeyboardInterrupt, MemoryError), e:
        raise
    except:
        # XXX This is not the Right Way to do this, but because the
        # underlying database implementation might differ in behavior
        # at this point, we can't reliably catch the right
        # exception(s) here.  Ideally, the SQL store in the OpenID
        # library would catch exceptions that it expects and fail
        # silently, but that could be bad, too.  More ideally, the SQL
        # store would not attempt to create tables it knows already
        # exists.
        pass

    return s

def renderTemplate(request, template_name, response_data):
    context = RequestContext(request, response_data)
    template = loader.get_template(template_name)
    return template.render(context)

00089 def sendResponse(func):
    """
    A decorator to make Django views nicer to use and easier to read.
    Wraps a normal Django view and adds these behaviors:

    * The view may return a tuple of (template_name, template_context)
      where template_name is a normal Django template name and
      template_context is a dict of template context data.

    * The view may return a plain http.HttpResponse object, which will
      be returned to Django as-is.

    * The view may return in its template_context a 'response_class'
      key whose value is an http.HttpResponse subclass.  If this is
      found, it will be used to instantiate the final response object
      returned to Django.
    """
    def _responseWrapper(request, *args, **kwargs):
        result = func(request, *args, **kwargs)

        if isinstance(result, http.HttpResponse):
            return result
        else:
            template_name, response_data = result

            response_class = response_data.get('response_class', http.HttpResponse)
            return response_class(renderTemplate(request, template_name, response_data))
    return _responseWrapper

00118 def getTrustRoot(req):
    """
    Given a Django web request object, returns the OpenID 'trust root'
    for that request; namely, the absolute URL to the site root which
    is serving the Django request.  The trust root will include the
    proper scheme and authority.  It will lack a port if the port is
    standard (80, 443).
    """
    name = req.META['HTTP_HOST']
    try:
        name = name[:name.index(':')]
    except:
        pass

    try:
        port = int(req.META['SERVER_PORT'])
    except:
        port = 80

    proto = req.META['SERVER_PROTOCOL']

    if 'HTTPS' in proto:
        proto = 'https'
    else:
        proto = 'http'

    if port in [80, 443] or not port:
        port = ''
    else:
        port = ':%s' % (port,)

    url = "%s://%s%s/" % (proto, name, port)
    return url

00152 def normalDict(request_data):
    """
    Converts a django request MutliValueDict (e.g., request.GET,
    request.POST) into a standard python dict whose values are the
    first value from each of the MultiValueDict's value lists.  This
    avoids the OpenID library's refusal to deal with dicts whose
    values are lists, because in OpenID, each key in the query arg set
    can have at most one value.
    """
    return dict((k, v[0]) for k, v in request_data.iteritems())

Generated by  Doxygen 1.6.0   Back to index