Commit 020a08fa authored by Christos Stavrakakis's avatar Christos Stavrakakis
Browse files

Use custom method for joining urls

Implement 'join_urls' method for joining a base URL with a path,
without worrying about redundant or missing "/". Do not use
'urlparse.urljoin', since it has different semantics.
parent 984f8dce
......@@ -30,7 +30,6 @@
# documentation are those of the authors and should not be
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
from urlparse import urljoin
from random import random
from datetime import datetime, timedelta
......@@ -55,6 +54,7 @@ from django.core import validators
from django.contrib.auth.models import AnonymousUser
from django.core.exceptions import PermissionDenied
from synnefo.lib import join_urls
from astakos.im.models import (
AstakosUser, EmailChange, Invitation,
Resource, PendingThirdPartyUser, get_latest_terms,
......@@ -539,7 +539,7 @@ class ExtendedPasswordResetForm(PasswordResetForm):
"""
for user in self.users_cache:
url = user.astakosuser.get_password_reset_url(token_generator)
url = urljoin(BASEURL, url)
url = join_urls(BASEURL, url)
t = loader.get_template(email_template_name)
c = {
'email': user.email,
......
......@@ -48,11 +48,11 @@ from django.db import IntegrityError
from django.http import Http404
from urllib import quote
from urlparse import urljoin
from smtplib import SMTPException
from datetime import datetime
from functools import wraps
from synnefo.lib import join_urls
from astakos.im.settings import (
CONTACT_EMAIL, SITENAME, BASEURL, LOGGING_LEVEL,
VERIFICATION_EMAIL_SUBJECT, ACCOUNT_CREATION_SUBJECT,
......@@ -104,9 +104,9 @@ def send_verification(user, template_name='im/activation_email.txt'):
Raises SendVerificationError
"""
url = '%s?auth=%s&next=%s' % (urljoin(BASEURL, reverse('activate')),
url = '%s?auth=%s&next=%s' % (join_urls(BASEURL, reverse('activate')),
quote(user.auth_token),
quote(urljoin(BASEURL, reverse('index'))))
quote(join_urls(BASEURL, reverse('index'))))
message = render_to_string(template_name, {
'user': user,
'url': url,
......@@ -192,7 +192,7 @@ def send_invitation(invitation, template_name='im/invitation.txt'):
Raises SendInvitationError
"""
subject = _(INVITATION_EMAIL_SUBJECT)
url = '%s?code=%d' % (urljoin(BASEURL, reverse('index')), invitation.code)
url = '%s?code=%d' % (join_urls(BASEURL, reverse('index')), invitation.code)
message = render_to_string(template_name, {
'invitation': invitation,
'url': url,
......@@ -223,7 +223,7 @@ def send_greeting(user, email_template_name='im/welcome_email.txt'):
subject = _(GREETING_EMAIL_SUBJECT)
message = render_to_string(email_template_name, {
'user': user,
'url': urljoin(BASEURL, reverse('index')),
'url': join_urls(BASEURL, reverse('index')),
'baseurl': BASEURL,
'site_name': SITENAME,
'support': CONTACT_EMAIL})
......
# Copyright (C) 2013 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.
def join_urls(a, b):
"""
>>> join_urls("http://www.test.org", "path")
'http://www.test.org/path'
>>> join_urls("http://www.test.org/", "path")
'http://www.test.org/path'
>>> join_urls("http://www.test.org", "/path")
'http://www.test.org/path'
>>> join_urls("http://www.test.org/", "/path")
'http://www.test.org/path'
>>> join_urls("http://www.test.org/", "/path/")
'http://www.test.org/path/'
>>> join_urls("http://www.test.org/a/b", "c/d")
'http://www.test.org/a/b/c/d'
>>> join_urls("http://www.test.org/a/b/", "c/d")
'http://www.test.org/a/b/c/d'
>>> join_urls("http://www.test.org/a/b", "/c/d")
'http://www.test.org/a/b/c/d'
>>> join_urls("http://www.test.org/a/b/", "/c/d")
'http://www.test.org/a/b/c/d'
"""
return a.rstrip("/") + "/" + b.lstrip("/")
if __name__ == "__main__":
import doctest
doctest.testmod()
......@@ -39,11 +39,13 @@ from django.views.decorators.csrf import csrf_exempt
from django.conf import settings
from synnefo.lib import join_urls
ASTAKOS_URL = getattr(settings, 'ASTAKOS_URL', None)
USER_QUOTA_URL = urlparse.urljoin(ASTAKOS_URL, "astakos/api/quotas")
RESOURCES_URL = urlparse.urljoin(ASTAKOS_URL, "astakos/api/resources")
USER_CATALOG_URL = urlparse.urljoin(ASTAKOS_URL, "astakos/api/user_catalogs")
USER_FEEDBACK_URL = urlparse.urljoin(ASTAKOS_URL, "astakos/api/feedback")
USER_QUOTA_URL = join_urls(ASTAKOS_URL, "astakos/api/quotas")
RESOURCES_URL = join_urls(ASTAKOS_URL, "astakos/api/resources")
USER_CATALOG_URL = join_urls(ASTAKOS_URL, "astakos/api/user_catalogs")
USER_FEEDBACK_URL = join_urls(ASTAKOS_URL, "astakos/api/feedback")
from objpool.http import PooledHTTPConnection
......
......@@ -35,7 +35,8 @@ from logging import getLogger
from django.utils import simplejson as json
from django.core.urlresolvers import reverse
from urlparse import urljoin
from synnefo.lib import join_urls
from synnefo.api.servers import server_created
from synnefo.vmapi import backend, get_key, get_uuid, settings
......@@ -50,8 +51,8 @@ def create_server_params(sender, created_vm_params, **kwargs):
log.info("Setting vmapi params with key %s for %s", key, sender)
backend.set(key, json_value)
config_url = urljoin(settings.BASE_URL, reverse("vmapi_server_params",
args=[uuid]))
config_url = join_urls(settings.BASE_URL, reverse("vmapi_server_params",
args=[uuid]))
# inject sender (vm) with its configuration url
setattr(sender, 'config_url', config_url)
return uuid
......
......@@ -35,7 +35,7 @@
from django.test import TestCase
from django.utils import simplejson as json
import urlparse
from synnefo.lib import join_urls
from synnefo.vmapi import settings
......@@ -68,8 +68,8 @@ class TestServerParams(TestCase):
uuid = create_server_params(sender=vm, created_vm_params=params)
self.assertEqual(vm.config_url,
urlparse.urljoin(settings.BASE_URL,
'/vmapi/server-params/%s' % uuid))
join_urls(settings.BASE_URL,
'/vmapi/server-params/%s' % uuid))
key = "vmapi_%s" % uuid
self.assertEqual(type(backend.get(key)), str)
data = json.loads(backend.get(key))
......
#coding=utf8
from django.conf import settings
from urlparse import urljoin
ASTAKOS_URL = getattr(settings, "ASTAKOS_URL",
"https://accounts.example.synnefo.org/")
......
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