<?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, 29 Dec 2011 05:05:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.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&#038;utm_medium=rss&#038;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 "(http://www.muhuk.com/2010/01/developing-reusable-django-apps/)" earlier. I think reusable apps should avoid hardcoding any kind of event handling and (http://docs.djangoproject.com/en/dev/topics/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 ...]]></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>
<div><a class="addthis_button" href="http://www.muhuk.com//addthis.com/bookmark.php?v=250" addthis:url='http://www.muhuk.com/2010/03/developing-reusable-django-apps-signals/' addthis:title='Developing Reusable Django Apps: Signals '><img src="//cache.addthis.com/cachefly/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a></div><p>Related posts:<ol>
<li><a href='http://www.muhuk.com/2011/11/working-with-files-in-django/' rel='bookmark' title='Working with files in Django &#8211; Part 1'>Working with files in Django &#8211; Part 1</a></li>
<li><a href='http://www.muhuk.com/2011/11/working-with-files-in-django-part-2/' rel='bookmark' title='Working with files in Django &#8211; Part 2'>Working with files in Django &#8211; Part 2</a></li>
<li><a href='http://www.muhuk.com/2011/11/working-with-files-in-django-part-3/' rel='bookmark' title='Working with files in Django &#8211; Part 3'>Working with files in Django &#8211; Part 3</a></li>
</ol></p><p class="wp-flattr-button"></p>]]></content:encoded>
			<wfw:commentRss>http://www.muhuk.com/2010/03/developing-reusable-django-apps-signals/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

