| 1 | from django.shortcuts import render_to_response |
|---|
| 2 | from django.template.context import RequestContext |
|---|
| 3 | from django.template import add_to_builtins |
|---|
| 4 | from django.db.models.signals import pre_init |
|---|
| 5 | from django.conf import settings |
|---|
| 6 | from django.contrib.contenttypes.models import ContentType |
|---|
| 7 | |
|---|
| 8 | from courant.core.gettag import gettag |
|---|
| 9 | from tagging.models import Tag, TaggedItem |
|---|
| 10 | |
|---|
| 11 | def render(request, *args, **kwargs): |
|---|
| 12 | kwargs['context_instance'] = RequestContext(request) |
|---|
| 13 | |
|---|
| 14 | # add appropriate file extension to template list |
|---|
| 15 | arguments = [arg for arg in args] |
|---|
| 16 | templates = [] |
|---|
| 17 | |
|---|
| 18 | for extension in request.extension: |
|---|
| 19 | for template in arguments[0]: |
|---|
| 20 | if template: |
|---|
| 21 | templates.append(''.join([template, '.', extension])) |
|---|
| 22 | |
|---|
| 23 | arguments[0] = templates |
|---|
| 24 | |
|---|
| 25 | return render_to_response(*arguments, **kwargs) |
|---|
| 26 | |
|---|
| 27 | #Make common_tags available everywhere |
|---|
| 28 | for library in settings.TEMPLATE_TAGS: |
|---|
| 29 | add_to_builtins(library) |
|---|
| 30 | |
|---|
| 31 | #Add a .type method to every object, so you can ask it what type it is from templates |
|---|
| 32 | def add_type(sender, *args, **kwargs): |
|---|
| 33 | sender.add_to_class('type', lambda self: self._meta.verbose_name ) |
|---|
| 34 | pre_init.connect(add_type) |
|---|
| 35 | |
|---|
| 36 | # register Tag model with gettag, since Tag model can't be modified directly (svn:external) |
|---|
| 37 | gettag.register(Tag, name_field='tag__name', |
|---|
| 38 | with_func=Tag.objects.usage_for_queryset, |
|---|
| 39 | in_func=TaggedItem.objects.get_by_model) |
|---|
| 40 | |
|---|
| 41 | # register ContentType with gettag |
|---|
| 42 | gettag.register(ContentType, name_field='model') |
|---|