<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>Django and Flex</title>
    <link rel="alternate" type="text/html" href="http://www.djangoandflex.org.uk/" />
    <link rel="self" type="application/atom+xml" href="http://www.djangoandflex.org.uk/atom.xml" />
   <id>tag:www.djangoandflex.org.uk,2007://5</id>
    <link rel="service.post" type="application/atom+xml" href="http://www.icosagon.com/mt/mt/mt-atom.cgi/weblog/blog_id=5" title="Django and Flex" />
    <updated>2007-07-21T02:34:01Z</updated>
    <subtitle>Trials and Joys of working with Django and Flex</subtitle>
    <generator uri="http://www.sixapart.com/movabletype/">Movable Type 3.2</generator>
 
<entry>
    <title>Object Oriented Databases with Django</title>
    <link rel="alternate" type="text/html" href="http://www.djangoandflex.org.uk/2007/07/object_oriented_databases_with_django.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://www.icosagon.com/mt/mt/mt-atom.cgi/weblog/blog_id=5/entry_id=175" title="Object Oriented Databases with Django" />
    <id>tag:www.djangoandflex.org.uk,2007://5.175</id>
    
    <published>2007-07-21T01:59:09Z</published>
    <updated>2007-07-21T02:34:01Z</updated>
    
    <summary>Three times over my Django-programming career I&apos;ve had this same problem. I have a highly polymorphic domain that I&apos;m trying to represent to Django. As is well known in the world of RDBMSes, this leads to very large and complex...</summary>
    <author>
        <name>Ian</name>
        
    </author>
            <category term="Django" />
    
    <content type="html" xml:lang="en" xml:base="http://www.djangoandflex.org.uk/">
        <![CDATA[<p>Three times over my Django-programming career I've had this same problem. I have a highly polymorphic domain that I'm trying to represent to Django. As is well known in the world of RDBMSes, this leads to very large and complex sets of database tables with correspondingly slow lookup and heavy validation requirements.</p>

<p>Lets, for a simple example, imagine building an ecommerce solution for selling antiques. The antiques we have in stock all belong to one or more categories of antiques. So far so simple: an Antique model with a many to many relationship with a Category model. For each antique we need to store various bits of information: information on its quality, provenance, and damage. But, crucially, the information we need to store for each antique depends on its categories. So for a piece of furniture our shop browsers will expect certain information, while collectors of miniature paintings will expect others.</p>

<p>We could solve this problem by allowing each antique to have any number of properties, and then enter all the relevant information ourselves each time we put a new antique in the system. This would work, but be very error prone: we might forget a salient piece of data. It is also laborious, in most cases there are sensible defaults for propoerties that we'd do well to use. And it begs the question what data type do we store data in? Some properties might be dates, others quality levels, yet others names.</p>

<p>So we could create a fiendishly complex model where categories define certain required parameters, defining their default value and data type, and some flexible parameter table holds the paramaters that are non-default for a particular antique, in such a way that any data can be input (or maybe different parameter tables for different data types). It works but it is very inelegant and laborious to code.</p>

<p>There's got to be a better way.</p>]]>
        <![CDATA[<p>There is (in my humble opinion).</p>

<p>This kind of polymorphism belongs in object-oriented code, not relational database semantics. In my application (which has nothing to do with antiques but I can't say what it is without giving the client away), I represent categories as classes: the django model holds the name of a class, each category maps to one class. That class then defines the required data and the defaults and the validation methods, and so on (they inherit from a base class, so most of this is pretty easy).</p>

<pre>
# A sample category class, with the properties it expects, and their data types.
class FurnitureAntique (Category):
   scratches = ListOf(LocatedDefect)
   period = TimePeriod()
   primaryWood = Choice(WOOD_TYPES)
</pre>

<p>The classes so defined are held in a simple registry (a dictionary mapping name to class)</p>

<pre>
# Add the category class to the registry, so it can be retrieved by name later.
Cateogry.registerClass(FurnitureAntique)
</pre>

<p>And the django database model for a category allows you to easily get the corresponding class.</p>

<pre>
# The django model representing a category of antique
class AntiqueCategory (models.Model):
   categoryClass = models.CharField(maxlength=64)
   def getClass(self):
       # Retrieve the corresponding category class from the registry by name
       Category.getCategoryByName(self.categoryClass)
</pre>

<p>Then I have another django model for the antiques. This has a text field that holds pickled data for its properties. I have added a method to the django model that will unpickle the data, find the classes for the categories, create and return a new instance of the category class with the unpickled data (below I've shown this where each antique has one and only one category, in my app it is a many to many relation, and for instances that belong to more than one category the correctly uses multiple inheritance and an anonymous class).</p>

<pre>
class Antique (models.Model):
    pickle = models.TextField()
    category = models.ForeignKey(AntiqueCategory) # Using a many-to-one for clarity here.
 
    def getCateogryInstance(self):
        cls = category.getClass()
        data = pickle.loads(self.pickle)
        return cls(**data)
</pre>

<p>This allows me to store any number of well-validated properties in the antique table, corresponding to validation requirements in the category table, without having to have lots and lots of tables in the database.</p>

<p>The downside is that I can't use the databases optimised searching systems (such as indexes) to search the pickled properties. In my application this doesn't matter (it would be difficult to create a user interface that allowed the user to specify such a highly polymorphic search in any case), but in other domains this may be a killer for this approach.</p>

<p>Now in the example above I'm using a very simple set of class properties in the Category class to define what properties we expect. Rather than create a whole new set of validation building blocks, I suspect I can reuse the newforms fields from Django for this same purpose (with the addition of fields representing compound data). But I haven't got far trying this yet. In my application parameters are all of one type (an enumerated choice), so I haven't need this yet, but I'm keen to have a go at some point.</p>]]>
    </content>
</entry>
<entry>
    <title>My Last Post on HTML (Honest)</title>
    <link rel="alternate" type="text/html" href="http://www.djangoandflex.org.uk/2007/07/my_last_post_on_html_honest.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://www.icosagon.com/mt/mt/mt-atom.cgi/weblog/blog_id=5/entry_id=174" title="My Last Post on HTML (Honest)" />
    <id>tag:www.djangoandflex.org.uk,2007://5.174</id>
    
    <published>2007-07-13T00:56:41Z</published>
    <updated>2007-07-13T01:07:02Z</updated>
    
    <summary>I promise I really will blog about Django and Flex again soon! There are some major faults with my column-HTML posts below: &quot;If I have seen further it is by standing on the shoulders of giants.&quot; -- Newton Of course,...</summary>
    <author>
        <name>Ian</name>
        
    </author>
    
    <content type="html" xml:lang="en" xml:base="http://www.djangoandflex.org.uk/">
        <![CDATA[<p>I promise I really will blog about Django and Flex again soon!</p>

<p>There are some major faults with my column-HTML posts below: "If I have seen further it is by standing on the shoulders of giants." -- Newton</p>

<p>Of course, the key part of the quote is that it is a wise move to find some giants and stand on their shoulders. In my case I stood around on tiptoe trying to see as far as possible because I couldn't immediately find a friendly giant to stand on. Thanks for everyone who commented and pointed out where the giants hang out.</p>

<p>So, in summary:</p>

<p>To put columns in any order, don't do the complex nesting stuff I recommended in <a href='http://www.djangoandflex.org.uk/2007/07/div_column_layouts_in_any_orde.html'>this post</a>, instead, use negative margins, as described <a href='http://alistapart.com/articles/negativemargins/'>here</a>.</p>

<p>And secondly, don't use a zero pixel div with clear:both that I blogged <a href='http://www.djangoandflex.org.uk/2007/07/wrapping_a_div_around_floats_i_1.html'>here</a>, use overlap:hidden in the outer div instead.</p>

<p>So basically, do it differently to the way I said :D</p>

<p>Thanks again everyone for the help.</p>]]>
        
    </content>
</entry>
<entry>
    <title>Welcome Djangoistae</title>
    <link rel="alternate" type="text/html" href="http://www.djangoandflex.org.uk/2007/07/welcome_djangoistae_1.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://www.icosagon.com/mt/mt/mt-atom.cgi/weblog/blog_id=5/entry_id=173" title="Welcome Djangoistae" />
    <id>tag:www.djangoandflex.org.uk,2007://5.173</id>
    
    <published>2007-07-10T00:28:42Z</published>
    <updated>2007-07-10T00:31:05Z</updated>
    
    <summary>It seems the site is listed on the Django main weblog (thanks Clint)! If you&apos;re joining from there and are interested in RIAs, please leave a comment or subscribe. I think it would be great to get together and discuss...</summary>
    <author>
        <name>Ian</name>
        
    </author>
            <category term="Meta" />
    
    <content type="html" xml:lang="en" xml:base="http://www.djangoandflex.org.uk/">
        <![CDATA[<p>It seems the site is listed on the Django main weblog (thanks Clint)!</p>

<p>If you're joining from there and are interested in RIAs, please leave a comment or subscribe. I think it would be great to get together and discuss issues particular to RIAs and django!</p>]]>
        
    </content>
</entry>
<entry>
    <title>A 3 Step Plan for SEO-friendly Rich Internet Applications</title>
    <link rel="alternate" type="text/html" href="http://www.djangoandflex.org.uk/2007/07/a_3_step_plan_for_seofriendly.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://www.icosagon.com/mt/mt/mt-atom.cgi/weblog/blog_id=5/entry_id=171" title="A 3 Step Plan for SEO-friendly Rich Internet Applications" />
    <id>tag:www.djangoandflex.org.uk,2007://5.171</id>
    
    <published>2007-07-08T22:43:52Z</published>
    <updated>2007-07-08T23:18:12Z</updated>
    
    <summary>Search engines are optimised for a non-RIA model of the web: a web made up of pages. SEO tuned web applications expose content in pages for search engines, even if the user will interact with the application through AJAX and...</summary>
    <author>
        <name>Ian</name>
        
    </author>
    
    <content type="html" xml:lang="en" xml:base="http://www.djangoandflex.org.uk/">
        <![CDATA[<p>Search engines are optimised for a non-RIA model of the web: a web made up of pages. SEO tuned web applications expose content in pages for search engines, even if the user will interact with the application through AJAX and page-rewriting (which is completely opaque to a search engine spider). </p>

<p>RIAs make this even harder, and most developers don't seem to bother at all. That's fine if your RIA is going to be used by employees or partners (i.e. people who know where to find it), but not if it will be public facing.</p>

<p>Fortunately, there's an easy way make your RIA search engine friendly. </p>]]>
        <![CDATA[<p>I've been working on a public-facing RIA that needs to play nicely with search. We've done a lot of work investigating how existing web-apps show up in search engine indices and how to SEO RIAs. After a surprising amount of experimental work, we came up with a pretty robust strategy. Although there is a lot of finesse needed to get this working really well, these are the basic three steps:</p>

<p>1. <strong>Provide multiple URLs to access content</strong>. Without this, you are completely lost!  In my application the user can interact with other user's profiles. We have exposed each person's profile in its own URL. Each URL maps to the same RIA, but it does so with the corresponding profile as the 'default'. The user can then navigate away, but they start on that content. If your RIA was a store you could do the same thing for products, or if you were working with flight details, do the same per flight code. One curved-ball in this: you have to make sure that when a user wants to bookmark a piece of content, that they get the right URL. You can do this by creating a 'bookmark this page', or 'link to this page' button and some javascript. Exact details I'll leave for another time, but it takes a bit of messing about.</p>

<p>2. <strong>On each content page, use javascript to dynamically insert the RIA code</strong>. This is slightly more difficult, but the basic idea is that each URL is mapped to a non-interactive page that provides a static view of the corresponding content. Javascript then detects that the viewer of the page is human, and replaces the static content with the RIAs viewing the same content. One other advantage of this (that we're not using) is that you can selectively replace the page's content. So you could, for example, have content-sensitive ads on the site that remain, with the RIA working around them.</p>

<p>3. <strong>Provide multiple SE-optimized navigation routes across your content.</strong> You should build both an XML and HTML site-map, linked from the static content at every URL. You should also provide inter-links between related content whenever possible. Search engine <em>rank</em> is only part of SEO - you also want users to see the best of your pages for their search term (because they'll be more likely to click your link rather than a competitor). On the same note, make sure you have content-specific titles for each page.</p>

<p>Anyway, like I said, there's a lot more to it and we're learning as we go, but this is the basics. Drop me a comment with your email if would be useful for me to go into more.</p>]]>
    </content>
</entry>
<entry>
    <title>Wrapping a Div around Floats in HTML</title>
    <link rel="alternate" type="text/html" href="http://www.djangoandflex.org.uk/2007/07/wrapping_a_div_around_floats_i_1.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://www.icosagon.com/mt/mt/mt-atom.cgi/weblog/blog_id=5/entry_id=170" title="Wrapping a Div around Floats in HTML" />
    <id>tag:www.djangoandflex.org.uk,2007://5.170</id>
    
    <published>2007-07-06T16:31:55Z</published>
    <updated>2007-07-06T16:49:19Z</updated>
    
    <summary><![CDATA[The second of my digressions onto HTML looks at how to make sure that a DIV is at least as high as the floating DIVs it contains. If you do something like: &lt;div id='outer'&gt; &lt;div style='float:left'&gt;Content One&lt;/div&gt; &lt;div style='float:left'&gt;Content Two&lt;/div&gt;...]]></summary>
    <author>
        <name>Ian</name>
        
    </author>
            <category term="HTML" />
    
    <content type="html" xml:lang="en" xml:base="http://www.djangoandflex.org.uk/">
        <![CDATA[<p>The second of my digressions onto HTML looks at how to make sure that a DIV is at least as high as the floating DIVs it contains.</p>

<p>If you do something like:</p>

<pre>
&lt;div id='outer'&gt;
   &lt;div style='float:left'&gt;Content One&lt;/div&gt;
   &lt;div style='float:left'&gt;Content Two&lt;/div&gt;
   &lt;div style='float:left'&gt;Content Three&lt;/div&gt;
&lt;/div&gt;
</pre>

<p>and want <var>div#outer</var> to encompass the three content divs, you find a browser incompatibility. IE7 does that I want - the enclosing div encloses its children. Other browsers do what the CSS standard says is the right thing: div#outer is one pixel high and the rest of the divs descend below it.</p>

<img alt="enclosing-div-blog.png" src="http://www.djangoandflex.org.uk/enclosing-div-blog.png" width="509" height="221" />

<p>I want it to work on all browsers, however. After various playing around I discovered that:</p>

<pre>
&lt;div id='outer'&gt;
   &lt;div style='float:left'&gt;Content One&lt;/div&gt;
   &lt;div style='float:left'&gt;Content Two&lt;/div&gt;
   &lt;div style='float:left'&gt;Content Three&lt;/div&gt;
   &lt;div style='clear: both; height: 0'&gt;&lt;/div&gt;
&lt;/div&gt;
</pre>

<p>seems to work. [Interestingly, although I'm working exclusively in XHTML, using <var>&lt;div/&gt;</var> doesn't work - the XHTML spec recommends that you don't have empty tags like div (anything that normally contains content), but this isn't a normative part of the spec, so browsers should really support full XML syntax in this regard.]</p>

<p>The application of all this is that I can now apply a vertically tiled 1px high background image to div#outer, with column shading on it, and have the different height columns all <em>appear</em> to be full-height - something that is very difficult to do by making the floating divs actually the same height. It is a solution that avoids many of the accessibility and fragility problems of the "one true layout" approach.</p>

<p>Combining this with the previous blog post I can now get full-height columns in any order, which is what I needed.</p>]]>
        
    </content>
</entry>
<entry>
    <title>DIV Column Layouts in Any Order (Almost)</title>
    <link rel="alternate" type="text/html" href="http://www.djangoandflex.org.uk/2007/07/div_column_layouts_in_any_orde.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://www.icosagon.com/mt/mt/mt-atom.cgi/weblog/blog_id=5/entry_id=168" title="DIV Column Layouts in Any Order (Almost)" />
    <id>tag:www.djangoandflex.org.uk,2007://5.168</id>
    
    <published>2007-07-05T15:02:28Z</published>
    <updated>2007-07-05T18:01:37Z</updated>
    
    <summary><![CDATA[This is a complete digression for a blog on Django and Flex, I want to talk about some HTML I've been working on. My project is some technology to automatically generate and design web-pages, through my R&amp;D company. There are...]]></summary>
    <author>
        <name>Ian</name>
        
    </author>
            <category term="HTML" />
    
    <content type="html" xml:lang="en" xml:base="http://www.djangoandflex.org.uk/">
        <![CDATA[<p>This is a complete digression for a blog on Django and Flex, I want to talk about some HTML I've been working on. My project is some technology to automatically generate and design web-pages, through my <a href='http://www.therndguy.com'>R&amp;D</a> company. There are two things I've discovered, so look out for a further post on the other.</p>

<p>This post is about how to create DIV column layouts where the columns are given in the HTML in any order, but are displayed correctly when rendered, allowing you to have SEO-friendly content first in the HTML page, even if it appears in the middle of your column layout.</p>

]]>
        <![CDATA[<p>To lay out my pages I needed a mechanism for creating columnal layouts without tables, so I've used the common floating DIV trick:</p>

<pre>
&lt;div&gt;
  &lt;div style="float: left; width: 180px"&gt;
    Column One Content
  &lt;/div&gt;
  &lt;div style="float: left; width: 500px"&gt;
    Column Two Content
  &lt;/div&gt;
  &lt;div style="float: left; width: 280px"&gt;
    Column Three Content
  &lt;/div&gt;
&lt;/div&gt;
</pre>

<p><small>(note that I'm doing these examples with fixed column widths in pixels. I normally prefer fixed-width in ems, but you could also use percentages for resizing. This approach doesn't allow you have some flexible size columns and others fixed width, however)</small></p>

<p>But column one (in many cases a navigation toolset) comes first. For SEO optimisation I want content to come first. This may be in a different column. In fact, I'd like to be able to put my column content into the HTML in <strong>any</strong> order, but have the layout work.</p>

<p>It turns out this is possible in most cases, by wrapping sets of columns in further div tags, and floating them as a whole. So, for example, if in a three column layout we want the middle column first in the HTML, then the right column, then the left, we'd do:</p>

<pre>
&lt;div&gt;
  &lt;div style="float: right; width: 780px"&gt;
    &lt;div style="float: left; width: 500px"&gt;
      Column Two Content
    &lt;/div&gt;
    &lt;div style="float: left; width: 280px"&gt;
      Column Three Content
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;div style="float: right; width: 180px"&gt;
    Column One Content
  &lt;/div&gt;
&lt;/div&gt;
</pre>

<p>In other words we group columns 2 and 3 together and right float the group, then add the leftmost column, also floating right. The logic is that a set of columns can be either left-to-right ordered (if each is floated left) or right-to-left ordered (if each is floated right). So with a combination of groups you can get most layouts.</p>

<p>Only most? Well yes. I wrote some software to calculate all possible permutations. For two columns you don't need to group at all, for three you can get all possible permutations this way. For four columns there are two permutations that are impossible. That number increases to 30 (out of 120) for five and by six, most (394 out of 720) orders are impossible. I would hazard a guess, however, that six columns isn't near the top of the average designer's toolbox.</p>

<p>So here's the meat. These tables show the order of columns on the left, and an abbreviated layout code on the right. The example from above is</p>

<p><nobr> 2, 3, 1: (&larr;2, &larr;3)&rarr;, 1&rarr; </nobr></p>

<p>We want the column two to appear first, then column three, then column one (hence 2,3,1 on the left). The code on the right tells us we need to put columns two and three, each floated left, into a div that is floated right, followed by column one on its own also floated right: exactly what we had in the HTML code above.</p>

<div style='float:left; padding: 0 24px 0 0'>
<h4>2 columns</h4><p>
<nobr> 1, 2: &larr;1, &larr;2 </nobr><br/>
<nobr> 2, 1: 2&rarr;, 1&rarr; </nobr><br/>
</p><p>2 out of 2 are possible</p>
</div>
<div style='float:left; padding: 0 24px 0 0'>
<h4>3 columns</h4><p>
<nobr> 1, 2, 3: &larr;1, &larr;2, &larr;3 </nobr><br/>
<nobr> 2, 1, 3: &larr;(2&rarr;, 1&rarr;), &larr;3 </nobr><br/>
<nobr> 2, 3, 1: (&larr;2, &larr;3)&rarr;, 1&rarr; </nobr><br/>
<nobr> 1, 3, 2: &larr;1, &larr;(3&rarr;, 2&rarr;) </nobr><br/>
<nobr> 3, 1, 2: 3&rarr;, (&larr;1, &larr;2)&rarr; </nobr><br/>
<nobr> 3, 2, 1: 3&rarr;, 2&rarr;, 1&rarr; </nobr><br/>
</p><p>6 out of 6 are possible</p>
</div>
<div style='float:left; padding: 0 24px 0 0'>
<h4>4 columns</h4><p>
<nobr> 1, 2, 3, 4: &larr;1, &larr;2, &larr;3, &larr;4 </nobr><br/>
<nobr> 2, 1, 3, 4: &larr;(2&rarr;, 1&rarr;), &larr;3, &larr;4 </nobr><br/>
<nobr> 2, 3, 1, 4: &larr;((&larr;2, &larr;3)&rarr;, 1&rarr;), &larr;4 </nobr><br/>
<nobr> 2, 3, 4, 1: (&larr;2, &larr;3, &larr;4)&rarr;, 1&rarr; </nobr><br/>
<nobr> 1, 3, 2, 4: &larr;1, &larr;(&larr;(3&rarr;, 2&rarr;), &larr;4) </nobr><br/>
<nobr> 3, 1, 2, 4: &larr;(3&rarr;, (&larr;1, &larr;2)&rarr;), &larr;4 </nobr><br/>
<nobr> 3, 2, 1, 4: &larr;(3&rarr;, 2&rarr;, 1&rarr;), &larr;4 </nobr><br/>
<nobr> 3, 2, 4, 1: (&larr;(3&rarr;, 2&rarr;), &larr;4)&rarr;, 1&rarr; </nobr><br/>
<nobr> 1, 3, 4, 2: &larr;1, &larr;((&larr;3, &larr;4)&rarr;, 2&rarr;) </nobr><br/>
<nobr> 3, 1, 4, 2: Impossible </nobr><br/>
<nobr> 3, 4, 1, 2: (&larr;3, &larr;4)&rarr;, (&larr;1, &larr;2)&rarr; </nobr><br/>
<nobr> 3, 4, 2, 1: (&larr;3, &larr;4)&rarr;, 2&rarr;, 1&rarr; </nobr><br/>
<nobr> 1, 2, 4, 3: &larr;1, &larr;2, &larr;(4&rarr;, 3&rarr;) </nobr><br/>
<nobr> 2, 1, 4, 3: &larr;(2&rarr;, 1&rarr;), &larr;(4&rarr;, 3&rarr;) </nobr><br/>
<nobr> 2, 4, 1, 3: Impossible </nobr><br/>
<nobr> 2, 4, 3, 1: (&larr;2, &larr;(4&rarr;, 3&rarr;))&rarr;, 1&rarr; </nobr><br/>
<nobr> 1, 4, 2, 3: &larr;1, &larr;(4&rarr;, (&larr;2, &larr;3)&rarr;) </nobr><br/>
<nobr> 4, 1, 2, 3: 4&rarr;, (&larr;1, &larr;2, &larr;3)&rarr; </nobr><br/>
<nobr> 4, 2, 1, 3: 4&rarr;, (&larr;(2&rarr;, 1&rarr;), &larr;3)&rarr; </nobr><br/>
<nobr> 4, 2, 3, 1: (4&rarr;, (&larr;2, &larr;3)&rarr;)&rarr;, 1&rarr; </nobr><br/>
<nobr> 1, 4, 3, 2: &larr;1, &larr;(4&rarr;, 3&rarr;, 2&rarr;) </nobr><br/>
<nobr> 4, 1, 3, 2: 4&rarr;, (&larr;1, &larr;(3&rarr;, 2&rarr;))&rarr; </nobr><br/>
<nobr> 4, 3, 1, 2: 4&rarr;, 3&rarr;, (&larr;1, &larr;2)&rarr; </nobr><br/>
<nobr> 4, 3, 2, 1: 4&rarr;, 3&rarr;, 2&rarr;, 1&rarr; </nobr><br/>
</p><p>22 out of 24 are possible</p>
</div>

<p>If you want the tables for higher numbers of columns let me know, and I'll email you them.</p>
]]>
    </content>
</entry>
<entry>
    <title>In with the New</title>
    <link rel="alternate" type="text/html" href="http://www.djangoandflex.org.uk/2007/07/in_with_the_new.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://www.icosagon.com/mt/mt/mt-atom.cgi/weblog/blog_id=5/entry_id=166" title="In with the New" />
    <id>tag:www.djangoandflex.org.uk,2007://5.166</id>
    
    <published>2007-07-04T14:32:32Z</published>
    <updated>2007-07-04T14:34:04Z</updated>
    
    <summary>A quick note to say that I&apos;ve moved the blog to this new site. Not that there were many folks who&apos;d found it in the five days of its existence, but still....</summary>
    <author>
        <name>Ian</name>
        
    </author>
            <category term="Meta" />
    
    <content type="html" xml:lang="en" xml:base="http://www.djangoandflex.org.uk/">
        <![CDATA[<p>A quick note to say that I've moved the blog to this new site. Not that there were many folks who'd found it in the five days of its existence, but still.</p>

]]>
        
    </content>
</entry>
<entry>
    <title>Z-Order of Components in a Flex Canvas</title>
    <link rel="alternate" type="text/html" href="http://www.djangoandflex.org.uk/2007/07/zorder_of_components_in_a_flex.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://www.icosagon.com/mt/mt/mt-atom.cgi/weblog/blog_id=5/entry_id=161" title="Z-Order of Components in a Flex Canvas" />
    <id>tag:www.djangoandflex.org.uk,2007://5.161</id>
    
    <published>2007-07-03T13:53:49Z</published>
    <updated>2007-07-05T14:59:50Z</updated>
    
    <summary>This isn&apos;t a blog about Flex bugs - honest. But I found another yesterday. If you have a bunch of components in a Flex Canvas, and show/hide/animate them with states and transitions, the components can change z-order on you without...</summary>
    <author>
        <name>Ian</name>
        
    </author>
            <category term="Fix" />
            <category term="Flex" />
    
    <content type="html" xml:lang="en" xml:base="http://www.djangoandflex.org.uk/">
        <![CDATA[<p>This isn't a blog about Flex bugs - honest. But I found another yesterday.</p>

<p>If you have a bunch of components in a Flex Canvas, and show/hide/animate them with states and transitions, the components can change z-order on you without warning. I had a pernicious problem today with something like:</p>

<pre>
&lt;mx:Canvas&gt; 
 &lt;mx:Panel id='content'/&gt; 
 &lt;mx:Panel id='tool_palette'/&gt; 
&lt;/mx:Canvas&gt;</pre>
<p>Where <tt>content</tt> started out behind <tt>tool_palette</tt>, but when the latter got hidden (using a RemoveChild node in a state), and reappeared (by transitioning back) it was shown behind the content panel. Clearly a problem.</p>

<p>The solution I found was to separate the objects into two canvases:</p>
<pre>
&lt;mx:Canvas&gt; 
 &lt;mx:Canvas height="100%" width="100%"&gt; 
  &lt;mx:Panel id='content'/&gt; 
 &lt;/mx:Canvas&gt; 
 &lt;mx:Canvas height="100%" width="100%"&gt; 
  &lt;mx:Panel id='tool_palette'/&gt; 
 &lt;/mx:Canvas&gt; 
&lt;/mx:Canvas&gt;</pre>
<p>which is a bit of a hack, but works. This is the first time I've missed old-flash's z-depth model.</p>]]>
        
    </content>
</entry>
<entry>
    <title>Binding Selected Items in a Flex List</title>
    <link rel="alternate" type="text/html" href="http://www.djangoandflex.org.uk/2007/07/binding_selected_items_in_a_fl_1.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://www.icosagon.com/mt/mt/mt-atom.cgi/weblog/blog_id=5/entry_id=162" title="Binding Selected Items in a Flex List" />
    <id>tag:www.djangoandflex.org.uk,2007://5.162</id>
    
    <published>2007-07-01T13:56:51Z</published>
    <updated>2007-07-03T20:49:31Z</updated>
    
    <summary>Just a quickie. Discovered today that if you try to programatically deselect the currently selected item in a ListBase subclass with selectedItem=null the change doesn&apos;t propagate to anything bound to {my_list.selectedItem}. In fact in my application it seems to permanently...</summary>
    <author>
        <name>Ian</name>
        
    </author>
            <category term="Fix" />
            <category term="Flex" />
    
    <content type="html" xml:lang="en" xml:base="http://www.djangoandflex.org.uk/">
        <![CDATA[<p>Just a quickie.</p>

<p>Discovered today that if you try to programatically deselect the currently selected item in a ListBase subclass with <tt>selectedItem=null</tt> the change doesn't propagate to anything bound to <tt>{my_list.selectedItem}</tt>. In fact in my application it seems to permanently break the data binding.</p>

<p>Instead, to deselect a list item, do <tt>selectedIndex=-1</tt>, that seems to work.</p>

<p>So I've got something like:</p>

<pre>
 &lt;mx:Button label="Clear" click="entries.selectedIndex=-1"/&gt; 
 &lt;mx:List id="entries" dataProvider="{my_data}"/&gt; 
 &lt;components:MyEntryDisplay entry="{entries.selectedItem}"/&gt;</pre>

<p>which is fine for me.</p>]]>
        
    </content>
</entry>
<entry>
    <title>Sending Images From Flex to a Server</title>
    <link rel="alternate" type="text/html" href="http://www.djangoandflex.org.uk/2007/06/sending_images_from_flex_to_a_1.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://www.icosagon.com/mt/mt/mt-atom.cgi/weblog/blog_id=5/entry_id=163" title="Sending Images From Flex to a Server" />
    <id>tag:www.djangoandflex.org.uk,2007://5.163</id>
    
    <published>2007-06-30T13:00:00Z</published>
    <updated>2007-07-03T20:49:59Z</updated>
    
    <summary>Today&apos;s challenge has been to allow a Flex app to create images, and have the new images uploaded back to the server as a bitmap. The application is for a simple buddy-icon editing application: users can upload their photos, drag...</summary>
    <author>
        <name>Ian</name>
        
    </author>
            <category term="Client-Server" />
            <category term="Django" />
            <category term="Flex" />
            <category term="Images" />
    
    <content type="html" xml:lang="en" xml:base="http://www.djangoandflex.org.uk/">
        <![CDATA[<p>Today's challenge has been to allow a Flex app to create images, and have the new images uploaded back to the server as a bitmap. The application is for a simple buddy-icon editing application: users can upload their photos, drag in bits of clip art, resize and so on, then use their generated buddy icon on the site. The same could be useful for doing basic image editing, uploading for print.</p>

<p>There are two issues. First is getting uploaded images into Flex. That's a topic for a future post (but basically you have to upload them to the server, then download them again).</p>

<p>The second is uploading a final image generated by Flex. And it turns out this is surprisingly easy.</p>]]>
        <![CDATA[Flash has the ability to render any display component to a bitmap, using the BitmapData class. So in my graphic-design-canvas component I did:</p>

<pre>
private var bitmapData:BitmapData; 
private function saveToBitmap():void 
{ 
    bitmapData = new BitmapData(48, 48, true, 0x00ffffff); 
    bitmapData.draw(this);</pre>


<p>The next stage was to convert the raw bitmap data into an image format (to reduce bandwidth requirements). In Flex 2.01 I used Adobe's PNGEncoder from their <a href="http://code.google.com/p/as3corelib/">corelib</a> package. Once you've set up a classpath, or copied the .swc file into <var>/frameworks/lib</var>, you can do this:
</p>

<pre>
    var imageData:ByteArray = PNGEncoder.encode(bitmapData);</pre>
<p>Easy as pie.</p>

<p>Finally to send it to the server I use a POST HTTP request. This requires that the bitmap data is encoded in Base 64:</p>
<pre>
    var encoder : Base64Encoder = new Base64Encoder(); 
    encoder.encodeBytes(imageData); 
    var params:Object = { image_data: encoder.flush() }; 
    imageSend.send(params); 
}</pre>
<p>where <var>imageSend</var> is the name of a HTTPService I created:</p>
<pre>
&lt;mx:HTTPService id="imageSend" showBusyCursor="true" 
 useProxy="false" url="http://www.example.com/upload_image/" 
 method="POST" result="imageSentConfirmation()"/&gt;</pre>
<p>Which is all very neat and simple: 8 lines of code.</p>

<p>On the Django end you simply need to extract the data from request.POST and decode the base-64 coded string:</p>
<pre>
def upload_image(request): 
    data = base64.b64decode(request.POST['image_data'])</pre>
You can feed this data into a file directly:
<pre>
    open("filename.png", "wb").write(data)</pre>
<p>or send it to the Python Imaging Library for size-checks, conversion, or other jiggery pokery:</p>
<pre>
    img = Image.open(StringIO(data)) 
    img = img.resize((48,48), Image.ANTIALIAS) 
    img.save(os.path.join(MEDIA_ROOT, "filename.png"))</pre>
<p>Then return something that your Flex component understands:</p>
<pre>
    return http.HttpResponse("&lt;ok/&gt;", mimetype="text/xml")</pre>
<p>Of course, you'll probably want to do some more error handling on the server: trapping garbage content, file issues and the like. But the basic functionality is yours for just two lines of Django code!</p>]]>
    </content>
</entry>
<entry>
    <title>Xml Generation Module</title>
    <link rel="alternate" type="text/html" href="http://www.djangoandflex.org.uk/2007/06/xml_generation_module_1.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://www.icosagon.com/mt/mt/mt-atom.cgi/weblog/blog_id=5/entry_id=164" title="Xml Generation Module" />
    <id>tag:www.djangoandflex.org.uk,2007://5.164</id>
    
    <published>2007-06-29T13:59:05Z</published>
    <updated>2007-07-03T20:50:45Z</updated>
    
    <summary>In all my apps I communicate client to server in XML. Flex has very nice XML processing capabilities through e4x. I typically generate only simple XML to send back to the server, where python&apos;s processing is fairly easy too. The...</summary>
    <author>
        <name>Ian</name>
        
    </author>
            <category term="Client-Server" />
            <category term="XML" />
    
    <content type="html" xml:lang="en" xml:base="http://www.djangoandflex.org.uk/">
        <![CDATA[<p>In all my apps I communicate client to server in XML. Flex has very nice XML processing capabilities through e4x. I typically generate only simple XML to send back to the server, where python's processing is fairly easy too.</p>

<p>The last link in the chain is getting Django to generate XML easily. I wrote a very simple xml generating module that might proove useful.</p>
]]>
        <![CDATA[<p>It is designed to be as simple as possible. So you might have</p>

<pre>&gt;&gt;&gt; b = xml.book( 
...     xml.title('The Title'), xml.author('Ian Millington') 
...     )</pre>

<p>and have it rendered to xml with:</p>

<pre>&gt;&gt;&gt; b.xml 
'&lt;book&gt;&lt;title&gt;The Title&lt;/title&gt;&lt;author&gt;Ian 
Millington&lt;/author&gt;&lt;/book&gt;'</pre>

<p>Of course, things can get much more complex, it handles tag properties, namespaces, DTDs and so on. For example:</p>

<pre>&gt;&gt;&gt; svg = XMLNamespace('svg', 'http://www.w3.org/2000/svg') 
&gt;&gt;&gt; makeXhtml(html.html(html.body(svg.svg(), class='main-page')))</pre>
produces
<pre>&lt;?xml version="1.0"?&gt; 
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; 
&lt;html xmlns:svg="http://www.w3.org/2000/svg"&gt; 
&lt;body class="main-page"&gt; 
&lt;svg:svg/&gt; 
&lt;/body&gt; 
&lt;/html&gt;</pre>

<p>You can find the python module on google code <a href="http://code.google.com/p/xml4py/">here</a>, with the actual python code <a href="http://xml4py.googlecode.com/svn/trunk/xmlgen.py">here</a>. The license is LGPL (i.e. no warranties, free for commercial use, non-copyleft).</p>

<p>The code is based on a HTML code generator <a href="http://www.therndguy.com/project/automatic-customised-content/">I wrote</a> as part of a research project last year. That in turn was inspired by the HTML creation system in <a href="http://mochikit.com/">MochiKit</a>, which is in turn based on a Python HTML library, I believe, but I don't know which one. Please fill me in on the missing inspiration if you know. In any case I think that the prior art used a fixed set of tag functions, rather than allowing any tags to be used. Correct me if I'm wrong!</p>

<p>EDIT: The module bears a striking resemblance to <a href='http://www.livinglogic.de/Python/xist/'>XIST</a> (thanks for the pointer Tom!). XIST seems to be slightly more complex to get going, but lots more capable too.</p>]]>
    </content>
</entry>
<entry>
    <title>Welcome</title>
    <link rel="alternate" type="text/html" href="http://www.djangoandflex.org.uk/2007/06/welcome.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://www.icosagon.com/mt/mt/mt-atom.cgi/weblog/blog_id=5/entry_id=165" title="Welcome" />
    <id>tag:www.djangoandflex.org.uk,2007://5.165</id>
    
    <published>2007-06-28T13:59:33Z</published>
    <updated>2007-07-03T20:51:15Z</updated>
    
    <summary>Welcome to Django&apos;n&apos;Flex. This site is about working with these two great new technologies, because that&apos;s what I&apos;m doing a lot at the moment. I thought I&apos;d start this blog to note down things I&apos;ve found out, numskull moments and...</summary>
    <author>
        <name>Ian</name>
        
    </author>
            <category term="Meta" />
    
    <content type="html" xml:lang="en" xml:base="http://www.djangoandflex.org.uk/">
        <![CDATA[<p>Welcome to Django'n'Flex. This site is about working with these two great new technologies, because that's what I'm doing a lot at the moment.</p>

<p>I thought I'd start this blog to note down things I've found out, numskull moments and the rare bit of insight.</p>]]>
        
    </content>
</entry>

</feed> 

