/
django_helpers.py
executable file
·43 lines (34 loc) · 1.49 KB
/
django_helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/python2.6
#
# Copyright 2011 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Wrapper methods to insulate us from Django nuances.
Provide Django setup and some utility methods.
"""
__author__ = 'aiuto@google.com (Tony Aiuto)'
import os
from django.conf import settings
settings.configure(
TEMPLATE_DIRS=('/', os.path.join(os.path.dirname(__file__))))
from django.template.loader import render_to_string
from django import template as django_template
# This is Django magic to add builtin tags and filters. They don't really
# support that use case. Instead you are supposed to put a package of filters
# in a specific place and the Django web server finds them for you. We are a
# standalone app, not running in their context, so we have to go under the hood
# a little.
django_template.add_to_builtins(
'googleapis.codegen.template_helpers')
def DjangoRenderTemplate(template_path, context_dict):
return render_to_string(template_path, context_dict)