From f3470f26935be2c38aad7f180e623581d39c8a73 Mon Sep 17 00:00:00 2001 From: Rafael Caricio Date: Mon, 22 Aug 2011 10:39:02 -0300 Subject: [PATCH] Fixing problem with parent configuration. --- django_pyvows/context.py | 19 +++++++++++++------ vows/sandbox/main/views.py | 19 +++++++++++++++++-- vows/sandbox/settings.py | 2 ++ vows/sandbox/urls.py | 1 + 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/django_pyvows/context.py b/django_pyvows/context.py index 8056f3b..460cf10 100644 --- a/django_pyvows/context.py +++ b/django_pyvows/context.py @@ -9,6 +9,7 @@ # Copyright (c) 2011 Rafael Caricio rafael@caricio.com import os +import re import urllib2 from pyvows import Vows @@ -28,9 +29,14 @@ class DjangoContext(Vows.Context): def __init__(self, parent): super(DjangoContext, self).__init__(parent) - self.port = 8080 - self.host = '127.0.0.1' - self.ignore('get_settings', 'template', 'request', 'model', 'url', 'start_environment', 'get_url', 'get', 'post') + if parent: + self.port = parent.port + self.host = parent.host + else: + self.port = 3331 + self.host = '127.0.0.1' + self.ignore('get_settings', 'template', 'request', 'model', 'url', + 'start_environment', 'port', 'host', 'get_url', 'get', 'post') def setup(self): DjangoContext.start_environment(self.get_settings()) @@ -75,9 +81,10 @@ class DjangoHTTPContext(DjangoContext): def __init__(self, parent): super(DjangoHTTPContext, self).__init__(parent) - self.port = 3331 - self.host = '127.0.0.1' - self.ignore('start_server', 'port', 'host') + self.ignore('start_server', 'settings') def get_url(self, path): + if re.match('^https?:\/\/', path): + return path return 'http://%s:%d%s' % (self.host, self.port, path) + diff --git a/vows/sandbox/main/views.py b/vows/sandbox/main/views.py index 1d51050..0a89d14 100644 --- a/vows/sandbox/main/views.py +++ b/vows/sandbox/main/views.py @@ -8,12 +8,27 @@ # http://www.opensource.org/licenses/mit-license # Copyright (c) 2011 Rafael Caricio rafael@caricio.com +from django.conf import settings from django.http import HttpResponse + def home(request): return HttpResponse('hello world') def say_hello(request): - if not 'name' in request.GET: + SAY_HELLO_WITHOUT_NAME = getattr(settings, "SAY_HELLO_WITHOUT_NAME", False) + + if 'name' in request.GET: + name = request.GET['name'] + + elif not SAY_HELLO_WITHOUT_NAME: return HttpResponse("What's your name?") - return HttpResponse("Hello, %s!" % request.GET['name']) + + elif SAY_HELLO_WITHOUT_NAME: + name = 'guess' + + return HttpResponse("Hello, %s!" % name) + +def get_setting(request, attr): + return HttpResponse(str(getattr(settings, attr))) + diff --git a/vows/sandbox/settings.py b/vows/sandbox/settings.py index affdd43..2cea468 100644 --- a/vows/sandbox/settings.py +++ b/vows/sandbox/settings.py @@ -130,6 +130,8 @@ INSTALLED_APPS = ( # 'django.contrib.admindocs', ) +SAY_HELLO_WITHOUT_NAME = False + # A sample logging configuration. The only tangible logging # performed by this configuration is to send an email to # the site admins on every HTTP 500 error. diff --git a/vows/sandbox/urls.py b/vows/sandbox/urls.py index 085bbff..a33791d 100644 --- a/vows/sandbox/urls.py +++ b/vows/sandbox/urls.py @@ -17,6 +17,7 @@ from django.conf.urls.defaults import patterns, include, url urlpatterns = patterns('', url(r'^$', 'sandbox.main.views.home', name='home'), url(r'^say/$', 'sandbox.main.views.say_hello', name='say_hello'), + url(r'^settings/(?P[\w_]+)/?$', 'sandbox.main.views.get_setting', name='get_setting'), # url(r'^sandbox/', include('sandbox.foo.urls')),