<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Furry Brains &#187; django</title>
	<atom:link href="http://furrybrains.com/tag/django/feed/" rel="self" type="application/rss+xml" />
	<link>http://furrybrains.com</link>
	<description>The Furry Brains blog tackles a wide range of issues related to web development and design.</description>
	<lastBuildDate>Mon, 22 Jun 2009 16:39:24 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Named Scopes for Django</title>
		<link>http://furrybrains.com/2009/06/22/named-scopes-for-django/</link>
		<comments>http://furrybrains.com/2009/06/22/named-scopes-for-django/#comments</comments>
		<pubDate>Mon, 22 Jun 2009 16:24:10 +0000</pubDate>
		<dc:creator>Jim Dalton</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://furrybrains.com/?p=155</guid>
		<description><![CDATA[There are really only a handful of features I miss about Ruby on Rails, having switched over to using only Django about a year ago. One of the biggest among these has been named scopes.
What a named scope does in Rails, in Django terms, is allow you to name a given query and use it [...]]]></description>
			<content:encoded><![CDATA[<p>There are really only a handful of features I miss about Ruby on Rails, having switched over to using only Django about a year ago. One of the biggest among these has been named scopes.</p>
<p>What a <a href="http://www.pathf.com/blogs/2008/06/named-scopes-are-awesome/">named scope</a> does in Rails, in Django terms, is allow you to name a given query and use it as a base, or &#8220;scope,&#8221; for further queries. In other words, your narrowing down the results upon which additional queries that you run will be executed.</p>
<p>It&#8217;s pretty much the same idea behind modifying initial Manager QuerySets <a href="http://docs.djangoproject.com/en/dev/topics/db/managers/#modifying-initial-manager-querysets">as outlined in the Django documentation</a>, with one major difference: Scopes can be chained. Chaining is the feature I had long coveted, and I had been banging my head around for a solution for the last few months.</p>
<p>The other day while I was poking around the internals a bit though, a decent idea came to me. I realized it would be quite simple for me to extend the default <code>Manager</code> class with an attribute where I could store <code>QuerySet</code> methods I wanted to execute as part of a scope, and then overwrite <code>get_query_set()</code> with a hook that ran these methods before executing the rest of the query.</p>
<p>This turned out to be a nice strategy, and with the addition of some decorators the interface itself turned out to be extraordinarily simple.</p>
<p><span id="more-155"></span></p>
<p>Anyhow, here&#8217;s a walkthrough of how my alpha &#8220;scopable&#8221; module would work, with example code.</p>
<p>Let&#8217;s start with a simplistic blog post model:</p>
<pre class="brush: python;">
class Post(models.Model):
    title           = models.CharField('title', max_length=200)
    author          = models.ForeignKey(User)
    body            = models.TextField('author')
    publish_at      = models.DateTimeField('published at', default=datetime.now)
    is_draft        = models.BooleanField('is draft', default=False)
    is_deleted      = models.BooleanField('deleted', default=False)
    objects         = PostManager()
</pre>
<p>That&#8217;s enough to provide for some good example scopes. Now, here&#8217;s what <code>PostManager</code> might look like, with explanation to follow:</p>
<pre class="brush: python;">
from scopable import ScopableManager, scope

class PostManager(ScopableManager):
    @property
    @scope
    def active(self):
       return self.filter(is_deleted=False, user__is_active=True)

    @property
    @scope
    def published(self):
       return self.filter(is_draft=False)

    @property
    @scope
    def public(self):
       return self.active.published
</pre>
<p>So as you can see, the <code>ScopableManager</code> class is very easy to use; you just add the <code>@scope</code> decorator to any method that you want to turn into a scope. (Note that I added the <code>@property</code> decorator as a stylistic preference; it&#8217;s not required.)</p>
<p>To use the code, you just have to be sure to call a QuerySet method after it, e.g.:</p>
<pre class="brush: python;">
Post.objects.active.all()
Post.objects.published.all()
Post.objects.active.published.filter(publish_at__lte=datetime.now())
Post.objects.public.all()
</pre>
<p>The only difference between a scoped and unscoped method is that execution of any <code>QuerySet</code> methods defined in a scoped method are deferred until a <code>QuerySet</code> method is called (e.g. <code>Post.objects.public.all()</code>), and scoped methods return an instance of your manager back, so they can be chained. As you can see from the <code>public</code> method, scoped methods can be nested as well.</p>
<p>Why is this awesome? Well the main advantage from my perspective is that it allows your business rules to be defined in one and only one discrete place.</p>
<p>So if, for example, I added a new business rule that blog posts shouldn&#8217;t be considered published unless they were not drafts and the publish date had already passed, I could just add that rule to the <code>published</code> method:</p>
<pre class="brush: python;">
    @property
    @scope
    def published(self):
       return self.filter(publish_at__lte=datetime.now(), is_draft=False)
</pre>
<p>This means that any other queries that were built on <code>published</code>, such as <code>public</code>, are now updated with the new business logic we added to <code>published</code>.</p>
<p>The implementation of all of this is pretty straightforward. The main complication comes from implementing scope as a decorator and trying to elegantly capture all of the <code>QuerySet</code> methods used in a scoped <code>Manager</code> method.</p>
<p>This is still alpha code, and as of now it doesn&#8217;t handle custom <code>Manager</code> methods that are not scoped. I mostly just wanted to get this idea out there before I do any more work:</p>
<pre class="brush: python;">
&quot;&quot;&quot;
scopable.py - A module for providing Rails-like named scopes to Django model Managers
Verson 0.1
&quot;&quot;&quot;

from functools import wraps
from django.db import models

class DummyManager(object):
    def __init__(self, actual_mgr, calls=[]):
        super(DummyManager, self).__init__()
        self.actual_mgr = actual_mgr
        self.calls = calls

    def __call__(self, *args, **kwargs):
        last_call = self.calls[-1]
        last_call[&quot;is_property&quot;] = False
        last_call[&quot;args&quot;] = args
        last_call[&quot;kwargs&quot;] = kwargs
        clone = DummyManager(self.actual_mgr, self.calls)
        self.calls = []
        return clone

    def __getattribute__(self, name):
        if name in [&quot;calls&quot;, &quot;actual_mgr&quot;]:
            return super(DummyManager, self).__getattribute__(name)

        # attr_type, args, and kwargs are set here by default in case the attr never gets called
        call = { &quot;attr&quot;: name, &quot;is_property&quot;: True, &quot;args&quot;: [], &quot;kwargs&quot;: {} }

        # if this is a default manager method
        if hasattr(models.Manager, name):
            call[&quot;definition&quot;] = &quot;default&quot;

        # else if this is a custom manager method
        elif hasattr(self.actual_mgr.__class__, name):
            call[&quot;definition&quot;] = &quot;custom&quot;

        # if it can't be found in those two classes, throw an error
        else:
            raise AttributeError(&quot;'%s' object has no attribute '%s'&quot; % (self.actual_mgr.__class__.__name__, name))

        self.calls.append(call)
        clone = DummyManager(self.actual_mgr, self.calls)
        self.calls = []
        return clone

def scope(fn):
    @wraps(fn)
    def scopified(*args, **kwargs):
        args_list = list(args)
        actual_mgr = args_list.pop(0)
        dummy = DummyManager(actual_mgr, [])
        args_list.insert(0, dummy)
        deferred_calls = get_deferred_calls(actual_mgr, fn, args_list, kwargs)
        a = actual_mgr.__class__(deferred_calls=deferred_calls)
        a.contribute_to_class(actual_mgr.model, actual_mgr.model.__name__)
        return a
    return scopified

def get_deferred_calls(mgr, fn, args, kwargs):
    deferred_calls = []
    result = fn(*args, **kwargs)
    if result.calls:
        for call in result.calls:
            if call[&quot;definition&quot;] == &quot;default&quot;:
                deferred_calls.append(call)
            elif call[&quot;definition&quot;] == &quot;custom&quot;:
                next_result = getattr(mgr, call[&quot;attr&quot;])
                if not call[&quot;is_property&quot;]:
                    next_result = next_result(*call[&quot;args&quot;], **call[&quot;kwargs&quot;])
                deferred_calls = deferred_calls + next_result.deferred_calls
            else:
                raise AssertionError(&quot;Something went wrong.&quot;)
    return deferred_calls

class ScopableManager(models.Manager):

    def __init__(self, deferred_calls=[]):
        super(ScopableManager, self).__init__()
        self.scopes = []
        self.deferred_calls = deferred_calls

    def get_query_set(self):
        qs = super(ScopableManager, self).get_query_set()
        if self.deferred_calls:
            for deferred_call in self.deferred_calls:
                next_qs = getattr(qs, deferred_call[&quot;attr&quot;])
                if not deferred_call[&quot;is_property&quot;]:
                    next_qs = next_qs(*deferred_call[&quot;args&quot;], **deferred_call[&quot;kwargs&quot;])
                qs = next_qs
        return qs
</pre>
<p>Please let me know in the comments if you have feedback, suggestions, bugs, grave warnings, etc.</p>
]]></content:encoded>
			<wfw:commentRss>http://furrybrains.com/2009/06/22/named-scopes-for-django/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Organizing Unit Tests in Django</title>
		<link>http://furrybrains.com/2009/02/25/organizaing-unit-tests-in-django/</link>
		<comments>http://furrybrains.com/2009/02/25/organizaing-unit-tests-in-django/#comments</comments>
		<pubDate>Thu, 26 Feb 2009 07:18:26 +0000</pubDate>
		<dc:creator>Jim Dalton</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[TDD]]></category>

		<guid isPermaLink="false">http://furrybrains.com/?p=136</guid>
		<description><![CDATA[I just started a small side project last week, and I finally decided to take the opportunity to really make TDD (test-driven development) happen for me using Django. Though I&#8217;ve dabbled with testing in Django and done quite a bit more with testing in Ruby, I never managed to embrace TDD fully. It&#8217;s about time, [...]]]></description>
			<content:encoded><![CDATA[<p>I just started a small side project last week, and I finally decided to take the opportunity to really make <a href="http://en.wikipedia.org/wiki/Test-driven_development">TDD</a> (test-driven development) happen for me using Django. Though I&#8217;ve dabbled with testing in <a href="http://www.djangoproject.com/">Django</a> and done quite a bit more with testing in Ruby, I never managed to embrace TDD fully. It&#8217;s about time, I figure.</p>
<p>Well, I hit some snags as I got underway. Partly this had to do with me being something of a novice when it comes to testing, and partly it has to do with the state of testing in Django at present.</p>
<p>My mini-gripe with testing in Django is that it&#8217;s just kind of loosey-goosey. You can use <a href="http://docs.python.org/library/doctest.html">doctest</a>, or you can use <a href="http://docs.python.org/library/unittest.html">unittest</a>. You can put your tests in your functions, or in your models, or in tests.py, or in a tests/ subfolder.</p>
<p>For me at least, I want my framework to be a bit more opinionated. I want a bunch of guys who are all smarter than me to have sat around and thought about (or better yet, generalized from a working production code) things like the best way to configure testing for a Python web framework, and then to tell me, in no uncertain terms, the exact way to do it.</p>
<p>I&#8217;m obviously exaggerating a bit, and I don&#8217;t really mean at all to rant about a phenomally awesome, not to mention open source, framework like Django. What all of the above amounts to is that I had to ponder deeply on the problem space myself and come up with a way of structuring my test code that would work for me.</p>
<p>So that&#8217;s what I did, and here is the organizational structure I intend to use as I proceed.</p>
<p><span id="more-136"></span></p>
<h3>An Organizational Structure for Test Code in Django</h3>
<p>This is how <a href="http://en.wikipedia.org/wiki/Unit_testing">Wikipedia defines unit testing</a>:</p>
<blockquote><p>In computer programming, unit testing is a software design and development method where the programmer verifies that individual units of source code are working properly. A unit is the smallest testable part of an application.</p></blockquote>
<p>One of the things that kept frustrating me about writing tests was that my tests were all methods of a test case class that referred to a model class or a view module. Most of the examples you see are written in that form, such as the following from the <a href="http://docs.djangoproject.com/en/dev/topics/testing/">Django testing documentation</a>:</p>
<pre class="brush: python;">
class AnimalTestCase(unittest.TestCase):
    def setUp(self):
        self.lion = Animal.objects.create(name=&quot;lion&quot;, sound=&quot;roar&quot;)
        self.cat = Animal.objects.create(name=&quot;cat&quot;, sound=&quot;meow&quot;)

    def testSpeaking(self):
        self.assertEquals(self.lion.speak(), 'The lion says &quot;roar&quot;')
        self.assertEquals(self.cat.speak(), 'The cat says &quot;meow&quot;')
</pre>
<p>What frustated me was that you&#8217;d end up with a big long catalog of test methods for all the various methods of Animal. It would also be hard to determine what the test was referring to by looking at the test method name. Sometimes a method might be <code>testSpeaking()</code>; other times it might be <code>testShouldSpeak()</code>. In either case, I didn&#8217;t feel like the methods were serving very well as documentation, which is one of the supposed benefits of user testing.</p>
<p>Then as I re-read the definition of a unit test to myself for the hundredth time, it dawned on me. Unit tests are supposed to focus on *the smallest testable part of an application.* But all the examples I was seeing had tests that were organized around the class, which is certainly not the smallest testable part. That distinction belongs to methods and class attributes.</p>
<p>I stumbled upon my first rule: <em>Organize test methods around the individual methods and attributes of a class, instead of the class itself.</em> How so? The easiest is to create a separate test case subclass for *each* method and attribute. Each test method of that subclass is then a test of a behavior of the method or attribute under test.</p>
<p>This mini-breakthrough also helped me flesh out a rule for writing better test method names: <em>Start every test method name with &#8220;test_should_&#8221;.</em> This borrows from a page in <a href="http://en.wikipedia.org/wiki/Behavior_driven_development">behavior-driven development</a>, but basically &#8220;should&#8221; is the perfect word to use to express a desired behavior.</p>
<h3>In Practice</h3>
<p>Here&#8217;s what all this looks like in practice, taking the <code>Animal</code> class as an example again from Django&#8217;s testing documentation:</p>
<pre class="brush: python;">
# models.py
from django.db import models

class Animal(models.Model):
    name = models.CharField(max_length=20)
    sound = models.CharField(max_length=20)

    def speak(self):
        return 'The %s says &quot;%s&quot;' % (self.name, self.sound)
</pre>
<p>And here&#8217;s how I would propose that test code for this class be structured (with a focus on the organization, not thoroughness of testing or appropriateness of implementation):</p>
<pre class="brush: python;">
from django.test import TestCase
from myapp.models import Animal

class AnimalTestCase(TestCase):
    def setUp(self):
        self.lion = Animal.objects.create(name=&quot;lion&quot;, sound=&quot;roar&quot;)

class Class(AnimalTestCase):
    # obviously a silly, trivial test
    def test_should_instantiate_object_of_its_own_type(self):
        self.failUnlessEqual(type(Animal()), Animal)

class NameAttribute(AnimalTestCase):
    def test_should_be_defined(self):
        self.failUnless(hasattr(self.lion, 'name'))

    def test_should_accept_string_value(self):
        a = Animal()
        a.name = &quot;bear&quot;
        a.save()
        self.failUnless(a.id) # object was saved successfully

    def test_should_allow_no_more_than_20_chars(self):
        self.lion.name = &quot;a&quot;*21
        self.failUnlessRaises(Warning, self.lion.save)

class SoundAttribute(AnimalTestCase):
    def test_should_be_defined(self):
        self.failUnless(hasattr(self.lion, 'sound'))

    # similar test_shoulds as with 'name' here

class SpeakMethod(AnimalTestCase):
    def test_should_be_defined(self):
        self.failUnless(hasattr(self.new_pp, 'speak'))

    def test_should_return_appropriate_string(self):
        self.failUnlessEquals(self.lion.speak(), 'The lion says &quot;roar&quot;')
</pre>
<p>(I have not executed the above code, so please don&#8217;t be surprised if it has errors.)</p>
<p>So, it&#8217;s pretty simple really. Each attribute or method being tested is given an explicitly named test class, which is a subclass of a single test case class for the class under test. Each specified behavior of that attribute or method is assigned a test method beneath the appropriate test class.</p>
<p>Here&#8217;s what I like about this approach:</p>
<ul>
<li>You don&#8217;t have to &#8220;think&#8221; while writing tests. If you&#8217;ve decided to define a method, you just list off to yourself all of the things it should do and/or require.</li>
<li>The tests actually <em>read</em> like documentation. Not that you&#8217;ll ever see them expressed anywhere in this format, but I like to read them as &#8220;NamedAttribute.should_accept_test_value&#8221; or &#8220;SpeakMethod.should_return_appropriate_string&#8221;. It&#8217;s just so easy to make sense out of exactly what each part of your model is supposed to do.</li>
<li>The class names provide some grouping structure to the test methods. Ten groups of ten methods, for example, are a lot easier to make sense of and keep organized than 100 test methods without any other structure.</li>
</ul>
<h3>Getting It To Work</h3>
<p>It&#8217;s actually quite easy to get this all to work. The only trick involved is that you have to specify which suites to run to the django test runner. (This may not in fact be a requirement, but I wanted adding new test case subclasses to be quick and easy; I didn&#8217;t want to spend my life configuring which tests to run in a bunch of places.)</p>
<p>Here&#8217;s a quick function I wrote to handle this:</p>
<pre class="brush: python;">
def build_test_suite_from(test_cases):
    &quot;&quot;&quot;
    Returns a single or group of unittest test suite(s) that's ready to be
    run. The function expects a list of classes that are subclasses of
    TestCase.

    The function will search the module where each class resides and
    build a test suite from that class and all subclasses of it.
    &quot;&quot;&quot;
    test_suites = []
    for test_case in test_cases:
        mod = __import__(test_case.__module__)
        components = test_case.__module__.split('.')
        for comp in components[1:]:
            mod = getattr(mod, comp)
        tests = []
        for item in mod.__dict__.values():
            if type(item) is type and issubclass(item, test_case):
                tests.append(item)
        test_suites.append(unittest.TestSuite(map(unittest.TestLoader().loadTestsFromTestCase, tests)))
    return unittest.TestSuite(test_suites)
</pre>
<p>Django allows you to define a <code>suite()</code> function in your test module, which is then automatically detected by the test runner when tests are executed. So we&#8217;ll just embed the above function in that, like this, for example:</p>
<pre class="brush: python;">
from myapp.test_utils import build_test_suite_from
from myapp.tests.models.some_model import SomeModelTestCase
from myapp.tests.models.another_model import AnotherModelTestCase

test_cases = [
    SomeModelTestCase,
    AnotherModelTestCase
]

def suite():
    return build_test_suite_from(test_cases)
</pre>
<p>Here&#8217;s what happens: <code>build_test_suite_from()</code> runs through each <code>TestCase</code> you pass to it, and searches the module where that <code>TestCase</code> is found for subclasses of that <code>TestCase</code> (including the <code>TestCase</code> itself). It then builds a test suite containing all of the test methods out of each of these, and then combines all of them together into one giant test suite which Django will run automatically when you do <code>./manage.py test</code>.</p>
<p>The idea again is to keep configuration to a minimum. For each model you create test cases for you have to import the model&#8217;s <code>TestCase</code> and add it to the <code>test_cases</code> list, but no additional configuration is required for subclasses you create of that <code>TestCase</code>.</p>
<p>I expect that this structure will evolve as I actually start using it more. In particular, tests for forms and views may or may not work as cleanly within this structure as do those for models.</p>
<p>If you have any comments or feedback (good or bad), I&#8217;d love to hear about it below.</p>
]]></content:encoded>
			<wfw:commentRss>http://furrybrains.com/2009/02/25/organizaing-unit-tests-in-django/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Prepending an Indefinite Article (a or an) in Front of a Word or Phrase</title>
		<link>http://furrybrains.com/2009/01/26/with_indefinite_article-a-django-template-tag/</link>
		<comments>http://furrybrains.com/2009/01/26/with_indefinite_article-a-django-template-tag/#comments</comments>
		<pubDate>Mon, 26 Jan 2009 22:29:06 +0000</pubDate>
		<dc:creator>Jim Dalton</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://furrybrains.com/?p=126</guid>
		<description><![CDATA[Today, I had to whip up a Django template tag to add &#8220;a&#8221; or &#8220;an&#8221; in front of a phrase.
It started off pretty easy &#8212; just use &#8220;an&#8221; when the first letter of the phrase is a vowel and otherwise use &#8220;a.&#8221; And then I remembered about abbreviations (&#8220;an HR department&#8221;), silent H words (&#8220;an [...]]]></description>
			<content:encoded><![CDATA[<p>Today, I had to whip up a Django template tag to add &#8220;a&#8221; or &#8220;an&#8221; in front of a phrase.</p>
<p>It started off pretty easy &#8212; just use &#8220;an&#8221; when the first letter of the phrase is a vowel and otherwise use &#8220;a.&#8221; And then I remembered about abbreviations (&#8220;an HR department&#8221;), silent H words (&#8220;an honorable man&#8221;), and exceptions among a few O and U words (&#8220;a one-time offer&#8221; or &#8220;a union representative&#8221;), and suddenly it wasn&#8217;t so easy.</p>
<p>Fortunately I managed to find an excellent <a href="http://www.speech.cs.cmu.edu/cgi-bin/cmudict">database of English words that includes pronunciations</a>, and in no time at all I was able to generate a list of exceptions.</p>
<p>Here&#8217;s the final result, implemented as a <a href="http://docs.djangoproject.com/en/dev/howto/custom-template-tags/">custom django template tag</a> (in my use case, I needed to able to wrap the original phrase in an HTML tag, so I couldn&#8217;t use a filter tag). This could also easily be a standalone Python function as well (literally by removing the <code>@register.simple_tag</code> decorator).</p>
<p><span id="more-126"></span></p>
<pre class="brush: python;">
import re

@register.simple_tag
def with_indefinite_article(phrase, before_phrase=&quot;&quot;, after_phrase=&quot;&quot;):
    silent_h_words = [&quot;homage&quot;, &quot;hour&quot;, &quot;herb&quot;, &quot;heir&quot;, &quot;honest&quot;, &quot;honor&quot;]
    letters_with_initial_vowel_sound = [&quot;a&quot;, &quot;e&quot;, &quot;f&quot;, &quot;h&quot;, &quot;i&quot;, &quot;l&quot;, &quot;m&quot;, &quot;n&quot;, &quot;o&quot;, &quot;r&quot;, &quot;s&quot;, &quot;x&quot;]
    vowels = [&quot;a&quot;, &quot;e&quot;, &quot;i&quot;, &quot;o&quot;, &quot;u&quot;]
    o_exceptions = [&quot;once&quot;,&quot;one&quot;,&quot;oneness&quot;,&quot;ones&quot;,&quot;oneself&quot;,&quot;onetime&quot;]
    u_exceptions = [&quot;ubiquitous&quot;,&quot;uganda&quot;,&quot;ugandan&quot;,&quot;ukraine&quot;,&quot;ukrainian&quot;,&quot;ukulele&quot;,&quot;ukuleles&quot;,&quot;unanimous&quot;,&quot;unanimously&quot;,&quot;unicellular&quot;,&quot;unicorn&quot;,&quot;unicorns&quot;,&quot;unicycle&quot;,&quot;unicycles&quot;,&quot;unification&quot;,&quot;unified&quot;,&quot;uniform&quot;,&quot;uniformed&quot;,&quot;uniformly&quot;,&quot;uniforms&quot;,&quot;unify&quot;,&quot;unifying&quot;,&quot;unilateral&quot;,&quot;unilateralism&quot;,&quot;unilaterally&quot;,&quot;union&quot;,&quot;unionist&quot;,&quot;unionists&quot;,&quot;unionization&quot;,&quot;unionized&quot;,&quot;unionizing&quot;,&quot;unions&quot;,&quot;unique&quot;,&quot;uniquely&quot;,&quot;uniqueness&quot;,&quot;unisex&quot;,&quot;unison&quot;,&quot;unisons&quot;,&quot;unit&quot;,&quot;unitarian&quot;,&quot;unitary&quot;,&quot;unite&quot;,&quot;united&quot;,&quot;uniting&quot;,&quot;units&quot;,&quot;unity&quot;,&quot;universal&quot;,&quot;universally&quot;,&quot;universe&quot;,&quot;universes&quot;,&quot;universities&quot;,&quot;university&quot;,&quot;universitys&quot;,&quot;unix&quot;,&quot;uranium&quot;,&quot;urinalysis&quot;,&quot;urinary&quot;,&quot;urinate&quot;,&quot;urinating&quot;,&quot;urine&quot;,&quot;urologist&quot;,&quot;urologists&quot;,&quot;urology&quot;,&quot;uruguay&quot;,&quot;uruguayan&quot;,&quot;uruguays&quot;,&quot;usable&quot;,&quot;usage&quot;,&quot;usages&quot;,&quot;use&quot;,&quot;used&quot;,&quot;useful&quot;,&quot;usefully&quot;,&quot;usefulness&quot;,&quot;useless&quot;,&quot;usenet&quot;,&quot;user&quot;,&quot;users&quot;,&quot;uses&quot;,&quot;usual&quot;,&quot;usually&quot;,&quot;usurp&quot;,&quot;usurpation&quot;,&quot;usurped&quot;,&quot;usurper&quot;,&quot;usurping&quot;,&quot;usury&quot;,&quot;utah&quot;,&quot;utahn&quot;,&quot;utahns&quot;,&quot;utahs&quot;,&quot;utensil&quot;,&quot;utensils&quot;,&quot;uterine&quot;,&quot;utero&quot;,&quot;uterus&quot;,&quot;utilitarian&quot;,&quot;utilities&quot;,&quot;utility&quot;,&quot;utilitys&quot;,&quot;utilization&quot;,&quot;utilize&quot;,&quot;utilized&quot;,&quot;utilizes&quot;,&quot;utilizing&quot;,&quot;utopia&quot;,&quot;utopian&quot;,&quot;utopians&quot;,&quot;utopias&quot;]

    # get rid of everything but letters and numbers and a few characters
    first_word = re.sub(r&quot;[^A-Za-z0-9\-]&quot;, &quot;&quot;, phrase.split()[0])
    first_letter = first_word[0].lower()

    # default to 'a'
    article = &quot;a&quot;

    if first_word.isupper() or (len(first_word) == 1):
        # if all caps or a single letter, we're going to assume it's an abbreviation
        if first_letter in letters_with_initial_vowel_sound:
            article = &quot;an&quot;
    elif first_letter == &quot;h&quot; and any( [re.match(r'(?i)%s' % w, first_word) for w in silent_h_words] ):
        article = &quot;an&quot;
    elif first_letter in vowels:
        if first_letter == 'e':
            article = &quot;a&quot; if first_word.lower().split('-')[0] == 'ewe' else &quot;an&quot;
        elif first_letter == 'o':
            article = &quot;a&quot; if first_word.lower().split('-')[0] in o_exceptions else &quot;an&quot;
        elif first_letter == 'u':
            article = &quot;a&quot; if first_word.lower().split('-')[0] in u_exceptions else &quot;an&quot;
        else:
            article = &quot;an&quot;

    return &quot;%s %s%s%s&quot; % (article, before_phrase, phrase, after_phrase)
</pre>
<p>Hopefully this will save someone who stumbles upon this some extra work down the road. If you find any bugs, please let me know in the comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://furrybrains.com/2009/01/26/with_indefinite_article-a-django-template-tag/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using &#8220;in&#8221; in QuerySets in Django</title>
		<link>http://furrybrains.com/2008/10/23/using-in-in-querysets-in-django/</link>
		<comments>http://furrybrains.com/2008/10/23/using-in-in-querysets-in-django/#comments</comments>
		<pubDate>Fri, 24 Oct 2008 03:11:14 +0000</pubDate>
		<dc:creator>Jim Dalton</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[queryset]]></category>

		<guid isPermaLink="false">http://furrybrains.com/?p=85</guid>
		<description><![CDATA[So the purpose of this post is just to look at a neat way of solving a problem in Django, and explain its mechanics and how it was put together.
In today&#8217;s example, the goal was pretty straightforward. We have a model class, Topic, that has a one-to-many relationship to a ThreadedComment model class. Each ThreadedComment [...]]]></description>
			<content:encoded><![CDATA[<p>So the purpose of this post is just to look at a neat way of solving a problem in Django, and explain its mechanics and how it was put together.</p>
<p>In today&#8217;s example, the goal was pretty straightforward. We have a model class, <code>Topic</code>, that has a one-to-many relationship to a <code>ThreadedComment</code> model class. Each <code>ThreadedComment</code> for each <code>Topic</code>, has a <code>User</code> associated with it; furthermore the <code>Topic</code> itself as a <code>User</code> associated with it (the creator of the topic).</p>
<p><span id="more-85"></span></p>
<p>The issue here is that it&#8217;s not actually a straightforward relationship between <code>Topic</code> and <code>ThreadedComment</code>, due to some complicating factors beyond the scope of this discussion. Suffice it to say what we&#8217;re looking for is a list of users who are participants in the discussion, inclusive of the topic creator. But we&#8217;re not looking for just any old list &#8212; what we need is a QuerySet, for the reason that we are making use of some specialized tags in our templates that require data to be in a QuerySet.</p>
<p>Here&#8217;s a quick example of what we want the interface to look like in the template:</p>
<pre class="brush: xml;">
	&lt;ul&gt;
	{% for participant in topic.participants %}
		&lt;li&gt;{{ participant.name }}&lt;/li&gt;
	{% endfor %}
	&lt;/ul&gt;
</pre>
<p>So let&#8217;s do this step by step.</p>
<p>First, let&#8217;s get a list of the <code>ThreadedComment</code> objects we need:</p>
<pre class="brush: python;">
ThreadedComment.public.all_for_object(topic))
</pre>
<p>The <code>all_for_object()</code> method is one of the &#8220;complications&#8221; we were referring to above. All we care about is that it returns a set of <code>ThreadedComment</code> objects for a given topic.</p>
<p>Okay, but now that we have this set of objects, how are we going to return a QuerySet that yields us User objects?</p>
<p>My co-worker actually showed this to me, so I&#8217;ll share it here. We&#8217;ll use <code>in</code> keyword argument in <code>filter()</code>, which maps to the SQL <code>IN</code> clause. The Django documentation has a simple example:</p>
<pre class="brush: python;">
Entry.objects.filter(id__in=[1, 3, 4])
</pre>
<p>is equivalent to:</p>
<pre class="brush: sql;">
SELECT ... WHERE id IN (1, 3, 4)
</pre>
<p>So that&#8217;s just what we&#8217;re going to do: pass a list of user ids as an <code>in</code> keyword argument to identify the users we want.</p>
<p>First, let&#8217;s extend our original QuerySet to get the values we need as a list:</p>
<pre class="brush: python;">
ThreadedComment.public.all_for_object(self).values_list('user_id', flat=True)
</pre>
<p>One thing is missing &mdash; we need to include the user id of the topic creator, as I mentioned above. We&#8217;ll just evaluate the QuerySet to a list and then add the topic creator to it:</p>
<pre class="brush: python;">
list(ThreadedComment.public.all_for_object(self).values_list('user_id', flat=True)) + [self.creator.id]
</pre>
<p>Now let&#8217;s take the list of ids and pass it to a QuerySet that we&#8217;ll return <code>User</code> objects. Our list goes in the <code>pk__in</code> keyword argument:</p>
<pre class="brush: python;">
User.objects.filter(
    pk__in=list(
        list(ThreadedComment.public.all_for_object(self).values_list('user_id', flat=True)) + [self.creator.id]
    )
)
</pre>
<p>Now let&#8217;s add this as a method <code>participants()</code> to the <code>Topic</code> model and throw a <code>@property</code> decorator on it so we can call it as an attribute:</p>
<pre class="brush: python;">
@property
def participants(self):
    return User.objects.filter(
        pk__in=list(
            list(ThreadedComment.public.all_for_object(self).values_list('user_id', flat=True)) + [self.creator.id]
        )
    )
</pre>
<p>And that&#8217;s all there is to it.</p>
<p>What I love about this approach is that it&#8217;s a pattern that fits in to so many places (so many that I almost feel like it deserves a higher level abstraction within the QuerySet API). There are variations that make it slightly more efficient as well; for example, if I didn&#8217;t need to append the topic creator id in my example, then I could have passed the list of ids as a query as opposed to evaluating it:</p>
<pre class="brush: python;">
User.objects.filter(
    pk__in=ThreadedComment.public.all_for_object(self).values_list('user_id', flat=True)).query
)
</pre>
<p>The advantage here is that this is evaluated as a subselect statement, so it only requires a single database call.</p>
<p>Anyhow, this technique is really fantastic when you have a set of related objects that for whatever reason you aren&#8217;t able to easily obtain as a QuerySet. Enjoy.</p>
]]></content:encoded>
			<wfw:commentRss>http://furrybrains.com/2008/10/23/using-in-in-querysets-in-django/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using the Campfire &#8220;API&#8221; in Django</title>
		<link>http://furrybrains.com/2008/10/08/using-the-campfire-api-in-django/</link>
		<comments>http://furrybrains.com/2008/10/08/using-the-campfire-api-in-django/#comments</comments>
		<pubDate>Thu, 09 Oct 2008 06:47:41 +0000</pubDate>
		<dc:creator>Jim Dalton</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[django]]></category>

		<guid isPermaLink="false">http://jsdalton.furrybrains.com/?p=28</guid>
		<description><![CDATA[I&#8217;m working on a group chat feature at my day job right now. The requirement is for a robust group chat widget to run alongside a live streaming video feed (via Ustream) of a two-day conference. If the solution works well, we want to consider using it for other purposes, e.g. webinars.
I was pretty dissatisified [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m working on a group chat feature at my day job right now. The requirement is for a robust group chat widget to run alongside a live streaming video feed (via <a href="http://www.ustream.tv/">Ustream</a>) of a two-day conference. If the solution works well, we want to consider using it for other purposes, e.g. webinars.</p>
<p>I was pretty dissatisified with my brief survey of existing chat widgets out there. This is a professional conference, so I really didn&#8217;t want a widget that was all yucked up with ads or smileys.</p>
<p>Anyhow, since I use <a href="http://www.campfirenow.com/">Campfire</a> a lot at my day job, I figured, why not give it a shot?</p>
<p>The good news is, Campfire does have an API, known as <a href="http://opensoul.org/2006/12/8/tinder-campfire-api">Tinder</a>.</p>
<p>The bad news is, it&#8217;s an unoffical API, written in Ruby. (Not that there&#8217;s anything wrong with that, but I didn&#8217;t want to deal with configuring a reliable production environment for what was hopefully a small project.)</p>
<p>The good news is, someone ported the Tinder API over to Python, calling it <a href="http://dev.oluyede.org/pinder/">Pinder</a>. Which rocks, because now I get to use Django.</p>
<p><span id="more-28"></span></p>
<p>To make a long story short, here&#8217;s what I ended up doing:</p>
<ul>
<li>Set up a custom django-admin command to execute the listener. So now I just run <code>$ python manage.py listen_to_campfire</code> at the command prompt and away it goes.</li>
<li>Created a listener class that implements the Pinder API and that basically continually loops over the transcript and adds any new messages it finds to the DB. Here&#8217;s the main function:</li>
</ul>
<pre class="brush: python;">
    def listening_to_campfire(self):
        &quot;&quot;&quot;
        The actual listening method. This method checks for new campfire messages and writes any it finds
        to the Message model. It returns True if everything goes okay, and False if any errors get caught.
        &quot;&quot;&quot;

        try:
            current_messages = self.room.transcript(self.room.transcripts()[0])
            most_current_message_id = int(current_messages[-1]['id'])
            if not most_current_message_id == self.last_message_id: # i.e. there are new messages

                # compares the id of each new message with the latest existing message id,
                # only keeps those that are newer
                new_messages = filter(
                    lambda cm: int(cm['id']) &gt; self.last_message_id,
                    current_messages
                )

                for nm in new_messages:
                        m = Message.create_from_campfire_message(nm)
                        if m:
                            m.save()
                            print m

                self.last_message_id = most_current_message_id
            else:
                print &quot;...&quot;
        except KeyboardInterrupt:
            script_halted()
        except:
            return False

        return True
</pre>
<ul>
<li>Created a view of the latest messages from the database and returned them in JSON.</li>
<li>Implemented an Ajax routine to poll the JSON feed and add any new results to the chat widget in real time.</li>
</ul>
<p>This works pretty nicely. The only thing the chat widget does not allow at present is the ability to type messages from the widget itself&#8230;for that you have to enter the Campfire chat room. This may turn out to be a important requirement though, in which case I will need to extend the Pinder API to allow for anonymous room logins.</p>
<p>To be continued&#8230;perhaps.</p>
<p><strong>UPDATE:</strong> The yucks won. I&#8217;m using a <a href="http://www.meebo.com/rooms/">Meebo room</a>, which I managed to format decently though.</p>
]]></content:encoded>
			<wfw:commentRss>http://furrybrains.com/2008/10/08/using-the-campfire-api-in-django/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
