Skip to content

Commit

Permalink
Merge commit 'dcramer/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
miracle2k committed Feb 27, 2010
2 parents c819b6f + 8949dba commit 77d0e11
Show file tree
Hide file tree
Showing 26 changed files with 176 additions and 45 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -4,3 +4,4 @@
Coffin.egg-info
/dist
.*.swp
/docs/_build
2 changes: 1 addition & 1 deletion coffin/__init__.py
Expand Up @@ -14,7 +14,7 @@


__all__ = ('__version__', '__build__', '__docformat__', 'get_revision')
__version__ = (0, 3)
__version__ = (0, 3, 3)
__docformat__ = 'restructuredtext en'

import os
Expand Down
18 changes: 6 additions & 12 deletions coffin/common.py
Expand Up @@ -4,17 +4,12 @@
from django import dispatch
from jinja2 import Environment, loaders

__all__ = ('env', 'need_env')
__all__ = ('env',)

env = None

_JINJA_I18N_EXTENSION_NAME = 'jinja2.ext.i18n'

# TODO: This should be documented (as even I'm not sure where it's use-case is)
need_env = dispatch.Signal(providing_args=['arguments', 'loaders',
'filters', 'extensions',
'globals', 'tests'])

class CoffinEnvironment(Environment):
def __init__(self, filters={}, globals={}, tests={}, loader=None, extensions=[], **kwargs):
if not loader:
Expand Down Expand Up @@ -80,8 +75,10 @@ def _get_templatelibs(self):
if f.endswith('.py'):
try:
# TODO: will need updating when #6587 lands
libs.append(get_library(
"django.templatetags.%s" % os.path.splitext(f)[0]))
# libs.append(get_library(
# "django.templatetags.%s" % os.path.splitext(f)[0]))
libs.append(get_library(os.path.splitext(f)[0]))

except InvalidTemplateLibrary:
pass
return libs
Expand Down Expand Up @@ -111,7 +108,7 @@ def from_setting(setting):
setting = getattr(settings, setting, {})
if isinstance(setting, dict):
for key, value in setting.iteritems():
retval[user] = callable(value) and value or get_callable(value)
retval[key] = callable(value) and value or get_callable(value)
else:
for value in setting:
value = callable(value) and value or get_callable(value)
Expand Down Expand Up @@ -140,9 +137,6 @@ def get_env():
"""
:return: A Jinja2 environment singleton.
"""
# need_env.send(sender=Environment, arguments=arguments,
# loaders=loaders_, extensions=extensions,
# filters=filters, tests=tests, globals=globals)
from django.conf import settings

kwargs = {
Expand Down
Empty file added coffin/contrib/auth/__init__.py
Empty file.
1 change: 1 addition & 0 deletions coffin/contrib/auth/admin.py
@@ -0,0 +1 @@
from django.contrib.auth.admin import *
1 change: 1 addition & 0 deletions coffin/contrib/auth/backends.py
@@ -0,0 +1 @@
from django.contrib.auth.backends import *
1 change: 1 addition & 0 deletions coffin/contrib/auth/decorators.py
@@ -0,0 +1 @@
from django.contrib.auth.decorators import *
1 change: 1 addition & 0 deletions coffin/contrib/auth/forms.py
@@ -0,0 +1 @@
from django.contrib.auth.forms import *
1 change: 1 addition & 0 deletions coffin/contrib/auth/handlers.py
@@ -0,0 +1 @@
from django.contrib.auth.handlers import *
1 change: 1 addition & 0 deletions coffin/contrib/auth/middleware.py
@@ -0,0 +1 @@
from django.contrib.auth.middleware import *
1 change: 1 addition & 0 deletions coffin/contrib/auth/models.py
@@ -0,0 +1 @@
from django.contrib.auth.models import *
1 change: 1 addition & 0 deletions coffin/contrib/auth/tokens.py
@@ -0,0 +1 @@
from django.contrib.auth.tokens import *
6 changes: 6 additions & 0 deletions coffin/contrib/auth/urls.py
@@ -0,0 +1,6 @@
import inspect

from django.contrib.auth import urls

exec inspect.getsource(urlpatterns)\
.replace('django.contrib.auth.views', 'coffin.contrib.auth.views')
50 changes: 50 additions & 0 deletions coffin/contrib/auth/views.py
@@ -0,0 +1,50 @@
import inspect

from django.contrib.auth.views import *

# XXX: maybe approach this as importing the entire model, and doing string replacements
# on the template and shortcut import lines?

from coffin.shortcuts import render_to_response
from coffin.template import RequestContext, loader

exec inspect.getsource(logout)
exec inspect.getsource(password_change_done)
exec inspect.getsource(password_reset)
exec inspect.getsource(password_reset_confirm)
exec inspect.getsource(password_reset_done)
exec inspect.getsource(password_reset_complete)

exec inspect.getsource(password_change.view_func)
password_change = login_required(password_change)

# XXX: this function uses a decorator, which calls functools.wraps, which compiles the code
# thus we cannot inspect the source
def login(request, template_name='registration/login.html', redirect_field_name=REDIRECT_FIELD_NAME):
"Displays the login form and handles the login action."
redirect_to = request.REQUEST.get(redirect_field_name, '')
if request.method == "POST":
form = AuthenticationForm(data=request.POST)
if form.is_valid():
# Light security check -- make sure redirect_to isn't garbage.
if not redirect_to or '//' in redirect_to or ' ' in redirect_to:
redirect_to = settings.LOGIN_REDIRECT_URL
from django.contrib.auth import login
login(request, form.get_user())
if request.session.test_cookie_worked():
request.session.delete_test_cookie()
return HttpResponseRedirect(redirect_to)
else:
form = AuthenticationForm(request)
request.session.set_test_cookie()
if Site._meta.installed:
current_site = Site.objects.get_current()
else:
current_site = RequestSite(request)
return render_to_response(template_name, {
'form': form,
redirect_field_name: redirect_to,
'site': current_site,
'site_name': current_site.name,
}, context_instance=RequestContext(request))
login = never_cache(login)
4 changes: 2 additions & 2 deletions coffin/template/__init__.py
@@ -1,7 +1,7 @@
from django.template import (
Context as DjangoContext,
add_to_builtins as django_add_to_builtins,
get_library)
import_library)
from jinja2 import Template as _Jinja2Template

