Such extensions already exists for Django and for Rails. Now it's available for Flask too.
Install with pip:
pip install Flask-Jasmine
Detailed instruction about configuration and usage
A blog about modern web development, programming in python, django, flask, javascript, coffeescript and other
pip install Flask-Jasmine
# -*- coding: utf-8 -*- import os import sys import unittest FILE_ABSPATH = lambda p: os.path.dirname(os.path.abspath(p)) COMPARE_DIRS = lambda a, b: FILE_ABSPATH(a) == FILE_ABSPATH(b) def collect_tests(): """ Function to collect tests from whole project """ basedir = os.path.abspath(os.path.dirname(__file__)) main_module = os.path.basename(basedir) test_file = "tests.py" test_dir = "tests" tests = [] new_locals = {} for root, dirs, files in os.walk(basedir): if test_file in files and \ os.path.join(root, test_file) != os.path.abspath(__file__): tests.append(os.path.join(root, test_file)) if os.path.dirname(root) == test_dir: tests.append(root) for test in tests: test_path = test.replace(basedir, '').replace('.py', '') module_name = "%s%s" % (main_module, test_path.replace('/', '.')) __import__(module_name) module = sys.modules[module_name] for cls in dir(module): attr = getattr(module, cls) try: attr.__module__ except AttributeError: continue # we don't want to import anything from current file if COMPARE_DIRS(sys.modules[attr.__module__].__file__, __file__): continue try: if issubclass(attr, unittest.TestCase): new_locals[cls] = attr except TypeError: pass return new_locals # execute tests if __name__ == '__main__': locals().update(collect_tests()) unittest.main()
pip install django-dtpanel-htmltidy
from django.views.generic.base import TemplateResponseMixin
from django.template import RequestContext
class RenderTemplate(TemplateResponseMixin):
"""django 1.3+ Class-Based replacement for
django.views.generic.simple.direct_to_template
Usage:
context = {}
return RenderTemplate.view(context, \
request=request, template_name="main.html")
"""
@classmethod
def view(cls, context, render_args={}, **initkwargs):
obj = cls()
for key, val in initkwargs.iteritems():
setattr(obj, key, val)
return obj.render_to_response(context, **render_args)
def render_to_response(self, context, **kwargs):
return super(RenderTemplate, self)\
.render_to_response(RequestContext(self.request, \
context), **kwargs)
# cache posts = Post.objects.all()[0:25] users = dict( (u.pk, u) for u in \ User.objects.filter(pk__in=set(p.user_id for p in posts)) ) for p in posts: p.__user_cache = users.get(p.user_id)