Commit 3595ea09 authored by Kostas Papadimitriou's avatar Kostas Papadimitriou
Browse files

django_lib: Component's urlpatterns utilty

extend_with_root_redirects can be used by components to easily extend the
registered urlpatterns with utility redirect views for the root urls.
parent 1c9a5d50
# Copyright 2011-2012 GRNET S.A. All rights reserved.
#
# Redistribution and use in source and binary forms, with or
# without modification, are permitted provided that the following
# conditions are met:
#
# 1. Redistributions of source code must retain the above
# copyright notice, this list of conditions and the following
# disclaimer.
#
# 2. Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
from django.conf.urls.defaults import url, patterns
from snf_django.lib.api.utils import prefix_pattern
from synnefo.lib.services import get_public_endpoint
from synnefo.lib import join_urls
def extend_with_root_redirects(patterns_obj, filled_services, service_type,
base_path):
"""
Append additional redirect url entries for `/` and `/<base_path>` paths.
`/` redirects to `/<base_path>` and `/<base_path>` to the resolved service
type url.
This is used in synnefo components root urlpatterns to append sane default
redirects to the chosen service url.
"""
service_url = get_public_endpoint(filled_services, service_type)
root_url_entry = url('^$', 'redirect_to',
{'url': join_urls('/', base_path)})
base_url_entry = url(prefix_pattern(base_path) + '$', 'redirect_to',
{'url': service_url})
# urls order matter. Setting base_url_entry first allows us to avoid
# redirect loops when base_path is empty or `/`
patterns_obj += patterns('django.views.generic.simple',
base_url_entry, root_url_entry)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment