<?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>Azavea Labs &#187; widgets</title>
	<atom:link href="http://www.azavea.com/blogs/labs/tag/widgets/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.azavea.com/blogs/labs</link>
	<description>Insight on what our engineers are doing</description>
	<lastBuildDate>Mon, 06 Feb 2012 22:32:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>jQuery Star Rating plugin as a frontend for Django-ratings</title>
		<link>http://www.azavea.com/blogs/labs/2011/04/jquery-star-rating-with-django-ratings/</link>
		<comments>http://www.azavea.com/blogs/labs/2011/04/jquery-star-rating-with-django-ratings/#comments</comments>
		<pubDate>Fri, 08 Apr 2011 14:15:06 +0000</pubDate>
		<dc:creator>Carissa Brittain</dc:creator>
				<category><![CDATA[Posts]]></category>
		<category><![CDATA[Django]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[widgets]]></category>

		<guid isPermaLink="false">http://www.azavea.com/blogs/labs/?p=1391</guid>
		<description><![CDATA[My current Django project, OpenDataPhilly, needed a ratings system and, guess what? Django has a ratings module! All done, right? Weeelllll.. the thing is, jQuery also has a beautiful and well-behaved ratings widget that is very easy to re-skin. How do you choose between two stable plugins? In this case there was no need. I [...]]]></description>
			<content:encoded><![CDATA[<p>My current Django project, <a title="Launching Soon! Will you be there?" href="http://opendataphilly.org/" target="_blank">OpenDataPhilly</a>, needed a ratings system and, guess what? Django has a <a title="Simple and easy to implement" href="https://github.com/dcramer/django-ratings" target="_blank">ratings module</a>! All done, right? Weeelllll.. the thing is, jQuery also has a beautiful and well-behaved <a title="Gorgeous and slick!" href="http://www.fyneworks.com/jquery/star-rating/#tab-Overview" target="_blank">ratings widget</a> that is very easy to re-skin. How do you choose between two stable plugins? In this case there was no need. I decided to use the Django-ratings module for everything but the visual element and the jQuery Star Rating Plugin for some flashy effect.</p>
<p>So why blog about it then? The implementation was <a title="It's harder than it looks..." href="http://tvtropes.org/pmwiki/pmwiki.php/Main/SurpriseDifficulty" target="_blank">not quite as easy as it sounds</a>.</p>
<p>First thing&#8217;s first: download and install the Django-ratings module the same way you would any other module. Follow their <a title="Django-ratings how-to" href="https://github.com/dcramer/django-ratings/blob/master/README.rst" target="_blank">instructions </a>to add a ratings field to your model and fix any import or database issues. You should wind up with something like this in your models file:</p>
<pre>from djangoratings.fields import RatingField

class MyClass(models.Model):
    ....
    rating = RatingField(range=5)
    ...</pre>
<p>Since we&#8217;re using jQuery for the front end, that&#8217;s all we need to do in the models file.</p>
<p>Next, download the jQuery plugin and put the files somewhere accessible by your app. I put them in my project&#8217;s <a title="Shocking!" href="http://en.wikipedia.org/wiki/Triboelectric_effect" target="_blank">static </a>folder, but I think anywhere is fine. The plugin worked right out of the box for me, no tweaking necessary.</p>
<p>On to the slightly hard part! If you look in the jQuery plugin&#8217;s <a title="Page source really.." href="http://www.fyneworks.com/jquery/star-rating/#tab-Testing" target="_blank">documentation</a>, it expects a pile of radio buttons to skin. No divs or lists or other tricks of nice formatting, because the plugin does all that. Unfortunately, Django&#8217;s radio button widgets think they need to do all that, too. Someone has to be told &#8216;<a title="Doesn't always work though" href="http://bartsblackboard.com/i-will-not-get-very-far-with-this-attitude/season-2/79/" target="_blank">No</a>&#8216; very firmly. Since the whole point of using the jQuery plugin is to have a pretty and easily skinable front end, we&#8217;re going to tell Django to stop rendering radio buttons quite as nicely as it does. And that&#8217;s going to take some doing.</p>
<p>Somewhere in your project, create a widgets.py file and hunt down the django.forms.widget.py file so you can copy a few things. The two classes we&#8217;re interested in are RadioInput and RadioFieldRenderer. Copy those two classes from Django&#8217;s file into your new one and close the Django file. We don&#8217;t want to accidentally mess something up in there.  First, we&#8217;ll need a few imports at the top of our file:</p>
<pre>from django.forms.util import flatatt

from django.utils.encoding import StrAndUnicode, force_unicode
from django.utils.html import conditional_escape
from django.utils.safestring import mark_safe</pre>
<p>Next, rename the classes to something not in Django. I chose StarsRadioInput and StarsRadioFieldRenderer. Also the StarsRadioFieldRenderer makes a few references to the RadioInput class. Change these to StarsRadioInput, too. Now that the classes are safely renamed, there are two methods in charge of the html coming out of our custom widget: StarsRadioInput.tag and StarsRadioFieldRenderer.render. We basically need to strip out all of the labels and list tags so that the jQuery plugin can do its css magic.</p>
<p>Change the StarsRadioInput.tag method to:</p>
<pre>def tag(self):
  if 'id' in self.attrs:
    self.attrs['id'] = '%s_%s' % (self.attrs['id'], self.index)
    final_attrs = dict(self.attrs, type='radio', name=self.name, value=self.choice_value)
  if self.is_checked():
    final_attrs['checked'] = 'checked'
  return mark_safe(u'&lt;input%s /&gt;' % flatatt(final_attrs))</pre>
<p>And change the StarsRadioFieldRenderer.render method to</p>
<pre>def render(self):
  return mark_safe(u'\n%s\n' % u'\n'.join([u'%s'
                   % force_unicode(w) for w in self]))</pre>
<p>Now, all you have to do is make your form add our special renderer to the field and push the rating value into the right data model when the form is submitted.</p>
<p>In the forms.py file, add a field to your form. Don&#8217;t forget to create the rating choices tuple and add the &#8216;star&#8217; class to the field.</p>
<pre>RATING_CHOICES = ((1,1), (2,2), (3,3), (4,4), (5,5),)
forms.CharField(widget=forms.RadioSelect(renderer=StarsRadioFieldRenderer, attrs={'class':'star'}, choices=RATING_CHOICES))</pre>
<p>In your form&#8217;s model, add this to the save method to connect the form data to the actual model&#8217;s data (if you need to):</p>
<pre>def save(self, *args, **kwargs):
  myobject.rating.add(score=self.rating, user=self.user, ip_address=self.ip_address)
  super(CommentWithRating, self).save(*args, **kwargs)</pre>
<p>Here&#8217;s what it looks like attached to a comments form:</p>
<div id="attachment_1392" class="wp-caption aligncenter" style="width: 485px"><a rel="attachment wp-att-1392" href="http://www.azavea.com/blogs/labs/2011/04/jquery-star-rating-with-django-ratings/comment_rating/"><img class="size-medium wp-image-1392 " title="django-rating and jquery star rating" src="http://www.azavea.com/blogs/labs/wp-content/uploads/2011/04/comment_rating-475x297.gif" alt="django-rating and jquery star rating" width="475" height="297" /></a><p class="wp-caption-text">django-rating and jquery star rating together</p></div>
<p style="text-align: center;">&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.azavea.com/blogs/labs/2011/04/jquery-star-rating-with-django-ratings/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Fun With Simile</title>
		<link>http://www.azavea.com/blogs/labs/2009/04/fun-with-simile/</link>
		<comments>http://www.azavea.com/blogs/labs/2009/04/fun-with-simile/#comments</comments>
		<pubDate>Thu, 30 Apr 2009 12:48:23 +0000</pubDate>
		<dc:creator>David Zwarg</dc:creator>
				<category><![CDATA[Posts]]></category>
		<category><![CDATA[mit]]></category>
		<category><![CDATA[opensource]]></category>
		<category><![CDATA[simile]]></category>
		<category><![CDATA[timeline]]></category>
		<category><![CDATA[widgets]]></category>

		<guid isPermaLink="false">http://www.azavea.com/blogs/labs/?p=28</guid>
		<description><![CDATA[I want to send a shout out to anyone working on the Simile Timeline project.  Born out of the Semantic Interoperability of Metadata and Information in unLike Environments (SIMILE) project conducted by the MIT Libraries and MIT CSAIL, they are part of a suite of tools that enable visualization of seemingly massive datasets. We are [...]]]></description>
			<content:encoded><![CDATA[<p>I want to send a shout out to anyone working on the <a href="http://www.simile-widgets.org/timeline/" target="_blank">Simile Timeline</a> project.  Born out of the <a href="http://simile.mit.edu/" target="_blank"><strong>S</strong>emantic <strong>I</strong>nteroperability of <strong>M</strong>etadata and <strong>I</strong>nformation in un<strong>L</strong>ike <strong>E</strong>nvironments (SIMILE)</a> project conducted by the <a class="external text" title="http://libraries.mit.edu/" rel="nofollow" href="http://libraries.mit.edu/">MIT Libraries</a> and <a class="external text" title="http://csail.mit.edu" rel="nofollow" href="http://csail.mit.edu/">MIT CSAIL</a>, they are part of a suite of tools that enable visualization of seemingly massive datasets. We are implementing an in-house solution for planning, and the timeline is fast, slick, and solid.</p>
<p>I was rockin&#8217; the maps with a version of timeline integrated with <a href="http://www.openlayers.org" target="_blank">OpenLayers</a>, appropriately named <a href="http://code.google.com/p/maptimeline/" target="_blank">maptimeline</a>. Abandoning all grasp of reality, I then added a <a href="http://www.simile-widgets.org/timeplot/" target="_blank">Simile Timeplot</a> to the interface, too.   I would have liked to be able to integrate the time plot and the timeline tighter, but at this point, that integration is still a bit clunky.</p>
<p>Coming back down to earth, I dropped the map and the timeplot, as they didn&#8217;t add value to an already cluttered interface. Nonetheless,  I am really impressed with how well all of these tools perform.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.azavea.com/blogs/labs/2009/04/fun-with-simile/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

