Code Snippets
Calling Django from Scons
Note I've updated this recently. It only works using Django > 0.95
# UPDATE - this has gotten easier as of Django 0.95 (Magic Removal)
from django.conf import settings as django_settings
MODULE_HOME = path.dirname('.')
template_dirs = (
path.join(MODULE_HOME, 'templates'),
)
django_settings.configure(DEBUG=True,
TEMPLATE_DEBUG=True,
TEMPLATE_DIRS=template_dirs,
)
from django.template import Context, Template
from django.template import loader
""" run django templates"""
def run_django(target, source, env):
""" runs a django template, given target and source and dict of values"""
input_file = str(source[0]).encode('utf-8')
output_file = str(target[0]).encode('utf-8')
t = Template(open(input_file).read())
# FIXME: could use this
#django.template.loader.get_template(template_name)
c = Context(env['context'])
outfile = file(output_file, 'w')
outfile.write(t.render(c))
outfile.close()
return None
django = Builder(action = run_django, suffix='.html', src_suffix='.html')
env = Environment(ENV = {'PATH' : os.environ['PATH']},
BUILDERS = {'Django' : django}
)
index_context = {"current_page": "default"}
index = env.Django(source="templates/default.html",target="site/default.html",context=index_context)
env.Alias("index", index)
env.Default("index")
09/13/06
Keeping a log of updates to records in Django
I had the task of creating an RSS feed for wiki items as they are updated. I figured I would just use Django's post_save method. But then there was the classic chicken/egg problem. Luckily Django has the undocumented replaces_module meta attribute.
2/14/2007: I've changed this recently. After the "magic-removal" branch - this was much easier. So this only works for Django >= 0.95 and the replaces_module is no longer used. In fact that makes this bit of code trivial and typical, but I'm leaving it here anyway
from django.db import models
import datetime
class Tag(models.Model):
"""Tag (subject) of a Wikipage"""
name = models.SlugField(maxlength=75)
class Admin:
pass
class Meta:
db_table = "wiki_tags"
def __str__(self):
return self.name
class Wikihistory(models.Model):
"""Wiki History for RSS"""
wikipage = models.ForeignKey("Wikipage",db_column="wiki_page_id")
created_at = models.DateTimeField(auto_now_add=True)
class Admin:
pass
class Meta:
db_table = "wiki_history"
def __str__(self):
if self.wikipage:
return self.wikipage.title + " " + self.created_at.strftime("%m/%d/%y %I:%M %p")
else:
return self.created_at
def get_absolute_url(self):
date_formatted = self.created_at.strftime("%Y%m%d:%I:%M:%p")
if self.wikipage:
return "/wiki/view/%s" % (self.wikipage.title)
else:
return ""
class Wikipage(models.Model):
"""Wiki page"""
title = models.CharField(maxlength=75)
content = models.TextField(null=True,blank=True)
tags = models.ManyToManyField(Tag,null=True,blank=True)
class Admin:
pass
class Meta:
db_table = "wiki_wikipages"
def first_letter(self):
return self.title[0].upper()
def __str__(self):
return self.title
def save(self):
super(Wikipage, self).save()
entry = Wikihistory(wikipage=self)
entry.save()
def get_absolute_url(self):
return "/wiki/%s/" % self.title
02/14/07