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

48 lines
1.3 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
2011-05-06 02:53:07 +00:00
from pyvows import Vows
2011-08-18 14:28:51 +00:00
from django_pyvows.assertions import Url, Model, Template
2011-05-10 00:30:27 +00:00
from django.http import HttpRequest
2011-05-06 03:48:39 +00:00
2011-05-10 02:33:56 +00:00
class DjangoContext(Vows.Context):
2011-08-18 14:28:51 +00:00
2011-05-10 02:14:48 +00:00
@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-05-06 03:48:39 +00:00
2011-05-10 02:33:56 +00:00
def __init__(self, parent):
super(DjangoContext, self).__init__(parent)
2011-08-18 14:28:51 +00:00
if not parent:
DjangoContext._start_environment(self._get_settings())
2011-05-10 02:33:56 +00:00
def _get_settings(self):
2011-08-18 14:28:51 +00:00
if 'DJANGO_SETTINGS_MODULE' in os.environ:
return os.environ['DJANGO_SETTINGS_MODULE']
else:
return 'settings'
2011-05-10 02:33:56 +00:00
def _url(self, path):
return Url(self, path)
2011-05-10 03:45:24 +00:00
def _template(self, template_name, context):
return Template(template_name, context)
2011-05-10 00:30:27 +00:00
def _request(self, **kw):
return HttpRequest(**kw)
2011-05-10 02:14:48 +00:00
def _model(self, model_class):
return Model(self, model_class)