<?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>muhuk.com &#187; signals</title>
	<atom:link href="http://www.muhuk.com/tag/signals/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.muhuk.com</link>
	<description>know thyself</description>
	<lastBuildDate>Thu, 02 Sep 2010 07:13:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Developing Reusable Django Apps: Signals</title>
		<link>http://www.muhuk.com/2010/03/developing-reusable-django-apps-signals/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=developing-reusable-django-apps-signals</link>
		<comments>http://www.muhuk.com/2010/03/developing-reusable-django-apps-signals/#comments</comments>
		<pubDate>Thu, 04 Mar 2010 08:49:17 +0000</pubDate>
		<dc:creator>Atamert Ölçgen</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[reusable]]></category>
		<category><![CDATA[signals]]></category>

		<guid isPermaLink="false">http://www.muhuk.com/?p=346</guid>
		<description><![CDATA[I wrote &#8220;signals provide a great way to propagate the events generated from your app&#8221; earlier. I think reusable apps should avoid hardcoding any kind of event handling and send signals instead. App consumers might prefer an email over an on-screen notification. They may even choose to ignore the event silently. A reusable app should [...]]]></description>
			<content:encoded><![CDATA[<p>I wrote &#8220;<a href="http://www.muhuk.com/2010/01/developing-reusable-django-apps/"><em>signals provide a great way to propagate the events generated from your app</em></a>&#8221; earlier. I think reusable apps should avoid hardcoding any kind of event handling and <a href="http://docs.djangoproject.com/en/dev/topics/signals/">send signals</a> instead. App consumers might prefer an email over an on-screen notification. They may even choose to ignore the event silently. A reusable app should give this choice to the consumer.</p>

<p>Taking advantage of signals doesn&#8217;t necessarily mean providing no sane defaults. You can send signals <strong>and</strong> provide default event handling. Here is a couple of ideas how this can be done:</p>

<ul>
<li>Your app can <a href="http://stackoverflow.com/questions/2209159/disconnect-signals-for-models-and-reconnect-in-django/2209804#2209804">check if there are any listeners</a> and connect the default handlers if there is none.</li>
<li>You can ship an auxiliary app that connects default handlers when added to <code>INSTALLED_APPS</code>.</li>
</ul>

<p>I personally prefer the second approach since it&#8217;s simpler and more explicit. I&#8217;m sure there are other ways to implement default handlers for signals.</p>

<h3>Dispatch_uid</h3>

<p>Don&#8217;t forget to assign a unique <code>dispatch_uid</code> for each <code>connect()</code> call. Otherwise your handler can get <a href="http://code.djangoproject.com/wiki/Signals#Helppost_saveseemstobeemittedtwiceforeachsave">connected twice</a>. I would also suggest you to use both module path <strong>and</strong> your handler function&#8217;s name in your <code>dispatch_uid</code>:</p>

<pre><code>"%s.%s" % (os.path.splitext(__file__)[0].replace(os.sep, '.')[1:],
           handler_name)
</code></pre>

<p>Now I should take my own advice and replace hardcoded <code>User.message_set.create()</code>s with signals in <a href="http://github.com/muhuk/django-simple-friends">django-simple-friends</a>.</p>


<p>Related posts:<ol><li><a href='http://www.muhuk.com/2010/01/developing-reusable-django-apps/' rel='bookmark' title='Permanent Link: Developing Reusable Django Apps'>Developing Reusable Django Apps</a></li>
<li><a href='http://www.muhuk.com/2010/01/developing-reusable-django-apps-app-settings/' rel='bookmark' title='Permanent Link: Developing Reusable Django Apps: App Settings'>Developing Reusable Django Apps: App Settings</a></li>
<li><a href='http://www.muhuk.com/2010/01/dynamic-translation-apps-for-django/' rel='bookmark' title='Permanent Link: Dynamic Translation Apps for Django'>Dynamic Translation Apps for Django</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.muhuk.com/2010/03/developing-reusable-django-apps-signals/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