# Merge with ``django.template``.
Expand Down Expand Up @@ -85,7 +85,7 @@ def add_to_builtins(module_name):
Library object is compatible, remember!? We can just add them
directly to Django's own list of builtins.
"""
builtins.append(get_library(module_name))
builtins.append(import_library(module_name))
django_add_to_builtins(module_name)


Expand Down
10 changes: 4 additions & 6 deletions coffin/template/defaultfilters.py
Expand Up @@ -15,20 +15,18 @@ def url(view_name, *args, **kwargs):
return url._reverse(view_name, args, kwargs)

@register.filter(jinja2_only=True)
def timesince(value, arg=None):
def timesince(value, *arg):
if value is None or isinstance(value, Undefined):
return u''
from django.utils.timesince import timesince
if arg:
return timesince(value, arg)
return timesince(value)
return timesince(value, *arg)

@register.filter(jinja2_only=True)
def timeuntil(value, arg=None):
def timeuntil(value, *args):
if value is None or isinstance(value, Undefined):
return u''
from django.utils.timesince import timeuntil
return timeuntil(date, arg)
return timeuntil(value, *args)

@register.filter(jinja2_only=True)
def date(value, arg=None):
Expand Down
22 changes: 12 additions & 10 deletions coffin/template/defaulttags.py
Expand Up @@ -15,7 +15,6 @@ class LoadExtension(Extension):
parser instance needs to be modified, but apparently the only way to
get access would be by hacking the stack.
"""

tags = set(['load'])

def parse(self, parser):
Expand Down Expand Up @@ -219,19 +218,19 @@ class WithExtension(Extension):

def parse(self, parser):
lineno = parser.stream.next().lineno

value = parser.parse_expression()
parser.stream.expect('name:as')
name = parser.stream.expect('name')

body = parser.parse_statements(['name:endwith'], drop_needle=True)
# Use a local variable instead of a macro argument to alias
# the expression. This allows us to nest "with" statements.
body.insert(0, nodes.Assign(nodes.Name(name.value, 'store'), value))
return nodes.CallBlock(
self.call_method('_render_block', args=[value]),
[nodes.Name(name.value, 'store')], [], body).\
self.call_method('_render_block'), [], [], body).\
set_lineno(lineno)

def _render_block(self, value, caller=None):
return caller(value)
def _render_block(self, caller=None):
return caller()


class CacheExtension(Extension):
Expand Down Expand Up @@ -311,14 +310,17 @@ def parse(self, parser):
def _cache_support(self, expire_time, fragm_name, vary_on, lineno, caller):
from django.core.cache import cache # delay depending in settings
from django.utils.http import urlquote

