This repository has been archived on 2023-01-02. You can view files and clone it, but cannot push or open issues or pull requests.
django-pyvows/django_pyvows/context.py

114 lines
3.2 KiB
Python
Raw Normal View History

2011-05-06 02:53:07 +00:00
#!/usr/bin/python
# -*- coding: utf-8 -*-
# django-pyvows extensions
# https://github.com/rafaelcaricio/django-pyvows
# Licensed under the MIT license:
# http://www.opensource.org/licenses/mit-license
# Copyright (c) 2011 Rafael Caricio rafael@caricio.com
2011-05-10 02:33:56 +00:00
import os
import re
from pyvows import Vows
from django.http import HttpRequest
2011-08-22 18:08:00 +00:00
from django_pyvows import http_helpers
from django_pyvows.assertions import Url, Model, Template
2011-08-19 14:57:24 +00:00
from django_pyvows.server import DjangoServer
2011-08-22 21:51:39 +00:00
from django_pyvows.settings_manager import settings_tracker
2011-08-22 14:00:19 +00:00
DEFAULT_PORT = 3331
DEFAULT_HOST = '127.0.0.1'
class DjangoContext(Vows.Context):
@classmethod
def start_environment(cls, settings_path):
if not settings_path:
raise RuntimeError('The settings_path argument is required.')
os.environ['DJANGO_SETTINGS_MODULE'] = settings_path
2011-08-22 21:51:39 +00:00
settings_tracker.install()
def __init__(self, parent):
super(DjangoContext, self).__init__(parent)
2011-08-22 20:06:54 +00:00
self.settings = {}
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())
def get_settings(self):
if 'DJANGO_SETTINGS_MODULE' in os.environ:
return os.environ['DJANGO_SETTINGS_MODULE']
else:
return 'settings'
def url(self, path):
return Url(self, path)
def template(self, template_name, context):
return Template(template_name, context)
def request(self, **kw):
return HttpRequest(**kw)
def model(self, model_class):
return Model(self, model_class)
2011-08-18 21:47:23 +00:00
def get(self, path):
2011-08-22 18:08:00 +00:00
return http_helpers.get(self.get_url(path))
2011-08-18 21:47:23 +00:00
def post(self, path, params):
2011-08-22 18:08:00 +00:00
return http_helpers.post(self.get_url(path), params)
def get_url(self, path):
ctx = self.parent
while ctx:
if hasattr(ctx, 'get_url'):
return ctx.get_url(path)
ctx = ctx.parent
2011-08-18 21:47:23 +00:00
return ""
class DjangoHTTPContext(DjangoContext):
2011-08-22 14:00:19 +00:00
def start_server(self, host=None, port=None):
if not port: port = DEFAULT_PORT
if not host: host = DEFAULT_HOST
self.address = (host, port)
self.server = DjangoServer(host, port)
2011-08-22 21:51:39 +00:00
self.server.start(self.settings)
def __init__(self, parent):
super(DjangoHTTPContext, self).__init__(parent)
self.ignore('start_server', 'settings')
2011-08-22 14:00:19 +00:00
@property
def host(self):
if hasattr(self, 'address'):
return self.address[0]
if self.parent and hasattr(self.parent, 'host'):
return self.parent.host
raise RuntimeError('Host could not be found in the context or any of its parents')
@property
def port(self):
if hasattr(self, 'address'):
return self.address[1]
if self.parent and hasattr(self.parent, 'port'):
return self.parent.port
raise RuntimeError('Port could not be found in the context or any of its parents')
def get_url(self, path):
if re.match('^https?:\/\/', path):
return path
return 'http://%s:%d%s' % (self.host, self.port, path)