from django.utils.hashcompat import md5_constructor

try:
expire_time = int(expire_time)
except (ValueError, TypeError):
raise TemplateSyntaxError('"%s" tag got a non-integer '
'timeout value: %r' % (list(self.tags)[0], expire_time), lineno)

cache_key = u':'.join([fragm_name] + [urlquote(v) for v in vary_on])
args_string = u':'.join([urlquote(v) for v in vary_on])
args_md5 = md5_constructor(args_string)
cache_key = 'template.cache.%s.%s' % (fragm_name, args_md5.hexdigest())
value = cache.get(cache_key)
if value is None:
value = caller()
Expand Down
7 changes: 4 additions & 3 deletions docs/conf.py
Expand Up @@ -43,11 +43,12 @@
# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
#

import coffin
# The short X.Y version.
version = '0.2'
version = '.'.join(map(str, coffin.__version__))
# The full version, including alpha/beta/rc tags.
release = '0.2'
release = '.'.join(map(str, coffin.__version__))

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down
3 changes: 3 additions & 0 deletions docs/contrib/auth.rst
@@ -0,0 +1,3 @@
==========================
:mod:`coffin.contrib.auth`
==========================
26 changes: 26 additions & 0 deletions docs/contrib/index.rst
@@ -0,0 +1,26 @@
=====================
:mod:`coffin.contrib`
=====================

Coffin includes replacements for several Django contrib modules.

To use this, simply change your import line from::

from django.contrib.<module>

To the following::

from coffin.contrib.<module>

-----------------
Supported Modules
-----------------

The following drop-in replacements are available:

.. toctree::
:maxdepth: 2

auth
markup
syndication
3 changes: 3 additions & 0 deletions docs/contrib/markup.rst
@@ -0,0 +1,3 @@
============================
:mod:`coffin.contrib.markup`
============================
3 changes: 3 additions & 0 deletions docs/contrib/syndication.rst
@@ -0,0 +1,3 @@
=================================
:mod:`coffin.contrib.syndication`
=================================
12 changes: 5 additions & 7 deletions docs/index.rst
@@ -1,16 +1,14 @@
.. Coffin documentation master file, created by
sphinx-quickstart on Tue Sep 8 15:22:15 2009.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
Welcome to Coffin's documentation!
==================================
Coffin Documentation
====================

Contents:

.. toctree::
:maxdepth: 2

install
contrib/index

Indices and tables
==================

Expand Down
38 changes: 38 additions & 0 deletions docs/install.rst
@@ -0,0 +1,38 @@
Installation
============

Install the package through PyPi::

easy_install Coffin

Or alternatively, get the source::

git clone git://github.com/dcramer/coffin.git
cd coffin
python setup.py install

Once installed, you will need to add Coffin to several places throughout your projects.

First, open up ``settings.py`` and add Coffin to your ``INSTALLED_APPS``::

INSTALLED_APPS = (
'coffin',
...
)

The easiest way to enable Jinja2, rather than Django, is to change your import paths. For example, if we're using the ``render_to_response`` shortcut, we simply need to tweak our import line::

from django.shortcuts import render_to_response

To the following::

from coffin.shortcuts import render_to_response

Coffin includes drop in replacements for the following Django modules:

* :mod:`django.shortcuts`
* :mod:`django.views.generic.simple`
* :mod:`django.contrib.auth`
* :mod:`django.contrib.markup`
* :mod:`django.contrib.syndication`
* :mod:`django.template`
2 changes: 2 additions & 0 deletions requirements.txt
@@ -0,0 +1,2 @@
-e svn+http://code.djangoproject.com/svn/django/trunk#egg=django
Jinja2
6 changes: 2 additions & 4 deletions setup.py
@@ -1,18 +1,16 @@
import os
from setuptools import setup, find_packages

import coffin

setup(name='Coffin',
version='.'.join(map(str, coffin.__version__)),
version=".".join(map(str, __import__("coffin").__version__)),
description='Jinja2 adapter for Django',
author='Christopher D. Leary',
author_email='cdleary@gmail.com',
maintainer='David Cramer',
maintainer_email='dcramer@gmail.com',
url='http://github.com/dcramer/coffin',
packages=find_packages(),
install_requires=['Jinja2', 'django>=1.0'],
#install_requires=['Jinja2', 'django>=1.2'],
classifiers=[
"Framework :: Django",
"Intended Audience :: Developers",
Expand Down

0 comments on commit 77d0e11

Please sign in to comment.